COMBINATORIAL_BLAS  1.6
heap-CLR.h
Go to the documentation of this file.
1 // Binary heaps using a straightforward implementation a la CLR
2 #ifndef HEAP2
3 #define HEAP2
4 // #include "util.h"
5 
6 template <class Key, class Value>
7 struct KNElement {Key key; Value value;};
8 
9 // fixed size binary heap
10 template <class Key, class Value>
11 class Heap2 {
12  // static const Key infimum = 4;
13  //static const Key supremum = numeric_limits<Key>.max();
15  Element *data;
16  int capacity;
17  int supremum;
18  int size; // index of last used element
19 public:
20  Heap2(Key sup, Key infimum, int cap):size(0),capacity(cap) {
21  data = new Element[cap + 2];
22  data[0].key = infimum; // sentinel
23  data[capacity + 1].key = sup;
24  supremum = sup;
25  reset();
26  }
27  ~Heap2() { delete data; }
28  Key getSupremum() { return supremum; }
29  void reset();
30  int getSize() const { return size; }
31  Key getMinKey() const { return data[1].key; }
32  Value getMinValue() const { return data[1].value; }
33  void deleteMinBasic();
34  void deleteMin(Key *key, Value *value) {
35  *key = getMinKey();
36  *value = getMinValue();
38  }
39  void insert(Key k, Value v);
40  void sortTo(Element *to); // sort in increasing order and empty
41  //void sortInPlace(); // in decreasing order
42  void print() {
43  for (int i = 1; i <= size; i++) {
44  cout << data[i].key << " ";
45  }
46  cout << endl;
47  }
48 };
49 
50 
51 // reset size to 0 and fill data array with sentinels
52 template <class Key, class Value>
54 reset() {
55  size = 0;
56  Key sup = getSupremum();
57  int cap = capacity;
58  for (int i = 1; i <= cap; i++) {
59  data[i].key = sup;
60  }
61  // if this becomes a bottle neck
62  // we might want to replace this by log KNN
63  // memcpy-s
64 }
65 
66 
67 
68 template <class Key, class Value>
71 { int i, l, r, smallest;
72  Assert2(size > 0);
73 
74  data[1] = data[size];
75  data[size].key = getSupremum();
76  size--;
77  i = 1;
78  for (;;) {
79  Debug3(print());
80  l = (i << 1);
81  r = (i << 1) + 1;
82  if ((l <= size) && data[l].key < data[i].key) {
83  smallest = l;
84  } else {
85  smallest = i;
86  }
87  if ((r <= size) && data[r].key < data[smallest].key) {
88  smallest = r;
89  }
90  if (smallest == i) break;
91  Element temp = data[i];
92  data[i] = data[smallest];
93  data[smallest] = temp;
94  i = smallest;
95  }
96 }
97 
98 
99 // empty the heap and put the element to "to"
100 // sorted in increasing order
101 template <class Key, class Value>
103 sortTo(Element *to)
104 {
105  const int sz = size;
106  const Key sup = getSupremum();
107  Element * const beyond = to + sz;
108  Element * const root = data + 1;
109  while (to < beyond) {
110  // copy minimun
111  *to = *root;
112  to++;
113 
114  // bubble up second smallest as in deleteMin
115  int hole = 1;
116  int succ = 2;
117  while (succ <= sz) {
118  Key key1 = data[succ ].key;
119  Key key2 = data[succ + 1].key;
120  if (key1 > key2) {
121  succ++;
122  data[hole].key = key2;
123  data[hole].value = data[succ].value;
124  } else {
125  data[hole].key = key1;
126  data[hole].value = data[succ].value;
127  }
128  hole = succ;
129  succ <<= 1;
130  }
131 
132  // just mark hole as deleted
133  data[hole].key = sup;
134  }
135  size = 0;
136 }
137 
138 
139 template <class Key, class Value>
141 insert(Key k, Value v)
142 {
143  Assert2(size < capacity);
144  Debug4(cout << "insert(" << k << ", " << v << ")" << endl);
145 
146  size++;
147  int hole = size;
148  while (hole > 1 && data[hole >> 1].key > k) {
149  data[hole] = data[hole >> 1];
150  hole = hole >> 1;
151  }
152  data[hole].key = k;
153  data[hole].value = v;
154 }
155 
156 #endif
Definition: heap-CLR.h:11
int getSize() const
Definition: heap-CLR.h:30
void sortTo(Element *to)
Definition: heap-CLR.h:103
void deleteMinBasic()
Definition: heap-CLR.h:70
Heap2(Key sup, Key infimum, int cap)
Definition: heap-CLR.h:20
void reset()
Definition: heap-CLR.h:54
Key getSupremum()
Definition: heap-CLR.h:28
Value getMinValue() const
Definition: heap-CLR.h:32
void insert(Key k, Value v)
Definition: heap-CLR.h:141
~Heap2()
Definition: heap-CLR.h:27
void deleteMin(Key *key, Value *value)
Definition: heap-CLR.h:34
void print()
Definition: heap-CLR.h:42
Key getMinKey() const
Definition: heap-CLR.h:31
int size
Definition: common.h:20
#define Debug4(A)
Definition: util.h:44
#define Debug3(A)
Definition: util.h:37
#define Assert2(C)
Definition: util.h:61
Value value
Definition: heap-CLR.h:7
Key key
Definition: heap-CLR.h:7