From 13bcbcb23f967a8beccdc985649c9dd4bf6ec5f5 Mon Sep 17 00:00:00 2001 From: IlyaShurupov Date: Mon, 1 Apr 2024 14:10:40 +0300 Subject: [PATCH] update numeric key interface --- Containers/public/IntervalTree.hpp | 2 +- Containers/public/Tree.hpp | 37 +++++++++++++++--------------- Modules/public/Common.hpp | 9 ++++---- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/Containers/public/IntervalTree.hpp b/Containers/public/IntervalTree.hpp index 6158698..f864680 100644 --- a/Containers/public/IntervalTree.hpp +++ b/Containers/public/IntervalTree.hpp @@ -30,7 +30,7 @@ namespace tp { inline const IntervalKey& keyInLeftSubtree(const IntervalKey& in) const { return in; } template - inline void updateTreeCacheCallBack(const tTreeNodeType* node) { + inline void updateNodeCache(const tTreeNodeType* node) { mMax = 0; if (node->mRight && node->mRight->key.mMax > mMax) mMax = node->mRight->key.mMax; if (node->mLeft && node->mLeft->key.mMax > mMax) mMax = node->mLeft->key.mMax; diff --git a/Containers/public/Tree.hpp b/Containers/public/Tree.hpp index 703cde0..bcfc339 100644 --- a/Containers/public/Tree.hpp +++ b/Containers/public/Tree.hpp @@ -6,26 +6,27 @@ namespace tp { template struct AvlNumericKey { - - NumericType val; - + public: AvlNumericKey() = default; AvlNumericKey(NumericType val) : val(val) {} - inline bool descentRight(AvlNumericKey in) const { return in.val > val; } - inline bool exactNode(AvlNumericKey in) const { return in.val == val; } + inline bool descentRight(const AvlNumericKey& in) const { return in.val > val; } + inline bool exactNode(const AvlNumericKey& in) const { return in.val == val; } + + inline const AvlNumericKey& keyInRightSubtree(const AvlNumericKey& in) const { return in; } + inline const AvlNumericKey& keyInLeftSubtree(const AvlNumericKey& in) const { return in; } template - inline AvlNumericKey getFindKey(const NodeType*) const { + inline void updateNodeCache(const NodeType*) {} + + template + inline const AvlNumericKey& getFindKey(const NodeType*) const { return *this; } - inline AvlNumericKey keyInRightSubtree(AvlNumericKey in) const { return in; } - inline AvlNumericKey keyInLeftSubtree(AvlNumericKey in) const { return in; } - - template - inline void updateTreeCacheCallBack(const NodeType*) {} + public: + NumericType val; }; template @@ -229,8 +230,8 @@ namespace tp { right->mHeight = 1 + max(getNodeHeight(right->mLeft), getNodeHeight(right->mRight)); // cache - head->key.updateTreeCacheCallBack(head); - right->key.updateTreeCacheCallBack(right); + head->key.updateNodeCache(head); + right->key.updateNodeCache(right); return right; } @@ -257,8 +258,8 @@ namespace tp { left->mHeight = 1 + max(getNodeHeight(left->mLeft), getNodeHeight(left->mRight)); // cache - head->key.updateTreeCacheCallBack(head); - left->key.updateTreeCacheCallBack(left); + head->key.updateNodeCache(head); + left->key.updateNodeCache(left); return left; } @@ -271,7 +272,7 @@ namespace tp { if (head == nullptr) { mSize++; Node* out = newNode(key, data); - out->key.updateTreeCacheCallBack(out); + out->key.updateNodeCache(out); return out; } else if (head->key.exactNode(key)) { return head; @@ -306,7 +307,7 @@ namespace tp { } } - head->key.updateTreeCacheCallBack(head); + head->key.updateNodeCache(head); return head; } @@ -362,7 +363,7 @@ namespace tp { } } - head->key.updateTreeCacheCallBack(head); + head->key.updateNodeCache(head); return head; } diff --git a/Modules/public/Common.hpp b/Modules/public/Common.hpp index 824d6d9..423a6c2 100644 --- a/Modules/public/Common.hpp +++ b/Modules/public/Common.hpp @@ -12,12 +12,13 @@ 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; + using SelectValueOrReference = typename TypeSelect<(sizeof(tType) > sizeof(void*)), const tType&, tType>::Result; + + template + using SelectValueOrReferenceOf = + typename TypeSelect<(sizeof(tType) > sizeof(void*)), const tTypeResult&, tTypeResult>::Result; template using VoidType = void;