diff --git a/CMakeLists.txt b/CMakeLists.txt index fb5abf3a..7de855f4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,6 +14,7 @@ include(cmake/function.cmake) # подхватываем функции, # и для создания исполняемого проекта в отдельные функции add_subdirectory(lib_easy_example) # подключаем дополнительный CMakeLists.txt из подкаталога с именем lib_easy_example +add_subdirectory(DSU) add_subdirectory(main) # подключаем дополнительный CMakeLists.txt из подкаталога с именем main diff --git a/DSU/CMakeLists.txt b/DSU/CMakeLists.txt new file mode 100644 index 00000000..424d2417 --- /dev/null +++ b/DSU/CMakeLists.txt @@ -0,0 +1 @@ +create_project_lib(DSU) \ No newline at end of file diff --git a/DSU/DSU.vcxproj b/DSU/DSU.vcxproj new file mode 100644 index 00000000..d0529f2b --- /dev/null +++ b/DSU/DSU.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {e64af666-afea-4583-acc2-9d2d6d9f47ce} + DSU + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/DSU/DSU.vcxproj.filters b/DSU/DSU.vcxproj.filters new file mode 100644 index 00000000..7556da46 --- /dev/null +++ b/DSU/DSU.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Исходные файлы + + + + + Файлы заголовков + + + \ No newline at end of file diff --git a/DSU/DSU.vcxproj.user b/DSU/DSU.vcxproj.user new file mode 100644 index 00000000..88a55094 --- /dev/null +++ b/DSU/DSU.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/DSU/dsu.cpp b/DSU/dsu.cpp new file mode 100644 index 00000000..ace097ef --- /dev/null +++ b/DSU/dsu.cpp @@ -0,0 +1,45 @@ +#include"dsu.h" + +#include + +DSU::DSU(int size) : _size(size) { + _parent = new int[size]; + _rank = new int[size]; + for (int i = 0; i < size; i++) { + _parent[i] = i; + _rank[i] = 0; + } +} + +DSU::~DSU() { + delete[] _parent; + delete[] _rank; +} + +int DSU::find(int x) { + if (_parent[x] == x) + return x; + else { + int tmp = find(_parent[x]); + _parent[x] = tmp; + return tmp; + } +} + +void DSU::unite(int x1, int x2) { + if (x1 != x2) { + int rank1 = _rank[find(x1)]; + int rank2 = _rank[find(x2)]; + if (rank1 < rank2) { + _parent[x1] = find(x2); + } + else { + _parent[x2] = find(x1); + if (rank1 == rank2) { + _rank[x1]++; + } + } + } + else + throw std::logic_error("Can unite element with themself"); +} \ No newline at end of file diff --git a/DSU/dsu.h b/DSU/dsu.h new file mode 100644 index 00000000..daa9e3f5 --- /dev/null +++ b/DSU/dsu.h @@ -0,0 +1,12 @@ +#pragma once + +class DSU { + int* _parent; + int* _rank; + int _size; +public: + DSU(int size); + void unite(int, int); + int find(int); + ~DSU(); +}; \ No newline at end of file diff --git a/tests/test_easy_example.cpp b/tests/test_easy_example.cpp index 3a67b612..ef1eaefb 100644 --- a/tests/test_easy_example.cpp +++ b/tests/test_easy_example.cpp @@ -1,5 +1,5 @@ // Copyright 2024 Marina Usova - +/* #include #include "../lib_easy_example/easy_example.h" @@ -48,3 +48,4 @@ TEST(TestEasyExampleLib, throw_when_try_div_by_zero) { // Act & Assert ASSERT_ANY_THROW(division(x, y)); } +*/ \ No newline at end of file