提交 440ba0c8 编写于 作者: 邹晓航

完成iterator

上级 e8ecf9df
......@@ -21,6 +21,7 @@ namespace TinySTL{
if (equal_func(pair.first.first, index))
return pair.first;
}
return node();
}
template<class Index, class Value, class EqualFunc>
void graph<Index, Value, EqualFunc>::cleanup(){
......@@ -48,23 +49,31 @@ namespace TinySTL{
return nodes_.empty();
}
template<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::bucket_iterator
typename graph<Index, Value, EqualFunc>::inner_iterator
graph<Index, Value, EqualFunc>::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<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::bucket_iterator
typename graph<Index, Value, EqualFunc>::inner_iterator
graph<Index, Value, EqualFunc>::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<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::iterator graph<Index, Value, EqualFunc>::begin(){
return iterator(this, nodes_.begin());
}
template<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::iterator graph<Index, Value, EqualFunc>::end(){
return iterator(this, nodes_.end());
}
template<class Index, class Value, class EqualFunc>
size_t graph<Index, Value, EqualFunc>::size()const{
......@@ -99,26 +108,46 @@ namespace TinySTL{
}
//********************************************************************************
template<class Index, class Value, class EqualFunc>
graph_iterator<Index, Value, EqualFunc>& graph_iterator<Index, Value, EqualFunc>::operator ++(){
inner_iterator<Index, Value, EqualFunc>& inner_iterator<Index, Value, EqualFunc>::operator ++(){
++inner_it_;
return *this;
}
template<class Index, class Value, class EqualFunc>
const graph_iterator<Index, Value, EqualFunc> graph_iterator<Index, Value, EqualFunc>::operator ++(int){
const inner_iterator<Index, Value, EqualFunc> inner_iterator<Index, Value, EqualFunc>::operator ++(int){
auto temp = *this;
++*this;
return temp;
}
template<class Index, class Value, class EqualFunc>
bool operator ==(const inner_iterator<Index, Value, EqualFunc>& lhs,
const inner_iterator<Index, Value, EqualFunc>& rhs){
return lhs.container_ == rhs.container_ && lhs.inner_it_ == rhs.inner_it_;
}
template<class Index, class Value, class EqualFunc>
bool operator !=(const inner_iterator<Index, Value, EqualFunc>& lhs,
const inner_iterator<Index, Value, EqualFunc>& rhs){
return !(lhs == rhs);
}
//*********************************************************************************
template<class Index, class Value, class EqualFunc>
outter_iterator<Index, Value, EqualFunc>& outter_iterator<Index, Value, EqualFunc>::operator ++(){
++outter_it_;
return *this;
}
template<class Index, class Value, class EqualFunc>
const outter_iterator<Index, Value, EqualFunc> outter_iterator<Index, Value, EqualFunc>::operator ++(int){
auto temp = *this;
++*this;
return temp;
}
template<class Index, class Value, class EqualFunc>
bool operator ==(const graph_iterator<Index, Value, EqualFunc>& lhs,
const graph_iterator<Index, Value, EqualFunc>& rhs){
return lhs.container_ == rhs.container_ &&
//lhs.outter_it_ == rhs.outter_it_ &&
lhs.inner_it_ == rhs.inner_it_;
bool operator ==(const outter_iterator<Index, Value, EqualFunc>& lhs,
const outter_iterator<Index, Value, EqualFunc>& rhs){
return lhs.container_ == rhs.container_ && lhs.outter_it_ == rhs.outter_it_;
}
template<class Index, class Value, class EqualFunc>
bool operator !=(const graph_iterator<Index, Value, EqualFunc>& lhs,
const graph_iterator<Index, Value, EqualFunc>& rhs){
bool operator !=(const outter_iterator<Index, Value, EqualFunc>& lhs,
const outter_iterator<Index, Value, EqualFunc>& rhs){
return !(lhs == rhs);
}
}//end of Detail
......
......@@ -12,19 +12,23 @@
namespace TinySTL{
namespace Detail{
template<class Index, class Value, class EqualFunc>
class graph_iterator;
class inner_iterator;
template<class Index, class Value, class EqualFunc>
class outter_iterator;
template<class Index, class Value, class EqualFunc = equal_to<Index>>
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<Index, Value> node;
typedef vector<node> node_sets;
typedef graph_iterator<Index, Value, EqualFunc> bucket_iterator;
typedef inner_iterator<Index, Value, EqualFunc> inner_iterator;
typedef outter_iterator<Index, Value, EqualFunc> iterator;
typedef allocator<node> nodeAllocator;
typedef std::function<void(node&)> 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<pair<node, list<node>>> nodes_;
equal_func_type equal_func;
......@@ -59,17 +65,17 @@ namespace TinySTL{
};
template<class Index, class Value, class EqualFunc = equal_to<Index>>
class graph_iterator{
class inner_iterator{
public:
friend class graph < Index, Value, EqualFunc > ;
typedef graph<Index, Value, EqualFunc>* cntrPtr;
typedef graph<Index, Value, EqualFunc> graph_type;
typedef typename list<typename graph_type::node>::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<class Index, class Value, class EqualFunc>
friend bool operator ==(const graph_iterator<Index, Value, EqualFunc>& lhs,
const graph_iterator<Index, Value, EqualFunc>& rhs);
friend bool operator ==(const inner_iterator<Index, Value, EqualFunc>& lhs,
const inner_iterator<Index, Value, EqualFunc>& rhs);
template<class Index, class Value, class EqualFunc>
friend bool operator !=(const inner_iterator<Index, Value, EqualFunc>& lhs,
const inner_iterator<Index, Value, EqualFunc>& rhs);
};
template<class Index, class Value, class EqualFunc = equal_to<Index>>
class outter_iterator{
public:
friend class graph < Index, Value, EqualFunc >;
typedef graph<Index, Value, EqualFunc>* cntrPtr;
typedef graph<Index, Value, EqualFunc> graph_type;
typedef typename list<pair<typename graph_type::node, list<typename graph_type::node>>>::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<class Index, class Value, class EqualFunc>
friend bool operator ==(const outter_iterator<Index, Value, EqualFunc>& lhs,
const outter_iterator<Index, Value, EqualFunc>& rhs);
template<class Index, class Value, class EqualFunc>
friend bool operator !=(const graph_iterator<Index, Value, EqualFunc>& lhs,
const graph_iterator<Index, Value, EqualFunc>& rhs);
friend bool operator !=(const outter_iterator<Index, Value, EqualFunc>& lhs,
const outter_iterator<Index, Value, EqualFunc>& rhs);
};
}//end of namespace Detail
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册