From 0b304bbd3bb36d1a80abe378626b3a490020ffba 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, 26 Nov 2014 10:46:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90List=E7=9A=84=E8=BF=AD?= =?UTF-8?q?=E4=BB=A3=E5=99=A8=E7=9B=B8=E5=85=B3=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/List.h | 44 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/TinySTL/List.h b/TinySTL/List.h index 4e84608..31f83fb 100644 --- a/TinySTL/List.h +++ b/TinySTL/List.h @@ -2,6 +2,8 @@ #define _LIST_H_ #include "Allocator.h" +#include "Iterator.h" +#include "ReverseIterator.h" #include "UninitializedFunctions.h" namespace TinySTL{ @@ -23,7 +25,7 @@ namespace TinySTL{ }; //the class of list iterator template - struct listIterator{ + struct listIterator :public bidirectional_iterator{ template friend class List; private: @@ -52,24 +54,34 @@ namespace TinySTL{ } T& operator *(){ return p->data; } T* operator &(){ return &(operator*()); } - bool operator ==(const listIterator& it){ - return *p == *(it.p); - } - bool operator !=(const listIterator& it){ - return !(*this == it); - } + + template + friend bool operator ==(const listIterator& lhs, const listIterator& rhs); + template + friend bool operator !=(const listIterator& lhs, const listIterator& rhs); }; + template + bool operator ==(const listIterator& lhs, const listIterator& rhs){ + return lhs.p == rhs.p; + } + template + bool operator !=(const listIterator& lhs, const listIterator& rhs){ + return !(lhs == rhs); + } }//end of namespace //the class of List template class List{ + template + friend struct listIterator; private: typedef allocator> nodeAllocator; typedef node *nodePtr; public: typedef T value_type; typedef listIterator iterator; + typedef reverse_iterator_t reverse_iterator; typedef T& reference; typedef size_t size_type; private: @@ -91,13 +103,27 @@ namespace TinySTL{ nodeAllocator::deallocate(tail.p); } + bool empty()const{ + return head == tail; + } + size_type size()const{ + size_type length = 0; + for (auto h = head; h != tail; ++h) + ++length; + return length; + } + reference front(){ return (head.p->data); } + reference back(){ return (tail.p->prev->data); } + void push_front(const value_type& val); void pop_front(); void push_back(const value_type& val); void pop_back(); - iterator begin()const{ return head; } - iterator end()const{ return tail; } + iterator begin(){ return head; } + iterator end(){ return tail; } + reverse_iterator rbegin(){ return reverse_iterator(tail); } + reverse_iterator rend(){ return reverse_iterator(head); } private: nodePtr newNode(const T& val = T()){ nodePtr res = nodeAllocator::allocate();