diff --git a/README.md b/README.md index 3f76540..72a07d5 100644 --- a/README.md +++ b/README.md @@ -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)); diff --git a/TinySTL/Test/UFSetTest.cpp b/TinySTL/Test/UFSetTest.cpp new file mode 100644 index 0000000..ea87fda --- /dev/null +++ b/TinySTL/Test/UFSetTest.cpp @@ -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(); + } + } +} \ No newline at end of file diff --git a/TinySTL/Test/UFSetTest.h b/TinySTL/Test/UFSetTest.h new file mode 100644 index 0000000..ed952b7 --- /dev/null +++ b/TinySTL/Test/UFSetTest.h @@ -0,0 +1,15 @@ +#ifndef _UF_SET_TEST_H_ +#define _UF_SET_TEST_H_ + +#include "../UFSet.h" +#include + +namespace TinySTL{ + namespace UFSetTest{ + void testCase1(); + + void testAllCases(); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/TinySTL.vcxproj b/TinySTL/TinySTL.vcxproj index 8901679..5e1c381 100644 --- a/TinySTL/TinySTL.vcxproj +++ b/TinySTL/TinySTL.vcxproj @@ -103,6 +103,7 @@ + @@ -159,11 +160,13 @@ + + diff --git a/TinySTL/TinySTL.vcxproj.filters b/TinySTL/TinySTL.vcxproj.filters index 166fc97..02e2c24 100644 --- a/TinySTL/TinySTL.vcxproj.filters +++ b/TinySTL/TinySTL.vcxproj.filters @@ -108,6 +108,9 @@ Test + + Test + @@ -290,6 +293,12 @@ Test + + Test + + + 头文件 + diff --git a/TinySTL/UFSet.h b/TinySTL/UFSet.h new file mode 100644 index 0000000..d00af29 --- /dev/null +++ b/TinySTL/UFSet.h @@ -0,0 +1,53 @@ +#ifndef _UF_SET_H_ +#define _UF_SET_H_ + +#include + +namespace TinySTL{ + template + 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 + uf_set::uf_set(){ + Clear(); + } + template + int uf_set::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 + void uf_set::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 + void uf_set::Clear(){ + memset(parent, -1, sizeof(int) * N); + } +} + +#endif \ No newline at end of file diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index 4c8bd60..df3e100 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -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();