COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
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
6template <class Key, class Value>
7struct KNElement {Key key; Value value;};
8
9// fixed size binary heap
10template <class Key, class Value>
11class Heap2 {
12 // static const Key infimum = 4;
13 //static const Key supremum = numeric_limits<Key>.max();
14 typedef KNElement<Key, Value> Element;
15 Element *data;
16 int capacity;
17 int supremum;
18 int size; // index of last used element
19public:
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; }
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
52template <class Key, class Value>
53inline void Heap2<Key, Value>::
54reset() {
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
68template <class Key, class Value>
69inline void Heap2<Key, 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
101template <class Key, class Value>
102inline void Heap2<Key, Value>::
103sortTo(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
139template <class Key, class Value>
140inline void Heap2<Key, Value>::
141insert(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
int getSize() const
Definition heap-CLR.h:30
void sortTo(Element *to)
void deleteMinBasic()
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)
~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