From 86f81fb53464b5d116d6e286ed4ab1ba0df31f81 Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Mon, 1 Apr 2024 13:46:13 +0300 Subject: [PATCH] Tree. remove redunant function --- Containers/public/IntervalTree.hpp | 7 +------ Containers/public/Tree.hpp | 30 ++++++++++++++---------------- Containers/tests/SortedStream.cpp | 16 ++++++++++++++++ Modules/public/Common.hpp | 3 +++ 4 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 Containers/tests/SortedStream.cpp diff --git a/Containers/public/IntervalTree.hpp b/Containers/public/IntervalTree.hpp index 261e402..a0ad1c4 100644 --- a/Containers/public/IntervalTree.hpp +++ b/Containers/public/IntervalTree.hpp @@ -14,16 +14,11 @@ namespace tp { mEnd = end; } - inline bool descentRight(const IntervalKey& in) const { + inline bool descentRight(const IntervalKey& in) const { if (in.mStart != mStart) return in.mStart > mStart; return in.mEnd > mEnd; } - inline bool descentLeft(const IntervalKey& in) const { - if (in.mStart != mStart) return in.mStart < mStart; - return in.mEnd < mEnd; - } - inline bool exactNode(const IntervalKey& in) const { return in.mStart == mStart && in.mEnd == mEnd; } inline const IntervalKey& getFindKey() const { return *this; } diff --git a/Containers/public/Tree.hpp b/Containers/public/Tree.hpp index 3b84fe8..6fc486e 100644 --- a/Containers/public/Tree.hpp +++ b/Containers/public/Tree.hpp @@ -14,7 +14,6 @@ namespace tp { val(val) {} inline bool descentRight(AvlNumericKey in) const { return in.val > val; } - inline bool descentLeft(AvlNumericKey in) const { return in.val < val; } inline bool exactNode(AvlNumericKey in) const { return in.val == val; } inline AvlNumericKey getFindKey(/**/) const { return *this; } @@ -51,7 +50,6 @@ namespace tp { private: inline bool descentRight(KeyArg aKey) const { return key.descentRight(aKey); } - inline bool descentLeft(KeyArg aKey) const { return key.descentLeft(aKey); } inline bool exactNode(KeyArg aKey) const { return key.exactNode(aKey); } inline KeyArg getFindKey(const Node* node = nullptr) const { return key.getFindKey(/*node*/); } @@ -100,12 +98,12 @@ namespace tp { while (true) { if (!iter) return nullptr; if (iter->exactNode(key)) return iter; - if (iter->descentLeft(key)) { - key = iter->keyInLeftSubtree(key); - iter = iter->mLeft; - } else { + if (iter->descentRight(key)) { key = iter->keyInRightSubtree(key); iter = iter->mRight; + } else { + key = iter->keyInLeftSubtree(key); + iter = iter->mLeft; } } } @@ -115,17 +113,17 @@ namespace tp { while (true) { if (!iter) return nullptr; if (iter->exactNode(key)) return iter; - if (iter->descentLeft(key)) { - if (iter->mLeft) { - key = iter->keyInLeftSubtree(key); - iter = iter->mLeft; + if (iter->descentRight(key)) { + if (iter->mRight) { + key = iter->keyInRightSubtree(key); + iter = iter->mRight; } else { return iter; } } else { - if (iter->mRight) { - key = iter->keyInRightSubtree(key); - iter = iter->mRight; + if (iter->mLeft) { + key = iter->keyInLeftSubtree(key); + iter = iter->mLeft; } else { return iter; } @@ -139,7 +137,7 @@ namespace tp { if (head->mLeft) { // TODO: incomplete test - if (!head->descentLeft(head->mLeft->getFindKey(head))) return head; + if (head->descentRight(head->mLeft->getFindKey(head))) return head; if (head->mLeft->mParent != head) return head; if (!head->mRight && head->mLeft->mHeight != head->mHeight - 1) return head; } @@ -307,7 +305,7 @@ namespace tp { return rotateLeft(head); } } else if (balance < -1) { - if (head->mLeft->descentLeft(head->keyInLeftSubtree(key))) { + if (!head->mLeft->descentRight(head->keyInLeftSubtree(key))) { return rotateRight(head); } else { head->mLeft = rotateLeft(head->mLeft); @@ -346,7 +344,7 @@ namespace tp { } } else if (head->descentRight(key)) { head->mRight = removeUtil(head->mRight, head->keyInRightSubtree(key)); - } else if (head->descentLeft(key)) { + } else { head->mLeft = removeUtil(head->mLeft, head->keyInLeftSubtree(key)); } diff --git a/Containers/tests/SortedStream.cpp b/Containers/tests/SortedStream.cpp new file mode 100644 index 0000000..286d119 --- /dev/null +++ b/Containers/tests/SortedStream.cpp @@ -0,0 +1,16 @@ + +#include "Tree.hpp" +#include "Timing.hpp" +#include "UnitTest++/UnitTest++.h" + +#include + +TEST(AVLTreeStream) { + tp::Timer timer(1000); + + tp::AvlTree> tree; + + while (!timer.isTimeout()) { + tree. + } +} diff --git a/Modules/public/Common.hpp b/Modules/public/Common.hpp index abf9e3b..824d6d9 100644 --- a/Modules/public/Common.hpp +++ b/Modules/public/Common.hpp @@ -12,6 +12,9 @@ namespace tp { template using InitialierList = std::initializer_list; + template + using ShouldPassByValue = typename TypeSelect<(sizeof(tType) > sizeof(tp::alni)), const tType&, tType>::Result; + // Selects whether to pass by constant reference or by value template using SelectValueOrReference = typename TypeSelect<(sizeof(tType) > sizeof(tp::alni)), const tType&, tType>::Result;