Skip to content

Commit

Permalink
Update ArrayShifter
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Nov 5, 2024
1 parent d2cb4bc commit 8952e68
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
18 changes: 7 additions & 11 deletions include/momo/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,10 @@ class Array
void InsertCrt(size_t index, ItemCreator&& itemCreator)
{
ItemHandler itemHandler(GetMemManager(), std::forward<ItemCreator>(itemCreator));
std::move_iterator<Item*> begin(&itemHandler);
pvInsert(index, begin, begin + 1);
size_t newCount = GetCount() + 1;
if (newCount > GetCapacity())
pvGrow(newCount, ArrayGrowCause::add);
ArrayShifter::InsertNogrow(*this, index, std::move(*&itemHandler));
}

template<typename... ItemArgs>
Expand All @@ -874,15 +876,9 @@ class Array
size_t grow = (initCount + 1 > GetCapacity());
size_t itemIndex = pvIndexOf(item);
if (grow || (index <= itemIndex && itemIndex < initCount))
{
InsertCrt(index, typename ItemTraits::template Creator<Item&&>(
GetMemManager(), std::move(item)));
}
InsertVar(index, std::move(item));
else
{
std::move_iterator<Item*> begin(std::addressof(item));
ArrayShifter::InsertNogrow(*this, index, begin, begin + 1);
}
ArrayShifter::InsertNogrow(*this, index, std::move(item));
}

void Insert(size_t index, const Item& item)
Expand Down Expand Up @@ -1092,7 +1088,7 @@ class Array
size_t newCount = GetCount() + count;
if (newCount > GetCapacity())
pvGrow(newCount, ArrayGrowCause::add);
ArrayShifter::InsertNogrow(*this, index, begin, end);
ArrayShifter::InsertNogrow(*this, index, begin, count);
}

template<typename ArgIterator>
Expand Down
11 changes: 7 additions & 4 deletions include/momo/ArrayUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,10 @@ namespace internal

template<typename ArgIterator>
static EnableIf<IsForwardIterator<ArgIterator>::value>
InsertNogrow(Array& array, size_t index, ArgIterator begin, ArgIterator end)
InsertNogrow(Array& array, size_t index, ArgIterator begin, size_t count)
{
size_t initCount = array.GetCount();
MOMO_CHECK(index <= initCount);
size_t count = UIntMath<>::Dist(begin, end);
MOMO_ASSERT(array.GetCapacity() >= initCount + count);
MemManager& memManager = array.GetMemManager();
if (index + count < initCount)
Expand Down Expand Up @@ -258,8 +257,7 @@ namespace internal
}

template<typename ArgIterator>
static EnableIf<!IsForwardIterator<ArgIterator>::value>
Insert(Array& array, size_t index, ArgIterator begin, ArgIterator end)
static void Insert(Array& array, size_t index, ArgIterator begin, ArgIterator end)
{
typedef typename ItemTraits::template Creator<
typename std::iterator_traits<ArgIterator>::reference> IterCreator;
Expand All @@ -269,6 +267,11 @@ namespace internal
array.InsertCrt(index + count, IterCreator(memManager, *iter));
}

static void InsertNogrow(Array& array, size_t index, Item&& item)
{
InsertNogrow(array, index, std::make_move_iterator(std::addressof(item)), 1);
}

static void Remove(Array& array, size_t index, size_t count)
{
size_t initCount = array.GetCount();
Expand Down
9 changes: 5 additions & 4 deletions include/momo/SegmentedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -532,8 +532,8 @@ class SegmentedArray
void InsertCrt(size_t index, ItemCreator&& itemCreator)
{
ItemHandler itemHandler(GetMemManager(), std::forward<ItemCreator>(itemCreator));
std::move_iterator<Item*> begin(&itemHandler);
pvInsert(index, begin, begin + 1);
Reserve(mCount + 1);
ArrayShifter::InsertNogrow(*this, index, std::move(*&itemHandler));
}

template<typename... ItemArgs>
Expand Down Expand Up @@ -724,8 +724,9 @@ class SegmentedArray
internal::EnableIf<internal::IsForwardIterator<ArgIterator>::value>
pvInsert(size_t index, ArgIterator begin, ArgIterator end)
{
Reserve(mCount + internal::UIntMath<>::Dist(begin, end));
ArrayShifter::InsertNogrow(*this, index, begin, end);
size_t count = internal::UIntMath<>::Dist(begin, end);
Reserve(mCount + count);
ArrayShifter::InsertNogrow(*this, index, begin, count);
}

template<typename ArgIterator>
Expand Down

0 comments on commit 8952e68

Please sign in to comment.