generated from puzzlef/louvain-communities
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_bitset.hxx
363 lines (320 loc) · 7.93 KB
/
_bitset.hxx
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#pragma once
#include <cstdint>
#include <utility>
#include <algorithm>
#include <vector>
#include "_ctypes.hxx"
#include "_algorithm.hxx"
using std::pair;
using std::vector;
using std::lower_bound;
#pragma region CLASSES
/**
* A Lazy bitset is a sparse integer key to value map that updates insertions
* and deletions upon calling update(). It maintains keys in ascending order.
* @tparam K key type
* @tparam V value type
*/
template <class K=uint32_t, class V=NONE>
class LazyBitset {
#pragma region TYPES
public:
/** The key type. */
using key_type = K;
/** The value type. */
using value_type = V;
/** The entry type. */
using entry_type = pair<K, V>;
#pragma endregion
#pragma region DATA
protected:
/** The pairs of keys and values. */
vector<pair<K, V>> pairs;
/** The number of unprocessed insertions and deletions (-ve). */
ssize_t unprocessed;
#pragma endregion
#pragma region METHODS
#pragma region SIZE
public:
/**
* Get the number of entries in the bitset.
* @returns |this|
*/
inline size_t size() const noexcept {
return pairs.size(); // - abs(unprocessed)
}
/**
* Check if the bitset is empty.
* @returns |this| == 0
*/
inline bool empty() const noexcept {
return size() == 0;
}
#pragma endregion
#pragma region AT
public:
/**
* Get the entry at given index.
* @param i index
* @returns this[i]
*/
inline pair<K, V> at(size_t i) const noexcept {
return pairs[i];
}
/**
* Get the key at given index.
* @param i index
* @returns this[i].first
*/
inline K keyAt(size_t i) const noexcept {
return pairs[i].first;
}
/**
* Get the value at given index.
* @param i index
* @returns this[i].second
*/
inline V valueAt(size_t i) const noexcept {
return pairs[i].second;
}
#pragma endregion
#pragma region FOREACH
public:
/**
* Iterate over the entries in the bitset.
* @param fp process function (key, value)
*/
template <class F>
inline void forEach(F fp) const noexcept {
for (const auto& p : pairs)
fp(p.first, p.second);
}
/**
* Iterate over the keys in the bitset.
* @param fp process function (key)
*/
template <class F>
inline void forEachKey(F fp) const noexcept {
for (const auto& p : pairs)
fp(p.first);
}
#pragma endregion
#pragma region ITERATOR
protected:
/**
* Get const iterator to the first entry.
* @returns const& this[0]
*/
inline auto begin() const noexcept {
return pairs.begin();
}
/**
* Get iterator to the first entry.
* @returns & this[0]
*/
inline auto begin() noexcept {
return pairs.begin();
}
/**
* Get const iterator to the end.
* @returns const& this[|this|]
*/
inline auto end() const noexcept {
return pairs.end(); // pairs.begin() + size();
}
/**
* Get iterator to the end.
* @returns & this[|this|]
*/
inline auto end() noexcept {
return pairs.end(); // pairs.begin() + size();
}
#pragma endregion
#pragma region FIND
protected:
/**
* Find the entry with given key.
* @param k key
* @returns const& this[i] where this[i].first == k, or & this[|this|] if not found
*/
inline auto find(K k) const noexcept {
auto fl = [](const auto& p, K k) { return p.first < k; };
auto it = lower_bound(begin(), end(), k, fl);
return it == end() || (*it).first != k? end() : it;
}
/**
* Find the entry with given key.
* @param k key
* @returns & this[i] where this[i].first == k, or & this[|this|] if not found
*/
inline auto find(K k) noexcept {
auto fl = [](const auto& p, K k) { return p.first < k; };
auto it = lower_bound(begin(), end(), k, fl);
return it == end() || (*it).first != k? end() : it;
}
#pragma endregion
#pragma region ACCESS
public:
/**
* Check if the bitset has an entry with given key.
* @param k key
* @returns has this[k]?
*/
inline bool has(K k) const noexcept {
auto it = find(k);
return it != end();
}
/**
* Get the value of the entry with given key.
* @param k key
* @returns this[k]
*/
inline V get(K k) const noexcept {
auto it = find(k);
return it == end()? V() : (*it).second;
}
/**
* Set the value of the entry with given key, if key is present.
* @param k key
* @param v value
* @returns success?
*/
inline bool set(K k, V v) noexcept {
auto it = find(k);
if (it == end()) return false;
(*it).second = v;
return true;
}
#pragma endregion
#pragma region UPDATE
protected:
/**
* Update the bitset by sorting out all unprocessed deletions.
* @note This is an expensive operation.
*/
inline void updateRemove() {
auto fl = [](const auto& p, const auto& q) { return p.first < q.first; };
auto fe = [](const auto& p, const auto& q) { return p.first == q.first; };
size_t N = pairs.size();
size_t n = N + unprocessed;
auto ib = pairs.begin();
auto ie = pairs.end();
auto im = ib + n;
sort_values(im, ie, fl);
auto it = set_difference_inplace(ib, im, im, ie, fl, fe);
pairs.resize(it - ib);
unprocessed = 0;
}
/**
* Update the bitset by sorting out all unprocessed insertions.
* @note This is an expensive operation.
*/
inline void updateAdd(vector<pair<K, V>> *buf=nullptr) {
auto fl = [](const auto& p, const auto& q) { return p.first < q.first; };
auto fe = [](const auto& p, const auto& q) { return p.first == q.first; };
size_t N = pairs.size();
size_t n = N - unprocessed;
size_t need = unprocessed + 4;
if (!buf) pairs.resize(N + need);
else if (buf->size() < need) buf->resize(need);
auto bb = buf? buf->begin() : pairs.begin() + N;
auto be = buf? buf->end() : pairs.end();
auto ib = pairs.begin();
auto im = ib + n;
auto ie = ib + N;
sort_values(im, ie, fl);
auto it = set_union_last_inplace(ib, im, im, ie, bb, be, fl, fe);
pairs.resize(it - ib);
unprocessed = 0;
}
public:
/**
* Reserve space for n entries.
* @param n number of entries
*/
inline void reserve(size_t n) {
pairs.reserve(n);
}
/**
* Clear the bitset.
*/
inline void clear() noexcept {
pairs.clear();
unprocessed = 0;
}
/**
* Update the bitset by sorting out all unprocessed insertions and deletions.
* @note This is an expensive operation.
*/
inline void update(vector<pair<K, V>> *buf=nullptr) {
if (unprocessed == 0) return;
if (unprocessed < 0) updateRemove();
else updateAdd(buf);
}
/**
* Remove the entry with given key.
* @param k key
* @param buf buffer for unprocessed insertions
* @note This operation is lazy.
*/
inline void remove(K k, vector<pair<K, V>> *buf=nullptr) {
if (unprocessed > 0) updateAdd(buf);
pairs.push_back({k, V()});
--unprocessed;
}
/**
* Add the entry with given key.
* @param k key
* @param v value
* @note This operation is lazy.
*/
inline void add(K k, V v=V()) {
if (unprocessed < 0) updateRemove();
pairs.push_back({k, v});
++unprocessed;
}
#pragma endregion
#pragma endregion
};
#pragma endregion
#pragma region METHODS
#pragma region WRITE
/**
* Write a bitset to a stream.
* @tparam B bitset type
* @param a stream
* @param x bitset
*/
template <class B>
void writeBitset(ostream& a, const B& x) {
a << "{\n";
x.forEach([&](auto k, auto v) {
a << " " << k << ": " << v << "\n";
});
a << "}";
}
/**
* Write a lazy bitset to a stream.
* @tparam K key type
* @tparam V value type
* @param a stream
* @param x bitset
*/
template <class K, class V>
inline void write(ostream& a, const LazyBitset<K, V>& x) {
writeBitset(a, x);
}
/**
* Write a lazy bitset to a stream.
* @tparam K key type
* @tparam V value type
* @param a stream
* @param x bitset
*/
template <class K, class V>
inline ostream& operator<<(ostream& a, const LazyBitset<K, V>& x) {
write(a, x);
return a;
}
#pragma endregion
#pragma endregion