From c835984a6f0d8826139def2d284e03310f8d050a Mon Sep 17 00:00:00 2001 From: Adrian Kummerländer Date: Tue, 4 Feb 2014 17:25:08 +0100 Subject: Removed std::unique_ptr and unnecessary initialization cruft * std::forward_list can be implicitly instantiated by braced initializer list --- trie.cc | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/trie.cc b/trie.cc index 15a74c9..3338a8c 100644 --- a/trie.cc +++ b/trie.cc @@ -1,7 +1,6 @@ #include #include #include -#include #include #include @@ -11,8 +10,6 @@ template < > class Trie { public: - typedef std::unique_ptr ptr; - Trie(): children_() { } @@ -23,22 +20,17 @@ class Trie { inline void add(std::forward_list& path, typename std::forward_list::const_iterator curr) { if ( curr != path.end() ) { - Trie::ptr& trie = this->children_[*curr]; + Trie& trie = this->children_[*curr]; - if ( trie ) { - trie->add(path, ++curr); - } else { - trie.reset(new Trie()); - trie->add(path, ++curr); - } + trie.add(path, ++curr); } } - inline Trie* resolve(std::forward_list path) const { + inline const Trie* resolve(std::forward_list path) const { return this->resolve(path, path.begin()); } - inline Trie* resolve( + inline const Trie* resolve( std::forward_list& path, typename std::forward_list::const_iterator curr ) const { @@ -48,29 +40,29 @@ class Trie { auto next = ++curr; if ( next == path.end() ) { - return (*trie).second.get(); + return &(*trie).second; } else { - return (*trie).second->resolve(path, next); + return (*trie).second.resolve(path, next); } } else { return nullptr; } } - protected: - std::map children_; + private: + std::map children_; }; int main() { Trie test; - test.add(std::forward_list{1, 2, 3}); - test.add(std::forward_list{1, 2, 4}); - test.add(std::forward_list{2, 1}); - test.add(std::forward_list{2, 1, 1}); + test.add({1, 2, 3}); + test.add({1, 2, 4}); + test.add({2, 1}); + test.add({2, 1, 1}); - assert(test.resolve(std::forward_list{1, 2}) != nullptr); - assert(test.resolve(std::forward_list{1, 2, 4}) != nullptr); - assert(test.resolve(std::forward_list{3}) == nullptr); + assert(test.resolve({1, 2}) != nullptr); + assert(test.resolve({1, 2, 4}) != nullptr); + assert(test.resolve({3}) == nullptr); } -- cgit v1.2.3