Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
TinySTL
提交
b601df79
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 搜索 >>
提交
b601df79
编写于
3月 21, 2015
作者:
邹
邹晓航
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
union-find set
上级
fce4f246
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
110 addition
and
0 deletion
+110
-0
README.md
README.md
+11
-0
TinySTL/Test/UFSetTest.cpp
TinySTL/Test/UFSetTest.cpp
+17
-0
TinySTL/Test/UFSetTest.h
TinySTL/Test/UFSetTest.h
+15
-0
TinySTL/TinySTL.vcxproj
TinySTL/TinySTL.vcxproj
+3
-0
TinySTL/TinySTL.vcxproj.filters
TinySTL/TinySTL.vcxproj.filters
+9
-0
TinySTL/UFSet.h
TinySTL/UFSet.h
+53
-0
TinySTL/main.cpp
TinySTL/main.cpp
+2
-0
未找到文件。
README.md
浏览文件 @
b601df79
...
...
@@ -582,6 +582,17 @@ TinySTL
*cp2 = ("C++");//write
assert(*cp1 == *cp3 && *cp3 == "zouxiaohang");
assert(*cp2 == "C++");
####(19):union-find set
uf_set<10> uf;
uf.Union(0, 1);
uf.Union(2, 3);
uf.Union(3, 1);
assert(uf.Find(0) == uf.Find(2));
TinySTL/Test/UFSetTest.cpp
0 → 100644
浏览文件 @
b601df79
#include "UFSetTest.h"
namespace
TinySTL
{
namespace
UFSetTest
{
void
testCase1
(){
uf_set
<
10
>
uf
;
uf
.
Union
(
0
,
1
);
uf
.
Union
(
2
,
3
);
uf
.
Union
(
3
,
1
);
assert
(
uf
.
Find
(
0
)
==
uf
.
Find
(
2
));
}
void
testAllCases
(){
testCase1
();
}
}
}
\ No newline at end of file
TinySTL/Test/UFSetTest.h
0 → 100644
浏览文件 @
b601df79
#ifndef _UF_SET_TEST_H_
#define _UF_SET_TEST_H_
#include "../UFSet.h"
#include <cassert>
namespace
TinySTL
{
namespace
UFSetTest
{
void
testCase1
();
void
testAllCases
();
}
}
#endif
\ No newline at end of file
TinySTL/TinySTL.vcxproj
浏览文件 @
b601df79
...
...
@@ -103,6 +103,7 @@
<ClCompile
Include=
"Test\StringTest.cpp"
/>
<ClCompile
Include=
"Test\SuffixArrayTest.cpp"
/>
<ClCompile
Include=
"Test\TrieTreeTest.cpp"
/>
<ClCompile
Include=
"Test\UFSetTest.cpp"
/>
<ClCompile
Include=
"Test\UniquePtrTest.cpp"
/>
<ClCompile
Include=
"Test\Unordered_setTest.cpp"
/>
<ClCompile
Include=
"Test\VectorTest.cpp"
/>
...
...
@@ -159,11 +160,13 @@
<ClInclude
Include=
"Test\SuffixArrayTest.h"
/>
<ClInclude
Include=
"Test\TestUtil.h"
/>
<ClInclude
Include=
"Test\TrieTreeTest.h"
/>
<ClInclude
Include=
"Test\UFSetTest.h"
/>
<ClInclude
Include=
"Test\UniquePtrTest.h"
/>
<ClInclude
Include=
"Test\Unordered_setTest.h"
/>
<ClInclude
Include=
"Test\VectorTest.h"
/>
<ClInclude
Include=
"TrieTree.h"
/>
<ClInclude
Include=
"TypeTraits.h"
/>
<ClInclude
Include=
"UFSet.h"
/>
<ClInclude
Include=
"UninitializedFunctions.h"
/>
<ClInclude
Include=
"Unordered_set.h"
/>
<ClInclude
Include=
"Utility.h"
/>
...
...
TinySTL/TinySTL.vcxproj.filters
浏览文件 @
b601df79
...
...
@@ -108,6 +108,9 @@
<ClCompile
Include=
"Test\COWPtrTest.cpp"
>
<Filter>
Test
</Filter>
</ClCompile>
<ClCompile
Include=
"Test\UFSetTest.cpp"
>
<Filter>
Test
</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude
Include=
"TypeTraits.h"
>
...
...
@@ -290,6 +293,12 @@
<ClInclude
Include=
"Test\COWPtrTest.h"
>
<Filter>
Test
</Filter>
</ClInclude>
<ClInclude
Include=
"Test\UFSetTest.h"
>
<Filter>
Test
</Filter>
</ClInclude>
<ClInclude
Include=
"UFSet.h"
>
<Filter>
头文件
</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None
Include=
"..\README.md"
/>
...
...
TinySTL/UFSet.h
0 → 100644
浏览文件 @
b601df79
#ifndef _UF_SET_H_
#define _UF_SET_H_
#include <cstring>
namespace
TinySTL
{
template
<
size_t
N
>
class
uf_set
{
public:
uf_set
();
int
Find
(
int
index
);
void
Union
(
int
index1
,
int
index2
);
void
Clear
();
private:
int
parent
[
N
];
//parent[i] = -n 表示节点i是根节点且以i为根的树中共有n个节点
};
template
<
size_t
N
>
uf_set
<
N
>::
uf_set
(){
Clear
();
}
template
<
size_t
N
>
int
uf_set
<
N
>::
Find
(
int
index
){
auto
root
=
index
;
for
(;
parent
[
root
]
>=
0
;
root
=
parent
[
root
]){}
while
(
root
!=
index
){
//路径压缩
auto
t
=
parent
[
index
];
parent
[
index
]
=
root
;
index
=
t
;
}
return
root
;
}
template
<
size_t
N
>
void
uf_set
<
N
>::
Union
(
int
index1
,
int
index2
){
auto
root1
=
Find
(
index1
),
root2
=
Find
(
index2
);
auto
total_nodes
=
parent
[
root1
]
+
parent
[
root2
];
//total nodes
if
(
parent
[
root1
]
>
parent
[
root2
]){
//加权合并
parent
[
root1
]
=
root2
;
parent
[
root2
]
=
total_nodes
;
}
else
{
parent
[
root2
]
=
root1
;
parent
[
root1
]
=
total_nodes
;
}
}
template
<
size_t
N
>
void
uf_set
<
N
>::
Clear
(){
memset
(
parent
,
-
1
,
sizeof
(
int
)
*
N
);
}
}
#endif
\ No newline at end of file
TinySTL/main.cpp
浏览文件 @
b601df79
...
...
@@ -21,6 +21,7 @@
#include "Test\StringTest.h"
#include "Test\SuffixArrayTest.h"
#include "Test\TrieTreeTest.h"
#include "Test\UFSetTest.h"
#include "Test\UniquePtrTest.h"
#include "Test\Unordered_setTest.h"
#include "Test\VectorTest.h"
...
...
@@ -46,6 +47,7 @@ int main(){
TinySTL
::
StringTest
::
testAllCases
();
TinySTL
::
SuffixArrayTest
::
testAllCases
();
TinySTL
::
TrieTreeTest
::
testAllCases
();
TinySTL
::
UFSetTest
::
testAllCases
();
TinySTL
::
UniquePtrTest
::
testAllCases
();
TinySTL
::
Unordered_setTest
::
testAllCases
();
TinySTL
::
VectorTest
::
testAllCases
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录