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