change shared_ptr to unique_ptr

This commit is contained in:
邹晓航
2015-02-15 19:57:20 +08:00
parent 88f30dbb55
commit 785a9727ea
2 changed files with 41 additions and 37 deletions

View File

@@ -24,16 +24,19 @@ namespace TinySTL{
else else
return _is_existed(word, res->second); return _is_existed(word, res->second);
} }
bool trie_tree::_is_existed(const string& word, std::shared_ptr<trie_node> sp)const{ bool trie_tree::_is_existed(const string& word, const node_ptr& up)const{
if (word.size() == 1) if (word.size() == 1)
return sp->is_a_word; return up->is_a_word;
char ch = word[1]; char ch = word[1];
auto res = sp->map_childs.find(ch); auto res = up->map_childs.find(ch);
if (res == sp->map_childs.end())//not found if (res == up->map_childs.end())//not found
return false; return false;
else else
return _is_existed(word.substr(1), res->second); 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<trie_node>(ch, is_a_word);
}
bool trie_tree::insert(const string& word){ bool trie_tree::insert(const string& word){
if (is_existed(word)) if (is_existed(word))
return true; return true;
@@ -45,29 +48,27 @@ namespace TinySTL{
if (res != root->map_childs.end()){ if (res != root->map_childs.end()){
return _insert(word.substr(1), res->second); return _insert(word.substr(1), res->second);
}else{ }else{
auto new_sp = std::make_shared<trie_node>(); auto is_a_word = (word.size() == 1 ? true : false);
new_sp->data = ch; auto node = make_node(ch, is_a_word);
new_sp->is_a_word = (word.size() == 1 ? true : false); root->map_childs[ch] = std::move(node);
root->map_childs[ch] = new_sp; return _insert(word.substr(1), root->map_childs[ch]);
return _insert(word.substr(1), new_sp);
} }
} }
bool trie_tree::_insert(const string& word, std::shared_ptr<trie_node> sp){ bool trie_tree::_insert(const string& word, const node_ptr& up){
if (word.empty()){ if (word.empty()){
++size_; ++size_;
sp->is_a_word = true; up->is_a_word = true;
return true; return true;
} }
char ch = word[0]; char ch = word[0];
auto res = sp->map_childs.find(ch); auto res = up->map_childs.find(ch);
if (res != sp->map_childs.end()){ if (res != up->map_childs.end()){
return _insert(word.substr(1), res->second); return _insert(word.substr(1), res->second);
}else{ }else{
auto new_sp = std::make_shared<trie_node>(); auto is_a_word = (word.size() == 1 ? true : false);
new_sp->data = ch; auto node = make_node(ch, is_a_word);
new_sp->is_a_word = (word.size() == 1 ? true : false); up->map_childs[ch] = std::move(node);
sp->map_childs[ch] = new_sp; return _insert(word.substr(1), up->map_childs[ch]);
return _insert(word.substr(1), new_sp);
} }
} }
void trie_tree::print_tree(std::ostream& os)const{ 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) for (auto cit = root->map_childs.cbegin(); cit != root->map_childs.cend(); ++cit)
_print_tree(os, cit->second, string()); _print_tree(os, cit->second, string());
} }
void trie_tree::_print_tree(std::ostream& os, std::shared_ptr<trie_node> sp, string word)const{ void trie_tree::_print_tree(std::ostream& os, const node_ptr& up, string word)const{
word += sp->data; word += up->data;
if (sp->is_a_word) if (up->is_a_word)
os << word << std::endl; 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); _print_tree(os, cit->second, word);
} }
} }
@@ -96,27 +97,27 @@ namespace TinySTL{
_get_word_by_prefix(prefix, res->second, prefix, words); _get_word_by_prefix(prefix, res->second, prefix, words);
return words; return words;
} }
void trie_tree::_get_word_by_prefix(const string& prefix, std::shared_ptr<trie_node> sp, void trie_tree::_get_word_by_prefix(const string& prefix, const node_ptr& up,
const string& real_prefix, vector<string>& words)const{ const string& real_prefix, vector<string>& words)const{
if (prefix.size() == 1){ if (prefix.size() == 1){
if (sp->is_a_word) if (up->is_a_word)
words.push_back(real_prefix); 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); __get_word_by_prefix(cit->second, string(), real_prefix, words);
} }
}else{ }else{
char ch = prefix[1]; char ch = prefix[1];
auto res = sp->map_childs.find(ch); auto res = up->map_childs.find(ch);
if (res != sp->map_childs.end()){ if (res != up->map_childs.end()){
_get_word_by_prefix(prefix.substr(1), res->second, real_prefix, words); _get_word_by_prefix(prefix.substr(1), res->second, real_prefix, words);
} }
} }
} }
void trie_tree::__get_word_by_prefix(std::shared_ptr<trie_node> sp, string& word, const string& prefix, vector<string>& words)const{ void trie_tree::__get_word_by_prefix(const node_ptr& up, string& word, const string& prefix, vector<string>& words)const{
word += sp->data; word += up->data;
if (sp->is_a_word) if (up->is_a_word)
words.push_back(prefix + 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); __get_word_by_prefix(cit->second, string(word), prefix, words);
} }
} }

View File

@@ -15,9 +15,11 @@ namespace TinySTL{
struct trie_node{ struct trie_node{
char data; char data;
bool is_a_word; bool is_a_word;
std::map<char, std::shared_ptr<trie_node>> map_childs; std::map<char, std::unique_ptr<trie_node>> map_childs;
trie_node() :data('\0'), is_a_word(false){} trie_node() :data('\0'), is_a_word(false){}
trie_node(char ch, bool is) :data(ch), is_a_word(is){}
}; };
typedef std::unique_ptr<trie_node> node_ptr;
public: public:
typedef string value_type; typedef string value_type;
typedef size_t size_type; typedef size_t size_type;
@@ -38,12 +40,13 @@ namespace TinySTL{
bool insert(const string& word); bool insert(const string& word);
bool is_existed(const string& word)const; bool is_existed(const string& word)const;
private: private:
node_ptr make_node(char ch, bool is_a_word);
inline trie_node* get_root()const{ return data; } inline trie_node* get_root()const{ return data; }
void _get_word_by_prefix(const string& prefix, std::shared_ptr<trie_node> sp, const string& real_prefix, vector<string>& words)const; void _get_word_by_prefix(const string& prefix, const node_ptr& up, const string& real_prefix, vector<string>& words)const;
void __get_word_by_prefix(std::shared_ptr<trie_node> sp, string& word, const string& prefix, vector<string>& words)const; void __get_word_by_prefix(const node_ptr& up, string& word, const string& prefix, vector<string>& words)const;
void _print_tree(std::ostream& os, std::shared_ptr<trie_node> sp, string word)const; void _print_tree(std::ostream& os, const node_ptr& up, string word)const;
bool _insert(const string& word, std::shared_ptr<trie_node> sp); bool _insert(const string& word, const node_ptr& up);
bool _is_existed(const string& word, std::shared_ptr<trie_node> sp)const; bool _is_existed(const string& word, const node_ptr& up)const;
};// end of trie_tree };// end of trie_tree
} }