Charades
pe_queue.h
1 #ifndef PE_QUEUE_H_
2 #define PE_QUEUE_H_
3 
4 #include "typedefs.h"
5 
6 class LPChare;
7 struct LPToken;
8 
9 // Priority queue of LPToken pointers weighted by timestamp. The heap is
10 // maintained as an array of LPToken pointers. The pointers are not owned by the
11 // queue. The queue must update the index and timestamp entries in the token.
12 // This allows us to find an entry in the heap in constant time when given a
13 // pointer to an LPToken, allowing us to update the token with a new timestamp
14 // and reposition it in the queue accordingly.
15 class PEQueue {
16  private:
17  unsigned capacity, size;
18  LPToken** heap;
19 
20  // Swaps the queue entries at the two indices and updates their index fields.
21  void swap(unsigned, unsigned);
22 
23  // Repositions entries located at the given index.
24  void pull_up(unsigned);
25  void push_down(unsigned);
26 
27  // Compares the entries at the given indices, and returns the index of the
28  // smallest entry.
29  unsigned smallest(unsigned, unsigned) const;
30  unsigned largest(unsigned, unsigned) const;
31 
32  // Helper methods for accessing parents and children based on indices.
33  bool has_parent(unsigned) const;
34  bool has_left(unsigned) const;
35  bool has_right(unsigned) const;
36  unsigned parent(unsigned) const;
37  unsigned left(unsigned) const;
38  unsigned right(unsigned) const;
39  public:
40  PEQueue();
41  ~PEQueue();
42 
43  // Returns the token on top of the queue.
44  // TODO: We may also want to be able to see the second token.
45  LPToken* top() const;
46  LPToken* second() const;
47 
48  // Inserts and removes tokens from the queue.
49  void insert(LPToken*, Time);
50  void remove(LPToken*);
51 
52  // Updates the given token with a new timestamp, and repositions it.
53  void update(LPToken*, Time);
54 
55  // TODO: Temporary direct access for iteration purposes
56  int get_size() const { return size; }
57  LPToken** as_array() const { return heap; }
58 };
59 
60 #endif
Definition: pe_queue.h:15
A chare that encapsulates a set of LPStructs and their events.
Definition: lp.h:64
A token representing a handle to an LP chare inside the scheduler queues.
Definition: lp.h:30
Declares most types used within the simulator and by models.