From 6a80778dbee8d7aeb1e6ef0684663ed974b10d82 Mon Sep 17 00:00:00 2001 From: morzhovets Date: Fri, 8 Sep 2023 00:42:18 +0400 Subject: [PATCH] Functors refactoring --- include/momo/Array.h | 34 ++++++++++---------- include/momo/HashSet.h | 5 +-- include/momo/TreeSet.h | 70 ++++++++++++++++++++++-------------------- 3 files changed, 56 insertions(+), 53 deletions(-) diff --git a/include/momo/Array.h b/include/momo/Array.h index 7e879b388..ac57fd439 100644 --- a/include/momo/Array.h +++ b/include/momo/Array.h @@ -302,8 +302,8 @@ class Array return pvReallocate(capacityExp, internal::BoolConstant()); } - template - void Reset(size_t capacity, size_t count, ItemsRelocator itemsRelocator) + template + void Reset(size_t capacity, size_t count, ItemsCreator&& itemsCreator) { MOMO_ASSERT(count <= capacity); pvCheckCapacity(capacity); @@ -312,7 +312,7 @@ class Array Item* items = pvAllocate(capacity); try { - itemsRelocator(items); + std::forward(itemsCreator)(items); } catch (...) { @@ -326,7 +326,7 @@ class Array } else { - pvReset(count, itemsRelocator); + pvReset(count, std::forward(itemsCreator)); } } @@ -448,23 +448,23 @@ class Array return false; } - template 0)> internal::EnableIf - pvReset(size_t count, ItemsRelocator itemsRelocator) + pvReset(size_t count, ItemsCreator&& itemsCreator) { MOMO_ASSERT(!pvIsInternal()); size_t initCapacity = mCapacity; - itemsRelocator(&mInternalItems); + std::forward(itemsCreator)(&mInternalItems); MemManagerProxy::Deallocate(GetMemManager(), mItems, initCapacity * sizeof(Item)); mItems = &mInternalItems; mCount = count; } - template 0)> internal::EnableIf - pvReset(size_t count, ItemsRelocator /*itemsRelocator*/) noexcept + pvReset(size_t count, ItemsCreator&& /*itemsCreator*/) noexcept { (void)count; MOMO_ASSERT(count == 0); @@ -677,7 +677,7 @@ class Array { size_t newCapacity = pvGrowCapacity(initCapacity, newCount, ArrayGrowCause::reserve, false); - auto itemsRelocator = [this, initCount, newCount, &multiItemCreator] (Item* newItems) + auto itemsCreator = [this, initCount, newCount, &multiItemCreator] (Item* newItems) { size_t index = initCount; try @@ -692,7 +692,7 @@ class Array throw; } }; - mData.Reset(newCapacity, newCount, itemsRelocator); + mData.Reset(newCapacity, newCount, itemsCreator); } } @@ -753,9 +753,9 @@ class Array capacity = count; if (!mData.Reallocate(capacity, capacity)) { - auto itemsRelocator = [this, count] (Item* newItems) + auto itemsCreator = [this, count] (Item* newItems) { ItemTraits::Relocate(GetMemManager(), GetItems(), newItems, count); }; - mData.Reset(capacity, count, itemsRelocator); + mData.Reset(capacity, count, itemsCreator); } } @@ -1006,9 +1006,9 @@ class Array if (!mData.Reallocate(newCapacityLin, newCapacityExp)) { size_t count = GetCount(); - auto itemsRelocator = [this, count] (Item* newItems) + auto itemsCreator = [this, count] (Item* newItems) { ItemTraits::Relocate(GetMemManager(), GetItems(), newItems, count); }; - mData.Reset(newCapacityExp, count, itemsRelocator); + mData.Reset(newCapacityExp, count, itemsCreator); } } @@ -1033,12 +1033,12 @@ class Array size_t initCount = GetCount(); size_t newCount = initCount + 1; size_t newCapacity = pvGrowCapacity(GetCapacity(), newCount, ArrayGrowCause::add, false); - auto itemsRelocator = [this, initCount, &itemCreator] (Item* newItems) + auto itemsCreator = [this, initCount, &itemCreator] (Item* newItems) { ItemTraits::RelocateCreate(GetMemManager(), GetItems(), newItems, initCount, std::forward(itemCreator), newItems + initCount); }; - mData.Reset(newCapacity, newCount, itemsRelocator); + mData.Reset(newCapacity, newCount, itemsCreator); } void pvAddBackGrow(Item&& item) diff --git a/include/momo/HashSet.h b/include/momo/HashSet.h index 3a7f3c6dc..a9b3f60cd 100644 --- a/include/momo/HashSet.h +++ b/include/momo/HashSet.h @@ -1182,7 +1182,7 @@ class HashSet } template - ConstIterator pvRemove(ConstIterator iter, ItemReplacer itemReplacer) + ConstIterator pvRemove(ConstIterator iter, ItemReplacer&& itemReplacer) { MOMO_CHECK(mBuckets != nullptr); ConstPosition pos = iter; @@ -1192,7 +1192,8 @@ class HashSet size_t bucketIndex = ConstPositionProxy::GetBucketIndex(pos); Buckets* buckets = pvFindBuckets(bucketIndex, bucketIter); Bucket& bucket = (*buckets)[bucketIndex]; - bucketIter = bucket.Remove(buckets->GetBucketParams(), bucketIter, itemReplacer); + bucketIter = bucket.Remove(buckets->GetBucketParams(), bucketIter, + std::forward(itemReplacer)); --mCount; mCrew.IncVersion(); if (!ConstIteratorProxy::IsMovable(iter)) diff --git a/include/momo/TreeSet.h b/include/momo/TreeSet.h index fe42ed27b..26bfcd818 100644 --- a/include/momo/TreeSet.h +++ b/include/momo/TreeSet.h @@ -838,11 +838,11 @@ class TreeSet ConstIterator Remove(ConstIterator iter) { - auto itemReplacer1 = [this] (Item& srcItem) - { ItemTraits::Destroy(&GetMemManager(), srcItem); }; - auto itemReplacer2 = [this] (Item& srcItem, Item& dstItem) + auto itemRemover = [this] (Item& item) + { ItemTraits::Destroy(&GetMemManager(), item); }; + auto itemReplacer = [this] (Item& srcItem, Item& dstItem) { ItemTraits::Replace(GetMemManager(), srcItem, dstItem); }; - return pvRemove(iter, itemReplacer1, itemReplacer2); + return pvRemove(iter, itemRemover, itemReplacer); } ConstIterator Remove(ConstIterator iter, ExtractedItem& extItem) @@ -886,10 +886,10 @@ class TreeSet size_t resItemIndex; if (node1 == node2 && node1->IsLeaf()) { - auto itemDestroyer = [this] (Item& item) + auto itemRemover = [this] (Item& item) { ItemTraits::Destroy(&GetMemManager(), item); }; for (size_t i = itemIndex2 + 1; i > itemIndex1; --i) - node1->Remove(*mNodeParams, i - 1, itemDestroyer); + node1->Remove(*mNodeParams, i - 1, itemRemover); resNode = node1; resItemIndex = itemIndex1; pvRebalance(resNode, resNode, true); @@ -1160,7 +1160,7 @@ class TreeSet } template - ConstIterator pvFindFirst(Predicate pred) const + ConstIterator pvFindFirst(const Predicate& pred) const { if (mRootNode == nullptr) return ConstIterator(); @@ -1179,7 +1179,7 @@ class TreeSet } template - size_t pvFindFirst(Node* node, Predicate pred) const + size_t pvFindFirst(Node* node, const Predicate& pred) const { if (TreeTraits::useLinearSearch) { @@ -1355,9 +1355,9 @@ class TreeSet pvUpdateParents(node); //? } - template - ConstIterator pvRemove(ConstIterator iter, ItemReplacer1 itemReplacer1, - ItemReplacer2 itemReplacer2) + template + ConstIterator pvRemove(ConstIterator iter, ItemRemover&& itemRemover, + ItemReplacer&& itemReplacer) { ConstIteratorProxy::Check(iter, mCrew.GetVersion(), false); MOMO_CHECK(iter != GetEnd()); @@ -1365,12 +1365,13 @@ class TreeSet size_t itemIndex = ConstIteratorProxy::GetItemIndex(iter); if (node->IsLeaf()) { - node->Remove(*mNodeParams, itemIndex, itemReplacer1); + node->Remove(*mNodeParams, itemIndex, std::forward(itemRemover)); pvRebalance(node, node, true); } else { - node = pvRemoveInternal(node, itemIndex, itemReplacer1, itemReplacer2); + node = pvRemoveInternal(node, itemIndex, std::forward(itemRemover), + std::forward(itemReplacer)); itemIndex = 0; } --mCount; @@ -1380,16 +1381,16 @@ class TreeSet ConstIterator pvExtract(ConstIterator iter, Item* extItem) { - auto itemReplacer1 = [this, extItem] (Item& srcItem) - { ItemTraits::Relocate(&GetMemManager(), srcItem, extItem); }; - auto itemReplacer2 = [this, extItem] (Item& srcItem, Item& dstItem) + auto itemRemover = [this, extItem] (Item& item) + { ItemTraits::Relocate(&GetMemManager(), item, extItem); }; + auto itemReplacer = [this, extItem] (Item& srcItem, Item& dstItem) { ItemTraits::ReplaceRelocate(GetMemManager(), srcItem, dstItem, extItem); }; - return pvRemove(iter, itemReplacer1, itemReplacer2); + return pvRemove(iter, itemRemover, itemReplacer); } - template - Node* pvRemoveInternal(Node* node, size_t itemIndex, ItemReplacer1 itemReplacer1, - ItemReplacer2 itemReplacer2) + template + Node* pvRemoveInternal(Node* node, size_t itemIndex, ItemRemover&& itemRemover, + ItemReplacer&& itemReplacer) { Node* childNode = node->GetChild(itemIndex); while (!childNode->IsLeaf()) @@ -1400,18 +1401,18 @@ class TreeSet if (childNode == node) { Node* rightNode = node->GetChild(itemIndex + 1); - pvDestroyInternal(node, itemIndex, false, itemReplacer1); + pvDestroyInternal(node, itemIndex, false, std::forward(itemRemover)); resNode = rightNode; } else { size_t childItemIndex = childNode->GetCount() - 1; - auto itemRemover = [node, itemIndex, itemReplacer2] (Item& item) - { itemReplacer2(item, *node->GetItemPtr(itemIndex)); }; + auto itemRemover2 = [node, itemIndex, &itemReplacer] (Item& item) + { std::forward(itemReplacer)(item, *node->GetItemPtr(itemIndex)); }; if (childNode->IsLeaf()) - childNode->Remove(*mNodeParams, childItemIndex, itemRemover); + childNode->Remove(*mNodeParams, childItemIndex, itemRemover2); else - pvDestroyInternal(childNode, childItemIndex, true, itemRemover); + pvDestroyInternal(childNode, childItemIndex, true, itemRemover2); resNode = node->GetChild(itemIndex + 1); } while (!resNode->IsLeaf()) @@ -1422,7 +1423,7 @@ class TreeSet Node* pvRemoveRange(Node* node1, size_t itemIndex1, Node* node2, size_t itemIndex2) { - auto itemDestroyer = [this] (Item& item) + auto itemRemover = [this] (Item& item) { ItemTraits::Destroy(&GetMemManager(), item); }; size_t comIndex1 = itemIndex1; size_t comIndex2 = itemIndex2; @@ -1451,7 +1452,7 @@ class TreeSet { node1->Remove(*mNodeParams, itemIndex1, itemReplacer); for (size_t i = node1->GetCount(); i > itemIndex1; --i) - node1->Remove(*mNodeParams, i - 1, itemDestroyer); + node1->Remove(*mNodeParams, i - 1, itemRemover); pvToParent(node1, itemIndex1); } else @@ -1461,7 +1462,7 @@ class TreeSet while (node1 != comNode) { for (size_t i = node1->GetCount(); i > itemIndex1; --i) - pvDestroyInternal(node1, i - 1, true, itemDestroyer); + pvDestroyInternal(node1, i - 1, true, itemRemover); pvToParent(node1, itemIndex1); } ++comIndex1; @@ -1471,17 +1472,17 @@ class TreeSet if (node2->IsLeaf()) { for (size_t i = itemIndex2 + 1; i > 0; --i) - node2->Remove(*mNodeParams, i - 1, itemDestroyer); + node2->Remove(*mNodeParams, i - 1, itemRemover); pvToParent(node2, itemIndex2); } else { - pvDestroyInternal(node2, itemIndex2, false, itemDestroyer); + pvDestroyInternal(node2, itemIndex2, false, itemRemover); } while (node2 != comNode) { for (size_t i = itemIndex2; i > 0; --i) - pvDestroyInternal(node2, i - 1, false, itemDestroyer); + pvDestroyInternal(node2, i - 1, false, itemRemover); pvToParent(node2, itemIndex2); } } @@ -1490,7 +1491,7 @@ class TreeSet ++comIndex2; } for (size_t i = comIndex2; i > comIndex1; --i) - pvDestroyInternal(comNode, i - 1, false, itemDestroyer); + pvDestroyInternal(comNode, i - 1, false, itemRemover); Node* resNode = comNode->GetChild(comIndex1); while (!resNode->IsLeaf()) resNode = resNode->GetChild(0); @@ -1501,12 +1502,13 @@ class TreeSet } template - void pvDestroyInternal(Node* node, size_t itemIndex, bool destroyRight, ItemRemover itemRemover) + void pvDestroyInternal(Node* node, size_t itemIndex, bool destroyRight, + ItemRemover&& itemRemover) { MOMO_ASSERT(!node->IsLeaf()); Node* leftNode = node->GetChild(itemIndex); Node* rightNode = node->GetChild(itemIndex + 1); - node->Remove(*mNodeParams, itemIndex, itemRemover); + node->Remove(*mNodeParams, itemIndex, std::forward(itemRemover)); pvDestroy(destroyRight ? rightNode : leftNode); node->SetChild(itemIndex, destroyRight ? leftNode : rightNode); }