重构
This commit is contained in:
@@ -3,16 +3,9 @@
|
||||
namespace TinySTL{
|
||||
namespace Detail{
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
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_type(index, val));
|
||||
return *ptr;
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
void graph<Index, Value, EqualFunc>::del_node(node_type *p){
|
||||
nodeAllocator::destroy(p);
|
||||
nodeAllocator::deallocate(p);
|
||||
typename graph<Index, Value, EqualFunc>::node_type
|
||||
graph<Index, Value, EqualFunc>::make_node(const Index& index, const Value& val){
|
||||
return node_type(index, val);
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
typename graph<Index, Value, EqualFunc>::node_type&
|
||||
@@ -24,14 +17,6 @@ namespace TinySTL{
|
||||
return node_type();
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
void graph<Index, Value, EqualFunc>::cleanup(){
|
||||
for (auto out = nodes_.begin(); out != nodes_.end(); ++out){
|
||||
for (auto in = (out->second).begin(); in != (out->second).end(); ++in){
|
||||
del_node(&(*in));
|
||||
}
|
||||
}
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
bool graph<Index, Value, EqualFunc>::is_contained(const Index& index){
|
||||
for (auto& pair : nodes_){
|
||||
if (equal_func(pair.first.first, index))
|
||||
@@ -64,7 +49,6 @@ namespace TinySTL{
|
||||
if (equal_func(pair.first.first, index))
|
||||
return inner_iterator(this, (pair.second).end());
|
||||
}
|
||||
//throw std::exception("return end error");
|
||||
return inner_iterator();
|
||||
}
|
||||
template<class Index, class Value, class EqualFunc>
|
||||
@@ -231,19 +215,13 @@ namespace TinySTL{
|
||||
for (auto oit = nodes_.begin(); oit != nodes_.end();){
|
||||
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{
|
||||
for (auto iit = l.begin(); iit != l.end();){
|
||||
if (equal_func(iit->first, index)){
|
||||
del_node(&(*iit));
|
||||
if (equal_func(iit->first, index))
|
||||
iit = l.erase(iit);
|
||||
}else{
|
||||
else
|
||||
++iit;
|
||||
}
|
||||
}
|
||||
++oit;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef _GRAPH_H_
|
||||
#define _GRAPH_H_
|
||||
|
||||
#include "Allocator.h"
|
||||
#include "Iterator.h"
|
||||
#include "List.h"
|
||||
#include "String.h"
|
||||
@@ -31,13 +30,12 @@ namespace TinySTL{
|
||||
typedef EqualFunc equal_func_type;
|
||||
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 inner_iterator<Index, Value, EqualFunc> inner_iterator;
|
||||
public:
|
||||
graph() :size_(0){};
|
||||
virtual ~graph(){ cleanup(); };
|
||||
virtual ~graph(){};
|
||||
|
||||
//node can be not in the graph
|
||||
virtual void add_node(const node_type& item, const nodes_set_type& nodes) = 0;
|
||||
@@ -50,8 +48,8 @@ namespace TinySTL{
|
||||
void DFS(const Index& index, visiter_func_type func);
|
||||
void BFS(const Index& index, visiter_func_type func);
|
||||
|
||||
node_type& new_node(const Index& index, const Value& val);
|
||||
void del_node(node_type *p);
|
||||
//node_type& new_node(const Index& index, const Value& val);
|
||||
node_type make_node(const Index& index, const Value& val);
|
||||
node_type& get_node(const Index& index);
|
||||
|
||||
bool is_contained(const Index& index);
|
||||
@@ -72,8 +70,6 @@ namespace TinySTL{
|
||||
list<pair<node_type, list<node_type>>> nodes_;
|
||||
equal_func_type equal_func;
|
||||
size_t size_;
|
||||
protected:
|
||||
void cleanup();
|
||||
};
|
||||
|
||||
template<class Index, class Value, class EqualFunc = equal_to<Index>>
|
||||
|
||||
Reference in New Issue
Block a user