MaxHeap.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* Copyright The kNet Project.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License. */
  11. #pragma once
  12. /** @file MaxHeap.h
  13. @brief The MaxHeap<T> template class. */
  14. #include "Array.h"
  15. #include "SortCmp.h"
  16. namespace kNet
  17. {
  18. /** Used as a no-functionality structure to pass as the default template parameter to MaxHeap when no index update
  19. notifications are needed. You can create a custom notification structure to track handles to the elements as
  20. they move around in memory. */
  21. template<typename T>
  22. class EmptyLookupNotify
  23. {
  24. public:
  25. /// Called whenever an element in the MaxHeap is moved around.
  26. /// @param element The element that was moved around.
  27. /// @param newIndex The new index of the element.
  28. void IndexUpdated(const T &element, int newIndex)
  29. {
  30. MARK_UNUSED(element);
  31. MARK_UNUSED(newIndex);
  32. }
  33. };
  34. /** Implements a max heap data structure, see http://en.wikipedia.org/wiki/Binary_heap.
  35. MaxHeap is a (binary) heap structure that can be used as a priority queue. The PriorityCmp object
  36. is used to perform comparisons between elements.
  37. int PriorityCmp(const T &a, const T &b);
  38. Returns <0 if a<b and b has a bigger priority.
  39. Returns >0 if a>b and a has a bigger priority.
  40. Returns 0 if it is indifferent in which order the elements come out of the priority queue.
  41. Returning 0 doesn't mean that the elements need to be the same however.
  42. int EqualCmp(const T &a, const T &b);
  43. Returns 0 if the elements are equal, != 0 if not. If equal priority <=> equal elements, then you can
  44. use PriorityCmp as EqualCmp. */
  45. template<typename T, typename PriorityCmp = sort::TriCmpObj<T>,
  46. typename EqualCmp = sort::TriCmpObj<T>,
  47. typename LookupNotify = EmptyLookupNotify<T>,
  48. typename AllocT = StdCAlloc>
  49. class MaxHeap
  50. {
  51. public:
  52. /// Gives access to the queue as a linear array form.
  53. Array<T, AllocT> data;
  54. PriorityCmp cmp; // The object to use in comparing the priorities of the elements.
  55. EqualCmp equal; // The object to use in comparing the equality of the elements.
  56. // This object is invoked whenever objects are moved around in the heap. The LookupNotify object
  57. // can be used to give O(1) membership search. Useful when IncreaseKey needs to be used.
  58. LookupNotify notify;
  59. /// Gives the index of the parent of the element at index i, or 0 if i==0 (i is the root)
  60. static inline int ParentIndex(int i) { return (i+1)/2-1; }
  61. /// Gives the left child of the element at index i. Note that this will address out-of-bounds if there is no child.
  62. static inline int LeftChildIndex(int i) { return i*2+1; }
  63. /// Gives the right child of the element at index i. Note that this will address out-of-bounds if there is no child.
  64. static inline int RightChildIndex(int i) { return i*2+2; }
  65. /** MaxHeapifies one node of the heap at index i and recursively calls itself to MaxHeapify the child nodes,
  66. so that all child nodes also satisfy the MaxHeap property.
  67. @param i The index of the element to start MaxHeapifying at.
  68. @return True if changes (swaps) needed to be made to the heap at the given position, false otherwise. */
  69. bool MaxHeapify(int i);
  70. /// Call this after increasing the key at index i (giving a bigger priority to element at index i).
  71. /// This will re-maxheapify the structure and keep it intact.
  72. void KeyIncreased(int i);
  73. /// Inserts a new value into the MaxHeap and works out the proper position for it in the queue.
  74. /// Running time is O(logn).
  75. void Insert(const T &value);
  76. /// Removes the item that is front of the queue. UDB if the queue is empty.
  77. void PopFront();
  78. /// Removes the item that is back of the queue. UDB if the queue is empty.
  79. void PopBack();
  80. /// Gives access to the frontmost item. UDB if the queue is empty.
  81. const T &Front() const { return data[0]; }
  82. /// Gives access to the item with lowest priority. Running time is O(logN). UDB if the queue is empty.
  83. const T &Back() const { return data[LowestPriorityIndex()]; }
  84. /// @return The index of the element with the lowest priority. UDB if queue is empty.
  85. int LowestPriorityIndex() const;
  86. /// How many elements are in the queue total.
  87. int Size() const { return (int)data.size(); }
  88. /// As the underlying data structure is an array, you can use this to preallocate space.
  89. void Reserve(int size) { data.reserve(size); }
  90. void Clear() { data.clear(); }
  91. /** Looks whether an item that is equal with respect to the PriorityCmp is present in the MaxHeap.
  92. Running time is O(n).
  93. @return The index of the element where the first equaling item was found, or -1 if none was found. */
  94. int Search(const T &value) { return Search(value, 0); }
  95. private:
  96. /** Internal helper for Search to start the search at the given index in the array. */
  97. int Search(const T &value, int index);
  98. };
  99. template<typename T, typename PriorityCmp, typename EqualCmp, typename LookupNotify, typename AllocT>
  100. bool MaxHeap<T, PriorityCmp, EqualCmp, LookupNotify, AllocT>::MaxHeapify(int i)
  101. {
  102. assert(i >= 0);
  103. if (i >= (int)data.size())
  104. return false;
  105. bool changes = false;
  106. for(;;) // Eventually we will run out of children or no swap will occur.
  107. {
  108. // Get the indices to both children. Note that these might point to >= heapSize if a child is not present.
  109. const int l = LeftChildIndex(i);
  110. const int r = RightChildIndex(i);
  111. // Determine the largest node
  112. int largest = i;
  113. if (l < (int)data.size() && cmp(data[l], data[i]) > 0)
  114. largest = l;
  115. if (r < (int)data.size() && cmp(data[r], data[largest]) > 0)
  116. largest = r;
  117. // If i is not the largest node, switch and continue to MaxHeapify the child node.
  118. if (largest != i)
  119. {
  120. std::swap(data[largest], data[i]);
  121. notify.IndexUpdated(data[largest], largest);
  122. notify.IndexUpdated(data[i], i);
  123. i = largest;
  124. changes = true;
  125. }
  126. else
  127. return changes; // Return whether changes were made to heap.
  128. }
  129. }
  130. template<typename T, typename PriorityCmp, typename EqualCmp, typename LookupNotify, typename AllocT>
  131. void MaxHeap<T, PriorityCmp, EqualCmp, LookupNotify, AllocT>::Insert(const T &value)
  132. {
  133. // New element is added to the end of the array.
  134. data.push_back(value);
  135. notify.IndexUpdated(data.back(), (int)data.size()-1);
  136. // We need to fix the heap by MaxHeapifying all the way to the top.
  137. KeyIncreased((int)data.size()-1);
  138. }
  139. template<typename T, typename PriorityCmp, typename EqualCmp, typename LookupNotify, typename AllocT>
  140. void MaxHeap<T, PriorityCmp, EqualCmp, LookupNotify, AllocT>::KeyIncreased(int i)
  141. {
  142. // We need to fix the heap by MaxHeapifying all the way to the top.
  143. bool changesMade = true;
  144. while(changesMade && i != 0)
  145. {
  146. i = ParentIndex(i);
  147. changesMade = MaxHeapify(i);
  148. }
  149. }
  150. template<typename T, typename PriorityCmp, typename EqualCmp, typename LookupNotify, typename AllocT>
  151. void MaxHeap<T, PriorityCmp, EqualCmp, LookupNotify, AllocT>::PopFront()
  152. {
  153. std::swap(data[0], data[(int)data.size()-1]);
  154. data.pop_back();
  155. MaxHeapify(0);
  156. }
  157. template<typename T, typename PriorityCmp, typename EqualCmp, typename LookupNotify, typename AllocT>
  158. void MaxHeap<T, PriorityCmp, EqualCmp, LookupNotify, AllocT>::PopBack()
  159. {
  160. int i = LowestPriorityIndex();
  161. std::swap(data[i], data[data.size()-1]);
  162. data.pop_back();
  163. KeyIncreased(i);
  164. }
  165. template<typename T, typename PriorityCmp, typename EqualCmp, typename LookupNotify, typename AllocT>
  166. int MaxHeap<T, PriorityCmp, EqualCmp, LookupNotify, AllocT>::LowestPriorityIndex() const
  167. {
  168. int i = data.size();
  169. int lowestIndex = data.size()-1;
  170. do
  171. {
  172. --i;
  173. if (cmp(data[i], data[lowestIndex]) < 0)
  174. lowestIndex = i;
  175. } while((i & (i-1)) != 0);
  176. return lowestIndex;
  177. }
  178. template<typename T, typename PriorityCmp, typename EqualCmp, typename LookupNotify, typename AllocT>
  179. int MaxHeap<T, PriorityCmp, EqualCmp, LookupNotify, AllocT>::Search(const T &value, int index)
  180. {
  181. assert(index >= 0);
  182. if (index >= (int)data.size())
  183. return -1;
  184. int order = cmp(value, data[index]);
  185. if (order == 0 && equal(value, data[index]) == 0)
  186. return index;
  187. if (order > 0) // The element at data[index] < value, and all the children are also smaller, so
  188. return -1; // there is no chance of finding the element.
  189. // case order < 0:
  190. int child = LeftChildIndex(index);
  191. assert(child >= 0);
  192. if (child >= (int)data.size()) // No left child, so no right child either, return not found.
  193. return -1;
  194. int result = Search(value, child);
  195. if (result != -1) // found on the left branch?
  196. return result;
  197. child = RightChildIndex(index);
  198. assert(child >= 0);
  199. if (child < (int)data.size()) // If right child exists, search there too.
  200. return Search(value, child);
  201. else
  202. return -1;
  203. }
  204. } // ~kNet