Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
TinySTL
提交
785a9727
T
TinySTL
项目概览
OpenDocCN
/
TinySTL
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TinySTL
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
785a9727
编写于
2月 15, 2015
作者:
邹
邹晓航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
change shared_ptr to unique_ptr
上级
88f30dbb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
41 addition
and
37 deletion
+41
-37
TinySTL/Detail/TireTree.cpp
TinySTL/Detail/TireTree.cpp
+32
-31
TinySTL/TrieTree.h
TinySTL/TrieTree.h
+9
-6
未找到文件。
TinySTL/Detail/TireTree.cpp
浏览文件 @
785a9727
...
...
@@ -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
>
s
p
)
const
{
bool
trie_tree
::
_is_existed
(
const
string
&
word
,
const
node_ptr
&
u
p
)
const
{
if
(
word
.
size
()
==
1
)
return
s
p
->
is_a_word
;
return
u
p
->
is_a_word
;
char
ch
=
word
[
1
];
auto
res
=
s
p
->
map_childs
.
find
(
ch
);
if
(
res
==
s
p
->
map_childs
.
end
())
//not found
auto
res
=
u
p
->
map_childs
.
find
(
ch
);
if
(
res
==
u
p
->
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
>
s
p
){
bool
trie_tree
::
_insert
(
const
string
&
word
,
const
node_ptr
&
u
p
){
if
(
word
.
empty
()){
++
size_
;
s
p
->
is_a_word
=
true
;
u
p
->
is_a_word
=
true
;
return
true
;
}
char
ch
=
word
[
0
];
auto
res
=
s
p
->
map_childs
.
find
(
ch
);
if
(
res
!=
s
p
->
map_childs
.
end
()){
auto
res
=
u
p
->
map_childs
.
find
(
ch
);
if
(
res
!=
u
p
->
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
>
s
p
,
string
word
)
const
{
word
+=
s
p
->
data
;
if
(
s
p
->
is_a_word
)
void
trie_tree
::
_print_tree
(
std
::
ostream
&
os
,
const
node_ptr
&
u
p
,
string
word
)
const
{
word
+=
u
p
->
data
;
if
(
u
p
->
is_a_word
)
os
<<
word
<<
std
::
endl
;
for
(
auto
cit
=
sp
->
map_childs
.
cbegin
();
cit
!=
s
p
->
map_childs
.
cend
();
++
cit
){
for
(
auto
cit
=
up
->
map_childs
.
cbegin
();
cit
!=
u
p
->
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
>
s
p
,
void
trie_tree
::
_get_word_by_prefix
(
const
string
&
prefix
,
const
node_ptr
&
u
p
,
const
string
&
real_prefix
,
vector
<
string
>&
words
)
const
{
if
(
prefix
.
size
()
==
1
){
if
(
s
p
->
is_a_word
)
if
(
u
p
->
is_a_word
)
words
.
push_back
(
real_prefix
);
for
(
auto
cit
=
sp
->
map_childs
.
cbegin
();
cit
!=
s
p
->
map_childs
.
cend
();
++
cit
){
for
(
auto
cit
=
up
->
map_childs
.
cbegin
();
cit
!=
u
p
->
map_childs
.
cend
();
++
cit
){
__get_word_by_prefix
(
cit
->
second
,
string
(),
real_prefix
,
words
);
}
}
else
{
char
ch
=
prefix
[
1
];
auto
res
=
s
p
->
map_childs
.
find
(
ch
);
if
(
res
!=
s
p
->
map_childs
.
end
()){
auto
res
=
u
p
->
map_childs
.
find
(
ch
);
if
(
res
!=
u
p
->
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
>
s
p
,
string
&
word
,
const
string
&
prefix
,
vector
<
string
>&
words
)
const
{
word
+=
s
p
->
data
;
if
(
s
p
->
is_a_word
)
void
trie_tree
::
__get_word_by_prefix
(
const
node_ptr
&
u
p
,
string
&
word
,
const
string
&
prefix
,
vector
<
string
>&
words
)
const
{
word
+=
u
p
->
data
;
if
(
u
p
->
is_a_word
)
words
.
push_back
(
prefix
+
word
);
for
(
auto
cit
=
sp
->
map_childs
.
cbegin
();
cit
!=
s
p
->
map_childs
.
cend
();
++
cit
){
for
(
auto
cit
=
up
->
map_childs
.
cbegin
();
cit
!=
u
p
->
map_childs
.
cend
();
++
cit
){
__get_word_by_prefix
(
cit
->
second
,
string
(
word
),
prefix
,
words
);
}
}
...
...
TinySTL/TrieTree.h
浏览文件 @
785a9727
...
...
@@ -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
>
s
p
,
const
string
&
real_prefix
,
vector
<
string
>&
words
)
const
;
void
__get_word_by_prefix
(
std
::
shared_ptr
<
trie_node
>
s
p
,
string
&
word
,
const
string
&
prefix
,
vector
<
string
>&
words
)
const
;
void
_print_tree
(
std
::
ostream
&
os
,
std
::
shared_ptr
<
trie_node
>
s
p
,
string
word
)
const
;
bool
_insert
(
const
string
&
word
,
std
::
shared_ptr
<
trie_node
>
s
p
);
bool
_is_existed
(
const
string
&
word
,
std
::
shared_ptr
<
trie_node
>
s
p
)
const
;
void
_get_word_by_prefix
(
const
string
&
prefix
,
const
node_ptr
&
u
p
,
const
string
&
real_prefix
,
vector
<
string
>&
words
)
const
;
void
__get_word_by_prefix
(
const
node_ptr
&
u
p
,
string
&
word
,
const
string
&
prefix
,
vector
<
string
>&
words
)
const
;
void
_print_tree
(
std
::
ostream
&
os
,
const
node_ptr
&
u
p
,
string
word
)
const
;
bool
_insert
(
const
string
&
word
,
const
node_ptr
&
u
p
);
bool
_is_existed
(
const
string
&
word
,
const
node_ptr
&
u
p
)
const
;
};
// end of trie_tree
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录