Statistics
| Branch: | Revision:

mobicen / util / EventScheduler.py @ c16015e2

History | View | Annotate | Download (903 Bytes)

1 9110387b LoreBz
import heapq
2
3
class EventScheduler:
4
        def __init__(self):
5
                self.queue = []
6
                self.time = 0
7
                self.last = 0
8 e1cf8bea LoreBz
                self._step = 0
9 fa4a0a42 LoreBz
                self.counter = 0
10 9110387b LoreBz
11
        def schedule_event(self, interval, e):
12
                t = self.time + interval
13
                if t > self.last:
14
                    self.last = t
15
                heapq.heappush(self.queue, (t, e))
16
17
        def pop_event(self):
18
                e = heapq.heappop(self.queue)
19 e1cf8bea LoreBz
                self._step = e[0] - self.time
20 9110387b LoreBz
                self.time = e[0]
21 fa4a0a42 LoreBz
                self.counter += 1
22 9110387b LoreBz
                return e[1]
23
24
        def elapsed_time(self):
25
                return self.time
26
27
        def last_event_time(self):
28 e1cf8bea LoreBz
                return self.last
29
30
        def step(self):
31 fa4a0a42 LoreBz
            return self._step
32
33
        def processed_events(self):
34
            return self.counter