Skip to content

Commit

Permalink
Functors refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Sep 7, 2023
1 parent 918d0fc commit 6a80778
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 53 deletions.
34 changes: 17 additions & 17 deletions include/momo/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ class Array
return pvReallocate(capacityExp, internal::BoolConstant<canReallocate>());
}

template<typename ItemsRelocator>
void Reset(size_t capacity, size_t count, ItemsRelocator itemsRelocator)
template<typename ItemsCreator>
void Reset(size_t capacity, size_t count, ItemsCreator&& itemsCreator)
{
MOMO_ASSERT(count <= capacity);
pvCheckCapacity(capacity);
Expand All @@ -312,7 +312,7 @@ class Array
Item* items = pvAllocate(capacity);
try
{
itemsRelocator(items);
std::forward<ItemsCreator>(itemsCreator)(items);
}
catch (...)
{
Expand All @@ -326,7 +326,7 @@ class Array
}
else
{
pvReset(count, itemsRelocator);
pvReset(count, std::forward<ItemsCreator>(itemsCreator));
}
}

Expand Down Expand Up @@ -448,23 +448,23 @@ class Array
return false;
}

template<typename ItemsRelocator,
template<typename ItemsCreator,
bool hasInternalCapacity = (internalCapacity > 0)>
internal::EnableIf<hasInternalCapacity>
pvReset(size_t count, ItemsRelocator itemsRelocator)
pvReset(size_t count, ItemsCreator&& itemsCreator)
{
MOMO_ASSERT(!pvIsInternal());
size_t initCapacity = mCapacity;
itemsRelocator(&mInternalItems);
std::forward<ItemsCreator>(itemsCreator)(&mInternalItems);
MemManagerProxy::Deallocate(GetMemManager(), mItems, initCapacity * sizeof(Item));
mItems = &mInternalItems;
mCount = count;
}

template<typename ItemsRelocator,
template<typename ItemsCreator,
bool hasInternalCapacity = (internalCapacity > 0)>
internal::EnableIf<!hasInternalCapacity>
pvReset(size_t count, ItemsRelocator /*itemsRelocator*/) noexcept
pvReset(size_t count, ItemsCreator&& /*itemsCreator*/) noexcept
{
(void)count;
MOMO_ASSERT(count == 0);
Expand Down Expand Up @@ -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
Expand All @@ -692,7 +692,7 @@ class Array
throw;
}
};
mData.Reset(newCapacity, newCount, itemsRelocator);
mData.Reset(newCapacity, newCount, itemsCreator);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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>(itemCreator), newItems + initCount);
};
mData.Reset(newCapacity, newCount, itemsRelocator);
mData.Reset(newCapacity, newCount, itemsCreator);
}

void pvAddBackGrow(Item&& item)
Expand Down
5 changes: 3 additions & 2 deletions include/momo/HashSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,7 @@ class HashSet
}

template<typename ItemReplacer>
ConstIterator pvRemove(ConstIterator iter, ItemReplacer itemReplacer)
ConstIterator pvRemove(ConstIterator iter, ItemReplacer&& itemReplacer)
{
MOMO_CHECK(mBuckets != nullptr);
ConstPosition pos = iter;
Expand All @@ -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>(itemReplacer));
--mCount;
mCrew.IncVersion();
if (!ConstIteratorProxy::IsMovable(iter))
Expand Down
70 changes: 36 additions & 34 deletions include/momo/TreeSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1160,7 +1160,7 @@ class TreeSet
}

template<typename Predicate>
ConstIterator pvFindFirst(Predicate pred) const
ConstIterator pvFindFirst(const Predicate& pred) const
{
if (mRootNode == nullptr)
return ConstIterator();
Expand All @@ -1179,7 +1179,7 @@ class TreeSet
}

template<typename Predicate>
size_t pvFindFirst(Node* node, Predicate pred) const
size_t pvFindFirst(Node* node, const Predicate& pred) const
{
if (TreeTraits::useLinearSearch)
{
Expand Down Expand Up @@ -1355,22 +1355,23 @@ class TreeSet
pvUpdateParents(node); //?
}

template<typename ItemReplacer1, typename ItemReplacer2>
ConstIterator pvRemove(ConstIterator iter, ItemReplacer1 itemReplacer1,
ItemReplacer2 itemReplacer2)
template<typename ItemRemover, typename ItemReplacer>
ConstIterator pvRemove(ConstIterator iter, ItemRemover&& itemRemover,
ItemReplacer&& itemReplacer)
{
ConstIteratorProxy::Check(iter, mCrew.GetVersion(), false);
MOMO_CHECK(iter != GetEnd());
Node* node = ConstIteratorProxy::GetNode(iter);
size_t itemIndex = ConstIteratorProxy::GetItemIndex(iter);
if (node->IsLeaf())
{
node->Remove(*mNodeParams, itemIndex, itemReplacer1);
node->Remove(*mNodeParams, itemIndex, std::forward<ItemRemover>(itemRemover));
pvRebalance(node, node, true);
}
else
{
node = pvRemoveInternal(node, itemIndex, itemReplacer1, itemReplacer2);
node = pvRemoveInternal(node, itemIndex, std::forward<ItemRemover>(itemRemover),
std::forward<ItemReplacer>(itemReplacer));
itemIndex = 0;
}
--mCount;
Expand All @@ -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<typename ItemReplacer1, typename ItemReplacer2>
Node* pvRemoveInternal(Node* node, size_t itemIndex, ItemReplacer1 itemReplacer1,
ItemReplacer2 itemReplacer2)
template<typename ItemRemover, typename ItemReplacer>
Node* pvRemoveInternal(Node* node, size_t itemIndex, ItemRemover&& itemRemover,
ItemReplacer&& itemReplacer)
{
Node* childNode = node->GetChild(itemIndex);
while (!childNode->IsLeaf())
Expand All @@ -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>(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>(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())
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);
}
}
Expand All @@ -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);
Expand All @@ -1501,12 +1502,13 @@ class TreeSet
}

template<typename ItemRemover>
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>(itemRemover));
pvDestroy(destroyRight ? rightNode : leftNode);
node->SetChild(itemIndex, destroyRight ? leftNode : rightNode);
}
Expand Down

0 comments on commit 6a80778

Please sign in to comment.