Skip to content

Commit

Permalink
Fast functors
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Sep 9, 2023
1 parent 1010f5f commit 42707ad
Show file tree
Hide file tree
Showing 18 changed files with 209 additions and 220 deletions.
62 changes: 29 additions & 33 deletions include/momo/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,7 @@ class Array
}

template<bool grow, internal::conceptMoveFunctor<void, Item*> ItemsCreator>
void Reset(size_t capacity, size_t count,
internal::FastMovableFunctor<ItemsCreator> itemsCreator)
void Reset(size_t capacity, size_t count, FastMovableFunctor<ItemsCreator> itemsCreator)
{
MOMO_ASSERT(count <= capacity);
if (grow || capacity > internalCapacity)
Expand Down Expand Up @@ -491,9 +490,9 @@ class Array
for (ArgIterator iter = begin; iter != end; ++iter)
{
if constexpr (internal::conceptIterator<ArgIterator, std::forward_iterator_tag>)
pvAddBackNogrow(internal::FastMovableFunctor(IterCreator(thisMemManager, *iter)));
pvAddBackNogrow(FastMovableFunctor(IterCreator(thisMemManager, *iter)));
else
pvAddBack(internal::FastMovableFunctor(IterCreator(thisMemManager, *iter)));
pvAddBack(FastMovableFunctor(IterCreator(thisMemManager, *iter)));
}
}

Expand Down Expand Up @@ -541,8 +540,8 @@ class Array
Array array = CreateCap(count, std::move(memManager));
for (size_t i = 0; i < count; ++i)
{
array.pvAddBackNogrow(internal::FastMovableFunctor(
internal::FastCopyableFunctor<MultiItemCreator>(multiItemCreator)));
array.pvAddBackNogrow(FastMovableFunctor(
FastCopyableFunctor<MultiItemCreator>(multiItemCreator)));
}
return array;
}
Expand Down Expand Up @@ -624,7 +623,7 @@ class Array
template<internal::conceptObjectMultiCreator<Item> MultiItemCreator>
void SetCountCrt(size_t count, MultiItemCreator multiItemCreator)
{
pvSetCount(count, internal::FastCopyableFunctor<MultiItemCreator>(multiItemCreator));
pvSetCount(count, FastCopyableFunctor<MultiItemCreator>(multiItemCreator));
}

void SetCount(size_t count)
Expand All @@ -633,7 +632,7 @@ class Array
MemManager& memManager = GetMemManager();
auto multiItemCreator = [&memManager] (Item* newItem)
{ (ItemCreator(memManager))(newItem); };
pvSetCount(count, internal::FastCopyableFunctor(multiItemCreator));
pvSetCount(count, FastCopyableFunctor(multiItemCreator));
}

void SetCount(size_t count, const Item& item)
Expand All @@ -642,7 +641,7 @@ class Array
MemManager& memManager = GetMemManager();
auto multiItemCreator = [&memManager, &item] (Item* newItem)
{ ItemCreator(memManager, item)(newItem); };
pvSetCount(count, internal::FastCopyableFunctor(multiItemCreator));
pvSetCount(count, FastCopyableFunctor(multiItemCreator));
}

bool IsEmpty() const noexcept
Expand Down Expand Up @@ -687,7 +686,7 @@ class Array
auto itemsCreator = [this, count] (Item* newItems)
{ ItemTraits::Relocate(GetMemManager(), GetItems(), newItems, count); };
mData.template Reset<false>(capacity, count,
internal::FastMovableFunctor(std::move(itemsCreator)));
FastMovableFunctor(std::move(itemsCreator)));
}
}

Expand Down Expand Up @@ -715,8 +714,7 @@ class Array
void AddBackNogrowCrt(ItemCreator itemCreator)
{
MOMO_CHECK(GetCount() < GetCapacity());
pvAddBackNogrow(
internal::FastMovableFunctor<ItemCreator>(std::forward<ItemCreator>(itemCreator)));
pvAddBackNogrow(FastMovableFunctor<ItemCreator>(std::forward<ItemCreator>(itemCreator)));
}

template<typename... ItemArgs>
Expand All @@ -740,7 +738,7 @@ class Array
template<internal::conceptObjectCreator<Item> ItemCreator>
void AddBackCrt(ItemCreator itemCreator)
{
pvAddBack(internal::FastMovableFunctor<ItemCreator>(std::forward<ItemCreator>(itemCreator)));
pvAddBack(FastMovableFunctor<ItemCreator>(std::forward<ItemCreator>(itemCreator)));
}

template<typename... ItemArgs>
Expand All @@ -758,7 +756,7 @@ class Array
size_t initCount = GetCount();
if (initCount < GetCapacity())
{
pvAddBackNogrow(internal::FastMovableFunctor(ItemCreator(memManager, std::move(item))));
pvAddBackNogrow(FastMovableFunctor(ItemCreator(memManager, std::move(item))));
}
else if constexpr (ItemTraits::isNothrowMoveConstructible)
{
Expand All @@ -773,7 +771,7 @@ class Array
}
else
{
pvAddBackGrow(internal::FastMovableFunctor(ItemCreator(memManager, std::move(item))));
pvAddBackGrow(FastMovableFunctor(ItemCreator(memManager, std::move(item))));
}
}

Expand All @@ -784,29 +782,28 @@ class Array
size_t initCount = GetCount();
if (initCount < GetCapacity())
{
pvAddBackNogrow(internal::FastMovableFunctor(ItemCreator(memManager, item)));
pvAddBackNogrow(FastMovableFunctor(ItemCreator(memManager, item)));
}
else if constexpr (ItemTraits::isNothrowRelocatable)
{
size_t newCount = initCount + 1;
ItemHandler itemHandler(memManager,
internal::FastMovableFunctor(ItemCreator(memManager, item)));
FastMovableFunctor(ItemCreator(memManager, item)));
pvGrow(newCount, ArrayGrowCause::add);
ItemTraits::Relocate(memManager, &itemHandler, GetItems() + initCount, 1);
itemHandler.Release();
mData.SetCount(newCount);
}
else
{
pvAddBackGrow(internal::FastMovableFunctor(ItemCreator(memManager, item)));
pvAddBackGrow(FastMovableFunctor(ItemCreator(memManager, item)));
}
}

template<internal::conceptObjectCreator<Item> ItemCreator>
void InsertCrt(size_t index, ItemCreator itemCreator)
{
pvInsert(index,
internal::FastMovableFunctor<ItemCreator>(std::forward<ItemCreator>(itemCreator)));
pvInsert(index, FastMovableFunctor<ItemCreator>(std::forward<ItemCreator>(itemCreator)));
}

template<typename... ItemArgs>
Expand Down Expand Up @@ -843,8 +840,7 @@ class Array
{
typedef typename ItemTraits::template Creator<const Item&> ItemCreator;
MemManager& memManager = GetMemManager();
ItemHandler itemHandler(memManager,
internal::FastMovableFunctor(ItemCreator(memManager, item)));
ItemHandler itemHandler(memManager, FastMovableFunctor(ItemCreator(memManager, item)));
if (grow)
pvGrow(newCount, ArrayGrowCause::add);
ArrayShifter::Insert(*this, index, count, *&itemHandler);
Expand Down Expand Up @@ -892,14 +888,14 @@ class Array
template<internal::conceptObjectPredicate<Item> Predicate>
size_t Remove(Predicate pred)
{
return ArrayShifter::Remove(*this, internal::FastCopyableFunctor<Predicate>(pred));
return ArrayShifter::Remove(*this, FastCopyableFunctor<Predicate>(pred));
}

template<typename ItemArg,
internal::conceptEqualFunc<Item, ItemArg> EqualFunc = std::equal_to<>>
bool Contains(const ItemArg& itemArg, EqualFunc equalFunc = EqualFunc()) const
{
internal::FastCopyableFunctor<EqualFunc> fastEqualFunc(equalFunc);
FastCopyableFunctor<EqualFunc> fastEqualFunc(equalFunc);
return std::any_of(GetBegin(), GetEnd(),
[&itemArg, fastEqualFunc] (const Item& item) { return fastEqualFunc(item, itemArg); });
}
Expand All @@ -908,7 +904,7 @@ class Array
bool IsEqual(const Array& array, EqualFunc equalFunc = EqualFunc()) const
{
return std::equal(GetBegin(), GetEnd(), array.GetBegin(), array.GetEnd(),
internal::FastCopyableFunctor<EqualFunc>(equalFunc));
FastCopyableFunctor<EqualFunc>(equalFunc));
}

private:
Expand Down Expand Up @@ -942,12 +938,12 @@ class Array
auto itemsCreator = [this, count] (Item* newItems)
{ ItemTraits::Relocate(GetMemManager(), GetItems(), newItems, count); };
mData.template Reset<true>(newCapacityExp, count,
internal::FastMovableFunctor(std::move(itemsCreator)));
FastMovableFunctor(std::move(itemsCreator)));
}
}

template<internal::conceptObjectMultiCreator<Item> MultiItemCreator>
void pvSetCount(size_t count, internal::FastCopyableFunctor<MultiItemCreator> multiItemCreator)
void pvSetCount(size_t count, FastCopyableFunctor<MultiItemCreator> multiItemCreator)
{
size_t newCount = count;
size_t initCount = GetCount();
Expand Down Expand Up @@ -990,7 +986,7 @@ class Array
}
};
mData.template Reset<true>(newCapacity, newCount,
internal::FastMovableFunctor(std::move(itemsCreator)));
FastMovableFunctor(std::move(itemsCreator)));
}
}

Expand All @@ -1002,7 +998,7 @@ class Array
}

template<internal::conceptObjectCreator<Item> ItemCreator>
void pvAddBack(internal::FastMovableFunctor<ItemCreator> itemCreator)
void pvAddBack(FastMovableFunctor<ItemCreator> itemCreator)
{
if (GetCount() < GetCapacity())
pvAddBackNogrow(std::move(itemCreator));
Expand All @@ -1011,15 +1007,15 @@ class Array
}

template<internal::conceptObjectCreator<Item> ItemCreator>
void pvAddBackNogrow(internal::FastMovableFunctor<ItemCreator> itemCreator)
void pvAddBackNogrow(FastMovableFunctor<ItemCreator> itemCreator)
{
size_t count = GetCount();
std::move(itemCreator)(GetItems() + count);
mData.SetCount(count + 1);
}

template<internal::conceptObjectCreator<Item> ItemCreator>
void pvAddBackGrow(internal::FastMovableFunctor<ItemCreator> itemCreator)
void pvAddBackGrow(FastMovableFunctor<ItemCreator> itemCreator)
{
size_t initCount = GetCount();
size_t newCount = initCount + 1;
Expand All @@ -1031,11 +1027,11 @@ class Array
std::move(itemCreator), newItems + initCount);
};
mData.template Reset<true>(newCapacity, newCount,
internal::FastMovableFunctor(std::move(itemsCreator)));
FastMovableFunctor(std::move(itemsCreator)));
}

template<internal::conceptObjectCreator<Item> ItemCreator>
void pvInsert(size_t index, internal::FastMovableFunctor<ItemCreator> itemCreator)
void pvInsert(size_t index, FastMovableFunctor<ItemCreator> itemCreator)
{
ItemHandler itemHandler(GetMemManager(), std::move(itemCreator));
size_t newCount = GetCount() + 1;
Expand Down
12 changes: 6 additions & 6 deletions include/momo/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ class HashMap
InsertResult InsertCrt(Key&& key, ValueCreator valueCreator)
{
return pvInsert(std::move(key),
internal::FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
}

template<typename... ValueArgs>
Expand All @@ -529,7 +529,7 @@ class HashMap
InsertResult InsertCrt(const Key& key, ValueCreator valueCreator)
{
return pvInsert(key,
internal::FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
}

template<typename... ValueArgs>
Expand Down Expand Up @@ -601,15 +601,15 @@ class HashMap
Position AddCrt(ConstPosition pos, PairCreator pairCreator)
{
return pvAdd<extraCheck>(pos,
internal::FastMovableFunctor<PairCreator>(std::forward<PairCreator>(pairCreator)));
FastMovableFunctor<PairCreator>(std::forward<PairCreator>(pairCreator)));
}

template<internal::conceptObjectCreator<Value> ValueCreator,
bool extraCheck = true>
Position AddCrt(ConstPosition pos, Key&& key, ValueCreator valueCreator)
{
return pvAdd<extraCheck>(pos, std::move(key),
internal::FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
}

template<typename... ValueArgs>
Expand All @@ -635,7 +635,7 @@ class HashMap
Position AddCrt(ConstPosition pos, const Key& key, ValueCreator valueCreator)
{
return pvAdd<extraCheck>(pos, key,
internal::FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
}

template<typename... ValueArgs>
Expand Down Expand Up @@ -743,7 +743,7 @@ class HashMap
template<internal::conceptMapPairPredicate<Key, Value> PairPredicate>
size_t Remove(PairPredicate pairPred)
{
internal::FastCopyableFunctor<PairPredicate> fastPairPred(pairPred);
FastCopyableFunctor<PairPredicate> fastPairPred(pairPred);
auto itemPred = [fastPairPred] (const KeyValuePair& item)
{ return fastPairPred(*item.GetKeyPtr(), std::as_const(*item.GetValuePtr())); };
return mHashSet.Remove(itemPred);
Expand Down
8 changes: 4 additions & 4 deletions include/momo/HashMultiMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,7 @@ class HashMultiMap
Iterator AddCrt(Key&& key, ValueCreator valueCreator)
{
return pvAdd(std::move(key),
internal::FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
}

template<typename... ValueArgs>
Expand All @@ -981,7 +981,7 @@ class HashMultiMap
Iterator AddCrt(const Key& key, ValueCreator valueCreator)
{
return pvAdd(key,
internal::FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
}

template<typename... ValueArgs>
Expand All @@ -1006,7 +1006,7 @@ class HashMultiMap
Iterator AddCrt(ConstKeyIterator keyIter, ValueCreator valueCreator)
{
return pvAdd(keyIter,
internal::FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
FastMovableFunctor<ValueCreator>(std::forward<ValueCreator>(valueCreator)));
}

template<typename... ValueArgs>
Expand Down Expand Up @@ -1058,7 +1058,7 @@ class HashMultiMap
KeyIterator AddKeyCrt(ConstKeyIterator keyIter, KeyCreator keyCreator)
{
return pvAddKey(keyIter,
internal::FastMovableFunctor<KeyCreator>(std::forward<KeyCreator>(keyCreator)));
FastMovableFunctor<KeyCreator>(std::forward<KeyCreator>(keyCreator)));
}

//KeyIterator AddKey(ConstKeyIterator keyIter, Key&& key)
Expand Down
4 changes: 2 additions & 2 deletions include/momo/HashSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ class HashSet
InsertResult InsertCrt(const Key& key, ItemCreator itemCreator)
{
return pvInsert<extraCheck>(key,
internal::FastMovableFunctor<ItemCreator>(std::forward<ItemCreator>(itemCreator)));
FastMovableFunctor<ItemCreator>(std::forward<ItemCreator>(itemCreator)));
}

template<typename... ItemArgs>
Expand Down Expand Up @@ -809,7 +809,7 @@ class HashSet
Position AddCrt(ConstPosition pos, ItemCreator itemCreator)
{
return pvAdd<extraCheck>(pos,
internal::FastMovableFunctor<ItemCreator>(std::forward<ItemCreator>(itemCreator)));
FastMovableFunctor<ItemCreator>(std::forward<ItemCreator>(itemCreator)));
}

template<typename... ItemArgs>
Expand Down
2 changes: 1 addition & 1 deletion include/momo/MemPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class MemPool : private TParams, private internal::MemManagerWrapper<TMemManager
if (pvUseCache())
pvFlushDeallocate();
if (mAllocCount > 0)
pvDeallocateIf(internal::FastCopyableFunctor<Predicate>(pred));
pvDeallocateIf(FastCopyableFunctor<Predicate>(pred));
}

void MergeFrom(MemPool& memPool)
Expand Down
Loading

0 comments on commit 42707ad

Please sign in to comment.