File: pointer_queue.cpp

package info (click to toggle)
cgal 3.6.1-2
  • links: PTS
  • area: non-free
  • in suites: squeeze
  • size: 62,184 kB
  • ctags: 95,782
  • sloc: cpp: 453,758; ansic: 96,821; sh: 226; makefile: 120; xml: 2
file content (77 lines) | stat: -rw-r--r-- 1,901 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <CGAL/Kinetic/basic.h>
#include <CGAL/Kinetic/Heap_pointer_event_queue.h>
#include <CGAL/Kinetic/Inexact_simulation_traits.h>
#include <CGAL/Kinetic/Event_base.h>
#include <cstdlib>

typedef double Time;

Time proc_time_=-1;


class Event: public CGAL::Kinetic::Event_base<void*>
{
public:
  Event(double i): i_(i) {
  }
  Time time() const
  {
    return Time(i_);
  };
  void process(){
    //std::cout << "Event at " << i_ << "\n";
    proc_time_=i_;
    /*if (sim_->current_time() != i_) {
      std::cerr << "ERROR: Times do not match. Got " << sim_->current_time()
		<< " expected " << i_ <<std::endl;
    }
    assert(sim_->current_time()==i_);*/
  }
protected:
  Time i_;
};

std::ostream &operator<<(std::ostream &out, Event e)
{
  out << e.time();
  return out;
}


int main(int, char *[])
{
  typedef CGAL::Kinetic::Inexact_simulation_traits::Kinetic_kernel::Function_kernel FK;
  typedef CGAL::Kinetic::Heap_pointer_event_queue<FK> Q;
  Q pq(0, 10000, FK());
  typedef Q::Key Key;
  std::vector<Key>  items;

  for (unsigned int i=0; i< 10000; ++i) {
    Time t(std::rand()/100000.0);
    items.push_back(pq.insert(t,Event(t)));
  }
  for (unsigned int i=0; i< 5000; ++i) {
    pq.erase(items[i]);
  }

  Time last_time = -1;
  while (!pq.empty()) {
    Time t= pq.front_priority();
    if (t < last_time) {
      std::cerr << "ERROR: priority of next event (" << pq.front_priority()
		<< ") is before the last one (" << last_time << ")." << std::cerr;
    }
    assert(t >= last_time);
    last_time=t;
    pq.process_front();
    if (t < last_time) {
      std::cerr << "ERROR: wrong event processed (" << pq.front_priority()
		<< ") instead of (" << proc_time_ << ")." << std::cerr;
    }
    if (proc_time_!= t) {
      std::cerr << "ERROR: wrong event time. Expected: " << t << " got " << proc_time_ <<std::endl;
    }
    assert(proc_time_==t);
  }
  return EXIT_SUCCESS;
}