bug fix,添加CB的size_成员
This commit is contained in:
@@ -5,13 +5,15 @@
|
|||||||
#include "Iterator.h"
|
#include "Iterator.h"
|
||||||
#include "UninitializedFunctions.h"
|
#include "UninitializedFunctions.h"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
namespace TinySTL{
|
namespace TinySTL{
|
||||||
template<class T, size_t N, class Alloc>
|
template<class T, size_t N, class Alloc>
|
||||||
class circular_buffer;
|
class circular_buffer;
|
||||||
namespace{
|
namespace{
|
||||||
//the iterator of circular buffer
|
//the iterator of circular buffer
|
||||||
template<class T, size_t N, class Alloc = allocator<T>>
|
template<class T, size_t N, class Alloc = allocator<T>>
|
||||||
class cb_iter:iterator<bidirectional_iterator_tag, T>{
|
class cb_iter:iterator<bidirectional_iterator_tag, T>{//bidirectional iterator
|
||||||
private:
|
private:
|
||||||
typedef circular_buffer<T, N, Alloc> cb;
|
typedef circular_buffer<T, N, Alloc> cb;
|
||||||
typedef cb *cbPtr;
|
typedef cb *cbPtr;
|
||||||
@@ -25,9 +27,11 @@ namespace TinySTL{
|
|||||||
ptr_(ptr), index_(ptr - container->start_), container_(container){}
|
ptr_(ptr), index_(ptr - container->start_), container_(container){}
|
||||||
public:
|
public:
|
||||||
operator T*(){ return ptr_; }
|
operator T*(){ return ptr_; }
|
||||||
|
T& operator *(){ return *ptr_; }
|
||||||
|
T *operator ->(){ return &(operator*()); }
|
||||||
|
|
||||||
cb_iter& operator ++(){
|
cb_iter& operator ++(){
|
||||||
setIndex_(++index_ % N);
|
setIndex_(nextIndex(index_));
|
||||||
setPtr_(container_->start_ + index_);
|
setPtr_(container_->start_ + index_);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -37,9 +41,7 @@ namespace TinySTL{
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
cb_iter& operator --(){
|
cb_iter& operator --(){
|
||||||
int index = index_ - 1;
|
setIndex_(prevIndex(index_));
|
||||||
index = (index == -1 ? index + N : index);
|
|
||||||
setIndex_(index);
|
|
||||||
setPtr_(container_->start_ + index_);
|
setPtr_(container_->start_ + index_);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -48,15 +50,37 @@ namespace TinySTL{
|
|||||||
--(*this);
|
--(*this);
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
bool operator == (const cb_iter& it)const{
|
||||||
|
return (container_ == it.container_) &&
|
||||||
|
(ptr_ == it.ptr_) && (index_ == it.index_);
|
||||||
|
}
|
||||||
|
bool operator != (const cb_iter& it)const{
|
||||||
|
return !((*this) == it);
|
||||||
|
}
|
||||||
|
/*bool operator <= (const cb_iter& it)const{
|
||||||
|
cb_iter cur = *this;
|
||||||
|
for (; cur != container_->last(); ++cur){
|
||||||
|
if (cur == it) return true;
|
||||||
|
}
|
||||||
|
return (cur == it ? true : false);
|
||||||
|
}*/
|
||||||
private:
|
private:
|
||||||
void setIndex_(int index){ index_ = index; }
|
void setIndex_(int index){ index_ = index; }
|
||||||
void setPtr_(T *ptr){ ptr_ = ptr; }
|
void setPtr_(T *ptr){ ptr_ = ptr; }
|
||||||
|
int nextIndex(int index){ return (++index) % N; }
|
||||||
|
int prevIndex(int index){
|
||||||
|
--index;
|
||||||
|
index = (index == -1 ? index + N : index);
|
||||||
|
return index;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}//end of anonymous namespace
|
}//end of anonymous namespace
|
||||||
|
|
||||||
//circular buffer
|
//circular buffer
|
||||||
template<class T, size_t N, class Alloc = allocator<T>>
|
template<class T, size_t N, class Alloc = allocator<T>>
|
||||||
class circular_buffer{
|
class circular_buffer{
|
||||||
|
template<class T, size_t N, class Alloc>
|
||||||
|
friend class cb_iter;
|
||||||
public:
|
public:
|
||||||
typedef T value_type;
|
typedef T value_type;
|
||||||
typedef cb_iter<T, N> iterator;
|
typedef cb_iter<T, N> iterator;
|
||||||
@@ -67,39 +91,52 @@ namespace TinySTL{
|
|||||||
private:
|
private:
|
||||||
T *start_;
|
T *start_;
|
||||||
T *finish_;
|
T *finish_;
|
||||||
int indexOfHead;
|
int indexOfHead;//the first position
|
||||||
int indexOfTail;//the last position
|
int indexOfTail;//the last position
|
||||||
|
size_type size_;
|
||||||
|
|
||||||
typedef Alloc dataAllocator;
|
typedef Alloc dataAllocator;
|
||||||
public:
|
public:
|
||||||
circular_buffer() :
|
circular_buffer() :
|
||||||
start_(0), finish_(0), indexOfHead(0), indexOfTail(0){}
|
start_(0), finish_(0), indexOfHead(0), indexOfTail(0), size_(0){}
|
||||||
explicit circular_buffer(const int& n, const value_type& val = value_type());
|
explicit circular_buffer(const int& n, const value_type& val = value_type());
|
||||||
template<class InputIterator>
|
template<class InputIterator>
|
||||||
circular_buffer(InputIterator first, InputIterator last);
|
circular_buffer(InputIterator first, InputIterator last);
|
||||||
|
~circular_buffer(){
|
||||||
|
/*dataAllocator::destroy(start_, finish_);
|
||||||
|
dataAllocator::deallocate(start_, finish_);*/
|
||||||
|
}
|
||||||
|
|
||||||
bool full(){ return ((indexOfTail + 1) % N) == indexOfHead; }
|
bool full(){ return size_ == N; }
|
||||||
bool empty(){ return indexOfHead == indexOfTail; }
|
bool empty(){ return size_ == 0; }
|
||||||
|
difference_type capacity(){ return finish_ - start_; }
|
||||||
|
size_type size(){ return size_; }
|
||||||
|
|
||||||
//just for test
|
//just for test
|
||||||
T *begin(){ return start_; }
|
/*T *begin(){ return start_; }
|
||||||
T *end(){ return finish_; }
|
T *end(){ return finish_; }*/
|
||||||
|
iterator first(){ return iterator(start_ + indexOfHead, this); }
|
||||||
|
iterator last(){ return iterator(start_ + indexOfTail, this); }
|
||||||
|
|
||||||
|
reference operator [](size_type i){ return *(start_ + i); }
|
||||||
private:
|
private:
|
||||||
void allocateAndFillN(const int& n, const value_type& val){
|
void allocateAndFillN(const int& n, const value_type& val){//only for ctor
|
||||||
start_ = dataAllocator::allocate(N);
|
start_ = dataAllocator::allocate(N);
|
||||||
finish_ = start_ + N;
|
finish_ = start_ + N;
|
||||||
indexOfHead = 0;
|
indexOfHead = 0;
|
||||||
if (N <= n){
|
if (N <= n){
|
||||||
finish_ = TinySTL::uninitialized_fill_n(start_, N, val);
|
finish_ = TinySTL::uninitialized_fill_n(start_, N, val);
|
||||||
indexOfTail = N - 1;
|
indexOfTail = N - 1;
|
||||||
|
size_ = N;
|
||||||
}else{//N > n
|
}else{//N > n
|
||||||
finish_ = TinySTL::uninitialized_fill_n(start_, n, val);
|
finish_ = TinySTL::uninitialized_fill_n(start_, n, val);
|
||||||
finish_ = TinySTL::uninitialized_fill_n(finish_, N - n, value_type());
|
finish_ = TinySTL::uninitialized_fill_n(finish_, N - n, value_type());
|
||||||
indexOfTail = n - 1;
|
indexOfTail = n - 1;
|
||||||
|
size_ = n;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
template<class InputIterator>
|
template<class InputIterator>
|
||||||
void allocateAndCopy(InputIterator first, InputIterator last){
|
void allocateAndCopy(InputIterator first, InputIterator last){//only for ctor
|
||||||
int n = last - first;
|
int n = last - first;
|
||||||
start_ = dataAllocator::allocate(N);
|
start_ = dataAllocator::allocate(N);
|
||||||
indexOfHead = 0;
|
indexOfHead = 0;
|
||||||
@@ -112,14 +149,17 @@ namespace TinySTL{
|
|||||||
indexOfTail = n - 1;
|
indexOfTail = n - 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
int nextIndex(int index){ return ++index % N; }
|
||||||
|
};//end of circular buffer
|
||||||
template<class T, size_t N, class Alloc>
|
template<class T, size_t N, class Alloc>
|
||||||
circular_buffer<T, N, Alloc>::circular_buffer(const int& n, const value_type& val = value_type()){
|
circular_buffer<T, N, Alloc>::circular_buffer(const int& n, const value_type& val = value_type()){
|
||||||
|
assert(n != 0);
|
||||||
allocateAndFillN(n, val);
|
allocateAndFillN(n, val);
|
||||||
}
|
}
|
||||||
template<class T, size_t N, class Alloc>
|
template<class T, size_t N, class Alloc>
|
||||||
template<class InputIterator>
|
template<class InputIterator>
|
||||||
circular_buffer<T, N, Alloc>::circular_buffer(InputIterator first, InputIterator last){
|
circular_buffer<T, N, Alloc>::circular_buffer(InputIterator first, InputIterator last){
|
||||||
|
assert((last - first) >= 2);
|
||||||
allocateAndCopy(first, last);
|
allocateAndCopy(first, last);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user