Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Jan 2, 2025
1 parent 23227e0 commit 61187cc
Show file tree
Hide file tree
Showing 15 changed files with 171 additions and 171 deletions.
100 changes: 50 additions & 50 deletions include/momo/HashSorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,27 @@ class HashSorter

private:
template<internal::conceptRandomIterator17 Iterator,
internal::conceptHashFunc<std::iter_value_t<Iterator>> HashFunc>
class IterHashFunc
internal::conceptHasher<std::iter_value_t<Iterator>> Hasher>
class IterHasher
{
public:
explicit IterHashFunc(FastCopyableFunctor<HashFunc> hashFunc) noexcept
: mHashFunc(hashFunc)
explicit IterHasher(FastCopyableFunctor<Hasher> hasher) noexcept
: mHasher(hasher)
{
}

auto operator()(Iterator iter) const
{
return mHashFunc(*iter);
return mHasher(*iter);
}

auto operator()(std::reverse_iterator<Iterator> iter) const
{
return mHashFunc(*iter);
return mHasher(*iter);
}

private:
FastCopyableFunctor<HashFunc> mHashFunc;
FastCopyableFunctor<Hasher> mHasher;
};

template<internal::conceptRandomIterator17 Iterator,
Expand Down Expand Up @@ -116,14 +116,14 @@ class HashSorter
public:
template<internal::conceptRandomIterator17 Iterator,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptHashFunc<Item> HashFunc = HashCoder<Item>,
internal::conceptHasher<Item> Hasher = HashCoder<Item>,
internal::conceptEqualFunc<Item> EqualFunc = std::equal_to<Item>,
internal::conceptConstFunctor<void, Iterator, Iterator> IterSwapper = Swapper<Iterator>>
static void Sort(Iterator begin, size_t count, HashFunc hashFunc = HashFunc(),
static void Sort(Iterator begin, size_t count, Hasher hasher = Hasher(),
EqualFunc equalFunc = EqualFunc(), IterSwapper iterSwapper = IterSwapper())
{
IterHashFunc<Iterator, HashFunc> iterHashFunc((FastCopyableFunctor<HashFunc>(hashFunc)));
pvSort(begin, count, FastCopyableFunctor(iterHashFunc),
IterHasher<Iterator, Hasher> iterHasher((FastCopyableFunctor<Hasher>(hasher)));
pvSort(begin, count, FastCopyableFunctor(iterHasher),
FastCopyableFunctor<EqualFunc>(equalFunc),
FastCopyableFunctor<IterSwapper>(iterSwapper));
}
Expand All @@ -149,13 +149,13 @@ class HashSorter

template<internal::conceptRandomIterator17 Iterator,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptHashFunc<Item> HashFunc = HashCoder<Item>,
internal::conceptHasher<Item> Hasher = HashCoder<Item>,
internal::conceptEqualFunc<Item> EqualFunc = std::equal_to<Item>>
static bool IsSorted(Iterator begin, size_t count, HashFunc hashFunc = HashFunc(),
static bool IsSorted(Iterator begin, size_t count, Hasher hasher = Hasher(),
EqualFunc equalFunc = EqualFunc())
{
IterHashFunc<Iterator, HashFunc> iterHashFunc((FastCopyableFunctor<HashFunc>(hashFunc)));
return pvIsSorted(begin, count, FastCopyableFunctor(iterHashFunc),
IterHasher<Iterator, Hasher> iterHasher((FastCopyableFunctor<Hasher>(hasher)));
return pvIsSorted(begin, count, FastCopyableFunctor(iterHasher),
FastCopyableFunctor<EqualFunc>(equalFunc));
}

Expand All @@ -174,14 +174,14 @@ class HashSorter

template<internal::conceptRandomIterator17 Iterator, typename ItemArg,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptHashFunc<Item> HashFunc = HashCoder<Item>,
internal::conceptHasher<Item> Hasher = HashCoder<Item>,
internal::conceptEqualFunc<Item, Item, ItemArg> EqualFunc = std::equal_to<>>
static FindResult<Iterator> Find(Iterator begin, size_t count,
const ItemArg& itemArg, HashCode argHashCode,
HashFunc hashFunc = HashFunc(), EqualFunc equalFunc = EqualFunc())
Hasher hasher = Hasher(), EqualFunc equalFunc = EqualFunc())
{
IterHashFunc<Iterator, HashFunc> iterHashFunc((FastCopyableFunctor<HashFunc>(hashFunc)));
return pvFind(begin, count, itemArg, argHashCode, FastCopyableFunctor(iterHashFunc),
IterHasher<Iterator, Hasher> iterHasher((FastCopyableFunctor<Hasher>(hasher)));
return pvFind(begin, count, itemArg, argHashCode, FastCopyableFunctor(iterHasher),
FastCopyableFunctor<EqualFunc>(equalFunc));
}

Expand All @@ -200,15 +200,15 @@ class HashSorter

template<internal::conceptRandomIterator17 Iterator, typename ItemArg,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptHashFunc<Item> HashFunc = HashCoder<Item>,
internal::conceptHasher<Item> Hasher = HashCoder<Item>,
internal::conceptEqualFunc<Item, Item, ItemArg> EqualFunc = std::equal_to<>>
static Bounds<Iterator> GetBounds(Iterator begin, size_t count,
const ItemArg& itemArg, HashCode argHashCode,
HashFunc hashFunc = HashFunc(), EqualFunc equalFunc = EqualFunc())
Hasher hasher = Hasher(), EqualFunc equalFunc = EqualFunc())
{
IterHashFunc<Iterator, HashFunc> iterHashFunc((FastCopyableFunctor<HashFunc>(hashFunc)));
IterHasher<Iterator, Hasher> iterHasher((FastCopyableFunctor<Hasher>(hasher)));
return pvGetBounds(begin, count, itemArg, argHashCode,
FastCopyableFunctor(iterHashFunc), FastCopyableFunctor<EqualFunc>(equalFunc));
FastCopyableFunctor(iterHasher), FastCopyableFunctor<EqualFunc>(equalFunc));
}

template<internal::conceptRandomIterator17 Iterator, typename ItemArg,
Expand All @@ -227,18 +227,18 @@ class HashSorter
private:
template<internal::conceptRandomIterator17 Iterator,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptConstFunctor<size_t, Iterator> IterHashFunc,
internal::conceptConstFunctor<size_t, Iterator> IterHasher,
internal::conceptEqualFunc<Item> EqualFunc,
internal::conceptConstFunctor<void, Iterator, Iterator> IterSwapper>
static void pvSort(Iterator begin, size_t count, FastCopyableFunctor<IterHashFunc> iterHashFunc,
static void pvSort(Iterator begin, size_t count, FastCopyableFunctor<IterHasher> iterHasher,
FastCopyableFunctor<EqualFunc> equalFunc, FastCopyableFunctor<IterSwapper> iterSwapper)
{
auto itemsGrouper = [equalFunc, iterSwapper] (Iterator begin, size_t count)
{
if (count > 2)
pvGroup(begin, count, equalFunc, iterSwapper);
};
internal::RadixSorter<>::Sort(begin, count, iterHashFunc, iterSwapper,
internal::RadixSorter<>::Sort(begin, count, iterHasher, iterSwapper,
FastCopyableFunctor(itemsGrouper));
}

Expand Down Expand Up @@ -266,16 +266,16 @@ class HashSorter

template<internal::conceptRandomIterator17 Iterator,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptConstFunctor<size_t, Iterator> IterHashFunc,
internal::conceptConstFunctor<size_t, Iterator> IterHasher,
internal::conceptEqualFunc<Item> EqualFunc>
static bool pvIsSorted(Iterator begin, size_t count,
FastCopyableFunctor<IterHashFunc> iterHashFunc, FastCopyableFunctor<EqualFunc> equalFunc)
FastCopyableFunctor<IterHasher> iterHasher, FastCopyableFunctor<EqualFunc> equalFunc)
{
size_t prevIndex = 0;
HashCode prevHashCode = iterHashFunc(begin);
HashCode prevHashCode = iterHasher(begin);
for (size_t i = 1; i < count; ++i)
{
HashCode hashCode = iterHashFunc(SMath::Next(begin, i));
HashCode hashCode = iterHasher(SMath::Next(begin, i));
if (hashCode < prevHashCode)
return false;
if (hashCode != prevHashCode)
Expand Down Expand Up @@ -309,34 +309,34 @@ class HashSorter

template<internal::conceptRandomIterator17 Iterator, typename ItemArg,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptConstFunctor<size_t, Iterator> IterHashFunc,
internal::conceptConstFunctor<size_t, Iterator> IterHasher,
internal::conceptEqualFunc<Item, Item, ItemArg> EqualFunc>
static FindResult<Iterator> pvFind(Iterator begin, size_t count, const ItemArg& itemArg,
HashCode argHashCode, FastCopyableFunctor<IterHashFunc> iterHashFunc,
HashCode argHashCode, FastCopyableFunctor<IterHasher> iterHasher,
FastCopyableFunctor<EqualFunc> equalFunc)
{
auto res = pvFindHashCode(begin, count, argHashCode, iterHashFunc);
auto res = pvFindHashCode(begin, count, argHashCode, iterHasher);
if (!res.found)
return res;
if (equalFunc(*res.iterator, itemArg))
return res;
auto revRes = pvFindNext(std::reverse_iterator<Iterator>(res.iterator + 1),
SMath::Dist(begin, res.iterator + 1), itemArg, argHashCode, iterHashFunc, equalFunc);
SMath::Dist(begin, res.iterator + 1), itemArg, argHashCode, iterHasher, equalFunc);
if (revRes.found)
return { revRes.iterator.base() - 1, true };
return pvFindNext(res.iterator, count - SMath::Dist(begin, res.iterator),
itemArg, argHashCode, iterHashFunc, equalFunc);
itemArg, argHashCode, iterHasher, equalFunc);
}

template<internal::conceptRandomIterator17 Iterator, typename ItemArg,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptConstFunctor<size_t, Iterator> IterHashFunc,
internal::conceptConstFunctor<size_t, Iterator> IterHasher,
internal::conceptEqualFunc<Item, Item, ItemArg> EqualFunc>
static Bounds<Iterator> pvGetBounds(Iterator begin, size_t count, const ItemArg& itemArg,
HashCode argHashCode, FastCopyableFunctor<IterHashFunc> iterHashFunc,
HashCode argHashCode, FastCopyableFunctor<IterHasher> iterHasher,
FastCopyableFunctor<EqualFunc> equalFunc)
{
auto res = pvFindHashCode(begin, count, argHashCode, iterHashFunc);
auto res = pvFindHashCode(begin, count, argHashCode, iterHasher);
if (!res.found)
return Bounds<Iterator>(res.iterator, res.iterator);
if (equalFunc(*res.iterator, itemArg))
Expand All @@ -347,15 +347,15 @@ class HashSorter
pvFindOther(res.iterator, count - SMath::Dist(begin, res.iterator), equalFunc));
}
auto revRes = pvFindNext(std::reverse_iterator<Iterator>(res.iterator + 1),
SMath::Dist(begin, res.iterator + 1), itemArg, argHashCode, iterHashFunc, equalFunc);
SMath::Dist(begin, res.iterator + 1), itemArg, argHashCode, iterHasher, equalFunc);
if (revRes.found)
{
Iterator resBegin = pvFindOther(revRes.iterator,
SMath::Dist(begin, revRes.iterator.base()), equalFunc).base();
return Bounds<Iterator>(resBegin, revRes.iterator.base());
}
res = pvFindNext(res.iterator, count - SMath::Dist(begin, res.iterator),
itemArg, argHashCode, iterHashFunc, equalFunc);
itemArg, argHashCode, iterHasher, equalFunc);
if (!res.found)
return Bounds<Iterator>(res.iterator, res.iterator);
return Bounds<Iterator>(res.iterator,
Expand All @@ -364,17 +364,17 @@ class HashSorter

template<internal::conceptRandomIterator17 Iterator, typename ItemArg,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptConstFunctor<size_t, Iterator> IterHashFunc,
internal::conceptConstFunctor<size_t, Iterator> IterHasher,
internal::conceptEqualFunc<Item, Item, ItemArg> EqualFunc>
static FindResult<Iterator> pvFindNext(Iterator begin, size_t count, const ItemArg& itemArg,
HashCode argHashCode, FastCopyableFunctor<IterHashFunc> iterHashFunc,
HashCode argHashCode, FastCopyableFunctor<IterHasher> iterHasher,
FastCopyableFunctor<EqualFunc> equalFunc)
{
Iterator iter = begin;
while (true)
{
iter = pvFindOther(iter, count - SMath::Dist(begin, iter), equalFunc);
if (iter == SMath::Next(begin, count) || iterHashFunc(iter) != argHashCode)
if (iter == SMath::Next(begin, count) || iterHasher(iter) != argHashCode)
break;
if (equalFunc(*iter, itemArg))
return { iter, true };
Expand All @@ -398,19 +398,19 @@ class HashSorter
}

template<internal::conceptRandomIterator17 Iterator,
internal::conceptConstFunctor<size_t, Iterator> IterHashFunc>
internal::conceptConstFunctor<size_t, Iterator> IterHasher>
static FindResult<Iterator> pvFindHashCode(Iterator begin, size_t count,
HashCode argHashCode, FastCopyableFunctor<IterHashFunc> iterHashFunc)
HashCode argHashCode, FastCopyableFunctor<IterHasher> iterHasher)
{
auto iterThreeComp = [argHashCode, iterHashFunc] (Iterator iter)
{ return iterHashFunc(iter) <=> argHashCode; };
auto iterThreeComp = [argHashCode, iterHasher] (Iterator iter)
{ return iterHasher(iter) <=> argHashCode; };
size_t leftIndex = 0;
size_t rightIndex = count;
size_t middleIndex = pvMultShift(argHashCode, count);
size_t step = pvGetStepCount(count);
while (true)
{
HashCode middleHashCode = iterHashFunc(SMath::Next(begin, middleIndex));
HashCode middleHashCode = iterHasher(SMath::Next(begin, middleIndex));
if (middleHashCode < argHashCode)
{
leftIndex = middleIndex + 1;
Expand All @@ -429,8 +429,8 @@ class HashSorter
if (step == 0)
{
typedef std::reverse_iterator<Iterator> ReverseIterator;
auto revIterThreeComp = [argHashCode, iterHashFunc] (ReverseIterator iter)
{ return argHashCode <=> iterHashFunc(iter); };
auto revIterThreeComp = [argHashCode, iterHasher] (ReverseIterator iter)
{ return argHashCode <=> iterHasher(iter); };
auto res = pvExponentialSearch(ReverseIterator(SMath::Next(begin, rightIndex)),
rightIndex - leftIndex, FastCopyableFunctor(revIterThreeComp));
return { res.iterator.base() - (res.found ? 1 : 0), res.found };
Expand Down
22 changes: 11 additions & 11 deletions include/momo/HashTraits.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,36 +135,36 @@ template<conceptObject TKey>
using HashTraitsOpen = HashTraits<TKey, HashBucketOpenDefault>;

template<conceptObject TKey,
internal::conceptCopyableHashFunc<TKey> THashFunc = HashCoder<TKey>,
internal::conceptCopyableHasher<TKey> THasher = HashCoder<TKey>,
internal::conceptCopyableEqualFunc<TKey> TEqualFunc = std::equal_to<TKey>,
typename THashBucket = HashBucketDefault>
class HashTraitsStd
{
public:
typedef TKey Key;
typedef THashFunc HashFunc;
typedef THasher Hasher;
typedef TEqualFunc EqualFunc;
typedef THashBucket HashBucket;

static const bool isFastNothrowHashable = IsFastNothrowHashable<Key>::value &&
(std::is_same_v<HashFunc, HashCoder<Key>> || std::is_same_v<HashFunc, std::hash<Key>>);
(std::is_same_v<Hasher, HashCoder<Key>> || std::is_same_v<Hasher, std::hash<Key>>);

template<typename ItemTraits>
using Bucket = typename HashBucket::template Bucket<ItemTraits, !isFastNothrowHashable>;

template<typename KeyArg>
using IsValidKeyArg = std::bool_constant<
internal::conceptTransparent<HashFunc> && internal::conceptTransparent<EqualFunc>>;
internal::conceptTransparent<Hasher> && internal::conceptTransparent<EqualFunc>>;

private:
static const bool staticIsEqual = std::is_empty_v<EqualFunc> &&
std::is_trivially_default_constructible_v<EqualFunc>;

public:
explicit HashTraitsStd(size_t startBucketCount = size_t{1} << HashBucket::logStartBucketCount,
const HashFunc& hashFunc = HashFunc(),
const Hasher& hasher = Hasher(),
const EqualFunc& equalFunc = EqualFunc())
: mHashFunc(hashFunc),
: mHasher(hasher),
mEqualFunc(equalFunc),
mMaxLoadFactor(0.0)
{
Expand All @@ -173,7 +173,7 @@ class HashTraitsStd
}

HashTraitsStd(const HashTraitsStd& hashTraits, float maxLoadFactor)
: mHashFunc(hashTraits.mHashFunc),
: mHasher(hashTraits.mHasher),
mEqualFunc(hashTraits.mEqualFunc),
mLogStartBucketCount(hashTraits.mLogStartBucketCount),
mMaxLoadFactor(maxLoadFactor)
Expand Down Expand Up @@ -202,7 +202,7 @@ class HashTraitsStd
template<typename KeyArg>
size_t GetHashCode(const KeyArg& key) const
{
return mHashFunc(key);
return mHasher(key);
}

template<typename KeyArg1, typename KeyArg2>
Expand All @@ -219,9 +219,9 @@ class HashTraitsStd
return mEqualFunc(key1, key2);
}

const HashFunc& GetHashFunc() const noexcept
const Hasher& GetHasher() const noexcept
{
return mHashFunc;
return mHasher;
}

const EqualFunc& GetEqualFunc() const noexcept
Expand All @@ -240,7 +240,7 @@ class HashTraitsStd
}

private:
HashFunc mHashFunc;
Hasher mHasher;
EqualFunc mEqualFunc;
uint8_t mLogStartBucketCount;
float mMaxLoadFactor;
Expand Down
10 changes: 5 additions & 5 deletions include/momo/KeyUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace internal
template<typename LessComparer, typename Key>
concept conceptLessComparer = conceptPredicate<LessComparer, const Key&, const Key&>;

template<typename HashFunc, typename Key>
concept conceptHashFunc = conceptConstFunctor<HashFunc, size_t, const Key&>;
template<typename Hasher, typename Key>
concept conceptHasher = conceptConstFunctor<Hasher, size_t, const Key&>;

template<typename EqualFunc, typename Key>
concept conceptCopyableEqualFunc = conceptEqualFunc<EqualFunc, Key> &&
Expand All @@ -43,9 +43,9 @@ namespace internal
concept conceptCopyableLessComparer = conceptLessComparer<LessComparer, Key> &&
std::copy_constructible<LessComparer>;

template<typename HashFunc, typename Key>
concept conceptCopyableHashFunc = conceptHashFunc<HashFunc, Key> &&
std::copy_constructible<HashFunc>;
template<typename Hasher, typename Key>
concept conceptCopyableHasher = conceptHasher<Hasher, Key> &&
std::copy_constructible<Hasher>;

template<typename Predicate>
concept conceptTransparent = requires { typename Predicate::is_transparent; };
Expand Down
4 changes: 2 additions & 2 deletions include/momo/MergeSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ class MergeSet
size_t segItemCount = initialItemCount << (segIndex - 1);
if constexpr (MergeTraits::func == MergeTraitsFunc::hash)
{
auto hashFunc = [&mergeTraits] (const Item& item)
auto hasher = [&mergeTraits] (const Item& item)
{ return mergeTraits.GetHashCode(ItemTraits::GetKey(item)); };
auto equalFunc = [&mergeTraits] (const Item& item1, const auto& item2)
{
Expand All @@ -781,7 +781,7 @@ class MergeSet
return mergeTraits.IsEqual(key1, ItemTraits::GetKey(item2));
};
auto findRes = HashSorter::Find(segItems, segItemCount, key,
hashCode, hashFunc, equalFunc);
hashCode, hasher, equalFunc);
if (findRes.found)
return pvMakePosition(*findRes.iterator);
}
Expand Down
Loading

0 comments on commit 61187cc

Please sign in to comment.