Skip to content

Commit

Permalink
Methods BlockItem::first, BlockItem::last, Node::contains_block added…
Browse files Browse the repository at this point in the history
… with tests
  • Loading branch information
kocotom committed Apr 17, 2024
1 parent 47b5930 commit 10f0cd2
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/mata/utils/partition.hh
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ class Partition {
}
const Node& node(void) const { return block().node(); }
const BlockItem& repr(void) const { return node().repr(); }
const BlockItem& first(void) const { return node().first(); }
const BlockItem& last(void) const { return node().last(); }
};

/**
Expand Down Expand Up @@ -430,6 +432,12 @@ class Partition {
size_t size(void) const {
return last().idx() - first().idx() + 1;
}

bool contains_block(size_t block_idx) const {
const Block& block = partition_.get_block(block_idx);
return first_ <= block.first().idx() &&
last_ >= block.last().idx();
}
};

public:
Expand Down
84 changes: 83 additions & 1 deletion tests/partition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,23 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p.get_block_idx(i) == 0);
CHECK(p.get_block_item(i).node().idx() == 0);
CHECK(p.get_block_item(i).repr().idx() == 0);
CHECK(p.get_block_item(i).first().idx() == 0);
CHECK(p.get_block_item(i).last().idx() == 9);
CHECK(p.get_block_item(i).node().first().idx() == 0);
CHECK(p.get_block_item(i).node().last().idx() == 9);
CHECK(p[i].idx() == i);
}
CHECK(p.get_block(0).idx() == 0);
CHECK(p.get_block(0).node().idx() == 0);
CHECK(p.get_block(0).repr().idx() == 0);
CHECK(p.get_block(0).first().idx() == 0);
CHECK(p.get_block(0).last().idx() == 9);
CHECK(p.get_block(0).size() == 10);
CHECK(p.get_node(0).idx() == 0);
CHECK(p.get_node(0).first().idx() == 0);
CHECK(p.get_node(0).last().idx() == 9);
CHECK(p.get_node(0).size() == 10);
CHECK(p.get_node(0).contains_block(0));
for(auto& block_item : p.get_block(0)) {
CHECK(block_item.block().idx() == 0);
}
Expand Down Expand Up @@ -62,18 +67,23 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p.get_block_item(i).block().idx() == 0);
CHECK(p.get_block_item(i).node().idx() == 0);
CHECK(p.get_block_item(i).repr().idx() == 0);
CHECK(p.get_block_item(i).first().idx() == 0);
CHECK(p.get_block_item(i).last().idx() == 9);
CHECK(p.get_block_item(i).node().first().idx() == 0);
CHECK(p.get_block_item(i).node().last().idx() == 9);
CHECK(p[i].idx() == i);
}
CHECK(p.get_block(0).idx() == 0);
CHECK(p.get_block(0).node().idx() == 0);
CHECK(p.get_block(0).repr().idx() == 0);
CHECK(p.get_block(0).first().idx() == 0);
CHECK(p.get_block(0).last().idx() == 9);
CHECK(p.get_block(0).size() == 10);
CHECK(p.get_node(0).idx() == 0);
CHECK(p.get_node(0).first().idx() == 0);
CHECK(p.get_node(0).last().idx() == 9);
CHECK(p.get_node(0).size() == 10);
CHECK(p.get_node(0).contains_block(0));
for(auto& block_item : p.get_block(0)) {
CHECK(block_item.block().idx() == 0);
}
Expand Down Expand Up @@ -103,8 +113,14 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p[0].block().idx() == 0);
CHECK(p[0].node().idx() == 0);
CHECK(p.get_block_item(0).node().idx() == 0);
CHECK(p.get_block_item(0).repr().idx() == 0);
CHECK(p.get_block_item(0).first().idx() == 0);
CHECK(p.get_block_item(0).last().idx() == 2);
CHECK(p[1].idx() == 3);
CHECK(p.get_block_item(3).state() == 1);
CHECK(p.get_block_item(3).repr().state() == 1);
CHECK(p.get_block_item(3).first().state() == 1);
CHECK(p.get_block_item(3).last().state() == 9);
CHECK(p[1].block().idx() == 1);
CHECK(p[1].node().idx() == 1);
CHECK(p.get_block_item(3).block().idx() == 1);
Expand All @@ -117,12 +133,16 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p.get_block(1).size() == 7);
CHECK(p.get_node(0).repr().state() == 0);
CHECK(p.get_node(0).repr().block().idx() == 0);
CHECK(p.get_node(0).contains_block(0));
CHECK(!p.get_node(0).contains_block(1));
CHECK(p.get_node(1).repr().state() == 1);
CHECK(p.get_node(1).repr().block().idx() == 1);
CHECK(p.get_node(0).first().idx() == 0);
CHECK(p.get_node(0).last().idx() == 2);
CHECK(p.get_node(1).first().idx() == 3);
CHECK(p.get_node(1).last().idx() == 9);
CHECK(!p.get_node(1).contains_block(0));
CHECK(p.get_node(1).contains_block(1));
CHECK(p.get_block(0).node().idx() == 0);
CHECK(p.get_block(1).node().idx() == 1);
CHECK(p.states_in_same_block(0).size() == 3);
Expand Down Expand Up @@ -150,11 +170,17 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p[0].block().idx() == 0);
CHECK(p[0].node().idx() == 0);
CHECK(p.get_block_item(0).block().idx() == 0);
CHECK(p.get_block_item(0).repr().idx() == 0);
CHECK(p.get_block_item(0).first().idx() == 0);
CHECK(p.get_block_item(0).last().idx() == 0);
CHECK(p.get_block(0).node().idx() == 0);
CHECK(p[1].block().idx() == 1);
CHECK(p[1].node().idx() == 1);
CHECK(p.get_block_item(1).block().idx() == 1);
CHECK(p.get_block_item(1).node().idx() == 1);
CHECK(p.get_block_item(1).repr().idx() == 1);
CHECK(p.get_block_item(1).first().idx() == 1);
CHECK(p.get_block_item(1).last().idx() == 2);
CHECK(p.get_block(0).repr().state() == 0);
CHECK(p.get_block(1).repr().state() == 1);
CHECK(p.get_block(2).repr().state() == 3);
Expand All @@ -166,13 +192,25 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p.get_node(1).first().idx() == 1);
CHECK(p.get_node(1).last().idx() == 2);
CHECK(p.get_node(2).first().idx() == 3);
CHECK(p.get_node(2).last().idx() == 5);
CHECK(p.get_node(2).last().idx() == 5);
CHECK(p.get_node(0).contains_block(0));
CHECK(!p.get_node(0).contains_block(1));
CHECK(!p.get_node(0).contains_block(2));
CHECK(!p.get_node(1).contains_block(0));
CHECK(p.get_node(1).contains_block(1));
CHECK(!p.get_node(1).contains_block(2));
CHECK(!p.get_node(2).contains_block(0));
CHECK(!p.get_node(2).contains_block(1));
CHECK(p.get_node(2).contains_block(2));
CHECK(p.get_block(0).node().idx() == 0);
CHECK(p.get_block(1).node().idx() == 1);
CHECK(p.get_block(2).node().idx() == 2);
CHECK(p.get_block(0).size() == 1);
CHECK(p.get_block(1).size() == 2);
CHECK(p.get_block(2).size() == 3);
CHECK(p.get_block_item(3).repr().idx() == 3);
CHECK(p.get_block_item(3).first().idx() == 3);
CHECK(p.get_block_item(3).last().idx() == 5);
CHECK(p.states_in_same_block(0).size() == 1);
CHECK(p.states_in_same_block(1).size() == 2);
CHECK(p.states_in_same_block(3).size() == 3);
Expand All @@ -191,6 +229,7 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p.states_in_same_block(0).size() == 10);
CHECK(p.partition().size() == 1);
CHECK(p.get_block(0).size() == 10);
CHECK(p.get_node(0).contains_block(0));
p.split_blocks({0, 1, 2, 3, 4});
CHECK(p.num_of_states() == 10);
CHECK(p.num_of_block_items() == 10);
Expand All @@ -205,6 +244,12 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p.partition().size() == 2);
CHECK(p.get_block(0).size() == 5);
CHECK(p.get_block(1).size() == 5);
CHECK(p.get_node(0).contains_block(0));
CHECK(p.get_node(0).contains_block(1));
CHECK(p.get_node(1).contains_block(0));
CHECK(!p.get_node(1).contains_block(1));
CHECK(!p.get_node(2).contains_block(0));
CHECK(p.get_node(2).contains_block(1));
p.split_blocks({0, 1, 2, 5, 6, 7});
CHECK(p.num_of_states() == 10);
CHECK(p.num_of_block_items() == 10);
Expand All @@ -221,6 +266,34 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p.states_in_same_block(5).size() == 3);
CHECK(p.states_in_same_block(8).size() == 2);
CHECK(p.partition().size() == 4);
CHECK(p.get_node(0).contains_block(0));
CHECK(p.get_node(0).contains_block(1));
CHECK(p.get_node(0).contains_block(2));
CHECK(p.get_node(0).contains_block(3));
CHECK(p.get_node(1).contains_block(0));
CHECK(!p.get_node(1).contains_block(1));
CHECK(p.get_node(1).contains_block(2));
CHECK(!p.get_node(1).contains_block(3));
CHECK(!p.get_node(2).contains_block(0));
CHECK(p.get_node(2).contains_block(1));
CHECK(!p.get_node(2).contains_block(2));
CHECK(p.get_node(2).contains_block(3));
CHECK(p.get_node(3).contains_block(0));
CHECK(!p.get_node(3).contains_block(1));
CHECK(!p.get_node(3).contains_block(2));
CHECK(!p.get_node(3).contains_block(3));
CHECK(!p.get_node(4).contains_block(0));
CHECK(!p.get_node(4).contains_block(1));
CHECK(p.get_node(4).contains_block(2));
CHECK(!p.get_node(4).contains_block(3));
CHECK(!p.get_node(5).contains_block(0));
CHECK(p.get_node(5).contains_block(1));
CHECK(!p.get_node(5).contains_block(2));
CHECK(!p.get_node(5).contains_block(3));
CHECK(!p.get_node(6).contains_block(0));
CHECK(!p.get_node(6).contains_block(1));
CHECK(!p.get_node(6).contains_block(2));
CHECK(p.get_node(6).contains_block(3));
p.split_blocks({0, 3, 5, 8});
CHECK(p.num_of_states() == 10);
CHECK(p.num_of_block_items() == 10);
Expand Down Expand Up @@ -370,11 +443,17 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p[0].block().idx() == 0);
CHECK(p[0].node().idx() == 0);
CHECK(p.get_block_item(0).block().idx() == 0);
CHECK(p.get_block_item(0).repr().idx() == 0);
CHECK(p.get_block_item(0).first().idx() == 0);
CHECK(p.get_block_item(0).last().idx() == 0);
CHECK(p.get_block(0).node().idx() == 0);
CHECK(p[1].block().idx() == 1);
CHECK(p[1].node().idx() == 1);
CHECK(p.get_block_item(1).block().idx() == 1);
CHECK(p.get_block_item(1).node().idx() == 1);
CHECK(p.get_block_item(1).repr().idx() == 1);
CHECK(p.get_block_item(1).first().idx() == 1);
CHECK(p.get_block_item(1).last().idx() == 2);
CHECK(p.get_block(0).repr().state() == 0);
CHECK(p.get_block(1).repr().state() == 1);
CHECK(p.get_block(2).repr().state() == 3);
Expand All @@ -390,6 +469,9 @@ TEST_CASE("mata::utils::Partition") {
CHECK(p.get_block(0).node().idx() == 0);
CHECK(p.get_block(1).node().idx() == 1);
CHECK(p.get_block(2).node().idx() == 2);
CHECK(p.get_block_item(3).repr().idx() == 3);
CHECK(p.get_block_item(3).first().idx() == 3);
CHECK(p.get_block_item(3).last().idx() == 5);
CHECK(p.states_in_same_block(0).size() == 1);
CHECK(p.states_in_same_block(1).size() == 2);
CHECK(p.states_in_same_block(3).size() == 3);
Expand Down

0 comments on commit 10f0cd2

Please sign in to comment.