经过长时间的学习终于可以开始tinystl的仿(chao)写工作了,本文参考了这位大神的github,坦白讲我只是补充了注释,因为tinystl的代码真的非常经典而我又没什么这种大型项目的经验,所以只能这样做,不过相信能够有助于大家的学习
#强烈建议按顺序阅读本专栏
vector.h
(做声明)
#pragma once
#ifndef _VECTOR_H_
#define _VECTOR_H_
#include <algorithm>
#include <type_traits>
#include "Allocator.h"
#include "Algorithm.h"
#include "Iterator.h"
#include "ReverseIterator.h"
#include "UninitializedFunctions.h"
namespace mySTL {
//********* vector *************
template<class T, class Alloc = allocator<T>>
class vector {
private:
T *start_;
T *finish_;
T *endOfStorage_;
typedef Alloc dataAllocator;
public:
typedef T value_type;
typedef T* iterator;
//typedef const iterator const_iterator;
typedef const T* const_iterator;
typedef reverse_iterator_t<T*> reverse_iterator;
typedef reverse_iterator_t<const T*> const_reverse_iterator;
typedef iterator pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
public:
//构造,复制,析构相关函数
vector()
:start_(0), finish_(0), endOfStorage_(0) {}
explicit vector(const size_type n);
vector(const size_type n, const value_type& value);
template<class InputIterator>
vector(InputIterator first, InputIterator last);
vector(const vector& v);
vector(vector&& v);
vector& operator = (const vector& v);
vector& operator = (vector&& v);
~vector();
//比较操作相关
bool operator == (const vector& v)const;
bool operator != (const vector& v)const;
//迭代器相关
iterator begin() { return (start_); }
const_iterator begin()const { return (start_); }
const_iterator cbegin()const { return (start_); }
iterator end() { return (finish_); }
const_iterator end()const { return (finish_); }
const_iterator cend()const { return (finish_); }
reverse_iterator rbegin() { return reverse_iterator(finish_); }
const_reverse_iterator crbegin()const { return const_reverse_iterator(finish_); }
reverse_iterator rend() { return reverse_iterator(start_); }
const_reverse_iterator crend()const { return const_reverse_iterator(start_); }
//与容量相关
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();
//访问元素相关
reference operator[](const difference_type i) { return *(begin() + i); }
const_reference operator[](const difference_type i)const { return *(cbegin() + i); }
reference front() { return *(begin()); }
reference back() { return *(end() - 1); }
pointer data() { return start_; }
//修改容器相关的操作
//清空容器,销毁容器中的所有对象并使容器的size为0,但不回收容器已有的空间
void clear();
void swap(vector& v);
void push_back(const value_type& value);
void pop_back();
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(iterator position, InputIterator first, InputIterator last);
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
//容器的空间配置器相关
Alloc get_allocator() { return dataAllocator; }
private:
void destroyAndDeallocateAll();
void allocateAndFillN(const size_type n, const value_type& value);
template<class InputIterator>
void allocateAndCopy(InputIterator first, InputIterator last);
template<class InputIterator>
void vector_aux(InputIterator first, InputIterator last, std::false_type);
template<class Integer>
void vector_aux(Integer n, const value_type& value, std::true_type);
template<class InputIterator>
void insert_aux(iterator position, InputIterator first, InputIterator last, std::false_type);
template<class Integer>
void insert_aux(iterator position, Integer n, const value_type& value, std::true_type);
template<class InputIterator>
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;
public:
template<class T, class Alloc>
friend bool operator == (const vector<T, Alloc>& v1, const vector<T, Alloc>& v2);
template<class T, class Alloc>
friend bool operator != (const vector<T, Alloc>& v1, const vector<T, Alloc>& v2);
};// end of class vector
}
#include "Detail\Vector.impl.h"
#endif
vector.impl.h(实现)
#pragma once
#ifndef _VECTOR_IMPL_H_
#define _VECTOR_IMPL_H_
namespace mySTL {
template<class T, class Alloc>
vector<T, Alloc>::~vector() {
destroyAndDeallocateAll();
}
template<class T, class Alloc>
vector<T, Alloc>::vector(const size_type n) {
allocateAndFillN(n, value_type());
}
template<class T, class Alloc>
vector<T, Alloc>::vector(const size_type n, const value_type& value) {
allocateAndFillN(n, value);
}
template<class T, class Alloc>
template<class InputIterator>
vector<T, Alloc>::vector(InputIterator first, InputIterator last) {
//处理指针和数字间的区别的函数
vector_aux(first, last, typename std::is_integral<InputIterator>::type());
}
template<class T, class Alloc>
vector<T, Alloc>::vector(const vector& v) {//浅拷贝使用常引用
allocateAndCopy(v.start_, v.finish_);
}
template<class T, class Alloc>
vector<T, Alloc>::vector(vector&& v) {//深拷贝右值引用
start_ = v.start_;
finish_ = v.finish_;
endOfStorage_ = v.endOfStorage_;
v.start_ = v.finish_ = v.endOfStorage_ = 0;//销毁复制对象(否则销毁原对象时会销毁复制体)
}
template<class T, class Alloc>
vector<T, Alloc>& vector<T, Alloc>::operator = (const vector& v) {
if (this != &v) {
allocateAndCopy(v.start_, v.finish_);
}
return *this;
}
template<class T, class Alloc>
vector<T, Alloc>& vector<T, Alloc>::operator = (vector&& v) {
if (this != &v) {
destroyAndDeallocateAll();//首先销毁本身,才好赋值
start_ = v.start_;
finish_ = v.finish_;
endOfStorage_ = v.endOfStorage_;
v.start_ = v.finish_ = v.endOfStorage_ = 0;
}
return *this;
}
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_ = TinySTL::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 = mySTL::uninitialized_copy(begin(), end(), newStart);
//调用底层的复制函数(这时候就不需要考虑pod型等等了)
destroyAndDeallocateAll();
start_ = newStart;
finish_ = newFinish;
endOfStorage_ = start_ + n;
}
template<class T, class Alloc>
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(iterator first, iterator last) {
//尾部残留对象数
difference_type lenOfTail = end() - last;
//删去的对象数目
difference_type lenOfRemoved = last - first;
finish_ = finish_ - lenOfRemoved;//有效范围缩减
for (; lenOfTail != 0; --lenOfTail) {
auto temp = (last - lenOfRemoved);
*temp = *(last++);//逐个移动赋值
}
return (first);
}
template<class T, class Alloc>
template<class InputIterator>
void vector<T, Alloc>::reallocateAndCopy(iterator position, InputIterator first, InputIterator last) {
difference_type newCapacity = getNewCapacity(last - first);
T *newStart = dataAllocator::allocate(newCapacity);
T *newEndOfStorage = newStart + newCapacity;
T *newFinish = mySTL::uninitialized_copy(begin(), position, newStart);
newFinish = mySTL::uninitialized_copy(first, last, newFinish);
newFinish = mySTL::uninitialized_copy(position, end(), newFinish);
destroyAndDeallocateAll();//删除
start_ = newStart;
finish_ = newFinish;
endOfStorage_ = newEndOfStorage;
}
template<class T, class Alloc>
//扩充大小并把扩充部分赋n
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 = mySTL::uninitialized_copy(begin(), position, newStart);//将原本的部分复制并存放在新地址
newFinish = mySTL::uninitialized_fill_n(newFinish, n, val);//赋值需要覆盖的一部分并后移尾部指示
newFinish = mySTL::uninitialized_copy(position, end(), newFinish);//完成后半部分的赋值
//删除原vector,载入新vector的数据
destroyAndDeallocateAll();
start_ = newStart;
finish_ = newFinish;
endOfStorage_ = newEndOfStorage;
}
template<class T, class Alloc>
template<class InputIterator>
void vector<T, Alloc>::insert_aux(iterator position,
InputIterator first,
InputIterator last,
std::false_type) {//可以运用内存复制的部分
difference_type locationLeft = endOfStorage_ - finish_; // the size of left storage
difference_type locationNeed = distance(first, last);//last - first;
if (locationLeft >= locationNeed) {//位置足够直接赋值
if (finish_ - position > locationNeed) {//插入部分只需要覆盖不需要插入
mySTL::uninitialized_copy(finish_ - locationNeed, finish_, finish_);
std::copy_backward(position, finish_ - locationNeed, finish_);
std::copy(first, last, position);
}
else {//插入部分既需要覆盖又需要插入
iterator temp = mySTL::uninitialized_copy(first + (finish_ - position), last, finish_);
mySTL::uninitialized_copy(position, finish_, temp);
std::copy(first, first + (finish_ - position), position);
}
finish_ += locationNeed;//移动尾节点
}
else {//不足需要reallocate
reallocateAndCopy(position, first, last);
}
}
template<class T, class Alloc>
template<class Integer>
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;
if (locationLeft >= locationNeed) {
auto tempPtr = end() - 1;
for (; tempPtr - position >= 0; --tempPtr) {//move the [position, finish_) back
//*(tempPtr + locationNeed) = *tempPtr;//bug
construct(tempPtr + locationNeed, *tempPtr);
}
mySTL::uninitialized_fill_n(position, n, value);
finish_ += locationNeed;
}
else {
reallocateAndFillN(position, n, value);
}
}
template<class T, class Alloc>
template<class InputIterator>
//不同的参数调用不同的insert
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(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>//没有个数的情况补1
typename vector<T, Alloc>::iterator vector<T, Alloc>::insert(iterator position, const value_type& val) {
const auto index = position - begin();
insert(position, 1, val);
return begin() + index;
}
template<class T, class Alloc>
void vector<T, Alloc>::push_back(const value_type& value) {
insert(end(), value);
}
//***********逻辑比较操作相关*******************这部分和其他的大同小异
template<class T, class Alloc>
bool vector<T, Alloc>::operator == (const vector& v)const {
if (size() != v.size()) {
return false;
}
else {
auto ptr1 = start_;
auto ptr2 = v.start_;
for (; ptr1 != finish_ && ptr2 != v.finish_; ++ptr1, ++ptr2) {
if (*ptr1 != *ptr2)
return false;
}
return true;
}
}
template<class T, class Alloc>
bool vector<T, Alloc>::operator != (const vector& v)const {
return !(*this == v);
}
template<class T, class Alloc>
bool operator == (const vector<T, Alloc>& v1, const vector<T, Alloc>& v2) {
//return v1 == v2;
return v1.operator==(v2);
}
template<class T, class Alloc>
bool operator != (const vector<T, Alloc>& v1, const vector<T, Alloc>& v2) {
return !(v1 == v2);
}
template<class T, class Alloc>
void vector<T, Alloc>::shrink_to_fit() {//收缩到合适大小
//dataAllocator::deallocate(finish_, endOfStorage_ - finish_);
//endOfStorage_ = finish_;
T* t = (T*)dataAllocator::allocate(size());
finish_ = TinySTL::uninitialized_copy(start_, finish_, t);
dataAllocator::deallocate(start_, capacity());
start_ = t;
endOfStorage_ = finish_;
}
template<class T, class Alloc>
void vector<T, Alloc>::clear() {//清空
dataAllocator::destroy(start_, finish_);
finish_ = start_;
}
template<class T, class Alloc>
void vector<T, Alloc>::swap(vector& v) {//交换
if (this != &v) {
TinySTL::swap(start_, v.start_);
TinySTL::swap(finish_, v.finish_);
TinySTL::swap(endOfStorage_, v.endOfStorage_);
}
}
template<class T, class Alloc>
void vector<T, Alloc>::pop_back() {
--finish_;
dataAllocator::destroy(finish_);
}
template<class T, class Alloc>//逐一析构
void vector<T, Alloc>::destroyAndDeallocateAll() {
if (capacity() != 0) {
dataAllocator::destroy(start_, finish_);
dataAllocator::deallocate(start_, capacity());
}
}
template<class T, class Alloc>//分配内存并赋值
void vector<T, Alloc>::allocateAndFillN(const size_type n, const value_type& value) {
start_ = dataAllocator::allocate(n);
mySTL::uninitialized_fill_n(start_, n, value);
finish_ = endOfStorage_ = start_ + n;
}
template<class T, class Alloc>//分配内存并复制
template<class InputIterator>
void vector<T, Alloc>::allocateAndCopy(InputIterator first, InputIterator last) {
start_ = dataAllocator::allocate(last - first);
finish_ = mySTL::uninitialized_copy(first, last, start_);
endOfStorage_ = finish_;
}
template<class T, class Alloc>//非构造类型的区间插入若是插入空vector可以调用
template<class InputIterator>
void vector<T, Alloc>::vector_aux(InputIterator first, InputIterator last, std::false_type) {
allocateAndCopy(first, last);
}
template<class T, class Alloc>
template<class Integer>
void vector<T, Alloc>::vector_aux(Integer n, const value_type& value, std::true_type) {
allocateAndFillN(n, value);
}
template<class T, class Alloc>
typename vector<T, Alloc>::size_type vector<T, Alloc>::getNewCapacity(size_type len)const {
size_type oldCapacity = endOfStorage_ - start_;
auto res = mySTL::max(oldCapacity, len);
size_type newCapacity = (oldCapacity != 0 ? (oldCapacity + res) : len);
return newCapacity;
}
}
#endif