From 61b97933b3a67f101a21a8b25162b2633e9b8226 Mon Sep 17 00:00:00 2001 From: morzhovets Date: Sun, 1 Oct 2023 22:45:10 +0400 Subject: [PATCH] Refactoring --- include/momo/HashSet.h | 12 ++++++------ include/momo/TreeSet.h | 24 ++++++++++++------------ include/momo/details/HashBucketLim4.h | 7 ++++--- include/momo/details/HashBucketLimP.h | 14 ++++++++------ include/momo/details/HashBucketLimP1.h | 7 ++++--- include/momo/details/HashBucketLimP4.h | 7 ++++--- include/momo/details/HashBucketOne.h | 7 ++++--- include/momo/details/HashBucketOpen2N2.h | 7 ++++--- include/momo/details/HashBucketOpen8.h | 9 +++++---- include/momo/details/HashBucketOpenN1.h | 7 ++++--- include/momo/details/HashBucketUnlimP.h | 7 ++++--- 11 files changed, 59 insertions(+), 49 deletions(-) diff --git a/include/momo/HashSet.h b/include/momo/HashSet.h index adfabf2ad..1ff42f7f0 100644 --- a/include/momo/HashSet.h +++ b/include/momo/HashSet.h @@ -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(); @@ -1056,16 +1056,16 @@ class HashSet return ConstPositionProxy(indexCode, bucketIter, mCrew.GetVersion()); } - template + template 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(bucketParams, pred, hashCode); + BucketIterator bucketIter = bucket->template Find(bucketParams, itemPred, hashCode); if (bucketIter != BucketIterator()) { indexCode = bucketIndex; @@ -1076,7 +1076,7 @@ class HashSet { bucketIndex = Bucket::GetNextBucketIndex(bucketIndex, hashCode, bucketCount, probe); bucket = &buckets[bucketIndex]; - bucketIter = bucket->template Find(bucketParams, pred, hashCode); + bucketIter = bucket->template Find(bucketParams, itemPred, hashCode); if (bucketIter != BucketIterator()) { indexCode = bucketIndex; diff --git a/include/momo/TreeSet.h b/include/momo/TreeSet.h index 75cff47d1..9530e5827 100644 --- a/include/momo/TreeSet.h +++ b/include/momo/TreeSet.h @@ -1145,22 +1145,22 @@ 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 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 - ConstIterator pvFindFirst(const Predicate& pred) const + template + ConstIterator pvFindFirst(const ItemPredicate& itemPred) const { if (mRootNode == nullptr) return ConstIterator(); @@ -1168,7 +1168,7 @@ class TreeSet 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()) @@ -1178,16 +1178,16 @@ class TreeSet return iter; } - template - size_t pvFindFirst(Node* node, const Predicate& pred) const + template + 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; } @@ -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; diff --git a/include/momo/details/HashBucketLim4.h b/include/momo/details/HashBucketLim4.h index 1daf5e532..c52ea18af 100644 --- a/include/momo/details/HashBucketLim4.h +++ b/include/momo/details/HashBucketLim4.h @@ -137,12 +137,13 @@ namespace internal return Bounds(items, data.count); } - template - MOMO_FORCEINLINE Iterator Find(Params& params, const Predicate& pred, size_t /*hashCode*/) + template + 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; diff --git a/include/momo/details/HashBucketLimP.h b/include/momo/details/HashBucketLimP.h index b388c57df..afb45f28e 100644 --- a/include/momo/details/HashBucketLimP.h +++ b/include/momo/details/HashBucketLimP.h @@ -127,8 +127,9 @@ namespace internal return Bounds(pvGetItems(), pvGetCount()); } - template - MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t /*hashCode*/) + template + MOMO_FORCEINLINE Iterator Find(Params& /*params*/, + const ItemPredicate& itemPred, size_t /*hashCode*/) { if (pvIsEmpty()) return nullptr; @@ -136,7 +137,7 @@ namespace internal Item* items = pvGetItems(); for (size_t i = 0; i < count; ++i) { - if (pred(items[i])) + if (itemPred(items[i])) return items + i; } return nullptr; @@ -406,14 +407,15 @@ namespace internal return pvGetBounds(); } - template - MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t /*hashCode*/) + template + 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; diff --git a/include/momo/details/HashBucketLimP1.h b/include/momo/details/HashBucketLimP1.h index 44e0c0573..52f7653e3 100644 --- a/include/momo/details/HashBucketLimP1.h +++ b/include/momo/details/HashBucketLimP1.h @@ -123,14 +123,15 @@ namespace internal return Bounds(pvGetItems(), pvGetCount()); } - template - MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t /*hashCode*/) + template + 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; diff --git a/include/momo/details/HashBucketLimP4.h b/include/momo/details/HashBucketLimP4.h index fa813b45a..45d2bc6a5 100644 --- a/include/momo/details/HashBucketLimP4.h +++ b/include/momo/details/HashBucketLimP4.h @@ -270,8 +270,9 @@ namespace internal return Bounds(mPtrState.GetPointer(), pvGetCount()); } - template - MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t hashCode) + template + 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) @@ -279,7 +280,7 @@ namespace internal if (mShortHashes[i] == shortHash) { Item* items = mPtrState.GetPointer(); - if (pred(items[i])) + if (itemPred(items[i])) return items + i; } } diff --git a/include/momo/details/HashBucketOne.h b/include/momo/details/HashBucketOne.h index fe6550e98..740366cfb 100644 --- a/include/momo/details/HashBucketOne.h +++ b/include/momo/details/HashBucketOne.h @@ -72,12 +72,13 @@ namespace internal return IsFull() ? Bounds(&mItemBuffer, 1) : Bounds(); } - template - MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t hashCode) + template + 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 diff --git a/include/momo/details/HashBucketOpen2N2.h b/include/momo/details/HashBucketOpen2N2.h index 6d2367c36..c6709b0d1 100644 --- a/include/momo/details/HashBucketOpen2N2.h +++ b/include/momo/details/HashBucketOpen2N2.h @@ -96,13 +96,14 @@ namespace internal return Bounds(Iterator(&mItems + maxCount), pvGetCount()); } - template - MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t hashCode) + template + 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(); diff --git a/include/momo/details/HashBucketOpen8.h b/include/momo/details/HashBucketOpen8.h index 91d9b2898..49b73c06f 100644 --- a/include/momo/details/HashBucketOpen8.h +++ b/include/momo/details/HashBucketOpen8.h @@ -56,8 +56,9 @@ namespace internal BucketOpen8& operator=(const BucketOpen8&) = delete; - template - MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t hashCode) + template + MOMO_FORCEINLINE Iterator Find(Params& /*params*/, + const ItemPredicate& itemPred, size_t hashCode) { #ifdef MOMO_PREFETCH if (first) @@ -74,7 +75,7 @@ namespace internal { size_t index = pvCountTrailingZeros15(static_cast(mask)); Item* itemPtr = BucketOpenN1::ptGetItemPtr(index); - if (pred(*itemPtr)) + if (itemPred(*itemPtr)) return itemPtr; } #else @@ -85,7 +86,7 @@ namespace internal { size_t index = static_cast(MOMO_CTZ64(mask)) >> 3; Item* itemPtr = BucketOpenN1::ptGetItemPtr(index); - if (pred(*itemPtr)) + if (itemPred(*itemPtr)) return itemPtr; } #endif diff --git a/include/momo/details/HashBucketOpenN1.h b/include/momo/details/HashBucketOpenN1.h index feb24b902..659e1b18a 100644 --- a/include/momo/details/HashBucketOpenN1.h +++ b/include/momo/details/HashBucketOpenN1.h @@ -71,14 +71,15 @@ namespace internal return Bounds(pvMakeIterator(ptGetItemPtr(0)), pvGetCount()); } - template - MOMO_FORCEINLINE Iterator Find(Params& /*params*/, const Predicate& pred, size_t hashCode) + template + 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(); diff --git a/include/momo/details/HashBucketUnlimP.h b/include/momo/details/HashBucketUnlimP.h index 99de789de..4c4568ed8 100644 --- a/include/momo/details/HashBucketUnlimP.h +++ b/include/momo/details/HashBucketUnlimP.h @@ -98,12 +98,13 @@ namespace internal return mArrayBucket.GetBounds(); } - template - MOMO_FORCEINLINE Iterator Find(Params& params, const Predicate& pred, size_t /*hashCode*/) + template + 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;