diff options
author | Adrian Kummerländer | 2014-02-04 17:25:08 +0100 |
---|---|---|
committer | Adrian Kummerländer | 2014-02-04 17:25:08 +0100 |
commit | c835984a6f0d8826139def2d284e03310f8d050a (patch) | |
tree | ec8bd4cde33198ff4efa3aece8ae262efa41783e | |
parent | fadf9b264376e7e5655a7aa3bbc9ad7d565d2212 (diff) | |
download | Trie-c835984a6f0d8826139def2d284e03310f8d050a.tar Trie-c835984a6f0d8826139def2d284e03310f8d050a.tar.gz Trie-c835984a6f0d8826139def2d284e03310f8d050a.tar.bz2 Trie-c835984a6f0d8826139def2d284e03310f8d050a.tar.lz Trie-c835984a6f0d8826139def2d284e03310f8d050a.tar.xz Trie-c835984a6f0d8826139def2d284e03310f8d050a.tar.zst Trie-c835984a6f0d8826139def2d284e03310f8d050a.zip |
Removed std::unique_ptr and unnecessary initialization cruft
* std::forward_list can be implicitly instantiated by braced initializer list
-rw-r--r-- | trie.cc | 38 |
1 files changed, 15 insertions, 23 deletions
@@ -1,7 +1,6 @@ #include <cstdint> #include <forward_list> #include <map> -#include <memory> #include <cassert> #include <iostream> @@ -11,8 +10,6 @@ template < > class Trie { public: - typedef std::unique_ptr<Trie> ptr; - Trie(): children_() { } @@ -23,22 +20,17 @@ class Trie { inline void add(std::forward_list<Key>& path, typename std::forward_list<Key>::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<Key>()); - trie->add(path, ++curr); - } + trie.add(path, ++curr); } } - inline Trie* resolve(std::forward_list<Key> path) const { + inline const Trie* resolve(std::forward_list<Key> path) const { return this->resolve(path, path.begin()); } - inline Trie* resolve( + inline const Trie* resolve( std::forward_list<Key>& path, typename std::forward_list<Key>::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<Key, Trie::ptr> children_; + private: + std::map<Key, Trie> children_; }; int main() { Trie<uint8_t> test; - test.add(std::forward_list<uint8_t>{1, 2, 3}); - test.add(std::forward_list<uint8_t>{1, 2, 4}); - test.add(std::forward_list<uint8_t>{2, 1}); - test.add(std::forward_list<uint8_t>{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<uint8_t>{1, 2}) != nullptr); - assert(test.resolve(std::forward_list<uint8_t>{1, 2, 4}) != nullptr); - assert(test.resolve(std::forward_list<uint8_t>{3}) == nullptr); + assert(test.resolve({1, 2}) != nullptr); + assert(test.resolve({1, 2, 4}) != nullptr); + assert(test.resolve({3}) == nullptr); } |