From 8d83cf06863c2f2ddfac1f80f2fc17267743b93e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Wed, 24 Sep 2014 16:37:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=8D=E5=88=B6=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E8=AF=AD=E4=B9=89=E5=92=8C=E7=9B=B8=E7=AD=89=E6=93=8D?= =?UTF-8?q?=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/CircularBuffer.h | 85 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/TinySTL/CircularBuffer.h b/TinySTL/CircularBuffer.h index da5cf06..a3a6630 100644 --- a/TinySTL/CircularBuffer.h +++ b/TinySTL/CircularBuffer.h @@ -57,9 +57,16 @@ namespace TinySTL{ bool operator != (const cb_iter& it)const{ return !((*this) == it); } + //fuck + //fuck + //fuck + //fuck //wish to do like this, but buggy //for (auto it = cb.first(); it <= cb.last(); ++it) // { cout << *it << endl; } + //fuck + //fuck + //fuck bool operator <= (const cb_iter& it)const{//fuck if (*this == it) return true; auto indexOfThis = ptr_ - container_->start_; @@ -115,9 +122,13 @@ namespace TinySTL{ explicit circular_buffer(const int& n, const value_type& val = value_type()); template circular_buffer(InputIterator first, InputIterator last); + circular_buffer(const circular_buffer& cb); + circular_buffer& operator = (const circular_buffer& cb); + circular_buffer& operator = (circular_buffer&& cb); + circular_buffer(circular_buffer&& cb); ~circular_buffer(){ - /*dataAllocator::destroy(start_, finish_); - dataAllocator::deallocate(start_, finish_);*/ + clear(); + dataAllocator::deallocate(start_, size_); } bool full(){ return size_ == N; } @@ -140,6 +151,17 @@ namespace TinySTL{ void push(const T& val); void pop(); + bool operator == (circular_buffer& cb){ + auto it1 = first(), it2 = cb.first(); + for (;it1 != last() && it2 != cb.last(); ++it1, ++it2){ + if (*it1 != *it2) return false; + } + return (it1 == last()) && (it2 == cb.last()) && (*(last()) == *(cb.last())); + } + bool operator != (circular_buffer& cb){ + return !(*this == cb); + } + Alloc get_allocator(){ return dataAllocator; } private: void allocateAndFillN(const int& n, const value_type& val){//only for ctor @@ -174,6 +196,28 @@ namespace TinySTL{ } } int nextIndex(int index){ return ++index % N; } + void copyAllMembers(const circular_buffer& cb){ + start_ = cb.start_; + finish_ = cb.finish_; + indexOfHead = cb.indexOfHead; + indexOfTail = cb.indexOfTail; + size_ = cb.size_; + } + void zeroCircular(circular_buffer& cb){ + cb.start_ = cb.finish_ = 0; + cb.indexOfHead = cb.indexOfTail = cb.size_ = 0; + } + void clone(const circular_buffer& cb){ + start_ = dataAllocator::allocate(N); + finish_ = start_ + N; + size_ = N; + indexOfHead = cb.indexOfHead; + indexOfTail = cb.indexOfTail; + TinySTL::uninitialized_copy(cb.start_, cb.finish_, start_); + } + public: + template + friend std::ostream& operator <<(std::ostream& os, circular_buffer& cb); };//end of circular buffer //**********构造,复制,析构相关***************** @@ -187,12 +231,33 @@ namespace TinySTL{ circular_buffer::circular_buffer(InputIterator first, InputIterator last){ allocateAndCopy(first, last); } + template + circular_buffer::circular_buffer(const circular_buffer& cb){ + clone(cb); + } + template + circular_buffer::circular_buffer(circular_buffer&& cb){ + copyAllMembers(cb); + zeroCircular(cb); + } + template + circular_buffer& circular_buffer::operator = (const circular_buffer& cb){ + if (this != &cb){ + clone(cb); + } + return *this; + } + template + circular_buffer& circular_buffer::operator = (circular_buffer&& cb){ + if (*this != cb){ + copyAllMembers(cb); + zeroCircular(cb); + } + return *this; + } //************插入,删除相关*********************** template void circular_buffer::push(const T& val){ - /*if (full()) - throw; - */ if (full()){ indexOfTail = nextIndex(indexOfTail); dataAllocator::construct(start_ + indexOfTail, val); @@ -211,6 +276,16 @@ namespace TinySTL{ indexOfHead = nextIndex(indexOfHead); --size_; } + + template + std::ostream& operator <<(std::ostream& os, circular_buffer& cb){ + os << "("; + for (auto it = cb.first(); it != cb.last(); ++it){ + os << *it << ", "; + } + os << *(cb.last()) << ")"; + return os; + } } #endif \ No newline at end of file