circular buffer complete
This commit is contained in:
@@ -57,13 +57,26 @@ namespace TinySTL{
|
||||
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;
|
||||
//wish to do like this, but buggy
|
||||
//for (auto it = cb.first(); it <= cb.last(); ++it)
|
||||
// { cout << *it << endl; }
|
||||
bool operator <= (const cb_iter& it)const{//fuck
|
||||
if (*this == it) return true;
|
||||
auto indexOfThis = ptr_ - container_->start_;
|
||||
auto indexOfIt = it.ptr_ - it.container_->start_;
|
||||
|
||||
if (indexOfThis < indexOfIt){
|
||||
if (container_->indexOfHead < indexOfThis || container_->indexOfHead > indexOfIt)
|
||||
return true;
|
||||
else if (container_->indexOfHead < indexOfIt && container_->indexOfHead > indexOfThis)
|
||||
return false;
|
||||
}else{//indexOfThis > indexOfIt
|
||||
if (container_->indexOfHead < indexOfIt || container_->indexOfHead > indexOfThis)
|
||||
return false;
|
||||
else if (container_->indexOfHead < indexOfThis && container_->indexOfHead > indexOfIt)
|
||||
return true;
|
||||
}
|
||||
return (cur == it ? true : false);
|
||||
}*/
|
||||
}
|
||||
private:
|
||||
void setIndex_(int index){ index_ = index; }
|
||||
void setPtr_(T *ptr){ ptr_ = ptr; }
|
||||
@@ -82,12 +95,12 @@ namespace TinySTL{
|
||||
template<class T, size_t N, class Alloc>
|
||||
friend class cb_iter;
|
||||
public:
|
||||
typedef T value_type;
|
||||
typedef T value_type;
|
||||
typedef cb_iter<T, N> iterator;
|
||||
typedef iterator pointer;
|
||||
typedef T& reference;
|
||||
typedef int size_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
typedef iterator pointer;
|
||||
typedef T& reference;
|
||||
typedef int size_type;
|
||||
typedef ptrdiff_t difference_type;
|
||||
private:
|
||||
T *start_;
|
||||
T *finish_;
|
||||
@@ -111,14 +124,23 @@ namespace TinySTL{
|
||||
bool empty(){ return size_ == 0; }
|
||||
difference_type capacity(){ return finish_ - start_; }
|
||||
size_type size(){ return size_; }
|
||||
void clear(){
|
||||
for (; !empty(); indexOfHead = nextIndex(indexOfHead), --size_){
|
||||
dataAllocator::destroy(start_ + indexOfHead);
|
||||
}
|
||||
indexOfHead = indexOfTail = 0;
|
||||
}
|
||||
|
||||
//just for test
|
||||
/*T *begin(){ return start_; }
|
||||
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); }
|
||||
reference front(){ return *(start_ + indexOfHead); }
|
||||
reference back(){ return *(start_ + indexOfTail); }
|
||||
void push(const T& val);
|
||||
void pop();
|
||||
|
||||
Alloc get_allocator(){ return dataAllocator; }
|
||||
private:
|
||||
void allocateAndFillN(const int& n, const value_type& val){//only for ctor
|
||||
start_ = dataAllocator::allocate(N);
|
||||
@@ -143,14 +165,18 @@ namespace TinySTL{
|
||||
if (N <= n){
|
||||
finish_ = TinySTL::uninitialized_copy(first, first + N, start_);
|
||||
indexOfTail = N - 1;
|
||||
size_ = N;
|
||||
}else{//N > n
|
||||
finish_ = TinySTL::uninitialized_copy(first, last, start_);
|
||||
finish_ = TinySTL::uninitialized_fill_n(finish_, N - n, value_type());
|
||||
indexOfTail = n - 1;
|
||||
size_ = n;
|
||||
}
|
||||
}
|
||||
int nextIndex(int index){ return ++index % N; }
|
||||
};//end of circular buffer
|
||||
|
||||
//**********<2A><><EFBFBD>죬<EFBFBD><ECA3AC><EFBFBD>ƣ<EFBFBD><C6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*****************
|
||||
template<class T, size_t N, class Alloc>
|
||||
circular_buffer<T, N, Alloc>::circular_buffer(const int& n, const value_type& val = value_type()){
|
||||
assert(n != 0);
|
||||
@@ -159,9 +185,25 @@ namespace TinySTL{
|
||||
template<class T, size_t N, class Alloc>
|
||||
template<class InputIterator>
|
||||
circular_buffer<T, N, Alloc>::circular_buffer(InputIterator first, InputIterator last){
|
||||
assert((last - first) >= 2);
|
||||
allocateAndCopy(first, last);
|
||||
}
|
||||
//************<2A><><EFBFBD>룬ɾ<EBA3AC><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>***********************
|
||||
template<class T, size_t N, class Alloc>
|
||||
void circular_buffer<T, N, Alloc>::push(const T& val){
|
||||
if (full())
|
||||
throw;
|
||||
indexOfTail = nextIndex(indexOfTail);
|
||||
dataAllocator::construct(start_ + indexOfTail, val);
|
||||
++size_;
|
||||
}
|
||||
template<class T, size_t N, class Alloc>
|
||||
void circular_buffer<T, N, Alloc>::pop(){
|
||||
if (empty())
|
||||
throw;
|
||||
dataAllocator::destroy(start_ + indexOfHead);
|
||||
indexOfHead = nextIndex(indexOfHead);
|
||||
--size_;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user