ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
Heap2.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_Heap2_h
22#define Glucose_Heap2_h
23
24#include "sat/glucose2/Vec.h"
25
27
28namespace Gluco2 {
29
30//=================================================================================================
31// A heap implementation with support for decrease/increase key.
32
33
34template<class Comp, class Obj>
35class Heap2 {
36 Comp lt; // The heap is a minimum-heap with respect to this comparator
37 vec<Obj> 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 inline int data (int i) const { return heap[i].data();}
46
47 void percolateUp(int i)
48 {
49 Obj x = heap[i];
50 int p = parent(i);
51
52 while (i != 0 && lt(x, heap[p])){
53 heap[i] = heap[p];
54 indices[data(p)] = i;
55 i = p;
56 p = parent(p);
57 }
58 heap [i] = x;
59 indices[x.data()] = i;
60 }
61
62
63 void percolateDown(int i)
64 {
65 Obj x = heap[i];
66 while (left(i) < heap.size()){
67 int child = right(i) < heap.size() && lt(heap[right(i)], heap[left(i)]) ? right(i) : left(i);
68 if (!lt(heap[child], x)) break;
69 heap[i] = heap[child];
70 indices[data(i)] = i;
71 i = child;
72 }
73 heap [i] = x;
74 indices[x.data()] = i;
75 }
76
77
78 public:
79 Heap2(const Comp& c) : lt(c) { }
80
81 int size () const { return heap.size(); }
82 bool empty () const { return heap.size() == 0; }
83 bool inHeap (int n) const { return n < indices.size() && indices[n] >= 0; }
84 int operator[](int index) const { assert(index < heap.size()); return heap[index].data(); }
85
86
87 void decrease (int n) { assert(inHeap(n)); percolateUp (indices[n]); }
88 void increase (int n) { assert(inHeap(n)); percolateDown(indices[n]); }
89 void prelocate (int ext_cap){ indices.prelocate(ext_cap); }
90 int data_attr (int n) const { return heap[indices[n]].attr(); }
91 // Safe variant of insert/decrease/increase:
92 void update(const Obj& x)
93 {
94 int n = x.data();
95 if (!inHeap(n))
96 insert(x);
97 else {
98 heap[indices[x.data()]] = x;
99 percolateUp(indices[n]);
100 percolateDown(indices[n]); }
101 }
102
103
104 void insert(const Obj& x)
105 {
106 int n = x.data();
107 indices.growTo(n+1, -1);
108 assert(!inHeap(n));
109
110 indices[n] = heap.size();
111 heap.push(x);
112 percolateUp(indices[n]);
113 }
114
115 //Obj prev;
116 int removeMin(int& _attr)
117 {
118 Obj x = heap[0];
119 heap[0] = heap.last();
120 indices[heap[0].data()] = 0;
121 indices[x.data()]= -1;
122 heap.pop();
123 if (heap.size() > 1) percolateDown(0);
124 //prev = x;
125 _attr = x.attr();
126 return x.data();
127 }
128
129
130 // Rebuild the heap from scratch, using the elements in 'ns':
131// void build(vec<int>& ns) {
132// int i;
133// for (i = 0; i < heap.size(); i++)
134// indices[heap[i]] = -1;
135// heap.clear();
136//
137// for (i = 0; i < ns.size(); i++){
138// indices[ns[i]] = i;
139// heap.push(ns[i]); }
140//
141// for (i = heap.size() / 2 - 1; i >= 0; i--)
142// percolateDown(i);
143// }
144
145 void clear(bool dealloc = false)
146 {
147 int i;
148 for (i = 0; i < heap.size(); i++)
149 indices[heap[i].data()] = -1;
150 heap.clear(dealloc);
151 }
152 void clear_(bool dealloc = false)
153 {
154 int i;
155 for (i = 0; i < heap.size(); i++)
156 indices[heap[i].data()] = -1;
157
158 if( ! dealloc ) heap.shrink_( heap.size() );
159 else heap.clear(true);
160 }
161};
162
163
164//=================================================================================================
165}
166
168
169#endif
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
void insert(const Obj &x)
Definition Heap2.h:104
Heap2(const Comp &c)
Definition Heap2.h:79
void prelocate(int ext_cap)
Definition Heap2.h:89
void update(const Obj &x)
Definition Heap2.h:92
bool empty() const
Definition Heap2.h:82
int removeMin(int &_attr)
Definition Heap2.h:116
void decrease(int n)
Definition Heap2.h:87
void increase(int n)
Definition Heap2.h:88
int data_attr(int n) const
Definition Heap2.h:90
void clear(bool dealloc=false)
Definition Heap2.h:145
int operator[](int index) const
Definition Heap2.h:84
bool inHeap(int n) const
Definition Heap2.h:83
void clear_(bool dealloc=false)
Definition Heap2.h:152
int size() const
Definition Heap2.h:81
Cube * p
Definition exorList.c:222
Definition Alg.h:28
#define assert(ex)
Definition util_old.h:213