3#include <boost/container/flat_map.hpp>
4#include <boost/optional.hpp>
9namespace liarsdice::data_structures {
12
13
14
15
16
17
18
32
33
34
35
36 void insert(
const std::string& pattern, T value) {
39 for (
char ch : pattern) {
40 auto& child = current->children[ch];
42 child = std::make_unique<TrieNode>();
44 current = child.get();
47 current->value = std::move(value);
51
52
53
54
55 [[
nodiscard]] boost::optional<T>
find(
const std::string& pattern)
const {
56 const TrieNode* current = root_.get();
58 for (
char ch : pattern) {
59 auto it = current->children.find(ch);
60 if (it == current->children.end()) {
63 current = it->second.get();
66 return current->value;
70
71
72
73
75 std::vector<T> results;
76 const TrieNode* current = root_.get();
78 for (
char ch : text) {
79 auto it = current->children.find(ch);
80 if (it == current->children.end()) {
83 current = it->second.get();
86 results.push_back(*current->value);
94
95
96
97
98 bool erase(
const std::string& pattern) {
return erase_helper(root_.get(), pattern, 0); }
101
102
103 void clear() { root_ = std::make_unique<TrieNode>(); }
106
107
108
109 [[
nodiscard]]
bool empty()
const {
return root_->children.empty() && !root_->value; }
112
113
114
126 if (index == pattern.size()) {
130 node->value = boost::none;
131 return node->children.empty();
134 auto it = node->children.find(pattern[index]);
135 if (it == node->children.end()) {
139 bool should_delete_child = erase_helper(it->second.get(), pattern, index + 1);
141 if (should_delete_child) {
142 node->children.erase(it);
145 return node->children.empty() && !node->value;
151 results.emplace_back(current, *node->value);
154 for (
const auto& [ch, child] : node->children) {
155 current.push_back(ch);
156 collect_all(child.get(), current, results);
163
164
165
166
High-performance Trie data structure for pattern storage.
Definition trie_map.hpp:19
std::vector< T > find_prefixes(const std::string &text) const
Find all values with patterns that are prefixes of the input.
Definition trie_map.hpp:74
boost::optional< T > find(const std::string &pattern) const
Find a value by exact pattern match.
Definition trie_map.hpp:55
bool erase_helper(TrieNode *node, const std::string &pattern, size_t index)
Definition trie_map.hpp:125
void clear()
Clear all patterns from the trie.
Definition trie_map.hpp:103
void collect_all(const TrieNode *node, std::string ¤t, std::vector< std::pair< std::string, T > > &results) const
Definition trie_map.hpp:148
bool erase(const std::string &pattern)
Remove a pattern from the trie.
Definition trie_map.hpp:98
bool empty() const
Check if the trie is empty.
Definition trie_map.hpp:109
void insert(const std::string &pattern, T value)
Insert a pattern-value pair into the trie.
Definition trie_map.hpp:36
TrieMap()
Definition trie_map.hpp:29
Specialized TrieMap for player behavior patterns.
Definition trie_map.hpp:167
size_t occurrences
Definition trie_map.hpp:170
double success_rate
Definition trie_map.hpp:169
double frequency
Definition trie_map.hpp:168
Definition trie_map.hpp:21
boost::optional< T > value
Definition trie_map.hpp:22