This commit is contained in:
邹晓航
2014-10-22 16:53:32 +08:00
parent 3e1b456bed
commit 6b90b823f5

View File

@@ -39,9 +39,12 @@ namespace TinySTL{
dq_iter& operator ++(){ dq_iter& operator ++(){
if (cur_ != getBuckTail(mapIndex_))//+1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬһ<CDAC><D2BB>Ͱ<EFBFBD><CDB0> if (cur_ != getBuckTail(mapIndex_))//+1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬһ<CDAC><D2BB>Ͱ<EFBFBD><CDB0>
++cur_; ++cur_;
else{ else if(mapIndex_ + 1 < container_->mapSize_){//+1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬһ<CDAC><D2BB>map<61><70>
++mapIndex_; ++mapIndex_;
cur_ = getBuckHead(mapIndex_); cur_ = getBuckHead(mapIndex_);
}else{//+1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>map
mapIndex_ = container_->mapSize_ - 1;
cur_ = container_->map_[mapIndex_] + getBuckSize();//ָ<><D6B8>map_[mapSize_-1]<5D><>β<EFBFBD><CEB2><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>λ<EFBFBD><CEBB>
} }
return *this; return *this;
} }
@@ -53,9 +56,12 @@ namespace TinySTL{
dq_iter& operator --(){ dq_iter& operator --(){
if (cur_ != getBuckHead(mapIndex_))//<2F><>ǰ<EFBFBD><C7B0>ָ<EFBFBD><D6B8>Ͱͷ if (cur_ != getBuckHead(mapIndex_))//<2F><>ǰ<EFBFBD><C7B0>ָ<EFBFBD><D6B8>Ͱͷ
--cur_; --cur_;
else{ else if(mapIndex_ - 1 >= 0){//-1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>map<61><70><EFBFBD><EFBFBD>
--mapIndex_; --mapIndex_;
cur_ = getBuckTail(mapIndex_); cur_ = getBuckTail(mapIndex_);
}else{
mapIndex_ = 0;
cur_ = container_->map_[mapIndex_];//ָ<><D6B8>map_[0]<5D><>ͷ
} }
return *this; return *this;
} }
@@ -73,11 +79,14 @@ namespace TinySTL{
} }
private: private:
T *getBuckTail(size_t mapIndex)const{ T *getBuckTail(size_t mapIndex)const{
return container_[mapIndex] + container_->getBuckSize() - 1; return container_->map_[mapIndex] + (container_->getBuckSize() - 1);
} }
T *getBuckHead(size_t mapIndex)const{ T *getBuckHead(size_t mapIndex)const{
return container_->map_[mapIndex]; return container_->map_[mapIndex];
} }
size_t getBuckSize()const{
return container_->getBuckSize();
}
public: public:
template<class T> template<class T>
friend dq_iter<T> operator + (const dq_iter<T>& it, typename dq_iter<T>::difference_type n); friend dq_iter<T> operator + (const dq_iter<T>& it, typename dq_iter<T>::difference_type n);
@@ -99,8 +108,10 @@ namespace TinySTL{
} }
else{ else{
n = n - m; n = n - m;
res.mapIndex_ += n / res.container_->getBuckSize(); res.mapIndex_ += (n / it.getBuckSize() + 1);
res.cur_ = res.getBuckHead(res.mapIndex_) + n % res.container_->getBuckSize(); auto p = res.getBuckHead(res.mapIndex_);
auto x = n % it.getBuckSize() - 1;
res.cur_ = p + x;
} }
return res; return res;
} }
@@ -116,8 +127,8 @@ namespace TinySTL{
res.cur_ -= n; res.cur_ -= n;
else{ else{
n = n - m; n = n - m;
res.mapIndex_ -= n / res.container_->getBuckSize(); res.mapIndex_ -= (n / res.getBuckSize() + 1);
res.cur_ = res.getBuckTail(res.mapIndex_) - n % res.container_->getBuckSize(); res.cur_ = res.getBuckTail(res.mapIndex_) - (n % res.getBuckSize() - 1);
} }
return res; return res;
} }
@@ -127,8 +138,8 @@ namespace TinySTL{
} }
template<class T> template<class T>
typename dq_iter<T>::difference_type operator - (const dq_iter<T>& it1, const dq_iter<T>& it2){ typename dq_iter<T>::difference_type operator - (const dq_iter<T>& it1, const dq_iter<T>& it2){
return typename dq_iter<T>::difference_type(it1.container_->getBuckSize()) * (it1.mapIndex_ - it2.mapIndex_ - 1) return typename dq_iter<T>::difference_type(it1.getBuckSize()) * (it1.mapIndex_ - it2.mapIndex_ - 1)
+ (it1.cur_ - it1.getBuckHead(it1.mapIndex_)) + (it2.getBuckTail(it2.mapIndex_) - it2.cur_); + (it1.cur_ - it1.getBuckHead(it1.mapIndex_)) + (it2.getBuckTail(it2.mapIndex_) - it2.cur_) + 1;
} }
} }
@@ -156,14 +167,12 @@ namespace TinySTL{
T **map_; T **map_;
public: public:
deque(); deque();
explicit deque(size_type n, const value_type& val = value_type());
template <class InputIterator>
deque(InputIterator first, InputIterator last);
deque(const deque& x); deque(const deque& x);
~deque(){ ~deque(){
for (int i = 0; i != mapSize_; ++i) for (int i = 0; i != mapSize_; ++i)
dataAllocator::deallocate(map_[i], getBuckSize()); if (!map_[i])
dataAllocator::deallocate(map_[i], getBuckSize());
delete[] map_; delete[] map_;
} }
@@ -173,42 +182,54 @@ namespace TinySTL{
iterator begin(){ return beg_; } iterator begin(){ return beg_; }
const_iterator begin() const{ return beg_; } const_iterator begin() const{ return beg_; }
iterator end(){ return end_; } iterator end(){ return end_; }
const_iterator end() const{return end_} const_iterator end() const{ return end_; }
const_iterator cbegin() const{ return beg_; } const_iterator cbegin() const{ return beg_; }
const_iterator cend() const{ return end_; } const_iterator cend() const{ return end_; }
size_type size() const{ return end() - begin(); } size_type size() const{ return end() - begin(); }
bool empty() const{ return begin() == end(); } bool empty() const{ return begin() == end(); }
reference operator[] (size_type n); reference operator[] (size_type n){ return *(begin() + n); }
const_reference operator[] (size_type n) const; const_reference operator[] (size_type n) const{ return *(cbegin() + n); }
reference front(); reference front(){ return *begin(); }
const_reference front() const; const_reference front() const{ return *cbegin(); }
reference back(); reference back(){ return *(end() - 1); }
const_reference back() const; const_reference back() const{ return *(cend() - 1); }
void push_back(const value_type& val); void push_back(const value_type& val);
void push_back(value_type&& val);
void push_front(const value_type& val); void push_front(const value_type& val);
void push_front(value_type&& val);
void pop_back(); void pop_back();
void pop_front(); void pop_front();
void swap(deque& x); void swap(deque& x);
void clear(); void clear();
private: private:
T *getABuck(){ T *getANewBuck(){
return dataAllocator::allocate(getBuckSize); return dataAllocator::allocate(getBuckSize());
} }
T** getANewMap(const size_t size){ T** getANewMap(const size_t size){
mapSize_ = size; mapSize_ = size;
return new T*[mapSize_](0); return new T*[mapSize_];
}
size_t getNewMapSize(const size_t size){
return (size == 0 ? 2 : size * 1.5);
} }
/*size_t getNewMapSize(const size_t size){
return (size == 0 ? 1 : size * 1.5);
}*/
size_t getBuckSize()const{ size_t getBuckSize()const{
return (size_t)EBucksSize::BUCKSIZE; return (size_t)EBucksSize::BUCKSIZE;
} }
void init(){
mapSize_ = 2;
map_ = getANewMap(mapSize_);
map_[mapSize_ - 1] = getANewBuck();
beg_.container_ = end_.container_ = this;
beg_.mapIndex_ = end_.mapIndex_ = mapSize_ - 1;
beg_.cur_ = end_.cur_ = map_[mapSize_ - 1];
}
bool back_full()const{
return map_[mapSize_ - 1] && (map_[mapSize_ - 1] + getBuckSize()) == end().cur_;
}
bool front_full()const{
return map_[0] && map_[0] == begin().cur_;
}
public: public:
template <class T, class Alloc> template <class T, class Alloc>
friend bool operator== (const deque<T, Alloc>& lhs, const deque<T, Alloc>& rhs); friend bool operator== (const deque<T, Alloc>& lhs, const deque<T, Alloc>& rhs);
@@ -229,15 +250,32 @@ namespace TinySTL{
template<class T, class Alloc> template<class T, class Alloc>
deque<T, Alloc>::deque() deque<T, Alloc>::deque()
:mapSize_(0), map_(0){} :mapSize_(0), map_(0){}
/*template<class T, class Alloc>
deque<T, Alloc>::deque(const deque& x){
}*/
template<class T, class Alloc> template<class T, class Alloc>
deque<T, Alloc>::deque(size_type n, const value_type& val = value_type()){ void deque<T, Alloc>::push_back(const value_type& val){
auto nMap = n / getBuckSize() + 1 + 2; if (empty()){
map_ = getANewMap(nMap); init();
}
else if (back_full()){
return;
}
*end_ = val;
++end_;
}
template<class T, class Alloc>
void deque<T, Alloc>::push_front(const value_type& val){
if (empty()){
init();
map_[0] = getANewBuck();
}
else if (front_full()){
return;
}
--beg_;
*beg_ = val;
} }
//template<class T, class Alloc>
//template <class InputIterator>
//deque<T, Alloc>::deque(InputIterator first, InputIterator last);
//template<class T, class Alloc>
//deque<T, Alloc>::deque(const deque& x);
} }
#endif #endif