From 6c14deb5b8e377a7bdc1a44d91458f17a4f24981 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 19 Dec 2014 20:19:36 +0100 Subject: Removed unnecessary `inline` declarations and changed recursions * `inline` is implicitly declared for function definitions inside the class declaration * recursive member methods are now provided with constant _current_ and _end_ iterators instead of the full _path_ vector --- src/trie.h | 105 +++++++++++++++++++++++++++------------------------------- src/utility.h | 4 +-- 2 files changed, 51 insertions(+), 58 deletions(-) diff --git a/src/trie.h b/src/trie.h index b31900f..d34912f 100644 --- a/src/trie.h +++ b/src/trie.h @@ -15,31 +15,29 @@ class Trie { typedef std::vector key_list; Trie(): - value_(), - children_() { - this->value_.first = false; - } + value_(false, Value{}), + children_() { } - inline void add(key_list path) { - this->add(path, path.begin()); + void add(key_list path) { + this->add(path.cbegin(), path.cend()); } - inline void add(key_list path, Value value) { - Trie* tmp = this->add(path, path.begin()); + void add(key_list path, Value value) { + Trie* const target = this->add(path.cbegin(), path.cend()); - tmp->value_.first = true; - tmp->value_.second = value; + target->value_.first = true; + target->value_.second = value; } - inline void remove(key_list path) { - this->remove(path, path.begin()); + void remove(key_list path) { + this->remove(path.cbegin(), path.cend()); } - inline bool check(key_list path) { + bool check(key_list path) { return this->resolve(path); } - inline detail::optional_ptr get() { + detail::optional_ptr get() { if ( this->value_.first ) { return detail::optional_ptr(&this->value_.second); } else { @@ -47,22 +45,22 @@ class Trie { } } - inline detail::optional_ptr get(key_list path) { - if ( auto tmp = this->resolve(path) ) { - return tmp.get()->get(); + detail::optional_ptr get(key_list path) { + if ( const auto source = this->resolve(path) ) { + return source.get()->get(); } else { return detail::optional_ptr(); } } - inline void set(Value value) { + void set(const Value& value) { this->value_.first = true; this->value_.second = value; } - inline bool set(key_list path, Value value) { - if ( auto tmp = this->resolve(path) ) { - tmp.get()->set(value); + bool set(key_list path, const Value& value) { + if ( auto target = this->resolve(path) ) { + target.get()->set(value); return true; } else { @@ -74,68 +72,63 @@ class Trie { std::pair value_; std::map children_; - inline Trie* add( - key_list& path, - typename key_list::const_iterator currStep + Trie* add( + typename key_list::const_iterator current, + typename key_list::const_iterator end ) { - if ( currStep != path.end() ) { - Trie& trie( - this->children_[*currStep] - ); - - return trie.add(path, ++currStep); + if ( current != end ) { + return this->children_[*current].add(++current, end); } else { return this; } } - inline void remove( - key_list& path, - typename key_list::const_iterator currStep + void remove( + typename key_list::const_iterator current, + typename key_list::const_iterator end ) { - if ( currStep != path.end() ) { - typename std::map::iterator matchingTrie( - this->children_.find(*currStep) + if ( current != end ) { + typename std::map::iterator matching( + this->children_.find(*current) ); - if ( matchingTrie != this->children_.end() ) { - typename key_list::const_iterator nextStep( - ++currStep + if ( matching != this->children_.cend() ) { + typename key_list::const_iterator next( + ++current ); - if ( (*matchingTrie).second.children_.empty() || - nextStep == path.end() ) { - this->children_.erase(matchingTrie); + if ( next == end ) { + this->children_.erase(matching); } else { - (*matchingTrie).second.remove(path, nextStep); + (*matching).second.remove(next, end); } } } } - inline detail::optional_ptr resolve(key_list path) { - return this->resolve(path, path.begin()); + detail::optional_ptr resolve(key_list path) { + return this->resolve(path.cbegin(), path.cend()); } - inline detail::optional_ptr resolve( - key_list& path, - typename key_list::const_iterator currStep + detail::optional_ptr resolve( + typename key_list::const_iterator current, + typename key_list::const_iterator end ) { - typename std::map::iterator matchingTrie( - this->children_.find(*currStep) + typename std::map::iterator matching( + this->children_.find(*current) ); - if ( matchingTrie != this->children_.end() ) { - typename key_list::const_iterator nextStep( - ++currStep + if ( matching != this->children_.cend() ) { + typename key_list::const_iterator next( + ++current ); - if ( nextStep == path.end() ) { + if ( next == end ) { return detail::optional_ptr( - &(*matchingTrie).second + &(*matching).second ); } else { - return (*matchingTrie).second.resolve(path, nextStep); + return (*matching).second.resolve(next, end); } } else { return detail::optional_ptr(); diff --git a/src/utility.h b/src/utility.h index db33608..6af6b6a 100644 --- a/src/utility.h +++ b/src/utility.h @@ -17,11 +17,11 @@ class optional_ptr { optional_ptr(pointer ptr): value_(true, ptr) { } - inline operator bool() const { + operator bool() const { return this->value_.first; } - inline pointer get() const { + pointer get() const { return this->value_.second; } -- cgit v1.2.3