yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Minisat::Heap< K, Comp, MkIndex > Class Template Reference

#include <Heap.h>

+ Collaboration diagram for Minisat::Heap< K, Comp, MkIndex >:

Public Member Functions

 Heap (const Comp &c, MkIndex _index=MkIndex())
 
int size () const
 
bool empty () const
 
bool inHeap (K k) const
 
int operator[] (int index) const
 
void decrease (K k)
 
void increase (K k)
 
void update (K k)
 
void insert (K k)
 
void remove (K k)
 
removeMin ()
 
void build (const vec< K > &ns)
 
void clear (bool dispose=false)
 

Private Member Functions

void percolateUp (int i)
 
void percolateDown (int i)
 

Static Private Member Functions

static int left (int i)
 
static int right (int i)
 
static int parent (int i)
 

Private Attributes

vec< K > heap
 
IntMap< K, int, MkIndex > indices
 
Comp lt
 

Detailed Description

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
class Minisat::Heap< K, Comp, MkIndex >

Definition at line 34 of file Heap.h.

Constructor & Destructor Documentation

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
Minisat::Heap< K, Comp, MkIndex >::Heap ( const Comp &  c,
MkIndex  _index = MkIndex() 
)
inline

Definition at line 77 of file Heap.h.

77 : indices(_index), lt(c) {}
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
Comp lt
Definition: Heap.h:37

Member Function Documentation

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
void Minisat::Heap< K, Comp, MkIndex >::build ( const vec< K > &  ns)
inline

Definition at line 140 of file Heap.h.

140  {
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  }
void percolateDown(int i)
Definition: Heap.h:61
bool has(K k) const
Definition: IntMap.h:37
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
void push(void)
Definition: Vec.h:74
Size size(void) const
Definition: Vec.h:64
void clear(bool dealloc=false)
Definition: Vec.h:125
vec< K > heap
Definition: Heap.h:35
template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
void Minisat::Heap< K, Comp, MkIndex >::clear ( bool  dispose = false)
inline

Definition at line 155 of file Heap.h.

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  }
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
Size size(void) const
Definition: Vec.h:64
void clear(bool dealloc=false)
Definition: Vec.h:125
vec< K > heap
Definition: Heap.h:35
template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
void Minisat::Heap< K, Comp, MkIndex >::decrease ( k)
inline

Definition at line 84 of file Heap.h.

84 { assert(inHeap(k)); percolateUp (indices[k]); }
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
bool inHeap(K k) const
Definition: Heap.h:81
void percolateUp(int i)
Definition: Heap.h:45
template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
bool Minisat::Heap< K, Comp, MkIndex >::empty ( ) const
inline

Definition at line 80 of file Heap.h.

80 { return heap.size() == 0; }
Size size(void) const
Definition: Vec.h:64
vec< K > heap
Definition: Heap.h:35
template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
void Minisat::Heap< K, Comp, MkIndex >::increase ( k)
inline

Definition at line 85 of file Heap.h.

85 { assert(inHeap(k)); percolateDown(indices[k]); }
void percolateDown(int i)
Definition: Heap.h:61
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
bool inHeap(K k) const
Definition: Heap.h:81
template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
bool Minisat::Heap< K, Comp, MkIndex >::inHeap ( k) const
inline

Definition at line 81 of file Heap.h.

81 { return indices.has(k) && indices[k] >= 0; }
bool has(K k) const
Definition: IntMap.h:37
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36

+ Here is the caller graph for this function:

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
void Minisat::Heap< K, Comp, MkIndex >::insert ( k)
inline

Definition at line 99 of file Heap.h.

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  }
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
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
void push(void)
Definition: Vec.h:74
Size size(void) const
Definition: Vec.h:64
vec< K > heap
Definition: Heap.h:35

+ Here is the caller graph for this function:

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
static int Minisat::Heap< K, Comp, MkIndex >::left ( int  i)
inlinestaticprivate

Definition at line 40 of file Heap.h.

40 { return i*2+1; }

+ Here is the caller graph for this function:

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
int Minisat::Heap< K, Comp, MkIndex >::operator[] ( int  index) const
inline

Definition at line 82 of file Heap.h.

82 { assert(index < heap.size()); return heap[index]; }
Size size(void) const
Definition: Vec.h:64
vec< K > heap
Definition: Heap.h:35
template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
static int Minisat::Heap< K, Comp, MkIndex >::parent ( int  i)
inlinestaticprivate

Definition at line 42 of file Heap.h.

42 { return (i-1) >> 1; }

+ Here is the caller graph for this function:

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
void Minisat::Heap< K, Comp, MkIndex >::percolateDown ( int  i)
inlineprivate

Definition at line 61 of file Heap.h.

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  }
static int left(int i)
Definition: Heap.h:40
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
Comp lt
Definition: Heap.h:37
static int right(int i)
Definition: Heap.h:41
Size size(void) const
Definition: Vec.h:64
vec< K > heap
Definition: Heap.h:35

+ Here is the caller graph for this function:

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
void Minisat::Heap< K, Comp, MkIndex >::percolateUp ( int  i)
inlineprivate

Definition at line 45 of file Heap.h.

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  }
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
Comp lt
Definition: Heap.h:37
static int parent(int i)
Definition: Heap.h:42
vec< K > heap
Definition: Heap.h:35

+ Here is the caller graph for this function:

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
void Minisat::Heap< K, Comp, MkIndex >::remove ( k)
inline

Definition at line 110 of file Heap.h.

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  }
void percolateDown(int i)
Definition: Heap.h:61
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
const T & last(void) const
Definition: Vec.h:84
bool inHeap(K k) const
Definition: Heap.h:81
Size size(void) const
Definition: Vec.h:64
vec< K > heap
Definition: Heap.h:35
template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
K Minisat::Heap< K, Comp, MkIndex >::removeMin ( )
inline

Definition at line 127 of file Heap.h.

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  }
void percolateDown(int i)
Definition: Heap.h:61
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
const T & last(void) const
Definition: Vec.h:84
vec< K > heap
Definition: Heap.h:35
template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
static int Minisat::Heap< K, Comp, MkIndex >::right ( int  i)
inlinestaticprivate

Definition at line 41 of file Heap.h.

41 { return (i+1)*2; }

+ Here is the caller graph for this function:

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
int Minisat::Heap< K, Comp, MkIndex >::size ( ) const
inline

Definition at line 79 of file Heap.h.

79 { return heap.size(); }
Size size(void) const
Definition: Vec.h:64
vec< K > heap
Definition: Heap.h:35
template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
void Minisat::Heap< K, Comp, MkIndex >::update ( k)
inline

Definition at line 89 of file Heap.h.

90  {
91  if (!inHeap(k))
92  insert(k);
93  else {
94  percolateUp(indices[k]);
95  percolateDown(indices[k]); }
96  }
void percolateDown(int i)
Definition: Heap.h:61
void insert(K k)
Definition: Heap.h:99
IntMap< K, int, MkIndex > indices
Definition: Heap.h:36
bool inHeap(K k) const
Definition: Heap.h:81
void percolateUp(int i)
Definition: Heap.h:45

Field Documentation

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
vec<K> Minisat::Heap< K, Comp, MkIndex >::heap
private

Definition at line 35 of file Heap.h.

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
IntMap<K,int,MkIndex> Minisat::Heap< K, Comp, MkIndex >::indices
private

Definition at line 36 of file Heap.h.

template<class K, class Comp, class MkIndex = MkIndexDefault<K>>
Comp Minisat::Heap< K, Comp, MkIndex >::lt
private

Definition at line 37 of file Heap.h.


The documentation for this class was generated from the following file: