-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPCSR.hpp
More file actions
290 lines (274 loc) · 8.75 KB
/
PCSR.hpp
File metadata and controls
290 lines (274 loc) · 8.75 KB
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
#pragma once
#include <bit>
#include <cstddef>
#include <limits>
#include <ranges>
#include <type_traits>
#include "PMA/internal/helpers.hpp"
#include "PMA/internal/leaf.hpp"
#include "ParallelTools/parallel.h"
#include "parlay/primitives.h"
#include "parlay/sequence.h"
#include "CPMA.hpp"
template <typename l, typename value_update_ =
overwrite_on_insert<typename l::element_ref_type,
typename l::element_type>>
using pcsr_settings =
PMA_traits<l, Linear, 0, false, false, false, 0, true, true, value_update_>;
template <typename key_type = uint32_t>
using simple_pcsr_settings = PMA_traits<uncompressed_leaf<key_type>, Linear, 0,
false, false, false, 0, true, true>;
template <typename key_type, typename value_type>
using simple_wpcsr_settings =
PMA_traits<uncompressed_leaf<key_type, value_type>, Linear, 0, false, false,
false, 0, true, true>;
template <typename traits> class PCSR {
using T = typename traits::key_type;
static_assert(std::is_integral_v<T>);
static constexpr bool binary = traits::binary;
static constexpr T top_bit = (std::numeric_limits<T>::max() >> 1) + 1;
static_assert(std::has_single_bit(top_bit));
CPMA<traits> pma;
public:
using node_t = T;
using value_type = typename traits::value_type;
// bool in binary/
// the value if there is only 1 value
// tuple of the values if there are multiple
static constexpr auto get_value_type() {
if constexpr (binary) {
return (bool)true;
} else {
value_type v;
if constexpr (std::tuple_size_v<value_type> == 1) {
return std::get<0>(v);
} else {
return v;
}
}
}
using weight_t = decltype(get_value_type());
using extra_data_t = std::nullptr_t;
PCSR(T num_nodes) : pma(make_pcsr(), num_nodes) {}
template <std::ranges::random_access_range R,
typename Projection = std::identity>
PCSR(T num_nodes, R &edges, bool sorted = false, Projection projection = {})
: pma(make_pcsr(), 16) {
if (!sorted) {
parlay::sort_inplace(edges);
}
auto maped_edges = parlay::delayed_map(edges, [projection](const auto &e) {
auto elem = projection(e);
assert(std::get<0>(elem) < top_bit);
assert(std::get<1>(elem) < top_bit);
std::get<1>(elem) = std::get<1>(elem) + 1;
return elem;
});
pma = CPMA<traits>(make_pcsr(), num_nodes, maped_edges);
}
bool contains(T src, T dest) const {
assert(src < top_bit);
assert(dest < top_bit);
return pma.contains_pcsr(src, dest + 1);
}
bool insert(T src, T dest) {
static_assert(binary);
assert(src < top_bit);
assert(dest < top_bit);
bool ret = pma.insert_pcsr(src, dest + 1);
assert(contains(src, dest));
return ret;
}
bool insert(T src, T dest, value_type val) {
static_assert(!binary);
assert(src < top_bit);
assert(dest < top_bit);
bool ret = pma.insert_pcsr(src, dest + 1, val);
assert(contains(src, dest));
return ret;
}
bool remove(T src, T dest) {
assert(src < top_bit);
assert(dest < top_bit);
bool ret = pma.remove_pcsr(src, dest + 1);
assert(!contains(src, dest));
return ret;
}
template <std::ranges::random_access_range R,
typename Projection = std::identity>
uint64_t insert_batch(R &es, bool sorted = false,
Projection projection = {}) {
auto ret = pma.insert_batch_pcsr(
es,
[projection](const auto &e) {
auto elem = projection(e);
assert(std::get<0>(elem) < top_bit);
assert(std::get<1>(elem) < top_bit);
std::get<1>(elem) = std::get<1>(elem) + 1;
return elem;
},
sorted);
#if DEBUG == 1
bool contains_all = true;
for (auto &elem : es) {
auto src = std::get<0>(elem);
if (src >= top_bit) {
src ^= top_bit;
}
if (!contains(src, std::get<1>(elem))) {
contains_all = false;
std::cout << "don't have something after inserts\n";
}
}
assert(contains_all);
#endif
return ret;
}
template <std::ranges::random_access_range R>
uint64_t remove_batch(R &es, bool sorted = false) {
auto ret = pma.remove_batch_pcsr(
es,
[](const auto &e) {
auto elem = e;
assert(std::get<0>(elem) < top_bit);
assert(std::get<1>(elem) < top_bit);
std::get<1>(elem) = std::get<1>(elem) + 1;
return elem;
},
sorted);
#if DEBUG == 1
bool contains_none = true;
for (auto &elem : es) {
auto src = std::get<0>(elem);
if (src >= top_bit) {
src ^= top_bit;
}
if (contains(src, std::get<1>(elem))) {
contains_none = false;
std::cout << "have " << src << ", " << std::get<1>(elem)
<< " after deletes\n";
}
}
assert(contains_none);
#endif
return ret;
}
size_t get_memory_size() { return pma.get_size(); }
void print() const {
pma.print_pma();
T node = 0;
if constexpr (binary) {
pma.template map<true>([&node](T elem) {
if (elem >= top_bit) {
std::cout << "\n node " << (elem ^ top_bit) << ":\n";
node += 1;
} else {
std::cout << elem - 1 << ", ";
}
});
std::cout << "\n";
} else {
pma.template map<true>([&node](const auto &elem) {
auto dest = std::get<0>(elem);
if (dest >= top_bit) {
std::cout << "\n node " << (dest ^ top_bit) << ":\n";
node += 1;
} else {
std::cout << "(" << dest - 1 << ", " << leftshift_tuple(elem)
<< "), ";
}
});
std::cout << "\n";
}
}
template <int early_exit = 0, class F>
static constexpr bool get_F_early_exit() {
static_assert(early_exit >= -1 && early_exit <= 1);
if constexpr (early_exit == 0) {
if constexpr (requires(const F &f) { F::no_early_exit; }) {
return F::no_early_exit;
} else {
return true;
}
} else if constexpr (early_exit == 1) {
return false;
} else if constexpr (early_exit == -1) {
return true;
}
}
[[nodiscard]] uint64_t get_size() const { return pma.get_size(); }
template <int early_exit = 0, class F>
void map_neighbors(uint64_t i, F f, [[maybe_unused]] std::nullptr_t unused,
bool run_parallel) const {
constexpr bool no_early_exit = get_F_early_exit<early_exit, F>();
if constexpr (binary) {
auto f2 = [&](auto src, auto dest) {
return f(src, element_or_first_element(dest) - 1);
};
if (run_parallel) {
pma.template map_neighbors_pcsr<no_early_exit, true>(i, f2);
} else {
pma.template map_neighbors_pcsr<no_early_exit, false>(i, f2);
}
} else {
auto f2 = [&](auto src, auto dest_and_val) {
if constexpr (std::tuple_size_v<value_type> == 1) {
return f(src, std::get<0>(dest_and_val) - 1,
std::get<1>(dest_and_val));
} else {
return f(src, std::get<0>(dest_and_val) - 1,
leftshift_tuple(dest_and_val));
}
};
if (run_parallel) {
pma.template map_neighbors_pcsr<no_early_exit, true>(i, f2);
} else {
pma.template map_neighbors_pcsr<no_early_exit, false>(i, f2);
}
}
}
[[nodiscard]] T num_nodes() const { return pma.num_nodes_pcsr(); }
[[nodiscard]] size_t num_edges() const { return pma.num_edges_pcsr(); }
[[nodiscard]] size_t
get_degree(T node, [[maybe_unused]] std::nullptr_t unused = {}) const {
return pma.degree_pcsr(node);
}
void write_adj_file(const std::string &filename) {
std::ofstream myfile;
myfile.open(filename);
myfile << "AdjacencyGraph\n";
myfile << num_nodes() << "\n";
myfile << num_edges() << "\n";
uint64_t running_edge_total = 0;
for (uint64_t i = 0; i < num_nodes(); i++) {
myfile << running_edge_total << "\n";
running_edge_total += get_degree(i);
}
if constexpr (binary) {
for (uint64_t i = 0; i < num_nodes(); i++) {
map_neighbors(
i,
[&]([[maybe_unused]] auto src, auto dest) {
myfile << dest << "\n";
},
nullptr, false);
}
} else {
for (uint64_t i = 0; i < num_nodes(); i++) {
map_neighbors(
i,
[&]([[maybe_unused]] auto src, auto dest,
[[maybe_unused]] auto val) { myfile << dest << "\n"; },
nullptr, false);
}
for (uint64_t i = 0; i < num_nodes(); i++) {
map_neighbors(
i,
[&]([[maybe_unused]] auto src, [[maybe_unused]] auto dest,
auto val) { myfile << val << "\n"; },
nullptr, false);
}
}
myfile.close();
}
};