Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

HotQueue.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <functional>
35 #include <utility>
36 #include <vector>
37 #include <limits>
38 #include <cmath>
39 
40 
41 namespace ogdf {
42 
43 
45 template<typename V, typename P>
46 struct HotQueueNode {
47  V value;
49 
52 
54  : prev(nullptr), next(nullptr)
55  {
56  }
57 
58  HotQueueNode(const V &val, const P &pr)
59  : value(val), priority(pr),
60  prev(nullptr), next(nullptr)
61  {
62  }
63 };
64 
65 
67 
75 template<typename V, typename P, typename HeapHandle>
77 private:
78  enum class Type {
79  heap,
80  bucket
81  } type;
82 
83  union {
85  HeapHandle heapHandle;
87  std::pair<std::size_t, HotQueueNode<V, P> *> bucketHandle;
88  };
89 
91  HotQueueHandle(HeapHandle handle)
92  : type(Type::heap), heapHandle(handle)
93  {
94  }
95 
97  HotQueueHandle(std::size_t index, HotQueueNode<V, P> *queueNode)
98  : type(Type::bucket), bucketHandle(index, queueNode)
99  {
100  }
101 
103  type = other.type;
104  switch(type) {
105  case Type::heap:
106  heapHandle = other.heapHandle;
107  break;
108  case Type::bucket:
109  bucketHandle = other.bucketHandle;
110  break;
111  }
112 
113  return *this;
114  }
115 
116  template<
117  typename V1, typename P1, template<typename T, typename C> class H1
118  >
119  friend class HotQueue;
120 };
121 
122 
124 
135 template<typename V, typename P, template<typename T, typename C> class H>
136 class HotQueue {
137 private:
138  struct HeapComparator;
139  using HeapHandle = typename H<std::pair<V, P>, HeapComparator>::Handle;
140 
141  std::size_t m_size;
142 
143  H<std::pair<V, P>, HeapComparator> m_heap;
144  std::size_t m_heapSize;
145 
146  std::vector<HotQueueNode<V, P> *> m_buckets;
147 
148 public:
150 
152 
156  HotQueue(const P &change, std::size_t levels);
157 
159  ~HotQueue();
160 
162  const V &top() const;
163 
165 
170  Handle push(const V &value, const P &priority);
171 
173 
176  void pop();
177 
179 
187  void decrease(Handle &handle, const P &priority);
188 
190  std::size_t size() const {
191  return m_size;
192  }
193 
195  bool empty() const {
196  return size() == 0;
197  }
198 
199 private:
201  struct HeapComparator {
202  bool operator()(const std::pair<V, P> &a, const std::pair<V, P> &b) const {
203  return std::get<1>(a) < std::get<1>(b);
204  }
205  };
206 
207  std::size_t m_heapedBucket;
208  std::size_t m_lastBucket;
209 
210  const P m_bucketSpan;
211 
213  std::size_t bucketIndex(const P &priority) {
214  return (std::size_t) std::round(priority / m_bucketSpan);
215  }
216 
218  HotQueueNode<V, P> *&bucketAt(std::size_t index) {
219  return m_buckets[index % m_buckets.size()];
220  }
221 
222  static constexpr std::size_t NONE = std::numeric_limits<size_t>::max();
223 };
224 
225 
226 template<typename V, typename P, template<typename T, typename C> class H>
227 HotQueue<V, P, H>::HotQueue(const P &change, std::size_t levels)
228 : m_size(0), m_heapSize(0),
229  m_buckets(levels, nullptr),
230  m_heapedBucket(NONE), m_lastBucket(0),
231  m_bucketSpan((int) std::round(change / (levels - 1)))
232 {
233 }
234 
235 template<typename V, typename P, template<typename T, typename C> class H>
237 {
238  if (empty()) {
239  return;
240  }
241  for (auto &bucket : m_buckets) {
242  if (bucket != nullptr) {
243  for (HotQueueNode<V, P> *it = bucket; it != nullptr;) {
244  HotQueueNode<V, P> *next = it->next;
245  delete it;
246  it = next;
247  }
248  }
249  }
250 }
251 
252 template<typename V, typename P, template<typename T, typename C> class H>
253 inline const V &HotQueue<V, P, H>::top() const
254 {
255  return std::get<0>(m_heap.top());
256 }
257 
258 
259 template<typename V, typename P, template<typename T, typename C> class H>
261  const V &value, const P &priority)
262 {
263  m_size++;
264 
265  std::size_t ind = bucketIndex(priority);
266 
267  if(m_heapedBucket == NONE) {
268  m_heapedBucket = ind;
269  }
270 
271  if(ind == m_heapedBucket) {
272  m_heapSize++;
273  HeapHandle handle = m_heap.push(std::make_pair(value, priority));
274  return Handle(handle);
275  } else {
276  HotQueueNode<V, P> *queueNode = new HotQueueNode<V, P>(value, priority);
277 
278  if(bucketAt(ind) != nullptr) {
279  bucketAt(ind)->prev = queueNode;
280  }
281  queueNode->next = bucketAt(ind);
282  bucketAt(ind) = queueNode;
283 
284  m_lastBucket = std::max(m_lastBucket, ind);
285  return Handle(ind, queueNode);
286  }
287 }
288 
289 
290 template<typename V, typename P, template<typename T, typename C> class H>
292 {
293  m_size--;
294 
295  m_heap.pop();
296  m_heapSize--;
297 
298  /*
299  * If the heap became empty and there is non-empty bucket afterwards
300  * we need to heapify first non-empty bucket. If the heap became empty
301  * but there is no non-empty bucket afterwards it may be the case that
302  * element will be inserted into the current heap (therefore, do nothing).
303  */
304  if(!(m_heapSize == 0 && m_heapedBucket != m_lastBucket)) {
305  return;
306  }
307 
308  // Find first non-empty bucket.
309  do {
310  m_heapedBucket++;
311  } while(bucketAt(m_heapedBucket) == nullptr);
312 
313  // Move bucket contents into a heap.
314  for(HotQueueNode<V, P> *it = bucketAt(m_heapedBucket); it != nullptr;) {
315  m_heapSize++;
316  m_heap.push(std::make_pair(it->value, it->priority));
317 
318  HotQueueNode<V, P> *next = it->next;
319  delete it;
320  it = next;
321  }
322  bucketAt(m_heapedBucket) = nullptr;
323 }
324 
325 
326 template<typename V, typename P, template<typename T, typename C> class H>
328  typename HotQueue<V, P, H>::Handle &handle,
329  const P &priority)
330 {
331  switch(handle.type) {
332  case Handle::Type::heap: {
333  // Simple case, just use native heap decrease key.
334  const HeapHandle &elem = handle.heapHandle;
335  m_heap.decrease(elem, std::make_pair(
336  std::get<0>(m_heap.value(elem)), priority)
337  );
338  break;
339  }
341  // Remove element from bucket (as with ordinary linked-list)...
342  HotQueueNode<V, P> *queueNode = std::get<1>(handle.bucketHandle);
343  if(queueNode->next != nullptr) {
344  queueNode->next->prev = queueNode->prev;
345  }
346  if(queueNode->prev != nullptr) {
347  queueNode->prev->next = queueNode->next;
348  } else {
349  m_buckets[std::get<0>(handle.bucketHandle)] = queueNode->next;
350  }
351 
352  // ... and push it again with new priority, releasing the memory.
353  m_size--;
354  handle = push(queueNode->value, priority);
355  delete queueNode;
356  break;
357  }
358 }
359 
360 }
Heap-on-Top handle to inserted items.
Definition: HotQueue.h:76
enum ogdf::HotQueueHandle::Type type
Union tag.
HotQueueNode< V, P > * next
Definition: HotQueue.h:51
std::size_t m_heapedBucket
Index of currently heaped bucket.
Definition: HotQueue.h:207
Definition: GML.h:110
H< std::pair< V, P >, HeapComparator > m_heap
Underlying heap structure.
Definition: HotQueue.h:143
HotQueueHandle< V, P, HeapHandle > Handle
Definition: HotQueue.h:149
void pop()
Removes the top element from the heap.
Definition: HotQueue.h:291
std::size_t m_size
Number of total elements in the heap.
Definition: HotQueue.h:141
static constexpr std::size_t NONE
Definition: HotQueue.h:222
bool operator()(const std::pair< V, P > &a, const std::pair< V, P > &b) const
Definition: HotQueue.h:202
HotQueueHandle & operator=(const HotQueueHandle &other)
Definition: HotQueue.h:102
typename H< std::pair< V, P >, HeapComparator >::Handle HeapHandle
Definition: HotQueue.h:139
Handle push(const V &value, const P &priority)
Inserts a new node with given value and priority into a heap.
Definition: HotQueue.h:260
std::size_t bucketIndex(const P &priority)
Computes bucket index of given priority.
Definition: HotQueue.h:213
HotQueue(const P &change, std::size_t levels)
Creates empty Heap-on-Top queue.
Definition: HotQueue.h:227
HotQueueHandle(HeapHandle handle)
Creates heap-type handle.
Definition: HotQueue.h:91
void decrease(Handle &handle, const P &priority)
Decreases value of the given handle to priority.
Definition: HotQueue.h:327
HotQueueHandle(std::size_t index, HotQueueNode< V, P > *queueNode)
Creates bucket-type handle.
Definition: HotQueue.h:97
std::size_t m_lastBucket
Index of highest, non-empty bucket.
Definition: HotQueue.h:208
const P m_bucketSpan
Length of the interval covered by each bucket.
Definition: HotQueue.h:210
HeapHandle heapHandle
Handle to underlying heap.
Definition: HotQueue.h:85
HotQueueNode< V, P > *& bucketAt(std::size_t index)
Provides access to bucket at given index.
Definition: HotQueue.h:218
std::vector< HotQueueNode< V, P > * > m_buckets
Array of buckets.
Definition: HotQueue.h:146
HotQueueNode(const V &val, const P &pr)
Definition: HotQueue.h:58
std::size_t size() const
Number of elements contained within the heap.
Definition: HotQueue.h:190
~HotQueue()
Releases all buckets on destruction.
Definition: HotQueue.h:236
std::pair< std::size_t, HotQueueNode< V, P > * > bucketHandle
Handle to bucket element (bucket index and list iterator).
Definition: HotQueue.h:87
Heap-on-Top queue implementation.
Definition: HotQueue.h:136
const V & top() const
Returns reference to the top element in the heap.
Definition: HotQueue.h:253
std::size_t m_heapSize
Size of underlying heap.
Definition: HotQueue.h:144
The namespace for all OGDF objects.
HotQueueNode< V, P > * prev
Definition: HotQueue.h:50
Heap-on-Top bucket element.
Definition: HotQueue.h:46
bool empty() const
Checks whether the heap is empty.
Definition: HotQueue.h:195
Comparator used by underlying heap.
Definition: HotQueue.h:201