增加vector的容量有关的操作接口
This commit is contained in:
@@ -113,6 +113,12 @@ namespace TinySTL{
|
||||
difference_type size()const{ return finish_ - start_; }
|
||||
difference_type capacity()const{ return endOfStorage_ - start_; }
|
||||
bool empty()const{ return start_ == finish_; }
|
||||
void resize(size_type n, value_type val = value_type());
|
||||
void reserve(size_type n);
|
||||
void shrink_to_fit(){
|
||||
dataAllocator::deallocate(finish_, endOfStorage_ - finish_);
|
||||
endOfStorage_ = finish_;
|
||||
}
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
value_type& operator[](const difference_type i){ return *(begin() + i); }
|
||||
@@ -138,12 +144,12 @@ namespace TinySTL{
|
||||
--finish_;
|
||||
dataAllocator::destroy(finish_);
|
||||
}
|
||||
iterator insert(const iterator& position, const value_type& val);
|
||||
void insert(const iterator& position, const size_type& n, const value_type& val);
|
||||
iterator insert(iterator position, const value_type& val);
|
||||
void insert(iterator position, const size_type& n, const value_type& val);
|
||||
template <class InputIterator>
|
||||
void insert(const iterator& position, InputIterator first, InputIterator last);
|
||||
iterator erase(const iterator& position);
|
||||
iterator erase(const iterator& first, const iterator& last);
|
||||
void insert(iterator position, InputIterator first, InputIterator last);
|
||||
iterator erase(iterator position);
|
||||
iterator erase(iterator first, iterator last);
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>Ŀռ<C4BF><D5BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
Alloc get_allocator(){ return dataAllocator; }
|
||||
@@ -175,12 +181,12 @@ namespace TinySTL{
|
||||
allocateAndFillN(n, value);
|
||||
}
|
||||
template<class InputIterator>
|
||||
void insert_aux(const iterator& position, InputIterator first, InputIterator last, std::false_type);
|
||||
void insert_aux(iterator position, InputIterator first, InputIterator last, std::false_type);
|
||||
template<class Integer>
|
||||
void insert_aux(const iterator& position, Integer n, const value_type& value, std::true_type);
|
||||
void insert_aux(iterator position, Integer n, const value_type& value, std::true_type);
|
||||
template<class InputIterator>
|
||||
void reallocateAndCopy(const iterator& position, InputIterator first, InputIterator last);
|
||||
void reallocateAndFillN(const iterator& position, const size_type& n, const value_type& val);
|
||||
void reallocateAndCopy(iterator position, InputIterator first, InputIterator last);
|
||||
void reallocateAndFillN(iterator position, const size_type& n, const value_type& val);
|
||||
size_type getNewCapacity(size_type len)const{
|
||||
size_type oldCapacity = endOfStorage_ - start_;
|
||||
auto res = std::max(oldCapacity, len);
|
||||
@@ -222,13 +228,46 @@ namespace TinySTL{
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
//*************<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>******************************
|
||||
template<class T, class Alloc>
|
||||
void vector<T, Alloc>::resize(size_type n, value_type val = value_type()){
|
||||
if (n < size()){
|
||||
dataAllocator::destroy(start_ + n, finish_);
|
||||
finish_ = start_ + n;
|
||||
}else if (n > size() && n <= capacity()){
|
||||
auto lengthOfInsert = n - size();
|
||||
finish_ = uninitialized_fill_n(finish_, lengthOfInsert, val);
|
||||
}else if (n > capacity()){
|
||||
auto lengthOfInsert = n - size();
|
||||
T *newStart = dataAllocator::allocate(getNewCapacity(lengthOfInsert));
|
||||
T *newFinish = TinySTL::uninitialized_copy(begin(), end(), newStart);
|
||||
newFinish = TinySTL::uninitialized_fill_n(newFinish, lengthOfInsert, val);
|
||||
|
||||
destroyAndDeallocateAll();
|
||||
start_ = newStart;
|
||||
finish_ = newFinish;
|
||||
endOfStorage_ = start_ + n;
|
||||
}
|
||||
}
|
||||
template<class T, class Alloc>
|
||||
void vector<T, Alloc>::reserve(size_type n){
|
||||
if (n <= capacity())
|
||||
return;
|
||||
T *newStart = dataAllocator::allocate(n);
|
||||
T *newFinish = TinySTL::uninitialized_copy(begin(), end(), newStart);
|
||||
destroyAndDeallocateAll();
|
||||
|
||||
start_ = newStart;
|
||||
finish_ = newFinish;
|
||||
endOfStorage_ = start_ + n;
|
||||
}
|
||||
//***************<2A><EFBFBD><DEB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ز<EFBFBD><D8B2><EFBFBD>**************************
|
||||
template<class T,class Alloc>
|
||||
typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(const iterator& position){
|
||||
typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(iterator position){
|
||||
return erase(position, position + 1);
|
||||
}
|
||||
template<class T, class Alloc>
|
||||
typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(const iterator& first, const iterator& last){
|
||||
typename vector<T, Alloc>::iterator vector<T, Alloc>::erase(iterator first, iterator last){
|
||||
//β<><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
difference_type lenOfTail = end() - last;
|
||||
//ɾȥ<C9BE>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD>Ŀ
|
||||
@@ -242,7 +281,7 @@ namespace TinySTL{
|
||||
}
|
||||
template<class T, class Alloc>
|
||||
template<class InputIterator>
|
||||
void vector<T, Alloc>::reallocateAndCopy(const iterator& position, InputIterator first, InputIterator last){
|
||||
void vector<T, Alloc>::reallocateAndCopy(iterator position, InputIterator first, InputIterator last){
|
||||
difference_type newCapacity = getNewCapacity(last - first);
|
||||
|
||||
T *newStart = dataAllocator::allocate(newCapacity);
|
||||
@@ -257,7 +296,7 @@ namespace TinySTL{
|
||||
endOfStorage_ = newEndOfStorage;
|
||||
}
|
||||
template<class T, class Alloc>
|
||||
void vector<T, Alloc>::reallocateAndFillN(const iterator& position, const size_type& n, const value_type& val){
|
||||
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);
|
||||
@@ -273,7 +312,7 @@ namespace TinySTL{
|
||||
}
|
||||
template<class T, class Alloc>
|
||||
template<class InputIterator>
|
||||
void vector<T, Alloc>::insert_aux(const iterator& position,
|
||||
void vector<T, Alloc>::insert_aux(iterator position,
|
||||
InputIterator first,
|
||||
InputIterator last,
|
||||
std::false_type){
|
||||
@@ -293,7 +332,7 @@ namespace TinySTL{
|
||||
}
|
||||
template<class T, class Alloc>
|
||||
template<class Integer>
|
||||
void vector<T, Alloc>::insert_aux(const iterator& position, Integer n, const value_type& value, std::true_type){
|
||||
void vector<T, Alloc>::insert_aux(iterator position, Integer n, const value_type& value, std::true_type){
|
||||
assert(n != 0);
|
||||
difference_type locationLeft = endOfStorage_ - finish_; // the size of left storage
|
||||
difference_type locationNeed = n;
|
||||
@@ -312,15 +351,15 @@ namespace TinySTL{
|
||||
}
|
||||
template<class T, class Alloc>
|
||||
template<class InputIterator>
|
||||
void vector<T, Alloc>::insert(const iterator& position, InputIterator first, InputIterator last){
|
||||
void vector<T, Alloc>::insert(iterator position, InputIterator first, InputIterator last){
|
||||
insert_aux(position, first, last, typename std::is_integral<InputIterator>::type());
|
||||
}
|
||||
template<class T, class Alloc>
|
||||
void vector<T, Alloc>::insert(const iterator& position, const size_type& n, const value_type& val){
|
||||
void vector<T, Alloc>::insert(iterator position, const size_type& n, const value_type& val){
|
||||
insert_aux(position, n, val, typename std::is_integral<size_type>::type());
|
||||
}
|
||||
template<class T, class Alloc>
|
||||
typename vector<T, Alloc>::iterator vector<T, Alloc>::insert(const iterator& position, const value_type& val){
|
||||
typename vector<T, Alloc>::iterator vector<T, Alloc>::insert(iterator position, const value_type& val){
|
||||
insert(position, 1, val);
|
||||
return position;
|
||||
}
|
||||
|
||||
@@ -4,26 +4,34 @@
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
|
||||
#include "Allocator.h"
|
||||
#include "Construct.h"
|
||||
#include "Vector.h"
|
||||
#include "Profiler\Profiler.h"
|
||||
#include "UninitializedFunctions.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace TinySTL::Profiler;
|
||||
|
||||
int main(){
|
||||
|
||||
std::vector<std::string> vec;
|
||||
//TinySTL::vector<std::string> vec;
|
||||
ProfilerInstance::start();
|
||||
int i = 0;
|
||||
for (; i != 100000; ++i){
|
||||
vec.push_back(std::string("zouxiaohang"));
|
||||
}
|
||||
ProfilerInstance::finish();
|
||||
ProfilerInstance::dumpDuringTime();
|
||||
//std::vector<std::string> vec;
|
||||
////TinySTL::vector<std::string> vec;
|
||||
//ProfilerInstance::start();
|
||||
//int i = 0;
|
||||
//for (; i != 1000; ++i){
|
||||
// vec.push_back(std::string("zouxiaohang"));
|
||||
//}
|
||||
//ProfilerInstance::finish();
|
||||
//ProfilerInstance::dumpDuringTime();
|
||||
TinySTL::vector<int> v(10, 1);
|
||||
for (auto i : v){ cout << i << endl; }
|
||||
cout << "size = " << v.size() << " capacity = " << v.capacity() << endl;
|
||||
v.resize(9);
|
||||
for (auto i : v){ cout << i << endl; }
|
||||
cout << "size = " << v.size() << " capacity = " << v.capacity() << endl;
|
||||
v.resize(20, -1);
|
||||
for (auto i : v){ cout << i << endl; }
|
||||
cout << "size = " << v.size() << " capacity = " << v.capacity() << endl;
|
||||
v.push_back(0);
|
||||
cout << "size = " << v.size() << " capacity = " << v.capacity() << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user