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

#include <Heap.h>

Public Member Functions

 Heap (const Comp &c)
 
int size () const
 
bool empty () const
 
bool inHeap (int n) const
 
int operator[] (int index) const
 
void decrease (int n)
 
void increase (int n)
 
void update (int n)
 
void insert (int n)
 
int removeMin ()
 
void build (vec< int > &ns)
 
void clear (bool dealloc=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

Comp lt
 
vec< int > heap
 
vec< int > indices
 

Detailed Description

template<class Comp>
class Minisat::Heap< Comp >

Definition at line 33 of file Heap.h.

Constructor & Destructor Documentation

template<class Comp>
Minisat::Heap< Comp >::Heap ( const Comp &  c)
inline

Definition at line 76 of file Heap.h.

76 : lt(c) { }
Comp lt
Definition: Heap.h:34

Member Function Documentation

template<class Comp>
void Minisat::Heap< Comp >::build ( vec< int > &  ns)
inline

Definition at line 123 of file Heap.h.

123  {
124  int i;
125  for (i = 0; i < heap.size(); i++)
126  indices[heap[i]] = -1;
127  heap.clear();
128 
129  for (i = 0; i < ns.size(); i++){
130  indices[ns[i]] = i;
131  heap.push(ns[i]); }
132 
133  for (i = heap.size() / 2 - 1; i >= 0; i--)
134  percolateDown(i);
135  }
void push(void)
Definition: Vec.h:73
int size(void) const
Definition: Vec.h:63
vec< int > indices
Definition: Heap.h:36
void percolateDown(int i)
Definition: Heap.h:60
void clear(bool dealloc=false)
Definition: Vec.h:121
vec< int > heap
Definition: Heap.h:35
template<class Comp>
void Minisat::Heap< Comp >::clear ( bool  dealloc = false)
inline

Definition at line 137 of file Heap.h.

138  {
139  for (int i = 0; i < heap.size(); i++)
140  indices[heap[i]] = -1;
141  heap.clear(dealloc);
142  }
int size(void) const
Definition: Vec.h:63
vec< int > indices
Definition: Heap.h:36
void clear(bool dealloc=false)
Definition: Vec.h:121
vec< int > heap
Definition: Heap.h:35
template<class Comp>
void Minisat::Heap< Comp >::decrease ( int  n)
inline

Definition at line 84 of file Heap.h.

84 { assert(inHeap(n)); percolateUp (indices[n]); }
vec< int > indices
Definition: Heap.h:36
void percolateUp(int i)
Definition: Heap.h:44
bool inHeap(int n) const
Definition: Heap.h:80
#define assert(ex)
Definition: util_old.h:213
template<class Comp>
bool Minisat::Heap< Comp >::empty ( ) const
inline

Definition at line 79 of file Heap.h.

79 { return heap.size() == 0; }
int size(void) const
Definition: Vec.h:63
vec< int > heap
Definition: Heap.h:35
template<class Comp>
void Minisat::Heap< Comp >::increase ( int  n)
inline

Definition at line 85 of file Heap.h.

85 { assert(inHeap(n)); percolateDown(indices[n]); }
vec< int > indices
Definition: Heap.h:36
void percolateDown(int i)
Definition: Heap.h:60
bool inHeap(int n) const
Definition: Heap.h:80
#define assert(ex)
Definition: util_old.h:213
template<class Comp>
bool Minisat::Heap< Comp >::inHeap ( int  n) const
inline

Definition at line 80 of file Heap.h.

80 { return n < indices.size() && indices[n] >= 0; }
int size(void) const
Definition: Vec.h:63
vec< int > indices
Definition: Heap.h:36
template<class Comp>
void Minisat::Heap< Comp >::insert ( int  n)
inline

Definition at line 99 of file Heap.h.

100  {
101  indices.growTo(n+1, -1);
102  assert(!inHeap(n));
103 
104  indices[n] = heap.size();
105  heap.push(n);
106  percolateUp(indices[n]);
107  }
void push(void)
Definition: Vec.h:73
int size(void) const
Definition: Vec.h:63
vec< int > indices
Definition: Heap.h:36
void percolateUp(int i)
Definition: Heap.h:44
bool inHeap(int n) const
Definition: Heap.h:80
vec< int > heap
Definition: Heap.h:35
#define assert(ex)
Definition: util_old.h:213
void growTo(int size)
Definition: Vec.h:113
template<class Comp>
static int Minisat::Heap< Comp >::left ( int  i)
inlinestaticprivate

Definition at line 39 of file Heap.h.

39 { return i*2+1; }
template<class Comp>
int Minisat::Heap< Comp >::operator[] ( int  index) const
inline

Definition at line 81 of file Heap.h.

81 { assert(index < heap.size()); return heap[index]; }
int size(void) const
Definition: Vec.h:63
vec< int > heap
Definition: Heap.h:35
#define assert(ex)
Definition: util_old.h:213
template<class Comp>
static int Minisat::Heap< Comp >::parent ( int  i)
inlinestaticprivate

Definition at line 41 of file Heap.h.

41 { return (i-1) >> 1; }
template<class Comp>
void Minisat::Heap< Comp >::percolateDown ( int  i)
inlineprivate

Definition at line 60 of file Heap.h.

61  {
62  int x = heap[i];
63  while (left(i) < heap.size()){
64  int child = right(i) < heap.size() && lt(heap[right(i)], heap[left(i)]) ? right(i) : left(i);
65  if (!lt(heap[child], x)) break;
66  heap[i] = heap[child];
67  indices[heap[i]] = i;
68  i = child;
69  }
70  heap [i] = x;
71  indices[x] = i;
72  }
int size(void) const
Definition: Vec.h:63
static int left(int i)
Definition: Heap.h:39
vec< int > indices
Definition: Heap.h:36
static int right(int i)
Definition: Heap.h:40
vec< int > heap
Definition: Heap.h:35
Comp lt
Definition: Heap.h:34
template<class Comp>
void Minisat::Heap< Comp >::percolateUp ( int  i)
inlineprivate

Definition at line 44 of file Heap.h.

45  {
46  int x = heap[i];
47  int p = parent(i);
48 
49  while (i != 0 && lt(x, heap[p])){
50  heap[i] = heap[p];
51  indices[heap[p]] = i;
52  i = p;
53  p = parent(p);
54  }
55  heap [i] = x;
56  indices[x] = i;
57  }
static Llb_Mgr_t * p
Definition: llb3Image.c:950
static int parent(int i)
Definition: Heap.h:41
vec< int > indices
Definition: Heap.h:36
vec< int > heap
Definition: Heap.h:35
Comp lt
Definition: Heap.h:34
template<class Comp>
int Minisat::Heap< Comp >::removeMin ( )
inline

Definition at line 110 of file Heap.h.

111  {
112  int x = heap[0];
113  heap[0] = heap.last();
114  indices[heap[0]] = 0;
115  indices[x] = -1;
116  heap.pop();
117  if (heap.size() > 1) percolateDown(0);
118  return x;
119  }
vec< int > indices
Definition: Heap.h:36
void percolateDown(int i)
Definition: Heap.h:60
vec< int > heap
Definition: Heap.h:35
const T & last(void) const
Definition: Vec.h:82
void pop(void)
Definition: Vec.h:76
template<class Comp>
static int Minisat::Heap< Comp >::right ( int  i)
inlinestaticprivate

Definition at line 40 of file Heap.h.

40 { return (i+1)*2; }
template<class Comp>
int Minisat::Heap< Comp >::size ( ) const
inline

Definition at line 78 of file Heap.h.

78 { return heap.size(); }
int size(void) const
Definition: Vec.h:63
vec< int > heap
Definition: Heap.h:35
template<class Comp>
void Minisat::Heap< Comp >::update ( int  n)
inline

Definition at line 89 of file Heap.h.

90  {
91  if (!inHeap(n))
92  insert(n);
93  else {
94  percolateUp(indices[n]);
95  percolateDown(indices[n]); }
96  }
vec< int > indices
Definition: Heap.h:36
void percolateDown(int i)
Definition: Heap.h:60
void insert(int n)
Definition: Heap.h:99
void percolateUp(int i)
Definition: Heap.h:44
bool inHeap(int n) const
Definition: Heap.h:80

Field Documentation

template<class Comp>
vec<int> Minisat::Heap< Comp >::heap
private

Definition at line 35 of file Heap.h.

template<class Comp>
vec<int> Minisat::Heap< Comp >::indices
private

Definition at line 36 of file Heap.h.

template<class Comp>
Comp Minisat::Heap< Comp >::lt
private

Definition at line 34 of file Heap.h.


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