union-find set
This commit is contained in:
11
README.md
11
README.md
@@ -582,6 +582,17 @@ TinySTL
|
|||||||
*cp2 = ("C++");//write
|
*cp2 = ("C++");//write
|
||||||
assert(*cp1 == *cp3 && *cp3 == "zouxiaohang");
|
assert(*cp1 == *cp3 && *cp3 == "zouxiaohang");
|
||||||
assert(*cp2 == "C++");
|
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));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
17
TinySTL/Test/UFSetTest.cpp
Normal file
17
TinySTL/Test/UFSetTest.cpp
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
TinySTL/Test/UFSetTest.h
Normal file
15
TinySTL/Test/UFSetTest.h
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef _UF_SET_TEST_H_
|
||||||
|
#define _UF_SET_TEST_H_
|
||||||
|
|
||||||
|
#include "../UFSet.h"
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
namespace TinySTL{
|
||||||
|
namespace UFSetTest{
|
||||||
|
void testCase1();
|
||||||
|
|
||||||
|
void testAllCases();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -103,6 +103,7 @@
|
|||||||
<ClCompile Include="Test\StringTest.cpp" />
|
<ClCompile Include="Test\StringTest.cpp" />
|
||||||
<ClCompile Include="Test\SuffixArrayTest.cpp" />
|
<ClCompile Include="Test\SuffixArrayTest.cpp" />
|
||||||
<ClCompile Include="Test\TrieTreeTest.cpp" />
|
<ClCompile Include="Test\TrieTreeTest.cpp" />
|
||||||
|
<ClCompile Include="Test\UFSetTest.cpp" />
|
||||||
<ClCompile Include="Test\UniquePtrTest.cpp" />
|
<ClCompile Include="Test\UniquePtrTest.cpp" />
|
||||||
<ClCompile Include="Test\Unordered_setTest.cpp" />
|
<ClCompile Include="Test\Unordered_setTest.cpp" />
|
||||||
<ClCompile Include="Test\VectorTest.cpp" />
|
<ClCompile Include="Test\VectorTest.cpp" />
|
||||||
@@ -159,11 +160,13 @@
|
|||||||
<ClInclude Include="Test\SuffixArrayTest.h" />
|
<ClInclude Include="Test\SuffixArrayTest.h" />
|
||||||
<ClInclude Include="Test\TestUtil.h" />
|
<ClInclude Include="Test\TestUtil.h" />
|
||||||
<ClInclude Include="Test\TrieTreeTest.h" />
|
<ClInclude Include="Test\TrieTreeTest.h" />
|
||||||
|
<ClInclude Include="Test\UFSetTest.h" />
|
||||||
<ClInclude Include="Test\UniquePtrTest.h" />
|
<ClInclude Include="Test\UniquePtrTest.h" />
|
||||||
<ClInclude Include="Test\Unordered_setTest.h" />
|
<ClInclude Include="Test\Unordered_setTest.h" />
|
||||||
<ClInclude Include="Test\VectorTest.h" />
|
<ClInclude Include="Test\VectorTest.h" />
|
||||||
<ClInclude Include="TrieTree.h" />
|
<ClInclude Include="TrieTree.h" />
|
||||||
<ClInclude Include="TypeTraits.h" />
|
<ClInclude Include="TypeTraits.h" />
|
||||||
|
<ClInclude Include="UFSet.h" />
|
||||||
<ClInclude Include="UninitializedFunctions.h" />
|
<ClInclude Include="UninitializedFunctions.h" />
|
||||||
<ClInclude Include="Unordered_set.h" />
|
<ClInclude Include="Unordered_set.h" />
|
||||||
<ClInclude Include="Utility.h" />
|
<ClInclude Include="Utility.h" />
|
||||||
|
|||||||
@@ -108,6 +108,9 @@
|
|||||||
<ClCompile Include="Test\COWPtrTest.cpp">
|
<ClCompile Include="Test\COWPtrTest.cpp">
|
||||||
<Filter>Test</Filter>
|
<Filter>Test</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Test\UFSetTest.cpp">
|
||||||
|
<Filter>Test</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="TypeTraits.h">
|
<ClInclude Include="TypeTraits.h">
|
||||||
@@ -290,6 +293,12 @@
|
|||||||
<ClInclude Include="Test\COWPtrTest.h">
|
<ClInclude Include="Test\COWPtrTest.h">
|
||||||
<Filter>Test</Filter>
|
<Filter>Test</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Test\UFSetTest.h">
|
||||||
|
<Filter>Test</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="UFSet.h">
|
||||||
|
<Filter>头文件</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\README.md" />
|
<None Include="..\README.md" />
|
||||||
|
|||||||
53
TinySTL/UFSet.h
Normal file
53
TinySTL/UFSet.h
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
#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 <20><>ʾ<EFBFBD>ڵ<EFBFBD>i<EFBFBD>Ǹ<EFBFBD><C7B8>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>iΪ<69><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD>n<EFBFBD><6E><EFBFBD>ڵ<EFBFBD>
|
||||||
|
};
|
||||||
|
|
||||||
|
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){//·<><C2B7>ѹ<EFBFBD><D1B9>
|
||||||
|
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]){//<2F><>Ȩ<EFBFBD>ϲ<EFBFBD>
|
||||||
|
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
|
||||||
@@ -21,6 +21,7 @@
|
|||||||
#include "Test\StringTest.h"
|
#include "Test\StringTest.h"
|
||||||
#include "Test\SuffixArrayTest.h"
|
#include "Test\SuffixArrayTest.h"
|
||||||
#include "Test\TrieTreeTest.h"
|
#include "Test\TrieTreeTest.h"
|
||||||
|
#include "Test\UFSetTest.h"
|
||||||
#include "Test\UniquePtrTest.h"
|
#include "Test\UniquePtrTest.h"
|
||||||
#include "Test\Unordered_setTest.h"
|
#include "Test\Unordered_setTest.h"
|
||||||
#include "Test\VectorTest.h"
|
#include "Test\VectorTest.h"
|
||||||
@@ -46,6 +47,7 @@ int main(){
|
|||||||
TinySTL::StringTest::testAllCases();
|
TinySTL::StringTest::testAllCases();
|
||||||
TinySTL::SuffixArrayTest::testAllCases();
|
TinySTL::SuffixArrayTest::testAllCases();
|
||||||
TinySTL::TrieTreeTest::testAllCases();
|
TinySTL::TrieTreeTest::testAllCases();
|
||||||
|
TinySTL::UFSetTest::testAllCases();
|
||||||
TinySTL::UniquePtrTest::testAllCases();
|
TinySTL::UniquePtrTest::testAllCases();
|
||||||
TinySTL::Unordered_setTest::testAllCases();
|
TinySTL::Unordered_setTest::testAllCases();
|
||||||
TinySTL::VectorTest::testAllCases();
|
TinySTL::VectorTest::testAllCases();
|
||||||
|
|||||||
Reference in New Issue
Block a user