Utilities
Utilities header only library
Dictionary.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_DICTIONARY_H
7 #define UTILS_DICTIONARY_H
8 #include <string>
9 #include <array>
10 #include <vector>
11 #include <memory>
12 
13 
14 
15 namespace utils
16 {
17 
19  class Dictionary
20  {
21  public:
22  Dictionary() = default;
23 
25  void Clear()
26  {
27  root_.ClearAll();
28  }
29 
31  bool IsAvailable(const std::string &key) const
32  {
33  return root_.Find(key.c_str()) != nullptr;
34  }
35 
37  void Set(const std::string &key, const std::string &word)
38  {
39  auto node = root_.CreateOrFind(key.c_str());
40  node->SetWord(word);
41  }
42 
44  std::string Get(const std::string &key, const std::string &default_word = "") const
45  {
46  const auto node = root_.Find(key.c_str());
47  return (node == nullptr) ? default_word : node->GetWord();
48  }
49 
51  bool Remove(const std::string &key)
52  {
53  const auto node = root_.Find(key.c_str());
54  if (node == nullptr)
55  {
56  return false;
57  }
58  return node->ClearWord();
59  }
60 
62  std::vector<std::string> GetAllWords() const
63  {
64  std::vector<std::string> result;
65  root_.CumulateWords(result);
66  return result;
67  }
68 
70  std::vector<std::string> GetAllWords(const std::string &key_prefix) const
71  {
72  std::vector<std::string> result;
73  root_.CumulateWords(key_prefix.c_str(), result);
74  return result;
75  }
76 
77  private:
78 
79  class Node
80  {
81  public:
82  Node() = default;
83 
84  Node(Node *parent)
85  : parent_(parent)
86  {
87  }
88 
89  void ClearAll()
90  {
91  for (auto &c : child_)
92  {
93  c.reset();
94  }
95  }
96 
97  const Node *Find(const char *key) const
98  {
99  if (*key == '\0')
100  {
101  return this;
102  }
103  const auto child_id = CharToChildId(*key);
104  const auto child = child_[child_id].get();
105  return (child == nullptr) ? nullptr : child->Find(key + 1);
106  }
107 
108  Node *Find(const char *key)
109  {
110  if (*key == '\0')
111  {
112  return this;
113  }
114  const auto child_id = CharToChildId(*key);
115  const auto child = child_[child_id].get();
116  return (child == nullptr) ? nullptr : child->Find(key + 1);
117  }
118 
119  Node *CreateOrFind(const char *key)
120  {
121  if (*key == '\0')
122  {
123  return this;
124  }
125  const auto child_id = CharToChildId(*key);
126  if (child_[child_id] == nullptr)
127  {
128  child_[child_id] = std::make_unique<Node>(this);
129  }
130  return child_[child_id]->CreateOrFind(key + 1);
131  }
132 
133  const std::string &GetWord() const
134  {
135  return word_;
136  }
137 
138  void SetWord(const std::string &val)
139  {
140  word_ = val;
141  }
142 
143  bool ClearWord()
144  {
145  if (word_.empty())
146  {
147  return false;
148  }
149  word_.clear();
150  if (!HasAnyChildren() && parent_ != nullptr)
151  {
152  parent_->DestroyChild(this);
153  }
154  return true;
155  }
156 
157  void CumulateWords(std::vector<std::string> &result) const
158  {
159  if (!word_.empty())
160  {
161  result.push_back(word_.c_str());
162  }
163  for (auto &child : child_)
164  {
165  if (child != nullptr)
166  {
167  child->CumulateWords(result);
168  }
169  }
170  }
171 
172  bool HasAnyChildren() const
173  {
174  for (const auto &ch : child_)
175  {
176  if (ch != nullptr)
177  {
178  return true;
179  }
180  }
181  return false;
182  }
183 
184  void DestroyChild(Node *child)
185  {
186  for (auto &c : child_)
187  {
188  if (child == c.get())
189  {
190  c.reset();
191  }
192  }
193  if (!HasAnyChildren() && parent_ != nullptr)
194  {
195  parent_->DestroyChild(this);
196  }
197  }
198 
199  void CumulateWords(const char *key_prefix, std::vector<std::string> &result) const
200  {
201  if (*key_prefix == '\0')
202  {
203  CumulateWords(result);
204  }
205  else
206  {
207  const auto child_id = CharToChildId(*key_prefix);
208  if (child_[child_id] != nullptr)
209  {
210  child_[child_id]->CumulateWords(key_prefix + 1, result);
211  }
212  }
213  }
214 
215  private:
216  Node *parent_ = nullptr;
217  std::string word_;
218  std::array<std::unique_ptr<Node>, 94> child_;
219  static size_t CharToChildId(char c)
220  {
221  if (c < 32 || c > 126)
222  {
223  throw std::invalid_argument("Unsupported character in dictionary key.");
224  }
225  return c - 32;
226  }
227  };
228 
229  Node root_;
230  };
231 
232 
233 }
234 #endif /* !UTILS_DICTIONARY_H */
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