From 76bc3b3677062c0c1a6b9fa08ff20d12e470159c Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 4 Nov 2024 02:19:09 -0500 Subject: Some old refactoring + some new refactoring --- prefix_search.h | 53 +++++++++++++++++------------------------------------ 1 file changed, 17 insertions(+), 36 deletions(-) (limited to 'prefix_search.h') 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 @@ #ifndef PREFIX_SEARCH_H_A6672A1D #define PREFIX_SEARCH_H_A6672A1D +#include #include #include -#include template class prefix_search { -public: + public: + prefix_search(size_t depth = 0) : depth_(depth), count_(0) {} - prefix_search(size_t depth = 0) : depth_(depth), count_(0) - { - } + size_t getDepth() const { return depth_; } - size_t getDepth() const - { - return depth_; - } + size_t getCount() const { return count_; } - size_t getCount() const - { - return count_; - } - - void add(std::string& key, T val, size_t i = 0) - { + void add(std::string& key, T val, size_t i = 0) { count_++; - if (i == key.length()) - { + if (i == key.length()) { elements_.push_back(val); } else { char next = key.at(i); - if (!children_.count(next)) - { - children_.emplace(next, prefix_search { depth_ + 1 } ); + if (!children_.count(next)) { + children_.emplace(next, prefix_search{depth_ + 1}); } children_.at(next).add(key, val, i + 1); } } - T at(size_t i) const - { - if (i < elements_.size()) - { + T at(size_t i) const { + if (i < elements_.size()) { return elements_.at(i); } else { i -= elements_.size(); - for (const auto& mapping : children_) - { - if (mapping.second.count_ > i) - { + for (const auto& mapping : children_) { + if (mapping.second.count_ > i) { return mapping.second.at(i); } else { i -= mapping.second.count_; @@ -64,18 +48,15 @@ public: throw std::out_of_range("Index out of range"); } - const prefix_search& find(const std::string& val, size_t i) const - { - if (i == val.length() || !children_.count(val.at(i))) - { + const prefix_search& find(const std::string& val, size_t i) const { + if (i == val.length() || !children_.count(val.at(i))) { return *this; } - return children_.at(val.at(i)).find(val, i+1); + return children_.at(val.at(i)).find(val, i + 1); } -private: - + private: size_t depth_; size_t count_; std::unordered_map> children_; -- cgit 1.4.1