From 440ba0c854c9d6e38950b0869fface1739a304f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Mon, 9 Feb 2015 21:57:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90iterator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Detail/Graph.impl.h | 57 ++++++++++++++++++++++++++++--------- TinySTL/Graph.h | 57 ++++++++++++++++++++++++++++--------- 2 files changed, 87 insertions(+), 27 deletions(-) diff --git a/TinySTL/Detail/Graph.impl.h b/TinySTL/Detail/Graph.impl.h index 38b7d27..d8ec21d 100644 --- a/TinySTL/Detail/Graph.impl.h +++ b/TinySTL/Detail/Graph.impl.h @@ -21,6 +21,7 @@ namespace TinySTL{ if (equal_func(pair.first.first, index)) return pair.first; } + return node(); } template void graph::cleanup(){ @@ -48,23 +49,31 @@ namespace TinySTL{ return nodes_.empty(); } template - typename graph::bucket_iterator + typename graph::inner_iterator graph::begin(const Index& index){ for (auto& pair : nodes_){ if (equal_func(pair.first.first, index)) - return bucket_iterator(this, (pair.second).begin()); + return inner_iterator(this, (pair.second).begin()); } return end(index); } template - typename graph::bucket_iterator + typename graph::inner_iterator graph::end(const Index& index){ for (auto& pair : nodes_){ if (equal_func(pair.first.first, index)) - return bucket_iterator(this, (pair.second).end()); + return inner_iterator(this, (pair.second).end()); } //throw std::exception("return end error"); - return bucket_iterator(); + return inner_iterator(); + } + template + typename graph::iterator graph::begin(){ + return iterator(this, nodes_.begin()); + } + template + typename graph::iterator graph::end(){ + return iterator(this, nodes_.end()); } template size_t graph::size()const{ @@ -99,26 +108,46 @@ namespace TinySTL{ } //******************************************************************************** template - graph_iterator& graph_iterator::operator ++(){ + inner_iterator& inner_iterator::operator ++(){ ++inner_it_; return *this; } template - const graph_iterator graph_iterator::operator ++(int){ + const inner_iterator inner_iterator::operator ++(int){ + auto temp = *this; + ++*this; + return temp; + } + template + bool operator ==(const inner_iterator& lhs, + const inner_iterator& rhs){ + return lhs.container_ == rhs.container_ && lhs.inner_it_ == rhs.inner_it_; + } + template + bool operator !=(const inner_iterator& lhs, + const inner_iterator& rhs){ + return !(lhs == rhs); + } + //********************************************************************************* + template + outter_iterator& outter_iterator::operator ++(){ + ++outter_it_; + return *this; + } + template + const outter_iterator outter_iterator::operator ++(int){ auto temp = *this; ++*this; return temp; } template - bool operator ==(const graph_iterator& lhs, - const graph_iterator& rhs){ - return lhs.container_ == rhs.container_ && - //lhs.outter_it_ == rhs.outter_it_ && - lhs.inner_it_ == rhs.inner_it_; + bool operator ==(const outter_iterator& lhs, + const outter_iterator& rhs){ + return lhs.container_ == rhs.container_ && lhs.outter_it_ == rhs.outter_it_; } template - bool operator !=(const graph_iterator& lhs, - const graph_iterator& rhs){ + bool operator !=(const outter_iterator& lhs, + const outter_iterator& rhs){ return !(lhs == rhs); } }//end of Detail diff --git a/TinySTL/Graph.h b/TinySTL/Graph.h index 84617a7..656d5f5 100644 --- a/TinySTL/Graph.h +++ b/TinySTL/Graph.h @@ -12,19 +12,23 @@ namespace TinySTL{ namespace Detail{ template - class graph_iterator; + class inner_iterator; + template + class outter_iterator; template> class graph{ public: - friend class graph_iterator < Index, Value, EqualFunc >; + friend class inner_iterator < Index, Value, EqualFunc >; + friend class outter_iterator < Index, Value, EqualFunc > ; public: typedef Index index_type; typedef Value value_type; typedef EqualFunc equal_func_type; typedef pair node; typedef vector node_sets; - typedef graph_iterator bucket_iterator; + typedef inner_iterator inner_iterator; + typedef outter_iterator iterator; typedef allocator nodeAllocator; typedef std::function visiter_func_type; public: @@ -48,8 +52,10 @@ namespace TinySTL{ inline bool empty()const; inline size_t size()const; - inline bucket_iterator begin(const Index& index); - inline bucket_iterator end(const Index& index); + inline inner_iterator begin(const Index& index); + inline inner_iterator end(const Index& index); + inline iterator begin(); + inline iterator end(); protected: list>> nodes_; equal_func_type equal_func; @@ -59,17 +65,17 @@ namespace TinySTL{ }; template> - class graph_iterator{ + class inner_iterator{ public: friend class graph < Index, Value, EqualFunc > ; typedef graph* cntrPtr; typedef graph graph_type; typedef typename list::iterator inner_it_type; public: - explicit graph_iterator(cntrPtr c = nullptr, inner_it_type iit = inner_it_type()) + explicit inner_iterator(cntrPtr c = nullptr, inner_it_type iit = inner_it_type()) :container_(c), inner_it_(iit){} - graph_iterator& operator ++(); - const graph_iterator operator ++(int); + inner_iterator& operator ++(); + const inner_iterator operator ++(int); typename graph_type::node& operator*(){ return *inner_it_; } typename graph_type::node* operator ->(){ return &(operator*()); } private: @@ -77,11 +83,36 @@ namespace TinySTL{ inner_it_type inner_it_; public: template - friend bool operator ==(const graph_iterator& lhs, - const graph_iterator& rhs); + friend bool operator ==(const inner_iterator& lhs, + const inner_iterator& rhs); + template + friend bool operator !=(const inner_iterator& lhs, + const inner_iterator& rhs); + }; + template> + class outter_iterator{ + public: + friend class graph < Index, Value, EqualFunc >; + typedef graph* cntrPtr; + typedef graph graph_type; + typedef typename list>>::iterator outter_it_type; + private: + cntrPtr container_; + outter_it_type outter_it_; + public: + explicit outter_iterator(cntrPtr c = nullptr, outter_it_type oit = outter_it_type()) + :container_(c), outter_it_(oit){} + outter_iterator& operator ++(); + const outter_iterator operator ++(int); + typename graph_type::node& operator*(){ return outter_it_->first; } + typename graph_type::node* operator ->(){ return &(operator*()); } + public: + template + friend bool operator ==(const outter_iterator& lhs, + const outter_iterator& rhs); template - friend bool operator !=(const graph_iterator& lhs, - const graph_iterator& rhs); + friend bool operator !=(const outter_iterator& lhs, + const outter_iterator& rhs); }; }//end of namespace Detail -- GitLab