Skip to content

Commit

Permalink
#79
Browse files Browse the repository at this point in the history
  • Loading branch information
netkeep80 committed Jan 20, 2021
1 parent d37cf5a commit 41b1470
Show file tree
Hide file tree
Showing 19 changed files with 642 additions and 596 deletions.
12 changes: 6 additions & 6 deletions cpp/Platform.Collections.Methods.Tests.Console/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,37 +30,37 @@ int main()
SizedAndThreadedAVLBalancedTree<10000, std::uint32_t> avlTree;

auto t1 = std::chrono::high_resolution_clock::now();
TestExtensions::TestMultipleCreationsAndDeletions<std::uint32_t>(recursionlessSizeBalancedTree, [&]()-> auto { return recursionlessSizeBalancedTree.Allocate(); }, [&](std::uint32_t link)-> auto { recursionlessSizeBalancedTree.Free(link); }, & recursionlessSizeBalancedTree.Root, [&]()-> auto { return recursionlessSizeBalancedTree.Count(); }, _n);
TestExtensions::TestMultipleCreationsAndDeletions<RecursionlessSizeBalancedTree<10000, std::uint32_t>, std::uint32_t>(recursionlessSizeBalancedTree, [&]()-> auto { return recursionlessSizeBalancedTree.Allocate(); }, [&](std::uint32_t link)-> auto { recursionlessSizeBalancedTree.Free(link); }, & recursionlessSizeBalancedTree.Root, [&]()-> auto { return recursionlessSizeBalancedTree.Count(); }, _n);
auto t2 = std::chrono::high_resolution_clock::now();
auto duration1 = std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
std::cout << duration1 << "ms (in order) for RecursionlessSizeBalancedTreeMethods\n";

auto t3 = std::chrono::high_resolution_clock::now();
TestExtensions::TestMultipleCreationsAndDeletions<std::uint32_t>(sizeBalancedTree, [&]()-> auto { return sizeBalancedTree.Allocate(); }, [&](std::uint32_t link)-> auto { sizeBalancedTree.Free(link); }, & sizeBalancedTree.Root, [&]()-> auto { return sizeBalancedTree.Count(); }, _n);
TestExtensions::TestMultipleCreationsAndDeletions<SizeBalancedTree<10000, std::uint32_t>, std::uint32_t>(sizeBalancedTree, [&]()-> auto { return sizeBalancedTree.Allocate(); }, [&](std::uint32_t link)-> auto { sizeBalancedTree.Free(link); }, & sizeBalancedTree.Root, [&]()-> auto { return sizeBalancedTree.Count(); }, _n);
auto t4 = std::chrono::high_resolution_clock::now();
auto duration2 = std::chrono::duration_cast<std::chrono::milliseconds>(t4 - t3).count();
std::cout << duration2 << "ms (in order) for SizeBalancedTreeMethods\n";

auto t5 = std::chrono::high_resolution_clock::now();
TestExtensions::TestMultipleCreationsAndDeletions<std::uint32_t>(avlTree, [&]()-> auto { return avlTree.Allocate(); }, [&](std::uint32_t link)-> auto { avlTree.Free(link); }, & avlTree.Root, [&]()-> auto { return avlTree.Count(); }, _n);
TestExtensions::TestMultipleCreationsAndDeletions<SizedAndThreadedAVLBalancedTree<10000, std::uint32_t>, std::uint32_t>(avlTree, [&]()-> auto { return avlTree.Allocate(); }, [&](std::uint32_t link)-> auto { avlTree.Free(link); }, & avlTree.Root, [&]()-> auto { return avlTree.Count(); }, _n);
auto t6 = std::chrono::high_resolution_clock::now();
auto duration3 = std::chrono::duration_cast<std::chrono::milliseconds>(t6 - t5).count();
std::cout << duration3 << "ms (in order) for SizedAndThreadedAVLBalancedTreeMethods\n";

auto t7 = std::chrono::high_resolution_clock::now();
TestExtensions::TestMultipleRandomCreationsAndDeletions<std::uint32_t>(recursionlessSizeBalancedTree, &recursionlessSizeBalancedTree.Root, [&]()-> auto { return recursionlessSizeBalancedTree.Count(); }, _n);
TestExtensions::TestMultipleRandomCreationsAndDeletions<RecursionlessSizeBalancedTree<10000, std::uint32_t>, std::uint32_t>(recursionlessSizeBalancedTree, &recursionlessSizeBalancedTree.Root, [&]()-> auto { return recursionlessSizeBalancedTree.Count(); }, _n);
auto t8 = std::chrono::high_resolution_clock::now();
auto duration4 = std::chrono::duration_cast<std::chrono::milliseconds>(t8 - t7).count();
std::cout << duration4 << "ms (random) for RecursionlessSizeBalancedTreeMethods\n";

auto t9 = std::chrono::high_resolution_clock::now();
TestExtensions::TestMultipleRandomCreationsAndDeletions<std::uint32_t>(sizeBalancedTree, &sizeBalancedTree.Root, [&]()-> auto { return sizeBalancedTree.Count(); }, _n);
TestExtensions::TestMultipleRandomCreationsAndDeletions<SizeBalancedTree<10000, std::uint32_t>, std::uint32_t>(sizeBalancedTree, &sizeBalancedTree.Root, [&]()-> auto { return sizeBalancedTree.Count(); }, _n);
auto t10 = std::chrono::high_resolution_clock::now();
auto duration5 = std::chrono::duration_cast<std::chrono::milliseconds>(t10 - t9).count();
std::cout << duration5 << "ms (random) for SizeBalancedTreeMethods\n";

auto t11 = std::chrono::high_resolution_clock::now();
TestExtensions::TestMultipleRandomCreationsAndDeletions<std::uint32_t>(avlTree, &avlTree.Root, [&]()-> auto { return avlTree.Count(); }, _n);
TestExtensions::TestMultipleRandomCreationsAndDeletions<SizedAndThreadedAVLBalancedTree<10000, std::uint32_t>, std::uint32_t>(avlTree, &avlTree.Root, [&]()-> auto { return avlTree.Count(); }, _n);
auto t12 = std::chrono::high_resolution_clock::now();
auto duration6 = std::chrono::duration_cast<std::chrono::milliseconds>(t12 - t11).count();
std::cout << duration6 << "ms (random) for SizedAndThreadedAVLBalancedTreeMethods\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace Platform::Collections::Methods::Tests
{
template <std::size_t N, typename ...> class RecursionlessSizeBalancedTree;
template <std::size_t N, typename TElement> class RecursionlessSizeBalancedTree<N, TElement> : public Platform::Collections::Methods::Trees::RecursionlessSizeBalancedTreeMethods<TElement>
template <std::size_t N, typename TElement> class RecursionlessSizeBalancedTree<N, TElement> : public Platform::Collections::Methods::Trees::RecursionlessSizeBalancedTreeMethods<RecursionlessSizeBalancedTree<N, TElement>, TElement>
{
using base_t = Platform::Collections::Methods::Trees::RecursionlessSizeBalancedTreeMethods<RecursionlessSizeBalancedTree<N, TElement>, TElement>;
friend base_t;
friend base_t::base_t;

struct TreeElement
{
public: TElement Size = 0;
Expand Down Expand Up @@ -52,25 +56,25 @@

public: bool IsEmpty(TElement node) { return iszero(this->GetElement(node), sizeof(TreeElement)); }

protected: bool FirstIsToTheLeftOfSecond(TElement first, TElement second) override { return first < second; }
protected: bool FirstIsToTheLeftOfSecond(TElement first, TElement second) { return first < second; }

protected: bool FirstIsToTheRightOfSecond(TElement first, TElement second) override { return first > second; }
protected: bool FirstIsToTheRightOfSecond(TElement first, TElement second) { return first > second; }

protected: TElement* GetLeftReference(TElement node) override { return &GetElement(node)->Left; }
protected: TElement* GetLeftReference(TElement node) { return &GetElement(node)->Left; }

protected: TElement GetLeft(TElement node) override { return this->GetElement(node)->Left; }
protected: TElement GetLeft(TElement node) { return this->GetElement(node)->Left; }

protected: TElement* GetRightReference(TElement node) override { return &GetElement(node)->Right; }
protected: TElement* GetRightReference(TElement node) { return &GetElement(node)->Right; }

protected: TElement GetRight(TElement node) override { return this->GetElement(node)->Right; }
protected: TElement GetRight(TElement node) { return this->GetElement(node)->Right; }

protected: TElement GetSize(TElement node) override { return this->GetElement(node)->Size; }
protected: TElement GetSize(TElement node) { return this->GetElement(node)->Size; }

protected: void SetLeft(TElement node, TElement left) override { this->GetElement(node)->Left = left; }
protected: void SetLeft(TElement node, TElement left) { this->GetElement(node)->Left = left; }

protected: void SetRight(TElement node, TElement right) override { this->GetElement(node)->Right = right; }
protected: void SetRight(TElement node, TElement right) { this->GetElement(node)->Right = right; }

protected: void SetSize(TElement node, TElement size) override { this->GetElement(node)->Size = size; }
protected: void SetSize(TElement node, TElement size) { this->GetElement(node)->Size = size; }

private: TreeElement* GetElement(TElement node) { return &_elements[node]; }
};
Expand Down
26 changes: 15 additions & 11 deletions cpp/Platform.Collections.Methods.Tests/SizeBalancedTree.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace Platform::Collections::Methods::Tests
{
template <std::size_t N, typename ...> class SizeBalancedTree;
template <std::size_t N, typename TElement> class SizeBalancedTree<N, TElement> : public Platform::Collections::Methods::Trees::SizeBalancedTreeMethods<TElement>
template <std::size_t N, typename TElement> class SizeBalancedTree<N, TElement> : public Platform::Collections::Methods::Trees::SizeBalancedTreeMethods<SizeBalancedTree<N, TElement>, TElement>
{
using base_t = Platform::Collections::Methods::Trees::SizeBalancedTreeMethods<SizeBalancedTree<N, TElement>, TElement>;
friend base_t;
friend base_t::base_t;

struct TreeElement
{
public: TElement Size = 0;
Expand Down Expand Up @@ -52,25 +56,25 @@

public: bool IsEmpty(TElement node) { return iszero(this->GetElement(node), sizeof(TreeElement)); }

protected: bool FirstIsToTheLeftOfSecond(TElement first, TElement second) override { return first < second; }
protected: bool FirstIsToTheLeftOfSecond(TElement first, TElement second) { return first < second; }

protected: bool FirstIsToTheRightOfSecond(TElement first, TElement second) override { return first > second; }
protected: bool FirstIsToTheRightOfSecond(TElement first, TElement second) { return first > second; }

protected: TElement* GetLeftReference(TElement node) override { return &GetElement(node)->Left; }
protected: TElement* GetLeftReference(TElement node) { return &GetElement(node)->Left; }

protected: TElement GetLeft(TElement node) override { return this->GetElement(node)->Left; }
protected: TElement GetLeft(TElement node) { return this->GetElement(node)->Left; }

protected: TElement* GetRightReference(TElement node) override { return &GetElement(node)->Right; }
protected: TElement* GetRightReference(TElement node) { return &GetElement(node)->Right; }

protected: TElement GetRight(TElement node) override { return this->GetElement(node)->Right; }
protected: TElement GetRight(TElement node) { return this->GetElement(node)->Right; }

protected: TElement GetSize(TElement node) override { return this->GetElement(node)->Size; }
protected: TElement GetSize(TElement node) { return this->GetElement(node)->Size; }

protected: void SetLeft(TElement node, TElement left) override { this->GetElement(node)->Left = left; }
protected: void SetLeft(TElement node, TElement left) { this->GetElement(node)->Left = left; }

protected: void SetRight(TElement node, TElement right) override { this->GetElement(node)->Right = right; }
protected: void SetRight(TElement node, TElement right) { this->GetElement(node)->Right = right; }

protected: void SetSize(TElement node, TElement size) override { this->GetElement(node)->Size = size; }
protected: void SetSize(TElement node, TElement size) { this->GetElement(node)->Size = size; }

private: TreeElement* GetElement(TElement node) { return &_elements[node]; }
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
namespace Platform::Collections::Methods::Tests
{
template <std::size_t N, typename ...> class SizedAndThreadedAVLBalancedTree;
template <std::size_t N, typename TElement> class SizedAndThreadedAVLBalancedTree<N, TElement> : public Platform::Collections::Methods::Trees::SizedAndThreadedAVLBalancedTreeMethods<TElement>
template <std::size_t N, typename TElement> class SizedAndThreadedAVLBalancedTree<N, TElement> : public Platform::Collections::Methods::Trees::SizedAndThreadedAVLBalancedTreeMethods<SizedAndThreadedAVLBalancedTree<N, TElement>, TElement>
{
using base_t = Platform::Collections::Methods::Trees::SizedAndThreadedAVLBalancedTreeMethods<SizedAndThreadedAVLBalancedTree<N, TElement>, TElement>;
friend base_t;
friend base_t::base_t;

struct TreeElement
{
public: TElement Size = 0;
Expand Down Expand Up @@ -55,37 +59,37 @@

public: bool IsEmpty(TElement node) { return iszero(this->GetElement(node), sizeof(TreeElement)); }

protected: bool FirstIsToTheLeftOfSecond(TElement first, TElement second) override { return first < second; }
protected: bool FirstIsToTheLeftOfSecond(TElement first, TElement second) { return first < second; }

protected: bool FirstIsToTheRightOfSecond(TElement first, TElement second) override { return first > second; }
protected: bool FirstIsToTheRightOfSecond(TElement first, TElement second) { return first > second; }

protected: std::int8_t GetBalance(TElement node) override { return this->GetElement(node)->Balance; }
protected: std::int8_t GetBalance(TElement node) { return this->GetElement(node)->Balance; }

protected: bool GetLeftIsChild(TElement node) override { return this->GetElement(node)->LeftIsChild; }
protected: bool GetLeftIsChild(TElement node) { return this->GetElement(node)->LeftIsChild; }

protected: TElement* GetLeftReference(TElement node) override { return &GetElement(node)->Left; }
protected: TElement* GetLeftReference(TElement node) { return &GetElement(node)->Left; }

protected: TElement GetLeft(TElement node) override { return this->GetElement(node)->Left; }
protected: TElement GetLeft(TElement node) { return this->GetElement(node)->Left; }

protected: bool GetRightIsChild(TElement node) override { return this->GetElement(node)->RightIsChild; }
protected: bool GetRightIsChild(TElement node) { return this->GetElement(node)->RightIsChild; }

protected: TElement* GetRightReference(TElement node) override { return &GetElement(node)->Right; }
protected: TElement* GetRightReference(TElement node) { return &GetElement(node)->Right; }

protected: TElement GetRight(TElement node) override { return this->GetElement(node)->Right; }
protected: TElement GetRight(TElement node) { return this->GetElement(node)->Right; }

protected: TElement GetSize(TElement node) override { return this->GetElement(node)->Size; }
protected: TElement GetSize(TElement node) { return this->GetElement(node)->Size; }

protected: void SetBalance(TElement node, std::int8_t value) override { this->GetElement(node)->Balance = value; }
protected: void SetBalance(TElement node, std::int8_t value) { this->GetElement(node)->Balance = value; }

protected: void SetLeft(TElement node, TElement left) override { this->GetElement(node)->Left = left; }
protected: void SetLeft(TElement node, TElement left) { this->GetElement(node)->Left = left; }

protected: void SetLeftIsChild(TElement node, bool value) override { this->GetElement(node)->LeftIsChild = value; }
protected: void SetLeftIsChild(TElement node, bool value) { this->GetElement(node)->LeftIsChild = value; }

protected: void SetRight(TElement node, TElement right) override { this->GetElement(node)->Right = right; }
protected: void SetRight(TElement node, TElement right) { this->GetElement(node)->Right = right; }

protected: void SetRightIsChild(TElement node, bool value) override { this->GetElement(node)->RightIsChild = value; }
protected: void SetRightIsChild(TElement node, bool value) { this->GetElement(node)->RightIsChild = value; }

protected: void SetSize(TElement node, TElement size) override { this->GetElement(node)->Size = size; }
protected: void SetSize(TElement node, TElement size) { this->GetElement(node)->Size = size; }

private: TreeElement* GetElement(TElement node) { return &_elements[node]; }
};
Expand Down
4 changes: 2 additions & 2 deletions cpp/Platform.Collections.Methods.Tests/TestExtensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
class TestExtensions
{
public: template <typename TElement> static void TestMultipleCreationsAndDeletions(Platform::Collections::Methods::Trees::SizedBinaryTreeMethodsBase<TElement>& tree, std::function<TElement()> allocate, std::function<void(TElement)> free, TElement* root, std::function<TElement()> treeCount, std::int32_t maximumOperationsPerCycle)
public: template <typename impl_t, typename TElement> static void TestMultipleCreationsAndDeletions(impl_t& tree, std::function<TElement()> allocate, std::function<void(TElement)> free, TElement* root, std::function<TElement()> treeCount, std::int32_t maximumOperationsPerCycle)
{
for (auto N = 1; N < maximumOperationsPerCycle; N++)
{
Expand All @@ -28,7 +28,7 @@
}
}

public: template <typename TElement> static void TestMultipleRandomCreationsAndDeletions(Platform::Collections::Methods::Trees::SizedBinaryTreeMethodsBase<TElement>& tree, TElement* root, std::function<TElement()> treeCount, std::int32_t maximumOperationsPerCycle)
public: template <typename impl_t, typename TElement> static void TestMultipleRandomCreationsAndDeletions(impl_t& tree, TElement* root, std::function<TElement()> treeCount, std::int32_t maximumOperationsPerCycle)
{
std::srand(0);
std::unordered_set<TElement> added;
Expand Down
Loading

0 comments on commit 41b1470

Please sign in to comment.