COMBINATORIAL_BLAS 1.6
 
Loading...
Searching...
No Matches
heap2.h
Go to the documentation of this file.
1// 4ary heap data structure
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};
43
44
45// reset size to 0 and fill data array with sentinels
46template <class Key, class Value>
47inline void Heap2<Key, Value>::
48reset() {
49 size = 0;
50 Key sup = getSupremum();
51 int cap = capacity;
52 for (int i = 1; i <= cap; i++) {
53 data[i].key = sup;
54 }
55 // if this becomes a bottle neck
56 // we might want to replace this by log KNN
57 // memcpy-s
58}
59
60template <class Key, class Value>
61inline void Heap2<Key, Value>::
63{
64 Assert2(size > 0);
65
66 // first move up elements on a min-path
67 int hole = 1;
68 int succ = 2;
69 int sz = size;
70 Element *dat = data;
71 while (succ < sz) {
72 Key key1 = dat[succ].key;
73 Key key2 = dat[succ + 1].key;
74 if (key1 > key2) {
75 succ++;
76 dat[hole].key = key2;
77 dat[hole].value = dat[succ].value;
78 } else {
79 dat[hole].key = key1;
80 dat[hole].value = dat[succ].value;
81 }
82 hole = succ;
83 succ <<= 1;
84 }
85
86 // bubble up rightmost element
87 Key bubble = dat[sz].key;
88 int pred = hole >> 1;
89 while (dat[pred].key > bubble) { // must terminate since min at root
90 dat[hole] = dat[pred];
91 hole = pred;
92 pred >>= 1;
93 }
94
95 // finally move data to hole
96 dat[hole].key = bubble;
97 dat[hole].value = dat[sz].value;
98
99 dat[size].key = getSupremum(); // mark as deleted
100 size = sz - 1;
101}
102
103
104// empty the heap and put the element to "to"
105// sorted in increasing order
106template <class Key, class Value>
107inline void Heap2<Key, Value>::
108sortTo(Element *to)
109{
110 const int sz = size;
111 const Key sup = getSupremum();
112 Element * const beyond = to + sz;
113 Element * const root = data + 1;
114 while (to < beyond) {
115 // copy minimun
116 *to = *root;
117 to++;
118
119 // bubble up second smallest as in deleteMin
120 int hole = 1;
121 int succ = 2;
122 while (succ <= sz) {
123 Key key1 = data[succ ].key;
124 Key key2 = data[succ + 1].key;
125 if (key1 > key2) {
126 succ++;
127 data[hole].key = key2;
128 data[hole].value = data[succ].value;
129 } else {
130 data[hole].key = key1;
131 data[hole].value = data[succ].value;
132 }
133 hole = succ;
134 succ <<= 1;
135 }
136
137 // just mark hole as deleted
138 data[hole].key = sup;
139 }
140 size = 0;
141}
142
143
144template <class Key, class Value>
145inline void Heap2<Key, Value>::
146insert(Key k, Value v)
147{
148 Assert2(size < capacity);
149 Debug4(cout << "insert(" << k << ", " << v << ")" << endl);
150
151 Element *dat = data;
152 size++;
153 int hole = size;
154 int pred = hole >> 1;
155 Key predKey = dat[pred].key;
156 while (predKey > k) { // must terminate due to sentinel at 0
157 dat[hole].key = predKey;
158 dat[hole].value = dat[pred].value;
159 hole = pred;
160 pred >>= 1;
161 predKey = dat[pred].key;
162 }
163
164 // finally move data to hole
165 dat[hole].key = k;
166 dat[hole].value = v;
167}
168
169#endif
int getSize() const
Definition heap2.h:30
void sortTo(Element *to)
void deleteMinBasic()
Heap2(Key sup, Key infimum, int cap)
Definition heap2.h:20
void reset()
Definition heap-CLR.h:54
Key getSupremum()
Definition heap2.h:28
Value getMinValue() const
Definition heap2.h:32
void insert(Key k, Value v)
~Heap2()
Definition heap2.h:27
void deleteMin(Key *key, Value *value)
Definition heap2.h:34
Key getMinKey() const
Definition heap2.h:31
int size
Definition common.h:20
#define Debug4(A)
Definition util.h:44
#define Assert2(C)
Definition util.h:61
Value value
Definition heap-CLR.h:7
Key key
Definition heap-CLR.h:7