完成add_node
This commit is contained in:
48
TinySTL/Detail/Graph.impl.h
Normal file
48
TinySTL/Detail/Graph.impl.h
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#include "../Graph.h"
|
||||||
|
|
||||||
|
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{
|
||||||
|
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){
|
||||||
|
for (auto& pair : nodes_){
|
||||||
|
if (equal_func(pair.first.first, n.first))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
template<class Index, class Value, class EqualFunc>
|
||||||
|
typename graph<Index, Value, EqualFunc>::node_sets
|
||||||
|
graph<Index, Value, EqualFunc>::empty_node_set(){
|
||||||
|
return node_sets();
|
||||||
|
}
|
||||||
|
}//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))
|
||||||
|
nodes_.push_front(make_pair(n, list<typename graph::node>()));
|
||||||
|
if (nodes.empty())
|
||||||
|
return;
|
||||||
|
//find node n's list
|
||||||
|
list<typename graph::node> 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
44
TinySTL/Graph.h
Normal file
44
TinySTL/Graph.h
Normal file
@@ -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 Index, class Value, class EqualFunc = equal_to<Index>>
|
||||||
|
class graph{
|
||||||
|
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 allocator<node> 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<pair<node, list<node>>> nodes_;
|
||||||
|
equal_func_type equal_func;
|
||||||
|
};
|
||||||
|
}//end of namespace Detail
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "Detail\Graph.impl.h"
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user