diff --git a/TinySTL/Vector.h b/TinySTL/Vector.h index 4e7785e..a27aff4 100644 --- a/TinySTL/Vector.h +++ b/TinySTL/Vector.h @@ -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_; + } //访问元素相关 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 - 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); //容器的空间配置器相关 Alloc get_allocator(){ return dataAllocator; } @@ -175,12 +181,12 @@ namespace TinySTL{ allocateAndFillN(n, value); } template - 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 - 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 - 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; } + //*************和容器的容量相关****************************** + template + void vector::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 + void vector::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; + } //***************修改容器的相关操作************************** template - typename vector::iterator vector::erase(const iterator& position){ + typename vector::iterator vector::erase(iterator position){ return erase(position, position + 1); } template - typename vector::iterator vector::erase(const iterator& first, const iterator& last){ + typename vector::iterator vector::erase(iterator first, iterator last){ //尾部残留对象数 difference_type lenOfTail = end() - last; //删去的对象数目 @@ -242,7 +281,7 @@ namespace TinySTL{ } template template - void vector::reallocateAndCopy(const iterator& position, InputIterator first, InputIterator last){ + void vector::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 - void vector::reallocateAndFillN(const iterator& position, const size_type& n, const value_type& val){ + void vector::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 template - void vector::insert_aux(const iterator& position, + void vector::insert_aux(iterator position, InputIterator first, InputIterator last, std::false_type){ @@ -293,7 +332,7 @@ namespace TinySTL{ } template template - void vector::insert_aux(const iterator& position, Integer n, const value_type& value, std::true_type){ + void vector::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 template - void vector::insert(const iterator& position, InputIterator first, InputIterator last){ + void vector::insert(iterator position, InputIterator first, InputIterator last){ insert_aux(position, first, last, typename std::is_integral::type()); } template - void vector::insert(const iterator& position, const size_type& n, const value_type& val){ + void vector::insert(iterator position, const size_type& n, const value_type& val){ insert_aux(position, n, val, typename std::is_integral::type()); } template - typename vector::iterator vector::insert(const iterator& position, const value_type& val){ + typename vector::iterator vector::insert(iterator position, const value_type& val){ insert(position, 1, val); return position; } diff --git a/TinySTL/main.cpp b/TinySTL/main.cpp index a6d60bf..7727e36 100644 --- a/TinySTL/main.cpp +++ b/TinySTL/main.cpp @@ -4,26 +4,34 @@ #include #include -#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 vec; - //TinySTL::vector vec; - ProfilerInstance::start(); - int i = 0; - for (; i != 100000; ++i){ - vec.push_back(std::string("zouxiaohang")); - } - ProfilerInstance::finish(); - ProfilerInstance::dumpDuringTime(); + //std::vector vec; + ////TinySTL::vector vec; + //ProfilerInstance::start(); + //int i = 0; + //for (; i != 1000; ++i){ + // vec.push_back(std::string("zouxiaohang")); + //} + //ProfilerInstance::finish(); + //ProfilerInstance::dumpDuringTime(); + TinySTL::vector 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; }