Graph.h 5.0 KB
Newer Older
邹晓航 已提交
1 2 3 4 5
#ifndef _GRAPH_H_
#define _GRAPH_H_

#include "Allocator.h"
#include "List.h"
6
#include "String.h"
邹晓航 已提交
7
#include "Unordered_set.h"
邹晓航 已提交
8 9 10
#include "Utility.h"
#include "Vector.h"

邹晓航 已提交
11
#include <functional>
12 13
#include <iomanip>
#include <sstream>
邹晓航 已提交
14

邹晓航 已提交
15 16
namespace TinySTL{
	namespace Detail{
邹晓航 已提交
17
		template<class Index, class Value, class EqualFunc>
邹晓航 已提交
18 19 20
		class inner_iterator;
		template<class Index, class Value, class EqualFunc>
		class outter_iterator;
邹晓航 已提交
21

邹晓航 已提交
22
		template<class Index, class Value, class EqualFunc = equal_to<Index>>
23
		class graph{//base class
邹晓航 已提交
24
		public:
邹晓航 已提交
25 26
			friend class inner_iterator < Index, Value, EqualFunc >;
			friend class outter_iterator < Index, Value, EqualFunc > ;
邹晓航 已提交
27 28 29 30 31 32
		public:
			typedef Index index_type;
			typedef Value value_type;
			typedef EqualFunc equal_func_type;
			typedef pair<Index, Value> node;
			typedef vector<node> node_sets;
邹晓航 已提交
33 34
			typedef inner_iterator<Index, Value, EqualFunc> inner_iterator;
			typedef outter_iterator<Index, Value, EqualFunc> iterator;
邹晓航 已提交
35
			typedef allocator<node> nodeAllocator;
邹晓航 已提交
36
			typedef std::function<void(node&)> visiter_func_type;
邹晓航 已提交
37
		public:
邹晓航 已提交
38
			graph() :size_(0){};
邹晓航 已提交
39 40
			virtual ~graph(){ cleanup(); };

41
			//node can be not in the graph
邹晓航 已提交
42
			virtual void add_node(const node& item, const node_sets& nodes) = 0;
43 44
			//node of the index must in the graph
			virtual void add_node(const Index& index, const node_sets& nodes) = 0;
邹晓航 已提交
45
			//virtual void delte_node(const node& item) = 0;
邹晓航 已提交
46 47

			void DFS(const Index& index, visiter_func_type func);
邹晓航 已提交
48
			//virtual void BFS(visiter_func_type func) = 0;
邹晓航 已提交
49

邹晓航 已提交
50 51 52 53
			node& new_node(const Index& index, const Value& val);
			void del_node(node *p);
			node& get_node(const Index& index);

邹晓航 已提交
54
			bool is_contained(const Index& index);
邹晓航 已提交
55
			inline static node_sets empty_node_set();
邹晓航 已提交
56 57 58
			node_sets adjacent_nodes(const Index& index);
			node_sets adjacent_nodes(const node& n);

邹晓航 已提交
59 60
			inline bool empty()const;
			inline size_t size()const;
邹晓航 已提交
61 62 63 64
			inline inner_iterator begin(const Index& index);
			inline inner_iterator end(const Index& index);
			inline iterator begin();
			inline iterator end();
65 66

			string to_string();
邹晓航 已提交
67 68 69
		protected:
			list<pair<node, list<node>>> nodes_;
			equal_func_type equal_func;
邹晓航 已提交
70
			size_t size_;
邹晓航 已提交
71 72
		protected:
			void cleanup();
邹晓航 已提交
73 74 75
		};

		template<class Index, class Value, class EqualFunc = equal_to<Index>>
邹晓航 已提交
76
		class inner_iterator{
邹晓航 已提交
77 78 79 80 81 82
		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:
邹晓航 已提交
83
			explicit inner_iterator(cntrPtr c = nullptr, inner_it_type iit = inner_it_type())
邹晓航 已提交
84
				:container_(c), inner_it_(iit){}
邹晓航 已提交
85 86
			inner_iterator& operator ++();
			const inner_iterator operator ++(int);
邹晓航 已提交
87 88 89 90 91 92 93
			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>
邹晓航 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
			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);
邹晓航 已提交
121
			template<class Index, class Value, class EqualFunc>
邹晓航 已提交
122 123
			friend bool operator !=(const outter_iterator<Index, Value, EqualFunc>& lhs,
				const outter_iterator<Index, Value, EqualFunc>& rhs);
邹晓航 已提交
124 125 126
		};
	}//end of namespace Detail

邹晓航 已提交
127
	//ͼ
邹晓航 已提交
128 129 130 131 132 133 134
	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;
135 136 137
		void add_node(const Index& index, const node_sets& nodes) override;
	private:
		void add_node_helper(const Index& index, const node_sets& nodes);
邹晓航 已提交
138 139 140 141 142
	};
}

#include "Detail\Graph.impl.h"
#endif