6 #ifndef UTILS_DICTIONARY_H 7 #define UTILS_DICTIONARY_H 33 return root_.Find(key.c_str()) !=
nullptr;
37 void Set(
const std::string &key,
const std::string &word)
39 auto node = root_.CreateOrFind(key.c_str());
44 std::string
Get(
const std::string &key,
const std::string &default_word =
"")
const 46 const auto node = root_.Find(key.c_str());
47 return (node ==
nullptr) ? default_word : node->GetWord();
53 const auto node = root_.Find(key.c_str());
58 return node->ClearWord();
64 std::vector<std::string> result;
65 root_.CumulateWords(result);
70 std::vector<std::string>
GetAllWords(
const std::string &key_prefix)
const 72 std::vector<std::string> result;
73 root_.CumulateWords(key_prefix.c_str(), result);
91 for (
auto &c : child_)
97 const Node *Find(
const char *key)
const 103 const auto child_id = CharToChildId(*key);
104 const auto child = child_[child_id].get();
105 return (child ==
nullptr) ? nullptr : child->Find(key + 1);
108 Node *Find(
const char *key)
114 const auto child_id = CharToChildId(*key);
115 const auto child = child_[child_id].get();
116 return (child ==
nullptr) ? nullptr : child->Find(key + 1);
119 Node *CreateOrFind(
const char *key)
125 const auto child_id = CharToChildId(*key);
126 if (child_[child_id] ==
nullptr)
128 child_[child_id] = std::make_unique<Node>(
this);
130 return child_[child_id]->CreateOrFind(key + 1);
133 const std::string &GetWord()
const 138 void SetWord(
const std::string &val)
150 if (!HasAnyChildren() && parent_ !=
nullptr)
152 parent_->DestroyChild(
this);
157 void CumulateWords(std::vector<std::string> &result)
const 161 result.push_back(word_.c_str());
163 for (
auto &child : child_)
165 if (child !=
nullptr)
167 child->CumulateWords(result);
172 bool HasAnyChildren()
const 174 for (
const auto &ch : child_)
184 void DestroyChild(Node *child)
186 for (
auto &c : child_)
188 if (child == c.get())
193 if (!HasAnyChildren() && parent_ !=
nullptr)
195 parent_->DestroyChild(
this);
199 void CumulateWords(
const char *key_prefix, std::vector<std::string> &result)
const 201 if (*key_prefix ==
'\0')
203 CumulateWords(result);
207 const auto child_id = CharToChildId(*key_prefix);
208 if (child_[child_id] !=
nullptr)
210 child_[child_id]->CumulateWords(key_prefix + 1, result);
216 Node *parent_ =
nullptr;
218 std::array<std::unique_ptr<Node>, 94> child_;
219 static size_t CharToChildId(
char c)
221 if (c < 32 || c > 126)
223 throw std::invalid_argument(
"Unsupported character in dictionary key.");
std::vector< std::string > GetAllWords() const
Returns all stored words.
Definition: Dictionary.h:62
bool IsAvailable(const std::string &key) const
Checks if there is a word available for a given key.
Definition: Dictionary.h:31
bool Remove(const std::string &key)
Removes word for a given key. Results in true if an existing word was cleared.
Definition: Dictionary.h:51
void Clear()
Clears whole database of words and keys.
Definition: Dictionary.h:25
std::string Get(const std::string &key, const std::string &default_word="") const
Returns a word for a given key. If word for such key does not exists, returns default_word.
Definition: Dictionary.h:44
Enables mapping string keys to string words efficiently.
Definition: Dictionary.h:19
General namespace for utilities.
Definition: BulletConversions.h:9
void Set(const std::string &key, const std::string &word)
Creates or changes the word of a given key.
Definition: Dictionary.h:37
std::vector< std::string > GetAllWords(const std::string &key_prefix) const
Returns all stored words whose key starts with defined key_prefix.
Definition: Dictionary.h:70