Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Oct 1, 2023
1 parent d3993c3 commit 61b9793
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 49 deletions.
12 changes: 6 additions & 6 deletions include/momo/HashSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1040,12 +1040,12 @@ class HashSet
BucketIterator bucketIter = BucketIterator();
if (mCount != 0)
{
auto pred = [&key, &hashTraits] (const Item& item)
auto itemPred = [&key, &hashTraits] (const Item& item)
{ return hashTraits.IsEqual(key, ItemTraits::GetKey(item)); };
Buckets* buckets = mBuckets;
while (true)
{
bucketIter = pvFind(indexCode, *buckets, pred);
bucketIter = pvFind(indexCode, *buckets, itemPred);
if (bucketIter != BucketIterator() || areItemsNothrowRelocatable)
break;
buckets = buckets->GetNextBuckets();
Expand All @@ -1056,16 +1056,16 @@ class HashSet
return ConstPositionProxy(indexCode, bucketIter, mCrew.GetVersion());
}

template<typename Predicate>
template<typename ItemPredicate>
MOMO_FORCEINLINE static BucketIterator pvFind(size_t& indexCode, Buckets& buckets,
const Predicate& pred)
const ItemPredicate& itemPred)
{
size_t hashCode = indexCode;
BucketParams& bucketParams = buckets.GetBucketParams();
size_t bucketCount = buckets.GetCount();
size_t bucketIndex = Bucket::GetStartBucketIndex(hashCode, bucketCount);
Bucket* bucket = &buckets[bucketIndex];
BucketIterator bucketIter = bucket->template Find<true>(bucketParams, pred, hashCode);
BucketIterator bucketIter = bucket->template Find<true>(bucketParams, itemPred, hashCode);
if (bucketIter != BucketIterator())
{
indexCode = bucketIndex;
Expand All @@ -1076,7 +1076,7 @@ class HashSet
{
bucketIndex = Bucket::GetNextBucketIndex(bucketIndex, hashCode, bucketCount, probe);
bucket = &buckets[bucketIndex];
bucketIter = bucket->template Find<false>(bucketParams, pred, hashCode);
bucketIter = bucket->template Find<false>(bucketParams, itemPred, hashCode);
if (bucketIter != BucketIterator())
{
indexCode = bucketIndex;
Expand Down
24 changes: 12 additions & 12 deletions include/momo/TreeSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -1145,30 +1145,30 @@ class TreeSet
ConstIterator pvGetLowerBound(const KeyArg& key) const
{
const TreeTraits& treeTraits = GetTreeTraits();
auto pred = [&treeTraits, &key] (const Item& item)
auto itemPred = [&treeTraits, &key] (const Item& item)
{ return !treeTraits.IsLess(ItemTraits::GetKey(item), key); };
return pvFindFirst(pred);
return pvFindFirst(itemPred);
}

template<typename KeyArg>
ConstIterator pvGetUpperBound(const KeyArg& key) const
{
const TreeTraits& treeTraits = GetTreeTraits();
auto pred = [&treeTraits, &key] (const Item& item)
auto itemPred = [&treeTraits, &key] (const Item& item)
{ return treeTraits.IsLess(key, ItemTraits::GetKey(item)); };
return pvFindFirst(pred);
return pvFindFirst(itemPred);
}

template<typename Predicate>
ConstIterator pvFindFirst(const Predicate& pred) const
template<typename ItemPredicate>
ConstIterator pvFindFirst(const ItemPredicate& itemPred) const
{
if (mRootNode == nullptr)
return ConstIterator();
ConstIterator iter = GetEnd();
Node* node = mRootNode;
while (true)
{
size_t index = pvFindFirst(node, pred);
size_t index = pvFindFirst(node, itemPred);
if (index < node->GetCount())
iter = pvMakeIterator(node, index, false);
if (node->IsLeaf())
Expand All @@ -1178,16 +1178,16 @@ class TreeSet
return iter;
}

template<typename Predicate>
size_t pvFindFirst(Node* node, const Predicate& pred) const
template<typename ItemPredicate>
size_t pvFindFirst(Node* node, const ItemPredicate& itemPred) const
{
if (TreeTraits::useLinearSearch)
{
size_t itemCount = node->GetCount();
if (itemCount == 0 || !pred(*node->GetItemPtr(itemCount - 1)))
if (itemCount == 0 || !itemPred(*node->GetItemPtr(itemCount - 1)))
return itemCount;
size_t index = 0;
while (!pred(*node->GetItemPtr(index)))
while (!itemPred(*node->GetItemPtr(index)))
++index;
return index;
}
Expand All @@ -1198,7 +1198,7 @@ class TreeSet
while (leftIndex < rightIndex)
{
size_t middleIndex = (leftIndex + rightIndex) / 2;
if (pred(*node->GetItemPtr(middleIndex)))
if (itemPred(*node->GetItemPtr(middleIndex)))
rightIndex = middleIndex;
else
leftIndex = middleIndex + 1;
Expand Down
7 changes: 4 additions & 3 deletions include/momo/details/HashBucketLim4.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,13 @@ namespace internal
return Bounds(items, data.count);
}

template<bool first, typename Predicate>
MOMO_FORCEINLINE Iterator Find(Params& params, const Predicate& pred, size_t /*hashCode*/)
template<bool first, typename ItemPredicate>
MOMO_FORCEINLINE Iterator Find(Params& params,
const ItemPredicate& itemPred, size_t /*hashCode*/)
{
for (Item& item : GetBounds(params))
{
if (pred(item))
if (itemPred(item))
return std::addressof(item);
}
return nullptr;
Expand Down
14 changes: 8 additions & 6 deletions include/momo/details/HashBucketLimP.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,17 @@ namespace internal
return Bounds(pvGetItems(), pvGetCount());
}

template<bool first, typename Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t /*hashCode*/)
template<bool first, typename ItemPredicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/,
const ItemPredicate& itemPred, size_t /*hashCode*/)
{
if (pvIsEmpty())
return nullptr;
size_t count = pvGetCount();
Item* items = pvGetItems();
for (size_t i = 0; i < count; ++i)
{
if (pred(items[i]))
if (itemPred(items[i]))
return items + i;
}
return nullptr;
Expand Down Expand Up @@ -406,14 +407,15 @@ namespace internal
return pvGetBounds();
}

template<bool first, typename Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t /*hashCode*/)
template<bool first, typename ItemPredicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/,
const ItemPredicate& itemPred, size_t /*hashCode*/)
{
if (pvIsEmpty())
return nullptr;
for (Item& item : pvGetBounds())
{
if (pred(item))
if (itemPred(item))
return std::addressof(item);
}
return nullptr;
Expand Down
7 changes: 4 additions & 3 deletions include/momo/details/HashBucketLimP1.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,15 @@ namespace internal
return Bounds(pvGetItems(), pvGetCount());
}

template<bool first, typename Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t /*hashCode*/)
template<bool first, typename ItemPredicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/,
const ItemPredicate& itemPred, size_t /*hashCode*/)
{
size_t count = pvGetCount();
Item* items = pvGetItems();
for (size_t i = 0; i < count; ++i)
{
if (pred(items[i]))
if (itemPred(items[i]))
return items + i;
}
return nullptr;
Expand Down
7 changes: 4 additions & 3 deletions include/momo/details/HashBucketLimP4.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,17 @@ namespace internal
return Bounds(mPtrState.GetPointer(), pvGetCount());
}

template<bool first, typename Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t hashCode)
template<bool first, typename ItemPredicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/,
const ItemPredicate& itemPred, size_t hashCode)
{
uint8_t shortHash = pvCalcShortHash(hashCode);
for (size_t i = 0; i < maxCount; ++i)
{
if (mShortHashes[i] == shortHash)
{
Item* items = mPtrState.GetPointer();
if (pred(items[i]))
if (itemPred(items[i]))
return items + i;
}
}
Expand Down
7 changes: 4 additions & 3 deletions include/momo/details/HashBucketOne.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ namespace internal
return IsFull() ? Bounds(&mItemBuffer, 1) : Bounds();
}

template<bool first, typename Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t hashCode)
template<bool first, typename ItemPredicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/,
const ItemPredicate& itemPred, size_t hashCode)
{
if (mHashState != pvGetHashState(hashCode))
return nullptr;
return pred(*&mItemBuffer) ? &mItemBuffer : nullptr;
return itemPred(*&mItemBuffer) ? &mItemBuffer : nullptr;
}

bool IsFull() const noexcept
Expand Down
7 changes: 4 additions & 3 deletions include/momo/details/HashBucketOpen2N2.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ namespace internal
return Bounds(Iterator(&mItems + maxCount), pvGetCount());
}

template<bool first, typename Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t hashCode)
template<bool first, typename ItemPredicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/,
const ItemPredicate& itemPred, size_t hashCode)
{
ShortHash shortHash = pvCalcShortHash(hashCode);
for (size_t i = 0; i < maxCount; ++i)
{
if (mHashData.shortHashes[i] == shortHash && pred((&mItems)[i]))
if (mHashData.shortHashes[i] == shortHash && itemPred((&mItems)[i]))
return Iterator(&mItems + i + 1);
}
return Iterator();
Expand Down
9 changes: 5 additions & 4 deletions include/momo/details/HashBucketOpen8.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ namespace internal

BucketOpen8& operator=(const BucketOpen8&) = delete;

template<bool first, typename Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t hashCode)
template<bool first, typename ItemPredicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/,
const ItemPredicate& itemPred, size_t hashCode)
{
#ifdef MOMO_PREFETCH
if (first)
Expand All @@ -74,7 +75,7 @@ namespace internal
{
size_t index = pvCountTrailingZeros15(static_cast<uint32_t>(mask));
Item* itemPtr = BucketOpenN1::ptGetItemPtr(index);
if (pred(*itemPtr))
if (itemPred(*itemPtr))
return itemPtr;
}
#else
Expand All @@ -85,7 +86,7 @@ namespace internal
{
size_t index = static_cast<size_t>(MOMO_CTZ64(mask)) >> 3;
Item* itemPtr = BucketOpenN1::ptGetItemPtr(index);
if (pred(*itemPtr))
if (itemPred(*itemPtr))
return itemPtr;
}
#endif
Expand Down
7 changes: 4 additions & 3 deletions include/momo/details/HashBucketOpenN1.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ namespace internal
return Bounds(pvMakeIterator(ptGetItemPtr(0)), pvGetCount());
}

template<bool first, typename Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t hashCode)
template<bool first, typename ItemPredicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/,
const ItemPredicate& itemPred, size_t hashCode)
{
uint8_t shortHash = ptCalcShortHash(hashCode);
const uint8_t* thisShortHashes = pvGetShortHashes();
for (size_t i = 0; i < maxCount; ++i)
{
if (thisShortHashes[i] == shortHash && pred(*&mItems[i]))
if (thisShortHashes[i] == shortHash && itemPred(*&mItems[i]))
return pvMakeIterator(&mItems[i]);
}
return Iterator();
Expand Down
7 changes: 4 additions & 3 deletions include/momo/details/HashBucketUnlimP.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ namespace internal
return mArrayBucket.GetBounds();
}

template<bool first, typename Predicate>
MOMO_FORCEINLINE Iterator Find(Params& params, const Predicate& pred, size_t /*hashCode*/)
template<bool first, typename ItemPredicate>
MOMO_FORCEINLINE Iterator Find(Params& params,
const ItemPredicate& itemPred, size_t /*hashCode*/)
{
for (Item& item : GetBounds(params))
{
if (pred(item))
if (itemPred(item))
return std::addressof(item);
}
return nullptr;
Expand Down

0 comments on commit 61b9793

Please sign in to comment.