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