Skip to content

Commit

Permalink
Merge pull request #252 from higra/fix_empty_tree
Browse files Browse the repository at this point in the history
fix empty tree creation from empty parent array
  • Loading branch information
PerretB authored Dec 5, 2022
2 parents 12e3230 + 9a69341 commit f58a301
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/higra/structure/tree_graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ namespace hg {
private:

void _init() {
if (_parents.size() == 0) {
_root = invalid_index;
_num_vertices = 0;
_num_leaves = 0;
return;
}
hg_assert(_parents.shape().size() == 1, "parents must be a linear (1d) array");
_num_vertices = _parents.size();
_root = _num_vertices - 1;
Expand Down
4 changes: 4 additions & 0 deletions test/cpp/structure/test_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace tree {
hg::tree t1;
REQUIRE(hg::num_vertices(t1) == 0);

array_1d<index_t> p1{};
hg::tree t11(p1);
REQUIRE(hg::num_vertices(t11) == 0);

array_1d<index_t> p2{5, 5, 6, 6, 6, 7, 7, 7};
hg::tree t2(p2);
REQUIRE(hg::num_vertices(t2) == 8);
Expand Down
9 changes: 9 additions & 0 deletions test/python/test_structure/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ def test_size_tree(self):
self.assertTrue(t.parent(4) == 6)
self.assertTrue(np.all(t.parent((0, 5, 2, 3, 7)) == (5, 7, 6, 6, 7)))

def test_empty_tree(self):
t = hg.Tree([])

self.assertTrue(t.category() == hg.TreeCategory.PartitionTree)
self.assertTrue(t.root() == -1)
self.assertTrue(t.num_vertices() == 0)
self.assertTrue(t.num_edges() == 0)
self.assertTrue(t.num_leaves() == 0)

def test_dynamic_attributes(self):
t = TestTree.get_tree()
t.new_attribute = 42
Expand Down

0 comments on commit f58a301

Please sign in to comment.