Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
TinySTL
提交
e8ecf9df
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 搜索 >>
提交
e8ecf9df
编写于
2月 09, 2015
作者:
邹
邹晓航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完成DFS
上级
a99ae600
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
46 addition
and
8 deletion
+46
-8
TinySTL/Detail/Graph.impl.h
TinySTL/Detail/Graph.impl.h
+34
-5
TinySTL/Graph.h
TinySTL/Graph.h
+12
-3
未找到文件。
TinySTL/Detail/Graph.impl.h
浏览文件 @
e8ecf9df
...
...
@@ -10,6 +10,27 @@ namespace TinySTL{
return
*
ptr
;
}
template
<
class
Index
,
class
Value
,
class
EqualFunc
>
void
graph
<
Index
,
Value
,
EqualFunc
>::
del_node
(
node
*
p
){
nodeAllocator
::
destroy
(
p
);
nodeAllocator
::
deallocate
(
p
);
}
template
<
class
Index
,
class
Value
,
class
EqualFunc
>
typename
graph
<
Index
,
Value
,
EqualFunc
>::
node
&
graph
<
Index
,
Value
,
EqualFunc
>::
get_node
(
const
Index
&
index
){
for
(
auto
&
pair
:
nodes_
){
if
(
equal_func
(
pair
.
first
.
first
,
index
))
return
pair
.
first
;
}
}
template
<
class
Index
,
class
Value
,
class
EqualFunc
>
void
graph
<
Index
,
Value
,
EqualFunc
>::
cleanup
(){
for
(
auto
out
=
nodes_
.
begin
();
out
!=
nodes_
.
end
();
++
out
){
for
(
auto
in
=
(
out
->
second
).
begin
();
in
!=
(
out
->
second
).
end
();
++
in
){
del_node
(
&
(
*
in
));
}
}
}
template
<
class
Index
,
class
Value
,
class
EqualFunc
>
bool
graph
<
Index
,
Value
,
EqualFunc
>::
is_contained
(
const
Index
&
index
){
for
(
auto
&
pair
:
nodes_
){
if
(
equal_func
(
pair
.
first
.
first
,
index
))
...
...
@@ -63,15 +84,23 @@ namespace TinySTL{
graph
<
Index
,
Value
,
EqualFunc
>::
adjacent_nodes
(
const
node
&
n
){
return
adjacent_nodes
(
n
.
first
);
}
template
<
class
Index
,
class
Value
,
class
EqualFunc
>
void
graph
<
Index
,
Value
,
EqualFunc
>::
DFS
(
const
Index
&
index
,
visiter_func_type
func
){
node
*
start
=
&
(
get_node
(
index
));
Unordered_set
<
Index
,
std
::
hash
<
Index
>
,
EqualFunc
>
visited
(
7
);
auto
nodes
=
adjacent_nodes
(
start
->
first
);
func
(
*
start
);
visited
.
insert
(
start
->
first
);
for
(
auto
&
n
:
nodes
){
if
(
visited
.
count
(
n
.
first
)
==
0
)
//has not visited
DFS
(
n
.
first
,
func
);
}
}
//********************************************************************************
template
<
class
Index
,
class
Value
,
class
EqualFunc
>
graph_iterator
<
Index
,
Value
,
EqualFunc
>&
graph_iterator
<
Index
,
Value
,
EqualFunc
>::
operator
++
(){
++
inner_it_
;
//if (inner_it_ == (outter_it_->second).end()){//to end
// ++outter_it_;
// if (outter_it_ != container_->nodes_.end())//not to end
// inner_it_ = (outter_it_->second).begin();
//}
return
*
this
;
}
template
<
class
Index
,
class
Value
,
class
EqualFunc
>
...
...
TinySTL/Graph.h
浏览文件 @
e8ecf9df
...
...
@@ -3,6 +3,7 @@
#include "Allocator.h"
#include "List.h"
#include "Unordered_set.h"
#include "Utility.h"
#include "Vector.h"
...
...
@@ -12,6 +13,7 @@ namespace TinySTL{
namespace
Detail
{
template
<
class
Index
,
class
Value
,
class
EqualFunc
>
class
graph_iterator
;
template
<
class
Index
,
class
Value
,
class
EqualFunc
=
equal_to
<
Index
>
>
class
graph
{
public:
...
...
@@ -27,13 +29,18 @@ namespace TinySTL{
typedef
std
::
function
<
void
(
node
&
)
>
visiter_func_type
;
public:
graph
()
:
size_
(
0
){};
virtual
~
graph
(){};
virtual
~
graph
(){
cleanup
();
};
virtual
void
add_node
(
const
node
&
item
,
const
node_sets
&
nodes
)
=
0
;
//virtual void delte_node(const node& item) = 0;
//virtual void DFS(visiter_func_type func) = 0;
void
DFS
(
const
Index
&
index
,
visiter_func_type
func
);
//virtual void BFS(visiter_func_type func) = 0;
static
node
&
new_node
(
const
Index
&
index
,
const
Value
&
val
);
node
&
new_node
(
const
Index
&
index
,
const
Value
&
val
);
void
del_node
(
node
*
p
);
node
&
get_node
(
const
Index
&
index
);
bool
is_contained
(
const
Index
&
index
);
inline
static
node_sets
empty_node_set
();
node_sets
adjacent_nodes
(
const
Index
&
index
);
...
...
@@ -47,6 +54,8 @@ namespace TinySTL{
list
<
pair
<
node
,
list
<
node
>>>
nodes_
;
equal_func_type
equal_func
;
size_t
size_
;
protected:
void
cleanup
();
};
template
<
class
Index
,
class
Value
,
class
EqualFunc
=
equal_to
<
Index
>
>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录