diff options
Diffstat (limited to 'prefix_search.h')
-rw-r--r-- | prefix_search.h | 53 |
1 files changed, 17 insertions, 36 deletions
diff --git a/prefix_search.h b/prefix_search.h index fe69ef7..39bc457 100644 --- a/prefix_search.h +++ b/prefix_search.h | |||
@@ -1,59 +1,43 @@ | |||
1 | #ifndef PREFIX_SEARCH_H_A6672A1D | 1 | #ifndef PREFIX_SEARCH_H_A6672A1D |
2 | #define PREFIX_SEARCH_H_A6672A1D | 2 | #define PREFIX_SEARCH_H_A6672A1D |
3 | 3 | ||
4 | #include <stdexcept> | ||
4 | #include <unordered_map> | 5 | #include <unordered_map> |
5 | #include <vector> | 6 | #include <vector> |
6 | #include <stdexcept> | ||
7 | 7 | ||
8 | template <typename T> | 8 | template <typename T> |
9 | class prefix_search { | 9 | class prefix_search { |
10 | public: | 10 | public: |
11 | prefix_search(size_t depth = 0) : depth_(depth), count_(0) {} | ||
11 | 12 | ||
12 | prefix_search(size_t depth = 0) : depth_(depth), count_(0) | 13 | size_t getDepth() const { return depth_; } |
13 | { | ||
14 | } | ||
15 | 14 | ||
16 | size_t getDepth() const | 15 | size_t getCount() const { return count_; } |
17 | { | ||
18 | return depth_; | ||
19 | } | ||
20 | 16 | ||
21 | size_t getCount() const | 17 | void add(std::string& key, T val, size_t i = 0) { |
22 | { | ||
23 | return count_; | ||
24 | } | ||
25 | |||
26 | void add(std::string& key, T val, size_t i = 0) | ||
27 | { | ||
28 | count_++; | 18 | count_++; |
29 | 19 | ||
30 | if (i == key.length()) | 20 | if (i == key.length()) { |
31 | { | ||
32 | elements_.push_back(val); | 21 | elements_.push_back(val); |
33 | } else { | 22 | } else { |
34 | char next = key.at(i); | 23 | char next = key.at(i); |
35 | 24 | ||
36 | if (!children_.count(next)) | 25 | if (!children_.count(next)) { |
37 | { | 26 | children_.emplace(next, prefix_search<T>{depth_ + 1}); |
38 | children_.emplace(next, prefix_search<T> { depth_ + 1 } ); | ||
39 | } | 27 | } |
40 | 28 | ||
41 | children_.at(next).add(key, val, i + 1); | 29 | children_.at(next).add(key, val, i + 1); |
42 | } | 30 | } |
43 | } | 31 | } |
44 | 32 | ||
45 | T at(size_t i) const | 33 | T at(size_t i) const { |
46 | { | 34 | if (i < elements_.size()) { |
47 | if (i < elements_.size()) | ||
48 | { | ||
49 | return elements_.at(i); | 35 | return elements_.at(i); |
50 | } else { | 36 | } else { |
51 | i -= elements_.size(); | 37 | i -= elements_.size(); |
52 | 38 | ||
53 | for (const auto& mapping : children_) | 39 | for (const auto& mapping : children_) { |
54 | { | 40 | if (mapping.second.count_ > i) { |
55 | if (mapping.second.count_ > i) | ||
56 | { | ||
57 | return mapping.second.at(i); | 41 | return mapping.second.at(i); |
58 | } else { | 42 | } else { |
59 | i -= mapping.second.count_; | 43 | i -= mapping.second.count_; |
@@ -64,18 +48,15 @@ public: | |||
64 | throw std::out_of_range("Index out of range"); | 48 | throw std::out_of_range("Index out of range"); |
65 | } | 49 | } |
66 | 50 | ||
67 | const prefix_search<T>& find(const std::string& val, size_t i) const | 51 | const prefix_search<T>& find(const std::string& val, size_t i) const { |
68 | { | 52 | if (i == val.length() || !children_.count(val.at(i))) { |
69 | if (i == val.length() || !children_.count(val.at(i))) | ||
70 | { | ||
71 | return *this; | 53 | return *this; |
72 | } | 54 | } |
73 | 55 | ||
74 | return children_.at(val.at(i)).find(val, i+1); | 56 | return children_.at(val.at(i)).find(val, i + 1); |
75 | } | 57 | } |
76 | 58 | ||
77 | private: | 59 | private: |
78 | |||
79 | size_t depth_; | 60 | size_t depth_; |
80 | size_t count_; | 61 | size_t count_; |
81 | std::unordered_map<char, prefix_search<T>> children_; | 62 | std::unordered_map<char, prefix_search<T>> children_; |