Void::Graph — DiGraph<V,E> directed adjacency-list graph: add_vertex/add_edge, has_vertex/has_edge, BFS/DFS traversal, topological sort (Kahn, returns nullopt on cycle), can_reach, shortest_path (BFS, unweighted). Void::Trie — Prefix tree Trie<V>: insert, contains, get, starts_with, erase (with node pruning), keys_with_prefix, size/empty. Void::LRU — LruCache<K,V>: get (promotes to MRU), put (evicts LRU when full), peek (no promotion), erase, clear, contains, full/empty/size. 42/42 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
147 lines
4.3 KiB
C++
147 lines
4.3 KiB
C++
#include <Void/Trie/Trie.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using BoolTrie = Void::Trie::Trie<bool>;
|
|
using IntTrie = Void::Trie::Trie<int>;
|
|
|
|
// ── insert / contains ─────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test trie_insert_and_contains()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("hello");
|
|
Void::Testing::expect(t.contains("hello"));
|
|
}
|
|
|
|
Void::Testing::Test trie_does_not_contain_prefix_only()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("hello");
|
|
Void::Testing::expect(!t.contains("hell"));
|
|
}
|
|
|
|
Void::Testing::Test trie_does_not_contain_extension()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("hell");
|
|
Void::Testing::expect(!t.contains("hello"));
|
|
}
|
|
|
|
Void::Testing::Test trie_insert_empty_key()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("");
|
|
Void::Testing::expect(t.contains(""));
|
|
}
|
|
|
|
Void::Testing::Test trie_multiple_words()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("car");
|
|
t.insert("card");
|
|
t.insert("care");
|
|
Void::Testing::expect(t.contains("car"));
|
|
Void::Testing::expect(t.contains("card"));
|
|
Void::Testing::expect(t.contains("care"));
|
|
Void::Testing::expect(!t.contains("ca"));
|
|
}
|
|
|
|
// ── get ───────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test trie_get_value()
|
|
{
|
|
IntTrie t;
|
|
t.insert("key", 42);
|
|
const auto v = t.get("key");
|
|
Void::Testing::expect(v.has_value());
|
|
Void::Testing::expect_eq(*v, 42);
|
|
}
|
|
|
|
Void::Testing::Test trie_get_missing_returns_nullopt()
|
|
{
|
|
IntTrie t;
|
|
Void::Testing::expect(!t.get("nope").has_value());
|
|
}
|
|
|
|
// ── starts_with ───────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test trie_starts_with_true()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("hello");
|
|
Void::Testing::expect(t.starts_with("hel"));
|
|
}
|
|
|
|
Void::Testing::Test trie_starts_with_false()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("hello");
|
|
Void::Testing::expect(!t.starts_with("world"));
|
|
}
|
|
|
|
// ── erase ────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test trie_erase_removes_word()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("hello");
|
|
Void::Testing::expect(t.erase("hello"));
|
|
Void::Testing::expect(!t.contains("hello"));
|
|
}
|
|
|
|
Void::Testing::Test trie_erase_prefix_word_keeps_extension()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("car");
|
|
t.insert("card");
|
|
t.erase("car");
|
|
Void::Testing::expect(!t.contains("car"));
|
|
Void::Testing::expect(t.contains("card"));
|
|
}
|
|
|
|
Void::Testing::Test trie_erase_missing_returns_false()
|
|
{
|
|
BoolTrie t;
|
|
Void::Testing::expect(!t.erase("nope"));
|
|
}
|
|
|
|
// ── keys_with_prefix ──────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test trie_keys_with_prefix()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("car");
|
|
t.insert("card");
|
|
t.insert("care");
|
|
t.insert("dog");
|
|
auto keys = t.keys_with_prefix("car");
|
|
std::sort(keys.begin(), keys.end());
|
|
Void::Testing::expect_eq(keys.size(), std::size_t{3});
|
|
Void::Testing::expect_eq(keys[0], std::string{"car"});
|
|
Void::Testing::expect_eq(keys[1], std::string{"card"});
|
|
Void::Testing::expect_eq(keys[2], std::string{"care"});
|
|
}
|
|
|
|
Void::Testing::Test trie_keys_with_prefix_no_match()
|
|
{
|
|
BoolTrie t;
|
|
t.insert("hello");
|
|
Void::Testing::expect(t.keys_with_prefix("xyz").empty());
|
|
}
|
|
|
|
// ── size ─────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test trie_size_tracks_inserts()
|
|
{
|
|
BoolTrie t;
|
|
Void::Testing::expect_eq(t.size(), std::size_t{0});
|
|
t.insert("a");
|
|
t.insert("b");
|
|
Void::Testing::expect_eq(t.size(), std::size_t{2});
|
|
}
|