完成graph_iterator
This commit is contained in:
@@ -4,15 +4,15 @@ namespace TinySTL{
|
||||
namespace Detail{
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
typename graph<Index, Value, EqualFunc>::node&
|
||||
graph<Index, Value, EqualFunc>::new_node(const Index& index, const Value& val)const{
|
||||
graph<Index, Value, EqualFunc>::new_node(const Index& index, const Value& val){
|
||||
auto ptr = nodeAllocator::allocate();
|
||||
nodeAllocator::construct(ptr, node(index, val));
|
||||
return *ptr;
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
bool graph<Index, Value, EqualFunc>::is_contained(const node& n){
|
||||
bool graph<Index, Value, EqualFunc>::is_contained(const Index& index){
|
||||
for (auto& pair : nodes_){
|
||||
if (equal_func(pair.first.first, n.first))
|
||||
if (equal_func(pair.first.first, index))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -22,25 +22,82 @@ namespace TinySTL{
|
||||
graph<Index, Value, EqualFunc>::empty_node_set(){
|
||||
return node_sets();
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
bool graph<Index, Value, EqualFunc>::empty()const{
|
||||
return nodes_.empty();
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
typename graph<Index, Value, EqualFunc>::bucket_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 end(index);
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
typename graph<Index, Value, EqualFunc>::bucket_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());
|
||||
}
|
||||
throw std::exception("return end error");
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
size_t graph<Index, Value, EqualFunc>::size()const{
|
||||
return size_;
|
||||
}
|
||||
//********************************************************************************
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
graph_iterator<Index, Value, EqualFunc>& graph_iterator<Index, Value, EqualFunc>::operator ++(){
|
||||
++inner_it_;
|
||||
//if (inner_it_ == (outter_it_->second).end()){//to end
|
||||
// ++outter_it_;
|
||||
// if (outter_it_ != container_->nodes_.end())//not to end
|
||||
// inner_it_ = (outter_it_->second).begin();
|
||||
//}
|
||||
return *this;
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
const graph_iterator<Index, Value, EqualFunc> graph_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_;
|
||||
}
|
||||
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 == rhs);
|
||||
}
|
||||
}//end of Detail
|
||||
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
directed_graph<Index, Value, EqualFunc>::directed_graph():graph(){}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
void directed_graph<Index, Value, EqualFunc>::add_node(const node& n, const node_sets& nodes){
|
||||
if (!is_contained(n))
|
||||
if (!is_contained(n.first)){
|
||||
nodes_.push_front(make_pair(n, list<typename graph::node>()));
|
||||
++size_;
|
||||
}
|
||||
if (nodes.empty())
|
||||
return;
|
||||
//find node n's list
|
||||
list<typename graph::node> l;
|
||||
list<typename graph::node>* l;
|
||||
for (auto& pair : nodes_){
|
||||
if (equal_func(pair.first.first, n.first))
|
||||
l = pair.second;
|
||||
l = &(pair.second);
|
||||
}
|
||||
for (const auto& item : nodes){
|
||||
l.push_front(item);
|
||||
if (is_contained(item)){
|
||||
l->push_front(item);
|
||||
if (!is_contained(item.first)){
|
||||
add_node(item, empty_node_set());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,30 +6,76 @@
|
||||
#include "Utility.h"
|
||||
#include "Vector.h"
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace TinySTL{
|
||||
namespace Detail{
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
class graph_iterator;
|
||||
template<class Index, class Value, class EqualFunc = equal_to<Index>>
|
||||
class graph{
|
||||
public:
|
||||
friend class graph_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 allocator<node> nodeAllocator;
|
||||
typedef std::function<void(node&)> visiter_func_type;
|
||||
public:
|
||||
graph() :size_(0){};
|
||||
virtual ~graph(){};
|
||||
virtual void add_node(const node& item, const node_sets& nodes) = 0;
|
||||
//virtual void delte_node(const node& item) = 0;
|
||||
//virtual node_sets adjacent_nodes(const node& n) = 0;
|
||||
//virtual node_sets adjacent_nodes(const Index& index) = 0;
|
||||
//virtual void DFS(visiter_func_type func) = 0;
|
||||
//virtual void BFS(visiter_func_type func) = 0;
|
||||
|
||||
node& new_node(const Index& index, const Value& val)const;
|
||||
bool is_contained(const node& n);
|
||||
static node& new_node(const Index& index, const Value& val);
|
||||
bool is_contained(const Index& index);
|
||||
inline static node_sets empty_node_set();
|
||||
inline bool empty()const;
|
||||
inline size_t size()const;
|
||||
inline bucket_iterator begin(const Index& index);
|
||||
inline bucket_iterator end(const Index& index);
|
||||
protected:
|
||||
list<pair<node, list<node>>> nodes_;
|
||||
equal_func_type equal_func;
|
||||
size_t size_;
|
||||
};
|
||||
|
||||
template<class Index, class Value, class EqualFunc = equal_to<Index>>
|
||||
class graph_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:
|
||||
graph_iterator(cntrPtr c, inner_it_type iit)
|
||||
:container_(c), inner_it_(iit){}
|
||||
graph_iterator& operator ++();
|
||||
const graph_iterator operator ++(int);
|
||||
typename graph_type::node& operator*(){ return *inner_it_; }
|
||||
typename graph_type::node* operator ->(){ return &(operator*()); }
|
||||
private:
|
||||
cntrPtr container_;
|
||||
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);
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
friend bool operator !=(const graph_iterator<Index, Value, EqualFunc>& lhs,
|
||||
const graph_iterator<Index, Value, EqualFunc>& rhs);
|
||||
};
|
||||
}//end of namespace Detail
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD>ͼ
|
||||
template<class Index, class Value, class EqualFunc = equal_to<Index>>
|
||||
class directed_graph :public Detail::graph < Index, Value, EqualFunc > {
|
||||
public:
|
||||
|
||||
Reference in New Issue
Block a user