From e280b2eb18525d2246ac5cf04f2db9c1a216897b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Mon, 22 Sep 2014 15:18:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=EF=BC=8C=E6=8F=90=E9=AB=98in?= =?UTF-8?q?sert=E7=9A=84=E6=95=88=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Vector.h | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/TinySTL/Vector.h b/TinySTL/Vector.h index fd03cee..e900db0 100644 --- a/TinySTL/Vector.h +++ b/TinySTL/Vector.h @@ -5,6 +5,7 @@ #include #include "Allocator.h" +#include "Algorithm.h" #include "Iterator.h" #include "UninitializedFunctions.h" @@ -180,6 +181,12 @@ namespace TinySTL{ template void reallocateAndCopy(iterator position, InputIterator first, InputIterator last); void reallocateAndFillN(iterator position, const size_type& n, const value_type& val); + difference_type getNewCapacity(difference_type len)const{ + difference_type oldCapacity = endOfStorage_ - start_; + oldCapacity = oldCapacity ? oldCapacity : 1; + difference_type newCapacity = oldCapacity + std::max(oldCapacity, len); + return newCapacity; + } };// end of class vector //***********************构造,复制,析构相关*********************** @@ -236,9 +243,7 @@ namespace TinySTL{ template template void vector::reallocateAndCopy(iterator position, InputIterator first, InputIterator last){ - difference_type oldCapacity = endOfStorage_ - start_; - oldCapacity = oldCapacity ? oldCapacity : 1; - difference_type newCapacity = oldCapacity + std::max(oldCapacity, last - first); + difference_type newCapacity = getNewCapacity(last - first); T *newStart = dataAllocator::allocate(newCapacity); T *newEndOfStorage = newStart + newCapacity; @@ -252,9 +257,25 @@ namespace TinySTL{ endOfStorage_ = newEndOfStorage; } template + void vector::reallocateAndFillN(iterator position, const size_type& n, const value_type& val){ + difference_type newCapacity = getNewCapacity(n); + + T *newStart = dataAllocator::allocate(newCapacity); + T *newEndOfStorage = newStart + newCapacity; + T *newFinish = uninitialized_copy(begin(), position, newStart); + newFinish = uninitialized_fill_n(newFinish, n, val); + newFinish = uninitialized_copy(position, end(), newFinish); + + destroyAndDeallocateAll(); + start_ = newStart; + finish_ = newFinish; + endOfStorage_ = newEndOfStorage; + } + template template void vector::insert_aux(iterator position, - InputIterator first, InputIterator last, + InputIterator first, + InputIterator last, std::false_type){ difference_type locationLeft = endOfStorage_ - finish_; // the size of left storage difference_type locationNeed = last - first; @@ -273,8 +294,20 @@ namespace TinySTL{ template template void vector::insert_aux(iterator position, Integer n, Integer value, std::true_type){ - vector v(n, value); - insert(position, v.begin(), v.end()); + difference_type locationLeft = endOfStorage_ - finish_; // the size of left storage + difference_type locationNeed = n; + + if (locationLeft >= locationNeed){ + auto tempPtr = end() - 1; + for (; tempPtr - position >= 0; --tempPtr){//move the [position, finish_) back + *(tempPtr + locationNeed) = *tempPtr; + } + uninitialized_fill_n(position, n, value); + finish_ += locationNeed; + } + else{ + reallocateAndFillN(position, n, value); + } } template template