-
Notifications
You must be signed in to change notification settings - Fork 8
/
ArrayLinearHeap.h
182 lines (141 loc) · 3.95 KB
/
ArrayLinearHeap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* ArrayLinearHeap.h
*
* Created on: 5Dec.,2017
* Author: Lijun Chang
* Email: ljchang@outlook.com
*/
#ifndef DATA_STRUCTURES_ARRAYLINEARHEAP_H_
#define DATA_STRUCTURES_ARRAYLINEARHEAP_H_
#include "../utilities/Defines.h"
class ArrayLinearHeap {
private:
ui n; // number vertices
ui key_cap; // the maximum allowed key value
ui max_key; // possible max key
ui min_key; // possible min key
ui *keys; // key of vertices
ui *heads; // head of array of all vertices with a specific weight
ui *ids; // ids of vertices in non-decreasing key order
ui *rids; // reverse of id, i.e., the position of vertex in the ids array
public:
ArrayLinearHeap(ui _n, ui _key_cap) {
n = _n;
key_cap = _key_cap;
max_key = 0; min_key = key_cap;
heads = ids = rids = keys = nullptr;
}
~ArrayLinearHeap() {
if(heads != nullptr) {
delete[] heads;
heads = nullptr;
}
if(ids != nullptr) {
delete[] ids;
ids = nullptr;
}
if(rids != nullptr) {
delete[] rids;
rids = nullptr;
}
if(keys != nullptr) {
delete[] keys;
keys = nullptr;
}
}
void init(ui _n, ui _key_cap, ui *_ids, ui *_keys) {
if(keys == nullptr) keys = new ui[n];
if(ids == nullptr) ids = new ui[n];
if(rids == nullptr) rids = new ui[n];
if(heads == nullptr) heads = new ui[key_cap+2];
max_key = 0; min_key = _key_cap;
ui *cnt = heads;
memset(cnt, 0, sizeof(ui)*(_key_cap+1));
for(ui i = 0;i < _n;i ++) {
ui key = _keys[i];
keys[_ids[i]] = key;
assert(key <= _key_cap);
++ cnt[key];
if(key > max_key) max_key = key;
if(key < min_key) min_key = key;
}
for(ui i = 1;i <= max_key;i ++) cnt[i] += cnt[i-1];
for(ui i = 0;i < _n;i ++) rids[_ids[i]] = -- cnt[keys[i]];
for(ui i = 0;i < _n;i ++) ids[rids[_ids[i]]] = _ids[i];
for(ui i = 0, j = 0;i <= max_key + 1;i ++) {
while(j < _n&&keys[ids[j]] < i) ++ j;
heads[i] = j;
}
}
ui get_n() { return n; }
ui get_key_cap() { return key_cap; }
ui get_key(ui id) { return keys[id]; }
bool empty() {
while(min_key <= max_key&&heads[min_key] >= heads[min_key+1]) ++ min_key;
while(min_key <= max_key&&heads[max_key] >= heads[max_key+1]) -- max_key;
return min_key > max_key;
}
// get the (id,key) pair with the maximum key value; return true if success, return false otherwise
bool get_max(ui &id, ui &key) {
if(empty()) return false;
id = ids[heads[max_key+1] - 1];
key = max_key;
assert(keys[id] == key);
return true;
}
// pop the (id,key) pair with the maximum key value; return true if success, return false otherwise
bool pop_max(ui &id, ui &key) {
if(empty()) return false;
id = ids[-- heads[max_key+1]];
key = max_key;
assert(keys[id] == key);
return true;
}
// get the (id,key) pair with the minimum key value; return true if success, return false otherwise
bool get_min(ui &id, ui &key) {
if(empty()) return false;
id = ids[heads[min_key]];
key = min_key;
assert(keys[id] == key);
return true;
}
// pop the (id,key) pair with the minimum key value; return true if success, return false otherwise
bool pop_min(ui &id, ui &key) {
if(empty()) return false;
id = ids[heads[min_key] ++];
key = min_key;
assert(keys[id] == key);
return true;
}
// increment the key of vertex id by 1
ui increment(ui id) {
assert(keys[id] + 1 <= key_cap);
ui &key = keys[id];
ui pos1 = heads[key+1]-1, pos2 = rids[id];
std::swap(ids[pos1], ids[pos2]);
rids[ids[pos1]] = pos1; rids[ids[pos2]] = pos2;
if(max_key == key) {
++ max_key;
heads[max_key+1] = heads[max_key];
}
++ key;
-- heads[key];
return keys[id];
}
// decrement the key of vertex id by 1
ui decrement(ui id) {
assert(keys[id] >= 1);
ui &key = keys[id];
ui pos1 = heads[key], pos2 = rids[id];
std::swap(ids[pos1], ids[pos2]);
rids[ids[pos1]] = pos1; rids[ids[pos2]] = pos2;
if(min_key == key) {
-- min_key;
heads[min_key] = heads[min_key+1];
}
++ heads[key];
-- key;
return keys[id];
}
};
#endif /* DATA_STRUCTURES_ARRAYLINEARHEAP_H_ */