完成List的迭代器相关操作

This commit is contained in:
邹晓航
2014-11-26 10:46:32 +08:00
parent 7c728663c6
commit 0b304bbd3b

View File

@@ -2,6 +2,8 @@
#define _LIST_H_ #define _LIST_H_
#include "Allocator.h" #include "Allocator.h"
#include "Iterator.h"
#include "ReverseIterator.h"
#include "UninitializedFunctions.h" #include "UninitializedFunctions.h"
namespace TinySTL{ namespace TinySTL{
@@ -23,7 +25,7 @@ namespace TinySTL{
}; };
//the class of list iterator //the class of list iterator
template<class T> template<class T>
struct listIterator{ struct listIterator :public bidirectional_iterator<T, ptrdiff_t>{
template<class T> template<class T>
friend class List; friend class List;
private: private:
@@ -52,24 +54,34 @@ namespace TinySTL{
} }
T& operator *(){ return p->data; } T& operator *(){ return p->data; }
T* operator &(){ return &(operator*()); } T* operator &(){ return &(operator*()); }
bool operator ==(const listIterator& it){
return *p == *(it.p); template<class T>
} friend bool operator ==(const listIterator<T>& lhs, const listIterator<T>& rhs);
bool operator !=(const listIterator& it){ template<class T>
return !(*this == it); friend bool operator !=(const listIterator<T>& lhs, const listIterator<T>& rhs);
}
}; };
template<class T>
bool operator ==(const listIterator<T>& lhs, const listIterator<T>& rhs){
return lhs.p == rhs.p;
}
template<class T>
bool operator !=(const listIterator<T>& lhs, const listIterator<T>& rhs){
return !(lhs == rhs);
}
}//end of namespace }//end of namespace
//the class of List //the class of List
template<class T> template<class T>
class List{ class List{
template<class T>
friend struct listIterator;
private: private:
typedef allocator<node<T>> nodeAllocator; typedef allocator<node<T>> nodeAllocator;
typedef node<T> *nodePtr; typedef node<T> *nodePtr;
public: public:
typedef T value_type; typedef T value_type;
typedef listIterator<T> iterator; typedef listIterator<T> iterator;
typedef reverse_iterator_t<iterator> reverse_iterator;
typedef T& reference; typedef T& reference;
typedef size_t size_type; typedef size_t size_type;
private: private:
@@ -91,13 +103,27 @@ namespace TinySTL{
nodeAllocator::deallocate(tail.p); 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 push_front(const value_type& val);
void pop_front(); void pop_front();
void push_back(const value_type& val); void push_back(const value_type& val);
void pop_back(); void pop_back();
iterator begin()const{ return head; } iterator begin(){ return head; }
iterator end()const{ return tail; } iterator end(){ return tail; }
reverse_iterator rbegin(){ return reverse_iterator(tail); }
reverse_iterator rend(){ return reverse_iterator(head); }
private: private:
nodePtr newNode(const T& val = T()){ nodePtr newNode(const T& val = T()){
nodePtr res = nodeAllocator::allocate(); nodePtr res = nodeAllocator::allocate();