From 785a9727eac14ec426ec3b4260d98dff84bad766 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Sun, 15 Feb 2015 19:57:20 +0800 Subject: [PATCH] change shared_ptr to unique_ptr --- TinySTL/Detail/TireTree.cpp | 63 +++++++++++++++++++------------------ TinySTL/TrieTree.h | 15 +++++---- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/TinySTL/Detail/TireTree.cpp b/TinySTL/Detail/TireTree.cpp index 177de76..fdf84e0 100644 --- a/TinySTL/Detail/TireTree.cpp +++ b/TinySTL/Detail/TireTree.cpp @@ -24,16 +24,19 @@ namespace TinySTL{ else return _is_existed(word, res->second); } - bool trie_tree::_is_existed(const string& word, std::shared_ptr sp)const{ + bool trie_tree::_is_existed(const string& word, const node_ptr& up)const{ if (word.size() == 1) - return sp->is_a_word; + return up->is_a_word; char ch = word[1]; - auto res = sp->map_childs.find(ch); - if (res == sp->map_childs.end())//not found + auto res = up->map_childs.find(ch); + if (res == up->map_childs.end())//not found return false; else return _is_existed(word.substr(1), res->second); } + trie_tree::node_ptr trie_tree::make_node(char ch, bool is_a_word){ + return std::make_unique(ch, is_a_word); + } bool trie_tree::insert(const string& word){ if (is_existed(word)) return true; @@ -45,29 +48,27 @@ namespace TinySTL{ if (res != root->map_childs.end()){ return _insert(word.substr(1), res->second); }else{ - auto new_sp = std::make_shared(); - new_sp->data = ch; - new_sp->is_a_word = (word.size() == 1 ? true : false); - root->map_childs[ch] = new_sp; - return _insert(word.substr(1), new_sp); + auto is_a_word = (word.size() == 1 ? true : false); + auto node = make_node(ch, is_a_word); + root->map_childs[ch] = std::move(node); + return _insert(word.substr(1), root->map_childs[ch]); } } - bool trie_tree::_insert(const string& word, std::shared_ptr sp){ + bool trie_tree::_insert(const string& word, const node_ptr& up){ if (word.empty()){ ++size_; - sp->is_a_word = true; + up->is_a_word = true; return true; } char ch = word[0]; - auto res = sp->map_childs.find(ch); - if (res != sp->map_childs.end()){ + auto res = up->map_childs.find(ch); + if (res != up->map_childs.end()){ return _insert(word.substr(1), res->second); }else{ - auto new_sp = std::make_shared(); - new_sp->data = ch; - new_sp->is_a_word = (word.size() == 1 ? true : false); - sp->map_childs[ch] = new_sp; - return _insert(word.substr(1), new_sp); + auto is_a_word = (word.size() == 1 ? true : false); + auto node = make_node(ch, is_a_word); + up->map_childs[ch] = std::move(node); + return _insert(word.substr(1), up->map_childs[ch]); } } void trie_tree::print_tree(std::ostream& os)const{ @@ -77,11 +78,11 @@ namespace TinySTL{ for (auto cit = root->map_childs.cbegin(); cit != root->map_childs.cend(); ++cit) _print_tree(os, cit->second, string()); } - void trie_tree::_print_tree(std::ostream& os, std::shared_ptr sp, string word)const{ - word += sp->data; - if (sp->is_a_word) + void trie_tree::_print_tree(std::ostream& os, const node_ptr& up, string word)const{ + word += up->data; + if (up->is_a_word) os << word << std::endl; - for (auto cit = sp->map_childs.cbegin(); cit != sp->map_childs.cend(); ++cit){ + for (auto cit = up->map_childs.cbegin(); cit != up->map_childs.cend(); ++cit){ _print_tree(os, cit->second, word); } } @@ -96,27 +97,27 @@ namespace TinySTL{ _get_word_by_prefix(prefix, res->second, prefix, words); return words; } - void trie_tree::_get_word_by_prefix(const string& prefix, std::shared_ptr sp, + void trie_tree::_get_word_by_prefix(const string& prefix, const node_ptr& up, const string& real_prefix, vector& words)const{ if (prefix.size() == 1){ - if (sp->is_a_word) + if (up->is_a_word) words.push_back(real_prefix); - for (auto cit = sp->map_childs.cbegin(); cit != sp->map_childs.cend(); ++cit){ + for (auto cit = up->map_childs.cbegin(); cit != up->map_childs.cend(); ++cit){ __get_word_by_prefix(cit->second, string(), real_prefix, words); } }else{ char ch = prefix[1]; - auto res = sp->map_childs.find(ch); - if (res != sp->map_childs.end()){ + auto res = up->map_childs.find(ch); + if (res != up->map_childs.end()){ _get_word_by_prefix(prefix.substr(1), res->second, real_prefix, words); } } } - void trie_tree::__get_word_by_prefix(std::shared_ptr sp, string& word, const string& prefix, vector& words)const{ - word += sp->data; - if (sp->is_a_word) + void trie_tree::__get_word_by_prefix(const node_ptr& up, string& word, const string& prefix, vector& words)const{ + word += up->data; + if (up->is_a_word) words.push_back(prefix + word); - for (auto cit = sp->map_childs.cbegin(); cit != sp->map_childs.cend(); ++cit){ + for (auto cit = up->map_childs.cbegin(); cit != up->map_childs.cend(); ++cit){ __get_word_by_prefix(cit->second, string(word), prefix, words); } } diff --git a/TinySTL/TrieTree.h b/TinySTL/TrieTree.h index 2d163bd..5f2bf3d 100644 --- a/TinySTL/TrieTree.h +++ b/TinySTL/TrieTree.h @@ -15,9 +15,11 @@ namespace TinySTL{ struct trie_node{ char data; bool is_a_word; - std::map> map_childs; + std::map> map_childs; trie_node() :data('\0'), is_a_word(false){} + trie_node(char ch, bool is) :data(ch), is_a_word(is){} }; + typedef std::unique_ptr node_ptr; public: typedef string value_type; typedef size_t size_type; @@ -38,12 +40,13 @@ namespace TinySTL{ bool insert(const string& word); bool is_existed(const string& word)const; private: + node_ptr make_node(char ch, bool is_a_word); inline trie_node* get_root()const{ return data; } - void _get_word_by_prefix(const string& prefix, std::shared_ptr sp, const string& real_prefix, vector& words)const; - void __get_word_by_prefix(std::shared_ptr sp, string& word, const string& prefix, vector& words)const; - void _print_tree(std::ostream& os, std::shared_ptr sp, string word)const; - bool _insert(const string& word, std::shared_ptr sp); - bool _is_existed(const string& word, std::shared_ptr sp)const; + void _get_word_by_prefix(const string& prefix, const node_ptr& up, const string& real_prefix, vector& words)const; + void __get_word_by_prefix(const node_ptr& up, string& word, const string& prefix, vector& words)const; + void _print_tree(std::ostream& os, const node_ptr& up, string word)const; + bool _insert(const string& word, const node_ptr& up); + bool _is_existed(const string& word, const node_ptr& up)const; };// end of trie_tree } -- GitLab