Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Oct 7, 2023
1 parent bd35a0a commit 1879167
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions include/momo/HashSorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace momo
class HashSorter
{
public:
typedef size_t HashFuncResult;
typedef size_t HashCode;

template<internal::conceptRandomIterator Iterator>
struct Swapper
Expand Down Expand Up @@ -133,7 +133,7 @@ class HashSorter
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptEqualFunc<Item> EqualFunc = std::equal_to<Item>,
internal::conceptConstFunctor<void, Iterator, Iterator> IterSwapper = Swapper<Iterator>>
requires std::is_same_v<HashFuncResult&, std::iter_reference_t<HashIterator>>
requires std::is_same_v<HashCode&, std::iter_reference_t<HashIterator>>
static void SortPrehashed(Iterator begin, size_t count, HashIterator hashBegin,
EqualFunc equalFunc = EqualFunc(), IterSwapper iterSwapper = IterSwapper())
{
Expand Down Expand Up @@ -163,7 +163,7 @@ class HashSorter
internal::conceptRandomIterator HashIterator,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptEqualFunc<Item> EqualFunc = std::equal_to<Item>>
requires std::is_same_v<HashFuncResult, std::iter_value_t<HashIterator>>
requires std::is_same_v<HashCode, std::iter_value_t<HashIterator>>
static bool IsSortedPrehashed(Iterator begin, size_t count, HashIterator hashBegin,
EqualFunc equalFunc = EqualFunc())
{
Expand All @@ -178,7 +178,7 @@ class HashSorter
internal::conceptEqualFunc<Item, ItemArg> EqualFunc = std::equal_to<>>
requires internal::conceptEqualFunc<EqualFunc, Item>
static FindResult<Iterator> Find(Iterator begin, size_t count,
const ItemArg& itemArg, HashFuncResult argHash,
const ItemArg& itemArg, HashCode argHash,
HashFunc hashFunc = HashFunc(), EqualFunc equalFunc = EqualFunc())
{
IterHashFunc<Iterator, HashFunc> iterHashFunc((FastCopyableFunctor<HashFunc>(hashFunc)));
Expand All @@ -190,10 +190,10 @@ class HashSorter
internal::conceptRandomIterator HashIterator,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptEqualFunc<Item, ItemArg> EqualFunc = std::equal_to<>>
requires std::is_same_v<HashFuncResult, std::iter_value_t<HashIterator>> &&
requires std::is_same_v<HashCode, std::iter_value_t<HashIterator>> &&
internal::conceptEqualFunc<EqualFunc, Item>
static FindResult<Iterator> FindPrehashed(Iterator begin, size_t count, const ItemArg& itemArg,
HashFuncResult argHash, HashIterator hashBegin, EqualFunc equalFunc = EqualFunc())
HashCode argHash, HashIterator hashBegin, EqualFunc equalFunc = EqualFunc())
{
return pvFind(begin, count, itemArg, argHash,
FastCopyableFunctor(IterPrehashFunc<Iterator, HashIterator>(begin, hashBegin)),
Expand All @@ -206,7 +206,7 @@ class HashSorter
internal::conceptEqualFunc<Item, ItemArg> EqualFunc = std::equal_to<>>
requires internal::conceptEqualFunc<EqualFunc, Item>
static Bounds<Iterator> GetBounds(Iterator begin, size_t count,
const ItemArg& itemArg, HashFuncResult argHash,
const ItemArg& itemArg, HashCode argHash,
HashFunc hashFunc = HashFunc(), EqualFunc equalFunc = EqualFunc())
{
IterHashFunc<Iterator, HashFunc> iterHashFunc((FastCopyableFunctor<HashFunc>(hashFunc)));
Expand All @@ -218,10 +218,10 @@ class HashSorter
internal::conceptRandomIterator HashIterator,
conceptObject Item = std::iter_value_t<Iterator>,
internal::conceptEqualFunc<Item, ItemArg> EqualFunc = std::equal_to<>>
requires std::is_same_v<HashFuncResult, std::iter_value_t<HashIterator>> &&
requires std::is_same_v<HashCode, std::iter_value_t<HashIterator>> &&
internal::conceptEqualFunc<EqualFunc, Item>
static Bounds<Iterator> GetBoundsPrehashed(Iterator begin, size_t count, const ItemArg& itemArg,
HashFuncResult argHash, HashIterator hashBegin, EqualFunc equalFunc = EqualFunc())
HashCode argHash, HashIterator hashBegin, EqualFunc equalFunc = EqualFunc())
{
return pvGetBounds(begin, count, itemArg, argHash,
FastCopyableFunctor(IterPrehashFunc<Iterator, HashIterator>(begin, hashBegin)),
Expand Down Expand Up @@ -273,10 +273,10 @@ class HashSorter
FastCopyableFunctor<IterHashFunc> iterHashFunc, FastCopyableFunctor<EqualFunc> equalFunc)
{
size_t prevIndex = 0;
HashFuncResult prevHash = iterHashFunc(begin);
HashCode prevHash = iterHashFunc(begin);
for (size_t i = 1; i < count; ++i)
{
HashFuncResult hash = iterHashFunc(SMath::Next(begin, i));
HashCode hash = iterHashFunc(SMath::Next(begin, i));
if (hash < prevHash)
return false;
if (hash != prevHash)
Expand Down Expand Up @@ -311,7 +311,7 @@ class HashSorter
internal::conceptConstFunctor<size_t, Iterator> IterHashFunc,
internal::conceptEqualFunc<std::iter_value_t<Iterator>, ItemArg> EqualFunc>
static FindResult<Iterator> pvFind(Iterator begin, size_t count, const ItemArg& itemArg,
HashFuncResult argHash, FastCopyableFunctor<IterHashFunc> iterHashFunc,
HashCode argHash, FastCopyableFunctor<IterHashFunc> iterHashFunc,
FastCopyableFunctor<EqualFunc> equalFunc)
{
auto res = pvFindHash(begin, count, argHash, iterHashFunc);
Expand All @@ -331,7 +331,7 @@ class HashSorter
internal::conceptConstFunctor<size_t, Iterator> IterHashFunc,
internal::conceptEqualFunc<std::iter_value_t<Iterator>, ItemArg> EqualFunc>
static Bounds<Iterator> pvGetBounds(Iterator begin, size_t count, const ItemArg& itemArg,
HashFuncResult argHash, FastCopyableFunctor<IterHashFunc> iterHashFunc,
HashCode argHash, FastCopyableFunctor<IterHashFunc> iterHashFunc,
FastCopyableFunctor<EqualFunc> equalFunc)
{
auto res = pvFindHash(begin, count, argHash, iterHashFunc);
Expand Down Expand Up @@ -364,7 +364,7 @@ class HashSorter
internal::conceptConstFunctor<size_t, Iterator> IterHashFunc,
internal::conceptEqualFunc<std::iter_value_t<Iterator>, ItemArg> EqualFunc>
static FindResult<Iterator> pvFindNext(Iterator begin, size_t count, const ItemArg& itemArg,
HashFuncResult argHash, FastCopyableFunctor<IterHashFunc> iterHashFunc,
HashCode argHash, FastCopyableFunctor<IterHashFunc> iterHashFunc,
FastCopyableFunctor<EqualFunc> equalFunc)
{
Iterator iter = begin;
Expand Down Expand Up @@ -396,7 +396,7 @@ class HashSorter
template<internal::conceptRandomIterator Iterator,
internal::conceptConstFunctor<size_t, Iterator> IterHashFunc>
static FindResult<Iterator> pvFindHash(Iterator begin, size_t count,
HashFuncResult argHash, FastCopyableFunctor<IterHashFunc> iterHashFunc)
HashCode argHash, FastCopyableFunctor<IterHashFunc> iterHashFunc)
{
auto iterComparer = [argHash, iterHashFunc] (Iterator iter)
{ return iterHashFunc(iter) <=> argHash; };
Expand All @@ -406,7 +406,7 @@ class HashSorter
size_t step = pvGetStepCount(count);
while (true)
{
HashFuncResult middleHash = iterHashFunc(SMath::Next(begin, middleIndex));
HashCode middleHash = iterHashFunc(SMath::Next(begin, middleIndex));
if (middleHash < argHash)
{
leftIndex = middleIndex + 1;
Expand Down Expand Up @@ -489,12 +489,12 @@ class HashSorter
return (count < 1 << 6) ? 0 : (count < 1 << 12) ? 1 : (count < 1 << 22) ? 2 : 3;
}

static size_t pvMultShift(HashFuncResult value1, size_t value2) noexcept
static size_t pvMultShift(HashCode value1, size_t value2) noexcept
{
static_assert(sizeof(HashFuncResult) >= sizeof(size_t));
static const size_t halfSize = 4 * sizeof(HashFuncResult);
static const HashFuncResult halfMask = (HashFuncResult{1} << halfSize) - 1;
HashFuncResult res = (value1 >> halfSize) * (value2 >> halfSize)
static_assert(sizeof(HashCode) >= sizeof(size_t));
static const size_t halfSize = 4 * sizeof(HashCode);
static const HashCode halfMask = (HashCode{1} << halfSize) - 1;
HashCode res = (value1 >> halfSize) * (value2 >> halfSize)
+ (((value1 >> halfSize) * (value2 & halfMask)) >> halfSize)
+ (((value2 >> halfSize) * (value1 & halfMask)) >> halfSize);
return static_cast<size_t>(res);
Expand Down

0 comments on commit 1879167

Please sign in to comment.