Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
id_queue.h
Go to the documentation of this file.
1#ifndef ROUTING_KIT_ID_QUEUE_H
2#define ROUTING_KIT_ID_QUEUE_H
3
5#include <vector>
6#include <algorithm>
7#include <assert.h>
8
9namespace RoutingKit{
10
11struct IDKeyPair{
12 unsigned id;
13 unsigned key;
14};
15
19private:
20 static const unsigned tree_arity = 4;
21public:
23
24 explicit MinIDQueue(unsigned id_count):
27 heap_size(0){}
28
30 bool empty()const{
31 return heap_size == 0;
32 }
33
35 unsigned size()const{
36 return heap_size;
37 }
38
40 unsigned id_count()const{
41 return id_pos.size();
42 }
43
45 bool contains_id(unsigned id){
46 assert(id < id_count());
47 return id_pos[id] != invalid_id;
48 }
49
51 void clear(){
52 for(unsigned i=0; i<heap_size; ++i)
53 id_pos[heap[i].id] = invalid_id;
54 heap_size = 0;
55 }
56
57 friend void swap(MinIDQueue&l, MinIDQueue&r){
58 using std::swap;
59 swap(l.id_pos, r.id_pos);
60 swap(l.heap, r.heap);
62 }
63
66 unsigned get_key(unsigned id)const{
67 assert(id < id_count());
68 assert(id_pos[id] != invalid_id);
69 return heap[id_pos[id]].key;
70 }
71
74 assert(!empty());
75 return heap.front();
76 }
77
80 assert(!empty());
81 --heap_size;
82 std::swap(heap[0].key, heap[heap_size].key);
83 std::swap(heap[0].id, heap[heap_size].id);
84 id_pos[heap[0].id] = 0;
86
88 return heap[heap_size];
89 }
90
93 void push(IDKeyPair p){
94 assert(p.id < id_count());
95 assert(!contains_id(p.id));
96
97 unsigned pos = heap_size;
98 ++heap_size;
99 heap[pos] = p;
100 id_pos[p.id] = pos;
101 move_up_in_tree(pos);
102 }
103
108 assert(p.id < id_count());
109 assert(contains_id(p.id));
110
111 unsigned pos = id_pos[p.id];
112
113 if(heap[pos].key > p.key){
114 heap[pos].key = p.key;
115 move_up_in_tree(pos);
116 return true;
117 } else {
118 return false;
119 }
120 }
121
126 assert(p.id < id_count());
127 assert(contains_id(p.id));
128
129 unsigned pos = id_pos[p.id];
130
131 if(heap[pos].key < p.key){
132 heap[pos].key = p.key;
134 return true;
135 } else {
136 return false;
137 }
138 }
139
140private:
141 void move_up_in_tree(unsigned pos){
142 while(pos != 0){
143 unsigned parent = (pos-1)/tree_arity;
144 if(heap[parent].key > heap[pos].key){
145 std::swap(heap[pos], heap[parent]);
146 std::swap(id_pos[heap[pos].id], id_pos[heap[parent].id]);
147 }
148 pos = parent;
149 }
150 }
151
152 void move_down_in_tree(unsigned pos){
153 for(;;){
154 unsigned first_child = tree_arity*pos+1;
155 if(first_child >= heap_size)
156 return; // no children
157 unsigned smallest_child = first_child;
158 for(unsigned c = first_child+1; c < std::min(tree_arity*pos+tree_arity+1, heap_size); ++c){
159 if(heap[smallest_child].key > heap[c].key){
160 smallest_child = c;
161 }
162 }
163
164 if(heap[smallest_child].key >= heap[pos].key)
165 return; // no child is smaller
166
167 std::swap(heap[pos], heap[smallest_child]);
168 std::swap(id_pos[heap[pos].id], id_pos[heap[smallest_child].id]);
169 pos = smallest_child;
170 }
171 }
172
173 std::vector<unsigned>id_pos;
174 std::vector<IDKeyPair>heap;
175
176 unsigned heap_size;
177};
178
179} // namespace RoutingKit
180
181#endif
unsigned id_count() const
Returns the id_count value passed to the constructor.
Definition id_queue.h:40
MinIDQueue(unsigned id_count)
Definition id_queue.h:24
void move_down_in_tree(unsigned pos)
Definition id_queue.h:152
bool empty() const
Returns whether the queue is empty. Equivalent to checking whether size() returns 0.
Definition id_queue.h:30
IDKeyPair pop()
Returns the smallest element key pair and removes it form the queue.
Definition id_queue.h:79
friend void swap(MinIDQueue &l, MinIDQueue &r)
Definition id_queue.h:57
bool contains_id(unsigned id)
Checks whether an element is in the queue.
Definition id_queue.h:45
unsigned get_key(unsigned id) const
Definition id_queue.h:66
bool decrease_key(IDKeyPair p)
Definition id_queue.h:107
void move_up_in_tree(unsigned pos)
Definition id_queue.h:141
void clear()
Removes all elements from the queue.
Definition id_queue.h:51
unsigned size() const
Returns the number of elements in the queue.
Definition id_queue.h:35
std::vector< unsigned > id_pos
Definition id_queue.h:173
void push(IDKeyPair p)
Definition id_queue.h:93
IDKeyPair peek() const
Returns the smallest element key pair without removing it from the queue.
Definition id_queue.h:73
bool increase_key(IDKeyPair p)
Definition id_queue.h:125
static const unsigned tree_arity
Definition id_queue.h:20
std::vector< IDKeyPair > heap
Definition id_queue.h:174
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition json.hpp:21884