Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
id_set_queue.h
Go to the documentation of this file.
1#ifndef ROUTING_KIT_ID_SET_QUEUE_H
2#define ROUTING_KIT_ID_SET_QUEUE_H
3
6#include <vector>
7#include <assert.h>
8
9namespace RoutingKit{
10
15
16private:
17 // Internally, the queue is represented using an implicit binary tree
18 // For every node in the tree, a bit is stored in data.
19 //
20 // Suppose that id_count() is 6, then the tree nodes are organized as
21 // follows:
22 // 1
23 // / |
24 // / |
25 // / |
26 // / |
27 // 2 3
28 // / | / |
29 // / | / |
30 // 4 5 6 7
31 // /| /| / |
32 // 8 9 10 11 12 13
33 //
34 // Node ids are counted starting from 1 and not from 0. IDs are counted
35 // starting from 0.
36 //
37 // The lowest level contains 6 bits because we have at most 6 IDs. Each of
38 // these lowest level bits encodes whether an ID is in the queue or not.
39 //
40 // If the number of IDs is uneven, for example 5, then we add another node
41 // to the lowest level to make sure that every node has a sibling. This
42 // padding bit will always remain false.
43 //
44 // The bit of upper level nodes is set, iff, there is a child whose bit is
45 // set. If the queue would contain the IDs 1 and 2, then the nodes
46 // 9, 10, 4, 5, 2, and 1 would have their bits sets.
47 //
48 // Whether a bit is set is stored in data. data[0] is not used. The first
49 // meaningful bit is data[1].
50 //
51 // offset stores the node number of the first node of the lowest level.
52 // Because of the way that the tree is constructed, this is always
53 // a power of two and it is never smaller than id_count().
54 //
55 // min_id stores the smallest id in the queue or invalid_id.
56 //
57 //
58 // parent maps a node onto its parent.
59 //
60 // other_child turns a node into its sibling.
61 // is_larger_child checks whether a node is a right child in the
62 // above diagram.
63 // is_smaller_child
64
65
66 static const unsigned root = 1;
67
69 static unsigned parent(unsigned x){
70 return x>>1;
71 }
72
74 static unsigned other_child(unsigned x){
75 return x^1;
76 }
77
79 static unsigned is_larger_child(unsigned x){
80 return x&1;
81 }
82
84 static unsigned is_smaller_child(unsigned x){
85 return !is_larger_child(x);
86 }
87
89 static unsigned smaller_child(unsigned x){
90 return x<<1;
91 }
92
94 static unsigned larger_child(unsigned x){
95 return (x<<1)|1;
96 }
97
98 static unsigned smallest_two_power_no_smaller_than(unsigned x){
99 unsigned y = 1;
100 while(y < x){
101 y <<= 1;
102 assert(y != 0);
103 }
104 return y;
105 }
106
107public:
109
111 explicit IDSetMinQueue(unsigned n):
115 data(n+offset+(n&1), false)// +(n&1) add an element if n is odd
116 {}
117
118 unsigned id_count() const {
119 return max_number_of_ids;
120 }
121
124 void push(unsigned id){
125 assert(id < id_count() && "id out of bounds");
126
127 if(min_id == invalid_id)
128 min_id = id;
129 else
130 min_to(min_id, id);
131
132 unsigned x = id + offset;
133
134 if(data[x])
135 return;
136
137 for(;;)
138 {
139 assert(!data[x]);
140 data[x] = true;
141
142 if(x == root)
143 break;
144
145 x = parent(x);
146
147 if(data[x])
148 break;
149 }
150 }
151
153 bool contains(unsigned id) const {
154 assert(id < id_count() && "id out of bounds");
155 return data[offset + id];
156 }
157
159 bool empty() const {
160 return min_id == invalid_id;
161 }
162
165 unsigned peek() const {
166 return min_id;
167 }
168
171 unsigned pop() {
172 assert(!empty());
173
174 unsigned ret = min_id;
175
176 unsigned x = min_id + offset;
177
178 for(;;)
179 {
180 assert(data[x]);
181 data[x] = false;
182
183 if(x == root){
185 return ret;
186 }
187
188 if(is_smaller_child(x)){
189 unsigned y = other_child(x);
190 assert(y < data.size());
191 if(data[y])
192 break;
193 } else {
194 assert(!data[other_child(x)]);
195 }
196
197 x = parent(x);
198 }
199
200 assert(is_smaller_child(x));
201 x = other_child(x);
202 assert(data[x]);
203 for(;;){
204 if(x >= offset)
205 break;
206
207 x = smaller_child(x);
208 if(!data[x])
209 x = other_child(x);
210 assert(data[x]);
211 }
212 min_id = x - offset;
213
214 return ret;
215 }
216
218 void clear(){
219 while(!empty())
220 pop();
221 }
222
223private:
224
226 unsigned min_id;
227 unsigned offset;
228 std::vector<bool>data;
229};
230
231} // RoutingKit
232
233#endif
234
unsigned id_count() const
std::vector< bool > data
void push(unsigned id)
static unsigned is_smaller_child(unsigned x)
True if other_child(x) > x. Only works if x != root.
bool empty() const
Checks whether the queue contains any element.
static unsigned is_larger_child(unsigned x)
True if other_child(x) < x. Only works if x != root.
static unsigned larger_child(unsigned x)
Returns the node y such that parent(y) == x and y > other_child(y).
void clear()
Removes all elements from the queue.
static unsigned smallest_two_power_no_smaller_than(unsigned x)
static unsigned other_child(unsigned x)
Maps a node onto its sibling. Only works if x != root.
bool contains(unsigned id) const
Checks whether an ID is in the queue.
static unsigned parent(unsigned x)
Maps a node onto its parent. Only works if x != root.
static const unsigned root
IDSetMinQueue(unsigned n)
The queue may contain IDs from 0 to n-1.
static unsigned smaller_child(unsigned x)
Returns the node y such that parent(y) == x and y < other_child(y).
void min_to(T &x, const T &y)
Definition min_max.h:10