33 #ifndef HSS_PARTITION_TREE_HPP
34 #define HSS_PARTITION_TREE_HPP
37 #include <unordered_map>
79 std::vector<HSSPartitionTree>
c;
105 if (
size >= 2*leaf_size) {
108 c[0].refine(leaf_size);
110 c[1].refine(leaf_size);
118 for (
auto& ch :
c) ch.print();
119 std::cout <<
size <<
" ";
131 nr_nodes += ch.nodes();
143 lvls = std::max(lvls, ch.levels());
156 truncate_complete_rec(1, min_levels());
175 expand_complete_rec(1,
levels(), allow_zero_nodes);
186 expand_complete_levels_rec(1, lvls);
217 t.de_serialize_rec(buf+1, buf+n+1, buf+2*n+1, pid);
232 int n =
nodes(), pid = 0;
233 std::vector<int> buf(3*n+1);
235 serialize_rec(buf.data()+1, buf.data()+n+1, buf.data()+2*n+1, pid);
246 if (
c.empty())
return true;
247 else return c[0].levels() ==
c[1].levels();
255 template<
typename integer_t>
257 std::vector<integer_t> lf;
269 std::pair<std::vector<int>,std::vector<int>>
271 int n = (1 << lvls) - 1;
272 std::vector<int> map0(n, -1), map1(n, -1);
274 complete_to_orig_rec(1, map0, map1, leaf);
275 for (
int i=0; i<n; i++) {
276 if (map0[i] == -1) map0[i] = map0[(i+1)/2-1];
277 if (map1[i] == -1) map1[i] = map1[(i+1)/2-1];
288 int min_levels()
const {
291 lvls = std::min(lvls, 1 + ch.min_levels());
294 void truncate_complete_rec(
int lvl,
int lvls) {
295 if (lvl == lvls)
c.clear();
298 ch.truncate_complete_rec(lvl+1, lvls);
300 void expand_complete_rec(
int lvl,
int lvls,
bool allow_zero_nodes) {
304 if (allow_zero_nodes) {
308 int l1 = 1 << (lvls - lvl - 1);
309 c[0].size =
size - l1;
312 c[0].expand_complete_rec(lvl+1, lvls, allow_zero_nodes);
313 c[1].expand_complete_rec(lvl+1, lvls, allow_zero_nodes);
317 ch.expand_complete_rec(lvl+1, lvls, allow_zero_nodes);
320 void complete_to_orig_rec
321 (
int id, std::vector<int>& map0, std::vector<int>& map1,
323 if (
c.empty()) map0[
id-1] = map1[
id-1] = leaf++;
325 c[0].complete_to_orig_rec(
id*2, map0, map1, leaf);
326 c[1].complete_to_orig_rec(
id*2+1, map0, map1, leaf);
327 map0[
id-1] = map0[
id*2-1];
328 map1[
id-1] = map1[
id*2];
332 void expand_complete_levels_rec(
int lvl,
int lvls) {
336 c[0].size =
size / 2;
338 c[0].expand_complete_levels_rec(lvl+1, lvls);
339 c[1].expand_complete_levels_rec(lvl+1, lvls);
343 ch.expand_complete_levels_rec(lvl+1, lvls);
345 template<
typename integer_t>
346 void leaf_sizes_rec(std::vector<integer_t>& lf)
const {
348 ch.leaf_sizes_rec(lf);
352 void serialize_rec(
int* sizes,
int* lchild,
int* rchild,
int& pid)
const {
354 c[0].serialize_rec(sizes, lchild, rchild, pid);
356 c[1].serialize_rec(sizes, lchild, rchild, pid);
357 lchild[pid] = lroot-1;
359 }
else lchild[pid] = rchild[pid] = -1;
363 template<
typename integer_t>
void de_serialize_rec
364 (
const integer_t* sizes,
const integer_t* lchild,
365 const integer_t* rchild,
int& pid) {
367 if (rchild[pid+1] != -1) {
369 c[1].de_serialize_rec(sizes, lchild, rchild, pid);
370 c[0].de_serialize_rec(sizes, lchild, rchild, pid);