Skip to content

Commit

Permalink
test remove word (broken)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph-Edwards committed Oct 11, 2023
1 parent 67e2205 commit fc10f6c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
10 changes: 9 additions & 1 deletion include/libsemigroups/aho-corasick.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ namespace libsemigroups {
}

// TODO to cpp
// Add links_exist flag
void add_word_no_checks(const_iterator first, const_iterator last) {
clear_suffix_links();
index_type current = root;
Expand Down Expand Up @@ -165,11 +166,16 @@ namespace libsemigroups {
add_word_no_checks(w.cbegin(), w.cend());
}

void rm_word_no_checks(word_type const& w) {
rm_word_no_checks(w.cbegin(), w.cend());
}

// TODO to cpp
[[nodiscard]] index_type traverse(const_iterator first,
const_iterator last) const {
index_type current = root;
for (auto it = first; it != last; ++it) {
// Uses private traverse by node function
current = traverse(current, *it);
}
return current;
Expand Down Expand Up @@ -232,9 +238,11 @@ namespace libsemigroups {
}
}

// This breaks traversal, as node numbers should correlate to their position
// in this vector
void rm_node(index_type i) {
LIBSEMIGROUPS_ASSERT(i < _nodes.size());
_nodes.erase(_nodes.begin() + i);
// _nodes.erase(_nodes.begin() + i);
}
};

Expand Down
54 changes: 52 additions & 2 deletions tests/test-aho-corasick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,58 @@ namespace libsemigroups {
REQUIRE(ac.traverse(00101_w) == 5);
REQUIRE(ac.traverse(010_w) == 7);

std::ofstream file("aho.gv");
file << dot(ac).to_string();
// std::ofstream file("aho.gv");
// file << dot(ac).to_string();
}

LIBSEMIGROUPS_TEST_CASE("AhoCorasick",
"001",
"all words size 4",
"[quick][aho-corasick]") {
AhoCorasick ac;
ac.add_word_no_checks(0000_w);
ac.add_word_no_checks(0001_w);
ac.add_word_no_checks(0010_w);
ac.add_word_no_checks(0011_w);
ac.add_word_no_checks(0100_w);
ac.add_word_no_checks(0101_w);
ac.add_word_no_checks(0110_w);
ac.add_word_no_checks(0111_w);
ac.add_word_no_checks(1000_w);
ac.add_word_no_checks(1001_w);
ac.add_word_no_checks(1010_w);
ac.add_word_no_checks(1011_w);
ac.add_word_no_checks(1100_w);
ac.add_word_no_checks(1101_w);
ac.add_word_no_checks(1110_w);
ac.add_word_no_checks(1111_w);

REQUIRE(ac.number_of_nodes() == 31);

REQUIRE(ac.traverse(0000_w) == 4);
REQUIRE(ac.traverse(0001_w) == 5);
REQUIRE(ac.traverse(0010_w) == 7);
REQUIRE(ac.traverse(0011_w) == 8);
REQUIRE(ac.traverse(0100_w) == 11);
REQUIRE(ac.traverse(0101_w) == 12);
REQUIRE(ac.traverse(0110_w) == 14);
REQUIRE(ac.traverse(0111_w) == 15);
REQUIRE(ac.traverse(1000_w) == 19);
REQUIRE(ac.traverse(1001_w) == 20);
REQUIRE(ac.traverse(1010_w) == 22);
REQUIRE(ac.traverse(1011_w) == 23);
REQUIRE(ac.traverse(1100_w) == 26);
REQUIRE(ac.traverse(1101_w) == 27);
REQUIRE(ac.traverse(1110_w) == 29);
REQUIRE(ac.traverse(1111_w) == 30);

// Should do nothing
ac.rm_word_no_checks(000_w);
REQUIRE(ac.number_of_nodes() == 31);

ac.rm_word_no_checks(0111_w);
REQUIRE(ac.number_of_nodes() == 30);
ac.traverse(0111_w);
}

LIBSEMIGROUPS_TEST_CASE("AhoCorasick",
Expand Down

0 comments on commit fc10f6c

Please sign in to comment.