yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Heap.h
Go to the documentation of this file.
1 /******************************************************************************************[Heap.h]
2 Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
3 Copyright (c) 2007-2010, Niklas Sorensson
4 
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:
10 
11 The above copyright notice and this permission notice shall be included in all copies or
12 substantial portions of the Software.
13 
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 **************************************************************************************************/
20 
21 #ifndef Minisat_Heap_h
22 #define Minisat_Heap_h
23 
24 #include "Vec.h"
25 #include "IntMap.h"
26 
27 namespace Minisat {
28 
29 //=================================================================================================
30 // A heap implementation with support for decrease/increase key.
31 
32 
33 template<class K, class Comp, class MkIndex = MkIndexDefault<K> >
34 class Heap {
35  vec<K> heap; // Heap of Keys
36  IntMap<K,int,MkIndex> indices; // Each Key's position (index) in the Heap
37  Comp lt; // The heap is a minimum-heap with respect to this comparator
38 
39  // Index "traversal" functions
40  static inline int left (int i) { return i*2+1; }
41  static inline int right (int i) { return (i+1)*2; }
42  static inline int parent(int i) { return (i-1) >> 1; }
43 
44 
45  void percolateUp(int i)
46  {
47  K x = heap[i];
48  int p = parent(i);
49 
50  while (i != 0 && lt(x, heap[p])){
51  heap[i] = heap[p];
52  indices[heap[p]] = i;
53  i = p;
54  p = parent(p);
55  }
56  heap [i] = x;
57  indices[x] = i;
58  }
59 
60 
61  void percolateDown(int i)
62  {
63  K x = heap[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];
68  indices[heap[i]] = i;
69  i = child;
70  }
71  heap [i] = x;
72  indices[x] = i;
73  }
74 
75 
76  public:
77  Heap(const Comp& c, MkIndex _index = MkIndex()) : indices(_index), lt(c) {}
78 
79  int size () const { return heap.size(); }
80  bool empty () const { return heap.size() == 0; }
81  bool inHeap (K k) const { return indices.has(k) && indices[k] >= 0; }
82  int operator[](int index) const { assert(index < heap.size()); return heap[index]; }
83 
84  void decrease (K k) { assert(inHeap(k)); percolateUp (indices[k]); }
85  void increase (K k) { assert(inHeap(k)); percolateDown(indices[k]); }
86 
87 
88  // Safe variant of insert/decrease/increase:
89  void update(K k)
90  {
91  if (!inHeap(k))
92  insert(k);
93  else {
94  percolateUp(indices[k]);
95  percolateDown(indices[k]); }
96  }
97 
98 
99  void insert(K k)
100  {
101  indices.reserve(k, -1);
102  assert(!inHeap(k));
103 
104  indices[k] = heap.size();
105  heap.push(k);
106  percolateUp(indices[k]);
107  }
108 
109 
110  void remove(K k)
111  {
112  assert(inHeap(k));
113 
114  int k_pos = indices[k];
115  indices[k] = -1;
116 
117  if (k_pos < heap.size()-1){
118  heap[k_pos] = heap.last();
119  indices[heap[k_pos]] = k_pos;
120  heap.pop();
121  percolateDown(k_pos);
122  }else
123  heap.pop();
124  }
125 
126 
128  {
129  K x = heap[0];
130  heap[0] = heap.last();
131  indices[heap[0]] = 0;
132  indices[x] = -1;
133  heap.pop();
134  if (heap.size() > 1) percolateDown(0);
135  return x;
136  }
137 
138 
139  // Rebuild the heap from scratch, using the elements in 'ns':
140  void build(const vec<K>& ns) {
141  for (int i = 0; i < heap.size(); i++)
142  indices[heap[i]] = -1;
143  heap.clear();
144 
145  for (int i = 0; i < ns.size(); i++){
146  // TODO: this should probably call reserve instead of relying on it being reserved already.
147  assert(indices.has(ns[i]));
148  indices[ns[i]] = i;
149  heap.push(ns[i]); }
150 
151  for (int i = heap.size() / 2 - 1; i >= 0; i--)
152  percolateDown(i);
153  }
154 
155  void clear(bool dispose = false)
156  {
157  // TODO: shouldn't the 'indices' map also be dispose-cleared?
158  for (int i = 0; i < heap.size(); i++)
159  indices[heap[i]] = -1;
160  heap.clear(dispose);
161  }
162 };
163 
164 
165 //=================================================================================================
166 }
167 
168 #endif
void percolateDown(int i)
Definition: Heap.h:61
void insert(K k)
Definition: Heap.h:99
int operator[](int index) const
Definition: Heap.h:82
bool has(K k) const
Definition: IntMap.h:37
bool empty() const
Definition: Heap.h:80
static int left(int i)
Definition: Heap.h:40
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
void build(const vec< K > &ns)
Definition: Heap.h:140
Comp lt
Definition: Heap.h:37
const T & last(void) const
Definition: Vec.h:84
static int right(int i)
Definition: Heap.h:41
void reserve(K key, V pad)
Definition: IntMap.h:47
bool inHeap(K k) const
Definition: Heap.h:81
void percolateUp(int i)
Definition: Heap.h:45
Heap(const Comp &c, MkIndex _index=MkIndex())
Definition: Heap.h:77
static int parent(int i)
Definition: Heap.h:42
void push(void)
Definition: Vec.h:74
K removeMin()
Definition: Heap.h:127
int size() const
Definition: Heap.h:79
Size size(void) const
Definition: Vec.h:64
void pop(void)
Definition: Vec.h:78
void decrease(K k)
Definition: Heap.h:84
void clear(bool dealloc=false)
Definition: Vec.h:125
void increase(K k)
Definition: Heap.h:85
void clear(bool dispose=false)
Definition: Heap.h:155
void update(K k)
Definition: Heap.h:89
vec< K > heap
Definition: Heap.h:35