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