heap.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /***********************************************************************
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright 2008-2009 Marius Muja (mariusm@cs.ubc.ca). All rights reserved.
  5. * Copyright 2008-2009 David G. Lowe (lowe@cs.ubc.ca). All rights reserved.
  6. *
  7. * THE BSD LICENSE
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. *************************************************************************/
  30. #ifndef OPENCV_FLANN_HEAP_H_
  31. #define OPENCV_FLANN_HEAP_H_
  32. //! @cond IGNORED
  33. #include <algorithm>
  34. #include <vector>
  35. namespace cvflann
  36. {
  37. /**
  38. * Priority Queue Implementation
  39. *
  40. * The priority queue is implemented with a heap. A heap is a complete
  41. * (full) binary tree in which each parent is less than both of its
  42. * children, but the order of the children is unspecified.
  43. */
  44. template <typename T>
  45. class Heap
  46. {
  47. /**
  48. * Storage array for the heap.
  49. * Type T must be comparable.
  50. */
  51. std::vector<T> heap;
  52. int length;
  53. /**
  54. * Number of element in the heap
  55. */
  56. int count;
  57. public:
  58. /**
  59. * Constructor.
  60. *
  61. * Params:
  62. * sz = heap size
  63. */
  64. Heap(int sz)
  65. {
  66. length = sz;
  67. heap.reserve(length);
  68. count = 0;
  69. }
  70. /**
  71. *
  72. * Returns: heap size
  73. */
  74. int size()
  75. {
  76. return count;
  77. }
  78. /**
  79. * Tests if the heap is empty
  80. *
  81. * Returns: true is heap empty, false otherwise
  82. */
  83. bool empty()
  84. {
  85. return size()==0;
  86. }
  87. /**
  88. * Clears the heap.
  89. */
  90. void clear()
  91. {
  92. heap.clear();
  93. count = 0;
  94. }
  95. struct CompareT
  96. {
  97. bool operator()(const T& t_1, const T& t_2) const
  98. {
  99. return t_2 < t_1;
  100. }
  101. };
  102. /**
  103. * Insert a new element in the heap.
  104. *
  105. * We select the next empty leaf node, and then keep moving any larger
  106. * parents down until the right location is found to store this element.
  107. *
  108. * Params:
  109. * value = the new element to be inserted in the heap
  110. */
  111. void insert(T value)
  112. {
  113. /* If heap is full, then return without adding this element. */
  114. if (count == length) {
  115. return;
  116. }
  117. heap.push_back(value);
  118. static CompareT compareT;
  119. std::push_heap(heap.begin(), heap.end(), compareT);
  120. ++count;
  121. }
  122. /**
  123. * Returns the node of minimum value from the heap (top of the heap).
  124. *
  125. * Params:
  126. * value = out parameter used to return the min element
  127. * Returns: false if heap empty
  128. */
  129. bool popMin(T& value)
  130. {
  131. if (count == 0) {
  132. return false;
  133. }
  134. value = heap[0];
  135. static CompareT compareT;
  136. std::pop_heap(heap.begin(), heap.end(), compareT);
  137. heap.pop_back();
  138. --count;
  139. return true; /* Return old last node. */
  140. }
  141. };
  142. }
  143. //! @endcond
  144. #endif //OPENCV_FLANN_HEAP_H_