You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.6 KiB

  1. /******************************************************************************************[Heap.h]
  2. Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
  3. Copyright (c) 2007-2010, Niklas Sorensson
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  5. associated documentation files (the "Software"), to deal in the Software without restriction,
  6. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  7. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all copies or
  10. substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  12. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  13. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  14. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  15. OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. **************************************************************************************************/
  17. #ifndef Minisat_Heap_h
  18. #define Minisat_Heap_h
  19. #include "mtl/Vec.h"
  20. namespace Minisat {
  21. //=================================================================================================
  22. // A heap implementation with support for decrease/increase key.
  23. template<class Comp>
  24. class Heap {
  25. Comp lt; // The heap is a minimum-heap with respect to this comparator
  26. vec<int> heap; // Heap of integers
  27. vec<int> indices; // Each integers position (index) in the Heap
  28. // Index "traversal" functions
  29. static inline int left (int i) { return i*2+1; }
  30. static inline int right (int i) { return (i+1)*2; }
  31. static inline int parent(int i) { return (i-1) >> 1; }
  32. void percolateUp(int i)
  33. {
  34. int x = heap[i];
  35. int p = parent(i);
  36. while (i != 0 && lt(x, heap[p])){
  37. heap[i] = heap[p];
  38. indices[heap[p]] = i;
  39. i = p;
  40. p = parent(p);
  41. }
  42. heap [i] = x;
  43. indices[x] = i;
  44. }
  45. void percolateDown(int i)
  46. {
  47. int x = heap[i];
  48. while (left(i) < heap.size()){
  49. int child = right(i) < heap.size() && lt(heap[right(i)], heap[left(i)]) ? right(i) : left(i);
  50. if (!lt(heap[child], x)) break;
  51. heap[i] = heap[child];
  52. indices[heap[i]] = i;
  53. i = child;
  54. }
  55. heap [i] = x;
  56. indices[x] = i;
  57. }
  58. public:
  59. Heap(const Comp& c) : lt(c) { }
  60. int size () const { return heap.size(); }
  61. bool empty () const { return heap.size() == 0; }
  62. bool inHeap (int n) const { return n < indices.size() && indices[n] >= 0; }
  63. int operator[](int index) const { assert(index < heap.size()); return heap[index]; }
  64. void decrease (int n) { assert(inHeap(n)); percolateUp (indices[n]); }
  65. void increase (int n) { assert(inHeap(n)); percolateDown(indices[n]); }
  66. // Safe variant of insert/decrease/increase:
  67. void update(int n)
  68. {
  69. if (!inHeap(n))
  70. insert(n);
  71. else {
  72. percolateUp(indices[n]);
  73. percolateDown(indices[n]); }
  74. }
  75. void insert(int n)
  76. {
  77. indices.growTo(n+1, -1);
  78. assert(!inHeap(n));
  79. indices[n] = heap.size();
  80. heap.push(n);
  81. percolateUp(indices[n]);
  82. }
  83. int removeMin()
  84. {
  85. int x = heap[0];
  86. heap[0] = heap.last();
  87. indices[heap[0]] = 0;
  88. indices[x] = -1;
  89. heap.pop();
  90. if (heap.size() > 1) percolateDown(0);
  91. return x;
  92. }
  93. // Rebuild the heap from scratch, using the elements in 'ns':
  94. void build(vec<int>& ns) {
  95. for (int i = 0; i < heap.size(); i++)
  96. indices[heap[i]] = -1;
  97. heap.clear();
  98. for (int i = 0; i < ns.size(); i++){
  99. indices[ns[i]] = i;
  100. heap.push(ns[i]); }
  101. for (int i = heap.size() / 2 - 1; i >= 0; i--)
  102. percolateDown(i);
  103. }
  104. void clear(bool dealloc = false)
  105. {
  106. for (int i = 0; i < heap.size(); i++)
  107. indices[heap[i]] = -1;
  108. heap.clear(dealloc);
  109. }
  110. };
  111. //=================================================================================================
  112. }
  113. #endif