重构,提高insert的效率

This commit is contained in:
邹晓航
2014-09-22 15:18:35 +08:00
parent 00ac143acf
commit e280b2eb18

View File

@@ -5,6 +5,7 @@
#include <type_traits> #include <type_traits>
#include "Allocator.h" #include "Allocator.h"
#include "Algorithm.h"
#include "Iterator.h" #include "Iterator.h"
#include "UninitializedFunctions.h" #include "UninitializedFunctions.h"
@@ -180,6 +181,12 @@ namespace TinySTL{
template<class InputIterator> template<class InputIterator>
void reallocateAndCopy(iterator position, InputIterator first, InputIterator last); void reallocateAndCopy(iterator position, InputIterator first, InputIterator last);
void reallocateAndFillN(iterator position, const size_type& n, const value_type& val); 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 };// end of class vector
//***********************<2A><><EFBFBD><EFBFBD><ECA3AC><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*********************** //***********************<2A><><EFBFBD><EFBFBD><ECA3AC><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>***********************
@@ -236,9 +243,7 @@ namespace TinySTL{
template<class T, class Alloc> template<class T, class Alloc>
template<class InputIterator> template<class InputIterator>
void vector<T, Alloc>::reallocateAndCopy(iterator position, InputIterator first, InputIterator last){ void vector<T, Alloc>::reallocateAndCopy(iterator position, InputIterator first, InputIterator last){
difference_type oldCapacity = endOfStorage_ - start_; difference_type newCapacity = getNewCapacity(last - first);
oldCapacity = oldCapacity ? oldCapacity : 1;
difference_type newCapacity = oldCapacity + std::max(oldCapacity, last - first);
T *newStart = dataAllocator::allocate(newCapacity); T *newStart = dataAllocator::allocate(newCapacity);
T *newEndOfStorage = newStart + newCapacity; T *newEndOfStorage = newStart + newCapacity;
@@ -252,9 +257,25 @@ namespace TinySTL{
endOfStorage_ = newEndOfStorage; endOfStorage_ = newEndOfStorage;
} }
template<class T, class Alloc> template<class T, class Alloc>
void vector<T, Alloc>::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<class T, class Alloc>
template<class InputIterator> template<class InputIterator>
void vector<T, Alloc>::insert_aux(iterator position, void vector<T, Alloc>::insert_aux(iterator position,
InputIterator first, InputIterator last, InputIterator first,
InputIterator last,
std::false_type){ std::false_type){
difference_type locationLeft = endOfStorage_ - finish_; // the size of left storage difference_type locationLeft = endOfStorage_ - finish_; // the size of left storage
difference_type locationNeed = last - first; difference_type locationNeed = last - first;
@@ -273,8 +294,20 @@ namespace TinySTL{
template<class T, class Alloc> template<class T, class Alloc>
template<class Integer> template<class Integer>
void vector<T, Alloc>::insert_aux(iterator position, Integer n, Integer value, std::true_type){ void vector<T, Alloc>::insert_aux(iterator position, Integer n, Integer value, std::true_type){
vector<value_type> v(n, value); difference_type locationLeft = endOfStorage_ - finish_; // the size of left storage
insert(position, v.begin(), v.end()); 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<class T, class Alloc> template<class T, class Alloc>
template<class InputIterator> template<class InputIterator>