提交 785a9727 编写于 作者: 邹晓航

change shared_ptr to unique_ptr

上级 88f30dbb
......@@ -24,16 +24,19 @@ namespace TinySTL{
else
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)
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<trie_node>(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<trie_node>();
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<trie_node> 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<trie_node>();
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<trie_node> 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<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{
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<trie_node> sp, string& word, const string& prefix, vector<string>& 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<string>& 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);
}
}
......
......@@ -15,9 +15,11 @@ namespace TinySTL{
struct trie_node{
char data;
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(char ch, bool is) :data(ch), is_a_word(is){}
};
typedef std::unique_ptr<trie_node> 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<trie_node> sp, 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 _print_tree(std::ostream& os, std::shared_ptr<trie_node> sp, string word)const;
bool _insert(const string& word, std::shared_ptr<trie_node> sp);
bool _is_existed(const string& word, std::shared_ptr<trie_node> sp)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(const node_ptr& up, string& word, const string& prefix, vector<string>& 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
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册