From 24dee38d72ffa8b0a2515dc40c00a43b46526438 Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Tue, 4 Feb 2014 17:40:40 +0100 Subject: Added key_list typedef and tried to improve expressiveness --- trie.cc | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/trie.cc b/trie.cc index 55dee38..1da306c 100644 --- a/trie.cc +++ b/trie.cc @@ -10,41 +10,49 @@ template < > class Trie { public: + typedef std::forward_list key_list; + Trie(): children_() { } - inline void add(std::forward_list path) { + inline void add(key_list path) { this->add(path, path.begin()); } - inline void add(std::forward_list& path, - typename std::forward_list::const_iterator curr) { + inline void add( + key_list& path, + typename key_list::const_iterator curr + ) { if ( curr != path.end() ) { - Trie& trie = this->children_[*curr]; + Trie& trie( + this->children_[*curr] + ); trie.add(path, ++curr); } } - inline std::pair resolve( - std::forward_list path - ) const { + inline std::pair resolve(key_list path) const { return this->resolve(path, path.begin()); } inline std::pair resolve( - std::forward_list& path, - typename std::forward_list::const_iterator currStep + key_list& path, + typename key_list::const_iterator currStep ) const { - auto matchingTrie = this->children_.find(*curr); + typename std::map::const_iterator matchingTrie( + this->children_.find(*currStep) + ); if ( matchingTrie != this->children_.end() ) { - auto nextStep = ++currStep; + typename key_list::const_iterator nextStep( + ++currStep + ); if ( nextStep == path.end() ) { - return std::make_pair(true, &(*trie).second); + return std::make_pair(true, &(*matchingTrie).second); } else { - return (*trie).second.resolve(path, nextStep); + return (*matchingTrie).second.resolve(path, nextStep); } } else { return std::make_pair(false, nullptr); -- cgit v1.2.3