heap.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include <algorithm>
  33. #include <vector>
  34. namespace cvflann
  35. {
  36. /**
  37. * Priority Queue Implementation
  38. *
  39. * The priority queue is implemented with a heap. A heap is a complete
  40. * (full) binary tree in which each parent is less than both of its
  41. * children, but the order of the children is unspecified.
  42. */
  43. template <typename T>
  44. class Heap
  45. {
  46. /**
  47. * Storage array for the heap.
  48. * Type T must be comparable.
  49. */
  50. std::vector<T> heap;
  51. int length;
  52. /**
  53. * Number of element in the heap
  54. */
  55. int count;
  56. public:
  57. /**
  58. * Constructor.
  59. *
  60. * Params:
  61. * sz = heap size
  62. */
  63. Heap(int sz)
  64. {
  65. length = sz;
  66. heap.reserve(length);
  67. count = 0;
  68. }
  69. /**
  70. *
  71. * Returns: heap size
  72. */
  73. int size()
  74. {
  75. return count;
  76. }
  77. /**
  78. * Tests if the heap is empty
  79. *
  80. * Returns: true is heap empty, false otherwise
  81. */
  82. bool empty()
  83. {
  84. return size()==0;
  85. }
  86. /**
  87. * Clears the heap.
  88. */
  89. void clear()
  90. {
  91. heap.clear();
  92. count = 0;
  93. }
  94. struct CompareT
  95. {
  96. bool operator()(const T& t_1, const T& t_2) const
  97. {
  98. return t_2 < t_1;
  99. }
  100. };
  101. /**
  102. * Insert a new element in the heap.
  103. *
  104. * We select the next empty leaf node, and then keep moving any larger
  105. * parents down until the right location is found to store this element.
  106. *
  107. * Params:
  108. * value = the new element to be inserted in the heap
  109. */
  110. void insert(T value)
  111. {
  112. /* If heap is full, then return without adding this element. */
  113. if (count == length) {
  114. return;
  115. }
  116. heap.push_back(value);
  117. static CompareT compareT;
  118. std::push_heap(heap.begin(), heap.end(), compareT);
  119. ++count;
  120. }
  121. /**
  122. * Returns the node of minimum value from the heap (top of the heap).
  123. *
  124. * Params:
  125. * value = out parameter used to return the min element
  126. * Returns: false if heap empty
  127. */
  128. bool popMin(T& value)
  129. {
  130. if (count == 0) {
  131. return false;
  132. }
  133. value = heap[0];
  134. static CompareT compareT;
  135. std::pop_heap(heap.begin(), heap.end(), compareT);
  136. heap.pop_back();
  137. --count;
  138. return true; /* Return old last node. */
  139. }
  140. };
  141. }
  142. #endif //OPENCV_FLANN_HEAP_H_