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 5fa7645 commit 7f4a81c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
8 changes: 4 additions & 4 deletions include/momo/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -934,11 +934,11 @@ class Array
ArrayShifter::Remove(*this, index, count);
}

template<typename Predicate>
internal::EnableIf<internal::IsInvocable<const Predicate&, bool, const Item&>::value,
size_t> Remove(const Predicate& pred)
template<typename ItemFilter>
internal::EnableIf<internal::IsInvocable<const ItemFilter&, bool, const Item&>::value,
size_t> Remove(const ItemFilter& itemFilter)
{
return ArrayShifter::Remove(*this, pred);
return ArrayShifter::Remove(*this, itemFilter);
}

template<typename EqualFunc = std::equal_to<Item>>
Expand Down
8 changes: 4 additions & 4 deletions include/momo/ArrayUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,17 +271,17 @@ namespace internal
array.RemoveBack(count);
}

template<typename Predicate>
static size_t Remove(Array& array, const Predicate& pred)
template<typename ItemFilter>
static size_t Remove(Array& array, const ItemFilter& itemFilter)
{
size_t initCount = array.GetCount();
size_t newCount = 0;
while (newCount < initCount && !pred(static_cast<const Item&>(array[newCount])))
while (newCount < initCount && !itemFilter(static_cast<const Item&>(array[newCount])))
++newCount;
MemManager& memManager = array.GetMemManager();
for (size_t i = newCount + 1; i < initCount; ++i)
{
if (pred(static_cast<const Item&>(array[i])))
if (itemFilter(static_cast<const Item&>(array[i])))
continue;
ItemTraits::Assign(memManager, std::move(array[i]), array[newCount]);
++newCount;
Expand Down
14 changes: 7 additions & 7 deletions include/momo/MemPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ class MemPool : private TParams, private internal::MemManagerWrapper<TMemManager
mCacheHead = nullptr;
}

template<typename Predicate> // bool Predicate(void*)
void DeallocateIf(const Predicate& pred)
template<typename BlockFilter> // bool BlockFilter(void*)
void DeallocateIf(const BlockFilter& blockFilter)
{
MOMO_EXTRA_CHECK(CanDeallocateAll());
if (pvUseCache())
Expand All @@ -337,7 +337,7 @@ class MemPool : private TParams, private internal::MemManagerWrapper<TMemManager
while (true)
{
Byte* nextBuffer = pvGetNextBuffer(buffer);
pvDeleteBlocks(buffer, pred);
pvDeleteBlocks(buffer, blockFilter);
if (nextBuffer == nullptr)
break;
buffer = nextBuffer;
Expand All @@ -346,7 +346,7 @@ class MemPool : private TParams, private internal::MemManagerWrapper<TMemManager
while (buffer != nullptr)
{
Byte* prevBuffer = pvGetPrevBuffer(buffer);
pvDeleteBlocks(buffer, pred);
pvDeleteBlocks(buffer, blockFilter);
buffer = prevBuffer;
}
}
Expand Down Expand Up @@ -652,8 +652,8 @@ class MemPool : private TParams, private internal::MemManagerWrapper<TMemManager
+ 2 * sizeof(Byte*) + sizeof(uint16_t);
}

template<typename Predicate>
void pvDeleteBlocks(Byte* buffer, const Predicate& pred)
template<typename BlockFilter>
void pvDeleteBlocks(Byte* buffer, const BlockFilter& blockFilter)
{
int8_t firstBlockIndex = pvGetFirstBlockIndex(buffer);
uint8_t freeBlockBits[16] = {};
Expand All @@ -671,7 +671,7 @@ class MemPool : private TParams, private internal::MemManagerWrapper<TMemManager
continue;
int8_t blockIndex = firstBlockIndex + static_cast<int8_t>(i);
Byte* block = pvGetBlock(buffer, blockIndex);
if (!pred(static_cast<void*>(block)))
if (!blockFilter(static_cast<void*>(block)))
continue;
pvDeleteBlock(block, buffer, blockIndex);
--mAllocCount;
Expand Down
8 changes: 4 additions & 4 deletions include/momo/SegmentedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,11 @@ class SegmentedArray
ArrayShifter::Remove(*this, index, count);
}

template<typename Predicate>
internal::EnableIf<internal::IsInvocable<const Predicate&, bool, const Item&>::value,
size_t> Remove(const Predicate& pred)
template<typename ItemFilter>
internal::EnableIf<internal::IsInvocable<const ItemFilter&, bool, const Item&>::value,
size_t> Remove(const ItemFilter& itemFilter)
{
return ArrayShifter::Remove(*this, pred);
return ArrayShifter::Remove(*this, itemFilter);
}

template<typename EqualFunc = std::equal_to<Item>>
Expand Down
10 changes: 5 additions & 5 deletions include/momo/stdish/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,15 @@ class vector
template<typename ValueArg>
friend size_type erase(vector& cont, const ValueArg& valueArg)
{
auto pred = [&valueArg] (const value_type& value)
auto valueFilter = [&valueArg] (const value_type& value)
{ return value == valueArg; };
return cont.mArray.Remove(pred);
return cont.mArray.Remove(valueFilter);
}

template<typename Predicate>
friend size_type erase_if(vector& cont, const Predicate& pred)
template<typename ValueFilter>
friend size_type erase_if(vector& cont, const ValueFilter& valueFilter)
{
return cont.mArray.Remove(pred);
return cont.mArray.Remove(valueFilter);
}

void assign(size_type count, const value_type& value)
Expand Down
4 changes: 2 additions & 2 deletions test/sources/SimpleMemPoolTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class SimpleMemPoolTester
}
assert(memPool.GetAllocateCount() == lim2);

auto pred = [&blocks, blockSize, lim1] (void* block)
auto blockFilter = [&blocks, blockSize, lim1] (void* block)
{
auto begin = blocks.GetBegin();
auto mid = begin + lim1;
Expand All @@ -172,7 +172,7 @@ class SimpleMemPoolTester
std::memset(block, 2, blockSize);
return res;
};
memPool.DeallocateIf(pred);
memPool.DeallocateIf(blockFilter);
blocks.SetCount(lim1);
assert(memPool.GetAllocateCount() == lim1);
}
Expand Down
6 changes: 3 additions & 3 deletions test/sources/SimpleTreeTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class SimpleTreeTester
{
typedef momo::TreeMap<std::string, std::string> TreeMap;
typedef TreeMap::ConstIterator::Reference Reference;
auto pred = [] (Reference ref1, Reference ref2)
auto equalFunc = [] (Reference ref1, Reference ref2)
{ return ref1.key == ref2.key && ref1.value == ref2.value; };

std::string s1 = "s1";
Expand Down Expand Up @@ -190,7 +190,7 @@ class SimpleTreeTester
assert(ep.IsEmpty());

map.Insert(map2.GetBegin(), std::next(map2.GetBegin()));
assert(std::equal(map.GetBegin(), map.GetEnd(), map2.GetBegin(), pred));
assert(std::equal(map.GetBegin(), map.GetEnd(), map2.GetBegin(), equalFunc));

map.Clear();
assert(map.IsEmpty());
Expand All @@ -199,7 +199,7 @@ class SimpleTreeTester
map.Add(map.GetUpperBound("s2"), s2, "s2");
map.Add(map.GetLowerBound(s3), "s3", s3);
map.Add(map.GetUpperBound(s4), "s4", "s4");
assert(std::equal(map.GetBegin(), map.GetEnd(), map2.GetBegin(), pred));
assert(std::equal(map.GetBegin(), map.GetEnd(), map2.GetBegin(), equalFunc));
}

static void TestTemplAll()
Expand Down

0 comments on commit 7f4a81c

Please sign in to comment.