diff --git a/TinySTL/Detail/Graph.impl.h b/TinySTL/Detail/Graph.impl.h new file mode 100644 index 0000000..308d01e --- /dev/null +++ b/TinySTL/Detail/Graph.impl.h @@ -0,0 +1,48 @@ +#include "../Graph.h" + +namespace TinySTL{ + namespace Detail{ + template + typename graph::node& + graph::new_node(const Index& index, const Value& val)const{ + auto ptr = nodeAllocator::allocate(); + nodeAllocator::construct(ptr, node(index, val)); + return *ptr; + } + template + bool graph::is_contained(const node& n){ + for (auto& pair : nodes_){ + if (equal_func(pair.first.first, n.first)) + return true; + } + return false; + } + template + typename graph::node_sets + graph::empty_node_set(){ + return node_sets(); + } + }//end of Detail + + template + directed_graph::directed_graph():graph(){} + template + void directed_graph::add_node(const node& n, const node_sets& nodes){ + if (!is_contained(n)) + nodes_.push_front(make_pair(n, list())); + if (nodes.empty()) + return; + //find node n's list + list l; + for (auto& pair : nodes_){ + if (equal_func(pair.first.first, n.first)) + l = pair.second; + } + for (const auto& item : nodes){ + l.push_front(item); + if (is_contained(item)){ + add_node(item, empty_node_set()); + } + } + } +} \ No newline at end of file diff --git a/TinySTL/Graph.h b/TinySTL/Graph.h new file mode 100644 index 0000000..bd20c24 --- /dev/null +++ b/TinySTL/Graph.h @@ -0,0 +1,44 @@ +#ifndef _GRAPH_H_ +#define _GRAPH_H_ + +#include "Allocator.h" +#include "List.h" +#include "Utility.h" +#include "Vector.h" + +namespace TinySTL{ + namespace Detail{ + template> + class graph{ + public: + typedef Index index_type; + typedef Value value_type; + typedef EqualFunc equal_func_type; + typedef pair node; + typedef vector node_sets; + typedef allocator nodeAllocator; + public: + virtual ~graph(){}; + virtual void add_node(const node& item, const node_sets& nodes) = 0; + + node& new_node(const Index& index, const Value& val)const; + bool is_contained(const node& n); + inline static node_sets empty_node_set(); + protected: + list>> nodes_; + equal_func_type equal_func; + }; + }//end of namespace Detail + + template> + 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; + }; +} + +#include "Detail\Graph.impl.h" +#endif \ No newline at end of file