-
Notifications
You must be signed in to change notification settings - Fork 8
/
ListLinearHeap.h
217 lines (174 loc) · 4.75 KB
/
ListLinearHeap.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
* ListLinearHeap.h
*
* Created on: 5Dec.,2017
* Author: Lijun Chang
* Email: ljchang@outlook.com
*/
#ifndef DATA_STRUCTURES_LISTLINEARHEAP_H_
#define DATA_STRUCTURES_LISTLINEARHEAP_H_
#include "../utilities/Defines.h"
class ListLinearHeap {
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; // keys of vertices
// keys[i] > key_cap if vertex i is not in the data structure
ui *heads; // head of doubly-linked list for a specific weight
ui *pres; // pre for doubly-linked list
ui *nexts; // next for doubly-linked list
public:
ListLinearHeap(ui _n, ui _key_cap) {
n = _n;
key_cap = _key_cap;
min_key = key_cap;
max_key = 0;
heads = keys = pres = nexts = nullptr;
}
~ListLinearHeap() {
if(heads != nullptr) {
delete[] heads;
heads = nullptr;
}
if(pres != nullptr) {
delete[] pres;
pres = nullptr;
}
if(nexts != nullptr) {
delete[] nexts;
nexts = nullptr;
}
if(keys != nullptr) {
delete[] keys;
keys = nullptr;
}
}
// initialize the data structure by (id, key) pairs
// _n is the number of pairs, _key_cap is the maximum possible key value
void init(ui _n, ui _key_cap, ui *_ids, ui *_keys) {
if(keys == nullptr) keys = new ui[n];
if(pres == nullptr) pres = new ui[n];
if(nexts == nullptr) nexts = new ui[n];
if(heads == nullptr) heads = new ui[key_cap+1];
assert(_key_cap <= key_cap);
min_key = _key_cap; max_key = 0;
for(ui i = 0;i <= _key_cap;i ++) heads[i] = n;
for(ui i = 0;i < _n;i ++) insert(_ids[i], _keys[i]);
}
// insert (id, key) pair into the data structure
void insert(ui id, ui key) {
assert(id < n); assert(key <= key_cap);
//assert(keys[id] > key_cap);
keys[id] = key; pres[id] = n; nexts[id] = heads[key];
if(heads[key] != n) pres[heads[key]] = id;
heads[key] = id;
if(key < min_key) min_key = key;
if(key > max_key) max_key = key;
}
// remove a vertex from the data structure
ui remove(ui id) {
assert(keys[id] <= max_key);
if(pres[id] == n) {
assert(heads[keys[id]] == id);
heads[keys[id]] = nexts[id];
if(nexts[id] != n) pres[nexts[id]] = n;
}
else {
ui pid = pres[id];
nexts[pid] = nexts[id];
if(nexts[id] != n) pres[nexts[id]] = pid;
}
return keys[id];
}
ui get_n() { return n; }
ui get_key_cap() { return key_cap; }
ui get_key(ui id) { return keys[id]; }
void get_ids(std::vector<ui> &ids) {
ids.clear();
tighten();
for(ui i = min_key;i <= max_key;i ++) {
for(ui id = heads[i];id != n;id = nexts[id]) {
ids.pb(id);
}
}
}
void get_ids_keys(std::vector<ui> &ids, std::vector<ui> &_keys) {
ids.clear(); _keys.clear();
tighten();
for(ui i = min_key;i <= max_key;i ++) {
for(ui id = heads[i];id != n;id = nexts[id]) {
ids.pb(id); _keys.pb(id);
}
}
}
bool empty() {
tighten();
return min_key > max_key;
}
ui size() {
tighten();
ui res = 0;
for(ui i = min_key;i <= max_key;i ++) for(ui id = heads[i];id != n;id = nexts[id]) ++ res;
return res;
}
// 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 = heads[max_key];
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 = heads[max_key];
key = max_key;
assert(keys[id] == key);
heads[max_key] = nexts[id];
if(heads[max_key] != n) pres[heads[max_key]] = n;
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 = 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 = heads[min_key];
key = min_key;
assert(keys[id] == key);
heads[min_key] = nexts[id];
if(heads[min_key] != n) pres[heads[min_key]] = n;
return true;
}
// increment the key of vertex id by inc
ui increment(ui id, ui inc = 1) {
assert(keys[id]+inc <= key_cap);
ui new_key = keys[id] + inc;
remove(id);
insert(id, new_key);
return new_key;
}
// decrement the key of vertex id by dec
ui decrement(ui id, ui dec = 1) {
assert(keys[id] >= dec);
ui new_key = keys[id] - dec;
remove(id);
insert(id, new_key);
return new_key;
}
private:
void tighten() {
while(min_key <= max_key&&heads[min_key] == n) ++ min_key;
while(min_key <= max_key&&heads[max_key] == n) -- max_key;
}
};
#endif /* DATA_STRUCTURES_LISTLINEARHEAP_H_ */