1 /******************************************************************************************[Heap.h]
2 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
6 associated documentation files (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge, publish, distribute,
8 sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15 NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
18 OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 **************************************************************************************************/
21 #ifndef Glucose_Heap_h
22 #define Glucose_Heap_h
28 //=================================================================================================
29 // A heap implementation with support for decrease/increase key.
34 Comp lt; // The heap is a minimum-heap with respect to this comparator
35 vec<int> heap; // Heap of integers
36 vec<int> indices; // Each integers position (index) in the Heap
38 // Index "traversal" functions
39 static inline int left (int i) { return i*2+1; }
40 static inline int right (int i) { return (i+1)*2; }
41 static inline int parent(int i) { return (i-1) >> 1; }
45 void percolateUp(int i)
50 while (i != 0 && lt(x, heap[p])){
61 void percolateDown(int i)
64 while (left(i) < heap.size()){
65 int child = right(i) < heap.size() && lt(heap[right(i)], heap[left(i)]) ? right(i) : left(i);
66 if (!lt(heap[child], x)) break;
67 heap[i] = heap[child];
77 Heap(const Comp& c) : lt(c) { }
79 int size () const { return heap.size(); }
80 bool empty () const { return heap.size() == 0; }
81 bool inHeap (int n) const { return n < indices.size() && indices[n] >= 0; }
82 int operator[](int index) const { assert(index < heap.size()); return heap[index]; }
85 void decrease (int n) { assert(inHeap(n)); percolateUp (indices[n]); }
86 void increase (int n) { assert(inHeap(n)); percolateDown(indices[n]); }
88 void copyTo(Heap& copy) const {heap.copyTo(copy.heap);indices.copyTo(copy.indices);}
90 // Safe variant of insert/decrease/increase:
96 percolateUp(indices[n]);
97 percolateDown(indices[n]); }
103 indices.growTo(n+1, -1);
106 indices[n] = heap.size();
108 percolateUp(indices[n]);
115 heap[0] = heap.last();
116 indices[heap[0]] = 0;
119 if (heap.size() > 1) percolateDown(0);
124 // Rebuild the heap from scratch, using the elements in 'ns':
125 void build(vec<int>& ns) {
126 for (int i = 0; i < heap.size(); i++)
127 indices[heap[i]] = -1;
130 for (int i = 0; i < ns.size(); i++){
134 for (int i = heap.size() / 2 - 1; i >= 0; i--)
138 void clear(bool dealloc = false)
140 for (int i = 0; i < heap.size(); i++)
141 indices[heap[i]] = -1;
147 //=================================================================================================