-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbit_graph.cpp
309 lines (272 loc) · 8.75 KB
/
bit_graph.cpp
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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
#include <array>
#include <vector>
#include <tuple>
#include <utility>
#include <algorithm>
namespace bit_graph_pkg
{
/// We'll use an array of unsigned long longs to represent our bits.
using BitWord = unsigned long long;
/// Number of bits per word.
static const constexpr int bits_per_word = sizeof(BitWord) * 8;
/**
* A bitset with a fixed maximum size. This only provides the operations
* we actually use in the bitset algorithms: it's more readable this way
* than doing all the bit voodoo inline.
*
* Indices start at 0.
*/
template <unsigned words_>
class FixedBitSet
{
private:
using Bits = std::array<BitWord, words_>;
Bits _bits = {{ }};
public:
/**
* Set a given bit 'on'.
*/
auto set(int a) -> void
{
// The 1 does have to be of type BitWord. If we just specify a
// literal, it ends up being an int, and it isn't converted
// upwards until after the shift is done.
_bits[a / bits_per_word] |= (BitWord{ 1 } << (a % bits_per_word));
}
/**
* Set a given bit 'on'.
*/
auto set_atomic(int a) -> void
{
// we don't have std::atomic_concurrent_view yet...
__sync_or_and_fetch(&_bits[a / bits_per_word], (BitWord{ 1 } << (a % bits_per_word)));
}
/**
* Set a given bit 'off'.
*/
auto unset(int a) -> void
{
_bits[a / bits_per_word] &= ~(BitWord{ 1 } << (a % bits_per_word));
}
/**
* Set all bits on.
*/
auto set_up_to(int size) -> void
{
unset_all();
for (int i = 0 ; i < size ; ++i)
set(i);
}
/**
* Set all bits off.
*/
auto unset_all() -> void
{
for (unsigned i = 0 ; i < words_ ; ++i)
_bits[i] = 0;
}
/**
* Complement.
*/
auto complement_up_to(int size) -> void
{
for (unsigned i = 0 ; i < words_ ; ++i)
_bits[i] = ~_bits[i];
for (unsigned i = size ; i < words_ * bits_per_word ; ++i)
unset(i);
}
/**
* Is a given bit on?
*/
auto test(int a) const -> bool
{
return _bits[a / bits_per_word] & (BitWord{ 1 } << (a % bits_per_word));
}
/**
* How many bits are on?
*/
auto popcount() const -> unsigned
{
unsigned result = 0;
for (auto & p : _bits)
result += __builtin_popcountll(p);
return result;
}
/**
* Are any bits on?
*/
auto empty() const -> bool
{
for (auto & p : _bits)
if (0 != p)
return false;
return true;
}
/**
* Intersect (bitwise-and) with another set.
*/
auto intersect_with(const FixedBitSet<words_> & other) -> void
{
for (typename Bits::size_type i = 0 ; i < words_ ; ++i)
_bits[i] = _bits[i] & other._bits[i];
}
/**
* Union (bitwise-or) with another set.
*/
auto union_with(const FixedBitSet<words_> & other) -> void
{
for (typename Bits::size_type i = 0 ; i < words_ ; ++i)
_bits[i] = _bits[i] | other._bits[i];
}
/**
* Intersect with the complement of another set.
*/
auto intersect_with_complement(const FixedBitSet<words_> & other) -> void
{
for (typename Bits::size_type i = 0 ; i < words_ ; ++i)
_bits[i] = _bits[i] & ~other._bits[i];
}
/**
* Return the index of the first set ('on') bit, or -1 if we are
* empty.
*/
auto first_set_bit() const -> int
{
for (typename Bits::size_type i = 0 ; i < _bits.size() ; ++i) {
int b = __builtin_ffsll(_bits[i]);
if (0 != b)
return i * bits_per_word + b - 1;
}
return -1;
}
/**
* Return the index of the last set ('on') bit, or -1 if we are
* empty.
*/
auto last_set_bit() const -> int
{
for (int i = _bits.size() - 1 ; i >= 0 ; --i) {
if (0 == _bits[i])
continue;
int b = __builtin_clzll(_bits[i]);
return (i + 1) * bits_per_word - b - 1;
}
return -1;
}
auto operator== (const FixedBitSet<words_> & other) const -> bool
{
if (_bits.size() != other._bits.size())
return false;
for (typename Bits::size_type i = 0 ; i < _bits.size() ; ++i)
if (_bits[i] != other._bits[i])
return false;
return true;
}
};
/**
* A bitgraph with a fixed maximum size. In effect this is an adjacency
* matrix representation. This only provides the operations we actually use
* in the bitset algorithms.
*
* Indices start at 0.
*/
template <unsigned size_>
class FixedBitGraph
{
private:
using Rows = std::vector<FixedBitSet<size_> >;
int _size = 0;
Rows _adjacency;
public:
/**
* Return the actual size (not the maximum).
*/
auto size() const -> int
{
return _size;
}
/**
* Change our actual size. Must be below the maximum.
*/
auto resize(int size) -> void
{
_size = size;
_adjacency.resize(size);
}
/**
* Add an edge from a to b (and from b to a).
*/
auto add_edge(int a, int b) -> void
{
_adjacency[a].set(b);
_adjacency[b].set(a);
}
/**
* Add an edge from a to b (and from b to a).
*/
auto add_edge_atomic(int a, int b) -> void
{
_adjacency[a].set_atomic(b);
_adjacency[b].set_atomic(a);
}
/**
* Are vertices a and b adjacent?
*/
auto adjacent(int a, int b) const -> bool
{
return _adjacency[a].test(b);
}
/**
* What is the degree of a given vertex?
*/
auto degree(int a) const -> int
{
return _adjacency[a].popcount();
}
/**
* Intersect the supplied bitset with a particular row.
*/
auto intersect_with_row(int row, FixedBitSet<size_> & p) const -> void
{
p.intersect_with(_adjacency[row]);
}
/**
* Intersect the supplied bitset with the complement of a
* particular row.
*/
auto intersect_with_row_complement(int row, FixedBitSet<size_> & p) const -> void
{
p.intersect_with_complement(_adjacency[row]);
}
/**
* Fetch the neighbourhood of a particular vertex.
*/
auto neighbourhood(int vertex) const -> FixedBitSet<size_>
{
return _adjacency[vertex];
}
/**
* Complement.
*/
auto complement() -> void
{
for (int i = 0 ; i < _size ; ++i)
_adjacency[i].complement_up_to(_size);
}
};
/**
* We have to decide at compile time what the largest graph we'll support
* is.
*/
constexpr auto max_graph_words __attribute__((unused)) = 1024;
/**
* Thrown if we exceed max_graph_words.
*/
class GraphTooBig :
public std::exception
{
public:
GraphTooBig() throw ();
};
}