Liar's Dice 0.1
Loading...
Searching...
No Matches
trie_map.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <boost/container/flat_map.hpp>
4#include <boost/optional.hpp>
5#include <memory>
6#include <string>
7#include <vector>
8
9namespace liarsdice::data_structures {
10
11 /**
12 * @brief High-performance Trie data structure for pattern storage
13 *
14 * Uses Boost flat_map for cache-efficient child node storage.
15 * Optimized for storing and retrieving player behavior patterns.
16 *
17 * @tparam T Value type to store at pattern endpoints
18 */
19 template <typename T> class TrieMap {
20 private:
21 struct TrieNode {
22 boost::optional<T> value;
24
25 TrieNode() = default;
26 };
27
28 public:
30
31 /**
32 * @brief Insert a pattern-value pair into the trie
33 * @param pattern The pattern string to insert
34 * @param value The value to associate with the pattern
35 */
36 void insert(const std::string& pattern, T value) {
37 TrieNode* current = root_.get();
38
39 for (char ch : pattern) {
40 auto& child = current->children[ch];
41 if (!child) {
42 child = std::make_unique<TrieNode>();
43 }
44 current = child.get();
45 }
46
47 current->value = std::move(value);
48 }
49
50 /**
51 * @brief Find a value by exact pattern match
52 * @param pattern The pattern to search for
53 * @return Optional containing the value if found
54 */
55 [[nodiscard]] boost::optional<T> find(const std::string& pattern) const {
56 const TrieNode* current = root_.get();
57
58 for (char ch : pattern) {
59 auto it = current->children.find(ch);
60 if (it == current->children.end()) {
61 return boost::none;
62 }
63 current = it->second.get();
64 }
65
66 return current->value;
67 }
68
69 /**
70 * @brief Find all values with patterns that are prefixes of the input
71 * @param text The text to search within
72 * @return Vector of found values
73 */
75 std::vector<T> results;
76 const TrieNode* current = root_.get();
77
78 for (char ch : text) {
79 auto it = current->children.find(ch);
80 if (it == current->children.end()) {
81 break;
82 }
83 current = it->second.get();
84
85 if (current->value) {
86 results.push_back(*current->value);
87 }
88 }
89
90 return results;
91 }
92
93 /**
94 * @brief Remove a pattern from the trie
95 * @param pattern The pattern to remove
96 * @return True if the pattern was found and removed
97 */
98 bool erase(const std::string& pattern) { return erase_helper(root_.get(), pattern, 0); }
99
100 /**
101 * @brief Clear all patterns from the trie
102 */
103 void clear() { root_ = std::make_unique<TrieNode>(); }
104
105 /**
106 * @brief Check if the trie is empty
107 * @return True if no patterns are stored
108 */
109 [[nodiscard]] bool empty() const { return root_->children.empty() && !root_->value; }
110
111 /**
112 * @brief Get all stored patterns and their values
113 * @return Vector of pattern-value pairs
114 */
119 return results;
120 }
121
122 private:
124
125 bool erase_helper(TrieNode* node, const std::string& pattern, size_t index) {
126 if (index == pattern.size()) {
127 if (!node->value) {
128 return false;
129 }
130 node->value = boost::none;
131 return node->children.empty();
132 }
133
134 auto it = node->children.find(pattern[index]);
135 if (it == node->children.end()) {
136 return false;
137 }
138
139 bool should_delete_child = erase_helper(it->second.get(), pattern, index + 1);
140
141 if (should_delete_child) {
142 node->children.erase(it);
143 }
144
145 return node->children.empty() && !node->value;
146 }
147
148 void collect_all(const TrieNode* node, std::string& current,
149 std::vector<std::pair<std::string, T>>& results) const {
150 if (node->value) {
151 results.emplace_back(current, *node->value);
152 }
153
154 for (const auto& [ch, child] : node->children) {
155 current.push_back(ch);
156 collect_all(child.get(), current, results);
157 current.pop_back();
158 }
159 }
160 };
161
162 /**
163 * @brief Specialized TrieMap for player behavior patterns
164 *
165 * Stores pattern strings mapped to behavior statistics
166 */
168 double frequency = 0.0;
169 double success_rate = 0.0;
170 size_t occurrences = 0;
171 };
172
173 using PlayerPatternTrie = TrieMap<BehaviorPattern>;
174
175} // namespace liarsdice::data_structures
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 &current, 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
boost::optional< T > value
Definition trie_map.hpp:22