diff --git a/include/momo/Array.h b/include/momo/Array.h index 3da60da8..b442b3df 100644 --- a/include/momo/Array.h +++ b/include/momo/Array.h @@ -857,8 +857,10 @@ class Array void InsertCrt(size_t index, ItemCreator&& itemCreator) { ItemHandler itemHandler(GetMemManager(), std::forward(itemCreator)); - std::move_iterator 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 @@ -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( - GetMemManager(), std::move(item))); - } + InsertVar(index, std::move(item)); else - { - std::move_iterator 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) @@ -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 diff --git a/include/momo/ArrayUtility.h b/include/momo/ArrayUtility.h index afdb36ca..d380126b 100644 --- a/include/momo/ArrayUtility.h +++ b/include/momo/ArrayUtility.h @@ -223,11 +223,10 @@ namespace internal template static EnableIf::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) @@ -258,8 +257,7 @@ namespace internal } template - static EnableIf::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::reference> IterCreator; @@ -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(); diff --git a/include/momo/SegmentedArray.h b/include/momo/SegmentedArray.h index 6d8f8d51..5d7ebdcf 100644 --- a/include/momo/SegmentedArray.h +++ b/include/momo/SegmentedArray.h @@ -532,8 +532,8 @@ class SegmentedArray void InsertCrt(size_t index, ItemCreator&& itemCreator) { ItemHandler itemHandler(GetMemManager(), std::forward(itemCreator)); - std::move_iterator begin(&itemHandler); - pvInsert(index, begin, begin + 1); + Reserve(mCount + 1); + ArrayShifter::InsertNogrow(*this, index, std::move(*&itemHandler)); } template @@ -724,8 +724,9 @@ class SegmentedArray internal::EnableIf::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