bug fix && change some names

This commit is contained in:
邹晓航
2015-02-10 15:32:38 +08:00
parent 833201262e
commit 7072a46050
2 changed files with 76 additions and 57 deletions

View File

@@ -3,25 +3,25 @@
namespace TinySTL{
namespace Detail{
template<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::node&
typename graph<Index, Value, EqualFunc>::node_type&
graph<Index, Value, EqualFunc>::new_node(const Index& index, const Value& val){
auto ptr = nodeAllocator::allocate();
nodeAllocator::construct(ptr, node(index, val));
nodeAllocator::construct(ptr, node_type(index, val));
return *ptr;
}
template<class Index, class Value, class EqualFunc>
void graph<Index, Value, EqualFunc>::del_node(node *p){
void graph<Index, Value, EqualFunc>::del_node(node_type *p){
nodeAllocator::destroy(p);
nodeAllocator::deallocate(p);
}
template<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::node&
typename graph<Index, Value, EqualFunc>::node_type&
graph<Index, Value, EqualFunc>::get_node(const Index& index){
for (auto& pair : nodes_){
if (equal_func(pair.first.first, index))
return pair.first;
}
return node();
return node_type();
}
template<class Index, class Value, class EqualFunc>
void graph<Index, Value, EqualFunc>::cleanup(){
@@ -40,9 +40,9 @@ namespace TinySTL{
return false;
}
template<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::node_sets
typename graph<Index, Value, EqualFunc>::nodes_set_type
graph<Index, Value, EqualFunc>::empty_node_set(){
return node_sets();
return nodes_set_type();
}
template<class Index, class Value, class EqualFunc>
bool graph<Index, Value, EqualFunc>::empty()const{
@@ -80,22 +80,22 @@ namespace TinySTL{
return size_;
}
template<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::node_sets
typename graph<Index, Value, EqualFunc>::nodes_set_type
graph<Index, Value, EqualFunc>::adjacent_nodes(const Index& index){
node_sets s;
nodes_set_type s;
for (auto it = begin(index); it != end(index); ++it){
s.push_back(*it);
}
return s;
}
template<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::node_sets
graph<Index, Value, EqualFunc>::adjacent_nodes(const node& n){
typename graph<Index, Value, EqualFunc>::nodes_set_type
graph<Index, Value, EqualFunc>::adjacent_nodes(const node_type& n){
return adjacent_nodes(n.first);
}
template<class Index, class Value, class EqualFunc>
void graph<Index, Value, EqualFunc>::DFS(const Index& index, visiter_func_type func){
node *start = &(get_node(index));
node_type *start = &(get_node(index));
Unordered_set<Index, std::hash<Index>, EqualFunc> visited(7);
auto nodes = adjacent_nodes(start->first);
@@ -108,14 +108,14 @@ namespace TinySTL{
}
template<class Index, class Value, class EqualFunc>
void graph<Index, Value, EqualFunc>::BFS(const Index& index, visiter_func_type func){
node *start = &(get_node(index));
node_type *start = &(get_node(index));
Unordered_set<Index, std::hash<Index>, EqualFunc> visited(7);
auto nodes = adjacent_nodes(start->first);
func(*start);
visited.insert(start->first);
do{
node_sets temp;
nodes_set_type temp;
for (auto it = nodes.begin(); it != nodes.end(); ++it){
if (visited.count(it->first) == 0){//has not visited
func(*it);
@@ -136,14 +136,19 @@ namespace TinySTL{
oss << "[" << oit->first << "," << oit->second << "]" << ":";
auto eit = end(oit->first);
for (auto iit = begin(oit->first); iit != eit; ++iit){
oss << "[" << iit->first << ", " << iit->second << "]" << "-";
oss << "[" << iit->first << ", " << iit->second << "]" << "->";
}
oss << "[nil,nil]" << std::endl << std::setw(4) << "|" << std::endl;
oss << "[nil]" << std::endl << std::setw(4) << "|" << std::endl;
}
oss << "[nil,nil]" << std::endl;
oss << "[nil]" << std::endl;
str.append(oss.str().c_str());
return str;
}
template<class Index, class Value, class EqualFunc>
typename graph<Index, Value, EqualFunc>::equal_func_type
graph<Index, Value, EqualFunc>::get_equal_func()const{
return equal_func;
}
//********************************************************************************
template<class Index, class Value, class EqualFunc>
inner_iterator<Index, Value, EqualFunc>& inner_iterator<Index, Value, EqualFunc>::operator ++(){
@@ -193,11 +198,11 @@ namespace TinySTL{
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_helper(const Index& index, const node_sets& nodes){
void directed_graph<Index, Value, EqualFunc>::add_node_helper(const Index& index, const nodes_set_type& nodes){
if (nodes.empty())
return;
//find node n's list
list<typename graph::node>* l;
list<typename graph::node_type>* l;
for (auto& pair : nodes_){
if (equal_func(pair.first.first, index))
l = &(pair.second);
@@ -210,36 +215,42 @@ namespace TinySTL{
}
}
template<class Index, class Value, class EqualFunc>
void directed_graph<Index, Value, EqualFunc>::add_node(const node& n, const node_sets& nodes){
void directed_graph<Index, Value, EqualFunc>::add_node(const node_type& n, const nodes_set_type& nodes){
if (!is_contained(n.first)){
nodes_.push_front(make_pair(n, list<typename graph::node>()));
nodes_.push_front(make_pair(n, list<typename graph::node_type>()));
++size_;
}
add_node_helper(n.first, nodes);
}
template<class Index, class Value, class EqualFunc>
void directed_graph<Index, Value, EqualFunc>::add_node(const Index& index, const node_sets& nodes){
void directed_graph<Index, Value, EqualFunc>::add_node(const Index& index, const nodes_set_type& nodes){
add_node_helper(index, nodes);
}
template<class Index, class Value, class EqualFunc>
void directed_graph<Index, Value, EqualFunc>::delete_node(const Index& index){
for (auto oit = nodes_.begin(); oit != nodes_.end();){
if (equal_func((oit->first).first, index))
auto& l = oit->second;
if (equal_func((oit->first).first, index)){
for (auto iit = l.begin(); iit != l.end(); ++iit){
del_node(&(*iit));
}
del_node(&(oit->first));
oit = nodes_.erase(oit);
else{
auto& l = oit->second;
}else{
for (auto iit = l.begin(); iit != l.end();){
if (equal_func(iit->first, index))
if (equal_func(iit->first, index)){
del_node(&(*iit));
iit = l.erase(iit);
else
}else{
++iit;
}
}
++oit;
}
}
}
template<class Index, class Value, class EqualFunc>
void directed_graph<Index, Value, EqualFunc>::delete_node(const node& item){
void directed_graph<Index, Value, EqualFunc>::delete_node(const node_type& item){
delete_node(item.first);
}
}

View File

@@ -2,6 +2,7 @@
#define _GRAPH_H_
#include "Allocator.h"
#include "Iterator.h"
#include "List.h"
#include "String.h"
#include "Unordered_set.h"
@@ -28,35 +29,35 @@ namespace TinySTL{
typedef Index index_type;
typedef Value value_type;
typedef EqualFunc equal_func_type;
typedef pair<Index, Value> node;
typedef vector<node> node_sets;
typedef inner_iterator<Index, Value, EqualFunc> inner_iterator;
typedef pair<Index, Value> node_type;
typedef vector<node_type> nodes_set_type;
typedef allocator<node_type> nodeAllocator;
typedef std::function<void(node_type&)> visiter_func_type;
typedef outter_iterator<Index, Value, EqualFunc> iterator;
typedef allocator<node> nodeAllocator;
typedef std::function<void(node&)> visiter_func_type;
typedef inner_iterator<Index, Value, EqualFunc> inner_iterator;
public:
graph() :size_(0){};
virtual ~graph(){ cleanup(); };
//node can be not in the graph
virtual void add_node(const node& item, const node_sets& nodes) = 0;
virtual void add_node(const node_type& item, const nodes_set_type& nodes) = 0;
//node of the index must in the graph
virtual void add_node(const Index& index, const node_sets& nodes) = 0;
virtual void add_node(const Index& index, const nodes_set_type& nodes) = 0;
virtual void delete_node(const node& item) = 0;
virtual void delete_node(const node_type& item) = 0;
virtual void delete_node(const Index& index) = 0;
void DFS(const Index& index, visiter_func_type func);
void BFS(const Index& index, visiter_func_type func);
node& new_node(const Index& index, const Value& val);
void del_node(node *p);
node& get_node(const Index& index);
node_type& new_node(const Index& index, const Value& val);
void del_node(node_type *p);
node_type& get_node(const Index& index);
bool is_contained(const Index& index);
inline static node_sets empty_node_set();
node_sets adjacent_nodes(const Index& index);
node_sets adjacent_nodes(const node& n);
inline static nodes_set_type empty_node_set();
nodes_set_type adjacent_nodes(const Index& index);
nodes_set_type adjacent_nodes(const node_type& n);
inline bool empty()const;
inline size_t size()const;
@@ -65,9 +66,10 @@ namespace TinySTL{
inline iterator begin();
inline iterator end();
equal_func_type get_equal_func()const;
string to_string();
protected:
list<pair<node, list<node>>> nodes_;
list<pair<node_type, list<node_type>>> nodes_;
equal_func_type equal_func;
size_t size_;
protected:
@@ -75,19 +77,22 @@ namespace TinySTL{
};
template<class Index, class Value, class EqualFunc = equal_to<Index>>
class inner_iterator{
class inner_iterator :public iterator<bidirectional_iterator_tag,
typename graph<Index, Value, EqualFunc>::node_type>{
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;
typedef typename list<typename graph_type::node_type>::iterator inner_it_type;
public:
explicit inner_iterator(cntrPtr c = nullptr, inner_it_type iit = inner_it_type())
:container_(c), inner_it_(iit){}
inner_iterator& operator ++();
const inner_iterator operator ++(int);
typename graph_type::node& operator*(){ return *inner_it_; }
typename graph_type::node* operator ->(){ return &(operator*()); }
typename graph_type::node_type& operator*(){ return *inner_it_; }
typename graph_type::node_type* operator ->(){ return &(operator*()); }
private:
cntrPtr container_;
inner_it_type inner_it_;
@@ -100,22 +105,25 @@ namespace TinySTL{
const inner_iterator<Index, Value, EqualFunc>& rhs);
};
template<class Index, class Value, class EqualFunc = equal_to<Index>>
class outter_iterator{
class outter_iterator :public iterator<bidirectional_iterator_tag,
typename graph<Index, Value, EqualFunc>::node_type>{
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;
typedef typename list<pair<typename graph_type::node_type, list<typename graph_type::node_type>>>::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*()); }
typename graph_type::node_type& operator*(){ return outter_it_->first; }
typename graph_type::node_type* operator ->(){ return &(operator*()); }
public:
template<class Index, class Value, class EqualFunc>
friend bool operator ==(const outter_iterator<Index, Value, EqualFunc>& lhs,
@@ -126,20 +134,20 @@ namespace TinySTL{
};
}//end of namespace Detail
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ
//directed graph
template<class Index, class Value, class EqualFunc = equal_to<Index>>
class directed_graph :public Detail::graph < Index, Value, EqualFunc > {
public:
directed_graph();
~directed_graph(){}
//node n -> every node in the nodes set
void add_node(const node& n, const node_sets& nodes) override final;
void add_node(const Index& index, const node_sets& nodes) override final;
//node n -> every node_type in the nodes set
void add_node(const node_type& n, const nodes_set_type& nodes) override final;
void add_node(const Index& index, const nodes_set_type& nodes) override final;
void delete_node(const node& item) override final;
void delete_node(const node_type& item) override final;
void delete_node(const Index& index) override final;
private:
void add_node_helper(const Index& index, const node_sets& nodes);
void add_node_helper(const Index& index, const nodes_set_type& nodes);
};
}