33 #ifndef CLUSTER_TREE_HPP
34 #define CLUSTER_TREE_HPP
37 #include <unordered_map>
42 namespace structured {
75 std::vector<ClusterTree>
c;
101 if (
size >= 2*leaf_size) {
104 c[0].refine(leaf_size);
106 c[1].refine(leaf_size);
115 for (
auto& ch :
c) ch.print();
116 std::cout <<
size <<
" ";
128 nr_nodes += ch.nodes();
140 lvls = std::max(lvls, ch.levels());
153 truncate_complete_rec(1, min_levels());
172 expand_complete_rec(1,
levels(), allow_zero_nodes);
183 expand_complete_levels_rec(1, lvls);
214 t.de_serialize_rec(buf+1, buf+n+1, buf+2*n+1, pid);
229 int n =
nodes(), pid = 0;
230 std::vector<int> buf(3*n+1);
232 serialize_rec(buf.data()+1, buf.data()+n+1, buf.data()+2*n+1, pid);
243 if (
c.empty())
return true;
244 else return c[0].levels() ==
c[1].levels();
252 template<
typename integer_t>
254 std::vector<integer_t> lf;
266 std::pair<std::vector<int>,std::vector<int>>
268 int n = (1 << lvls) - 1;
269 std::vector<int> map0(n, -1), map1(n, -1);
271 complete_to_orig_rec(1, map0, map1, leaf);
272 for (
int i=0; i<n; i++) {
273 if (map0[i] == -1) map0[i] = map0[(i+1)/2-1];
274 if (map1[i] == -1) map1[i] = map1[(i+1)/2-1];
285 int min_levels()
const {
288 lvls = std::min(lvls, 1 + ch.min_levels());
291 void truncate_complete_rec(
int lvl,
int lvls) {
292 if (lvl == lvls)
c.clear();
295 ch.truncate_complete_rec(lvl+1, lvls);
297 void expand_complete_rec(
int lvl,
int lvls,
bool allow_zero_nodes) {
301 if (allow_zero_nodes) {
305 int l1 = 1 << (lvls - lvl - 1);
306 c[0].size =
size - l1;
309 c[0].expand_complete_rec(lvl+1, lvls, allow_zero_nodes);
310 c[1].expand_complete_rec(lvl+1, lvls, allow_zero_nodes);
314 ch.expand_complete_rec(lvl+1, lvls, allow_zero_nodes);
317 void complete_to_orig_rec
318 (
int id, std::vector<int>& map0, std::vector<int>& map1,
320 if (
c.empty()) map0[
id-1] = map1[
id-1] = leaf++;
322 c[0].complete_to_orig_rec(
id*2, map0, map1, leaf);
323 c[1].complete_to_orig_rec(
id*2+1, map0, map1, leaf);
324 map0[
id-1] = map0[
id*2-1];
325 map1[
id-1] = map1[
id*2];
329 void expand_complete_levels_rec(
int lvl,
int lvls) {
333 c[0].size =
size / 2;
335 c[0].expand_complete_levels_rec(lvl+1, lvls);
336 c[1].expand_complete_levels_rec(lvl+1, lvls);
340 ch.expand_complete_levels_rec(lvl+1, lvls);
342 template<
typename integer_t>
343 void leaf_sizes_rec(std::vector<integer_t>& lf)
const {
345 ch.leaf_sizes_rec(lf);
349 void serialize_rec(
int* sizes,
int* lchild,
int* rchild,
int& pid)
const {
351 c[0].serialize_rec(sizes, lchild, rchild, pid);
353 c[1].serialize_rec(sizes, lchild, rchild, pid);
354 lchild[pid] = lroot-1;
356 }
else lchild[pid] = rchild[pid] = -1;
360 template<
typename integer_t>
void de_serialize_rec
361 (
const integer_t* sizes,
const integer_t* lchild,
362 const integer_t* rchild,
int& pid) {
364 if (rchild[pid+1] != -1) {
366 c[1].de_serialize_rec(sizes, lchild, rchild, pid);
367 c[0].de_serialize_rec(sizes, lchild, rchild, pid);
The cluster tree, or partition tree that represents the partitioning of the rows or columns of a hier...
Definition: ClusterTree.hpp:62
std::pair< std::vector< int >, std::vector< int > > map_from_complete_to_leafs(int lvls) const
Definition: ClusterTree.hpp:267
bool is_complete() const
Definition: ClusterTree.hpp:242
void expand_complete(bool allow_zero_nodes)
Definition: ClusterTree.hpp:171
const ClusterTree & refine(int leaf_size)
Definition: ClusterTree.hpp:99
void print() const
Definition: ClusterTree.hpp:114
std::vector< integer_t > leaf_sizes() const
Definition: ClusterTree.hpp:253
void truncate_complete()
Definition: ClusterTree.hpp:152
static ClusterTree deserialize(const integer_t *buf)
Definition: ClusterTree.hpp:210
int levels() const
Definition: ClusterTree.hpp:137
ClusterTree()
Definition: ClusterTree.hpp:80
static ClusterTree deserialize(const std::vector< integer_t > &buf)
Definition: ClusterTree.hpp:196
int size
Definition: ClusterTree.hpp:68
ClusterTree(int n)
Definition: ClusterTree.hpp:89
int nodes() const
Definition: ClusterTree.hpp:125
std::vector< ClusterTree > c
Definition: ClusterTree.hpp:75
std::vector< int > serialize() const
Definition: ClusterTree.hpp:228
void expand_complete_levels(int lvls)
Definition: ClusterTree.hpp:182
Definition: StrumpackOptions.hpp:42