From 584a986ed4af042f932e336e60ce40ca4f933f36 Mon Sep 17 00:00:00 2001 From: LiZhenhuan1019 Date: Fri, 5 Aug 2016 00:24:34 +0800 Subject: [PATCH 1/2] done,far from perfect --- Graph.depend | 11 +- Graph.layout | 13 +- include/Graph.h | 351 +++++++++++++++++++++++-------------------- include/repository.h | 119 ++++----------- test/test.cpp | 164 ++++++++++---------- 5 files changed, 312 insertions(+), 346 deletions(-) diff --git a/Graph.depend b/Graph.depend index 93ee03c..0b38503 100644 --- a/Graph.depend +++ b/Graph.depend @@ -1,22 +1,23 @@ # depslib dependency file v1.0 -1469371255 source:d:\lzh\mylibrary\graph\test\test.cpp +1470326548 source:d:\lzh\mylibrary\graph\test\test.cpp "include\Graph.h" -1469371255 d:\lzh\mylibrary\graph\\include\graph.h +1470326755 d:\lzh\mylibrary\graph\\include\graph.h "repository.h" + 1468043652 d:\lzh\mylibrary\graph\\include\respository.h -1469371255 d:\lzh\mylibrary\graph\\include\repository.h +1470325765 d:\lzh\mylibrary\graph\\include\repository.h - - + + diff --git a/Graph.layout b/Graph.layout index 3e9a980..c625962 100644 --- a/Graph.layout +++ b/Graph.layout @@ -2,19 +2,20 @@ - + - + - + - + + - + - + diff --git a/include/Graph.h b/include/Graph.h index d497d48..8e2ca4c 100644 --- a/include/Graph.h +++ b/include/Graph.h @@ -3,6 +3,7 @@ #include "repository.h" #include #include +#include #include #include #include @@ -29,152 +30,180 @@ Structures that associate values to the edges usually also provide: */ namespace lzhlib { - namespace exceptions - { - class require_edge_that_does_not_exist; - } - class vertex_id - { - friend struct invalid_vertex; - template - friend class repository; - friend class exceptions::require_edge_that_does_not_exist; - friend struct invalid_vertex; - private: - vertex_id(unsigned long long i) - : id(i) - {} - public: - vertex_id() = default; - bool operator<(vertex_id rhs) const - { - return id < rhs.id; - } - bool operator==(vertex_id rhs) const - { - return id == rhs.id; - } - private: - unsigned long long id = static_cast(-1); //vector::size_type 为unsigned long long - }; - struct invalid_vertex - { - static constexpr vertex_id id{}; - }; - constexpr vertex_id invalid_vertex_id = invalid_vertex::id; - class edge_id - { - template - friend class repository; - private: - edge_id() = default; - edge_id(unsigned long long i) - : id(i) - {} - public: - bool operator<(edge_id rhs) const - { - return id < rhs.id; - } - bool operator==(edge_id rhs) const - { - return id == rhs.id; - } - bool operator!=(edge_id rhs) const - { - return !(*this == rhs); - } - private: - unsigned long long id; - }; - +// namespace exceptions +// { +// class require_edge_that_does_not_exist; +// } +// class vertex_id +// { +// friend struct invalid_vertex; +// template +// friend class repository; +// friend class exceptions::require_edge_that_does_not_exist; +// friend struct invalid_vertex; +// private: +// vertex_id(unsigned long long i) +// : id(i) +// {} +// public: +// vertex_id() = default; +// bool operator<(vertex_id rhs) const +// { +// return id < rhs.id; +// } +// bool operator==(vertex_id rhs) const +// { +// return id == rhs.id; +// } +// private: +// unsigned long long id = static_cast(-1); //vector::size_type 为unsigned long long +// }; + template + constexpr id invalid_vertex_id{}; +// class edge_id +// { +// template +// friend class repository; +// private: +// edge_id() = default; +// edge_id(unsigned long long i) +// : id(i) +// {} +// public: +// bool operator<(edge_id rhs) const +// {id +// return id < rhs.id; +// } +// bool operator==(edge_id rhs) const +// { +// return id == rhs.id; +// } +// bool operator!=(edge_id rhs) const +// { +// return !(*this == rhs); +// } +// private: +// unsigned long long id; +// }; +// namespace exceptions { class require_edge_that_does_not_exist: public std::logic_error { public: - require_edge_that_does_not_exist(vertex_id x, vertex_id y) - : std::logic_error(std::string("require edge that doesn't exist!The vertices ids are ") + std::to_string(x.id) + " and " + std::to_string(y.id) + ".") + require_edge_that_does_not_exist() + : std::logic_error(std::string("require edge that doesn't exist!")) { } }; - [[noreturn]] void throw_exception_require_edge_that_does_not_exist(vertex_id x, vertex_id y) + [[noreturn]] void throw_exception_require_edge_that_does_not_exist() { - throw exceptions::require_edge_that_does_not_exist(x, y); + throw exceptions::require_edge_that_does_not_exist(); } } class null_value_tag; + template + struct traits + { + using vertex_value_t = VertexValueT; + using edge_value_t = EdgeValueT; + }; namespace detail { - - template + template > class vertex; //only used by graph_impl - template <> - class vertex //only used by graph_impl + template > + class edge; + template + struct typedef_base + { + using traits_t = Traits; + + using vertex_t = vertex; + using vertex_value_t = typename traits_t::vertex_value_t; + using vertex_id_t = id; + + using edge_t = edge; + using edge_value_t = typename traits_t::edge_value_t; + using edge_id_t = id; + }; + template //only used by graph_impl + class vertex>: public typedef_base>//强制使用traits { public: - using vertex_value_t = null_value_tag; -// bool is_associated(edge_id i) +// bool is_associated(edge_id_t i) // { // return edges.find(i)!=edges.end(); //a possibly more efficent way :ask edge for this // } - std::setconst& get_associated_edges() const + std::vectorconst& get_associated_edges() const { return edges; } - void add_associated_edge(edge_id i) + void add_associated_edge(typename vertex::edge_id_t i) { - edges.insert(i); + if(find(edges.begin(),edges.end(),i) == edges.end()) + edges.push_back(i); } - void remove_associated_edge(edge_id i) + void remove_associated_edge(typename vertex::edge_id_t i) { - edges.erase(i); + edges.erase(remove(edges.begin(),edges.end(),i),edges.end()); } private: - std::set edges; + std::vector edges; }; - template - class vertex: public vertex + template + class vertex>: public typedef_base> //强制使用traits { public: - using vertex_value_t = VertexValueT; + std::vectorconst& get_associated_edges() const + { + return edges; + } + void add_associated_edge(typename vertex::edge_id_t i) + { + if(find(edges.begin(),edges.end(),i) == edges.end()) + edges.push_back(i); + } + void remove_associated_edge(typename vertex::edge_id_t i) + { + edges.erase(remove(edges.begin(),edges.end(),i),edges.end()); + } + template vertex(Args&&... args) : value(std::forward(args)...) { } - vertex_value_t& vertex_value() & + typename vertex::vertex_value_t& vertex_value() & { return value; } - vertex_value_t&& vertex_value() && + typename vertex::vertex_value_t&& vertex_value() && { return std::move(value); } - vertex_value_t const& vertex_value() const& + typename vertex::vertex_value_t const& vertex_value() const& { return value; } - vertex_value_t const&& vertex_value() const&& + typename vertex::vertex_value_t const&& vertex_value() const&& { return std::move(value); } private: - vertex_value_t value; + std::vector edges; + typename vertex::vertex_value_t value; }; - template - class edge; - template <> - class edge + template + class edge>: public typedef_base> { public: - using edge_value_t = null_value_tag; - using pair_t = std::pair; - bool is_associated(vertex_id i) const + using pair_t = std::pair; + bool is_associated(typename edge::vertex_id_t i) const { return i == vertices.first || i == vertices.second; } @@ -182,132 +211,132 @@ namespace lzhlib { return vertices; } - void set_associated_vertices(vertex_id v1, vertex_id v2) + void set_associated_vertices(typename edge::vertex_id_t v1, typename edge::vertex_id_t v2) { vertices.first = v1; vertices.second = v2; } void detach() { - vertices.first = vertices.second = invalid_vertex_id; + vertices.first = vertices.second = invalid_vertex_id; } private: pair_t vertices; }; - template - class edge: public edge + template + class edge>: public typedef_base> { public: - using edge_value_t = EdgeValueT; + using pair_t = std::pair; + bool is_associated(typename edge::vertex_id_t i) const + { + return i == vertices.first || i == vertices.second; + } + pair_t get_associated_vertices() const + { + return vertices; + } + void set_associated_vertices(typename edge::vertex_id_t v1, typename edge::vertex_id_t v2) + { + vertices.first = v1; + vertices.second = v2; + } + void detach() + { + vertices.first = vertices.second = invalid_vertex_id; + } + template edge(Args&&...args) : value(std::forward(args)...) { } - edge_value_t& edge_value() & + typename edge::edge_value_t& edge_value() & { return value; } - edge_value_t&& edge_value() && + typename edge::edge_value_t&& edge_value() && { return std::move(value); } - edge_value_t const& edge_value() const& + typename edge::edge_value_t const& edge_value() const& { return value; } - edge_value_t const&& edge_value() const&& + typename edge::edge_value_t const&& edge_value() const&& { return std::move(value); } private: - edge_value_t value; - }; - } - namespace traits - { - template - struct stock_traits> - { - using stock_t = detail::vertex; - using id_t = vertex_id; - }; - template - struct stock_traits> - { - using stock_t = detail::edge; - using id_t = edge_id; + pair_t vertices; + typename edge::edge_value_t value; }; } - template - class undirected_graph + template + class undirected_graph: public detail::typedef_base { public: - using vertex_t = detail::vertex; - using vertex_value_t = typename vertex_t::vertex_value_t; - using edge_t = detail::edge; - using edge_value_t = typename edge_t::edge_value_t; - using pair_t = typename edge_t::pair_t; + using pair_t = typename undirected_graph::edge_t::pair_t; - bool adjacent(vertex_id x, vertex_id y) const + bool adjacent(typename undirected_graph::vertex_id_t x, typename undirected_graph::vertex_id_t y) const { if(get_vertex(x).get_associated_edges().size() < get_vertex(y).get_associated_edges().size()) return adjacent_impl(x, y); else return adjacent_impl(y, x); } - std::vector neighbors(vertex_id v) const + std::vector neighbors(typename undirected_graph::vertex_id_t v) const { - std::set const& edges = get_vertex(v).get_associated_edges(); - std::vector ret; + auto const& edges = get_vertex(v).get_associated_edges(); + std::vector ret; ret.reserve(edges.size()); - for(edge_id e : edges) + for(auto e : edges) { ret.push_back(get_opposite_vertex(v, e)); } return ret; } - std::setconst& get_associated_edges(vertex_id v) const + auto const& get_associated_edges(typename undirected_graph::vertex_id_t v) const { return get_vertex(v).get_associated_edges(); } template - vertex_id add_vertex(Args&&...args) + typename undirected_graph::vertex_id_t add_vertex(Args&&...args) { return vertex_repository.add_stock(std::forward(args)...); } - void remove_vertex(vertex_id v) + void remove_vertex(typename undirected_graph::vertex_id_t v) { - std::set edges = get_vertex(v).get_associated_edges(); - for(edge_id e : edges) + auto edges = get_vertex(v).get_associated_edges(); + for(auto e : edges) { remove_edge_from(e, v); } vertex_repository.remove_stock(v); } template - edge_id add_edge(vertex_id x, vertex_id y,Args&& ...args) + typename undirected_graph::edge_id_t add_edge(typename undirected_graph::vertex_id_t x, typename undirected_graph::vertex_id_t y,Args&& ...args) { - edge_id result = edge_repository.add_stock(std::forward(args)...); + typename undirected_graph::edge_id_t result = edge_repository.add_stock(std::forward(args)...); get_edge(result).set_associated_vertices(x,y); get_vertex(x).add_associated_edge(result); get_vertex(y).add_associated_edge(result); return result; } - void remove_edge(vertex_id x, vertex_id y) + void remove_edge(typename undirected_graph::vertex_id_t x, typename undirected_graph::vertex_id_t y) { remove_edge(get_edge(x,y)); } - void remove_edge(edge_id e) + void remove_edge(typename undirected_graph::edge_id_t e) { pair_t vertices = get_edge(e).get_associated_vertices(); get_vertex(vertices.first).remove_associated_edge(e); remove_edge_from(e,vertices.second); } - edge_id get_edge(vertex_id x, vertex_id y) const + typename undirected_graph::edge_id_t get_edge(typename undirected_graph::vertex_id_t x, typename undirected_graph::vertex_id_t y) const { assert(adjacent(x, y)); if(get_vertex(x).get_associated_edges().size() < get_vertex(y).get_associated_edges().size()) @@ -315,69 +344,69 @@ namespace lzhlib else return get_edge_impl(y, x); } - pair_t get_associated_vertices(edge_id e) const + pair_t get_associated_vertices(typename undirected_graph::edge_id_t e) const { return get_edge(e).get_associated_vertices(); } - vertex_value_t& value(vertex_id v) + typename undirected_graph::vertex_value_t& value(typename undirected_graph::vertex_id_t v) { return get_vertex(v).vertex_value(); } - vertex_value_t const& value(vertex_id v) const + typename undirected_graph::vertex_value_t const& value(typename undirected_graph::vertex_id_t v) const { return get_vertex(v).vertex_value(); } - edge_value_t& value(edge_id e) + typename undirected_graph::edge_value_t& value(typename undirected_graph::edge_id_t e) { return get_edge(e).edge_value(); } - edge_value_t const& value(edge_id e) const + typename undirected_graph::edge_value_t const& value(typename undirected_graph::edge_id_t e) const { return get_edge(e).edge_value(); } - - vertex_id first_vertex() const + + typename undirected_graph::vertex_id_t first_vertex() const { return vertex_repository.first_stock(); } - bool is_last_vertex(vertex_id id) const + bool is_last_vertex(typename undirected_graph::vertex_id_t id) const { return vertex_repository.is_last_stock(id); } - vertex_id next_vertex(vertex_id id) const + typename undirected_graph::vertex_id_t next_vertex(typename undirected_graph::vertex_id_t id) const { return vertex_repository.next_stock(id); } private: - vertex_t& get_vertex(vertex_id v) + typename undirected_graph::vertex_t& get_vertex(typename undirected_graph::vertex_id_t v) { return vertex_repository.get_stock(v); } - vertex_t const& get_vertex(vertex_id v) const + typename undirected_graph::vertex_t const& get_vertex(typename undirected_graph::vertex_id_t v) const { return vertex_repository.get_stock(v); } - edge_t& get_edge(edge_id e) + typename undirected_graph::edge_t& get_edge(typename undirected_graph::edge_id_t e) { return edge_repository.get_stock(e); } - edge_t const& get_edge(edge_id e) const + typename undirected_graph::edge_t const& get_edge(typename undirected_graph::edge_id_t e) const { return edge_repository.get_stock(e); } - bool adjacent_impl(vertex_id x, vertex_id y) const + bool adjacent_impl(typename undirected_graph::vertex_id_t x, typename undirected_graph::vertex_id_t y) const { - std::set const& edges = get_vertex(x).get_associated_edges(); - for(edge_id e : edges) + auto const& edges = get_vertex(x).get_associated_edges(); + for(auto e : edges) { if(get_edge(e).is_associated(y)) return true; } return false; } - vertex_id get_opposite_vertex(vertex_id v, edge_id e) const + typename undirected_graph::vertex_id_t get_opposite_vertex(typename undirected_graph::vertex_id_t v, typename undirected_graph::edge_id_t e) const { pair_t vertices = get_edge(e).get_associated_vertices(); if(vertices.first == v) @@ -390,24 +419,24 @@ namespace lzhlib return vertices.first; } } - void remove_edge_from(edge_id e, vertex_id v) + void remove_edge_from(typename undirected_graph::edge_id_t e, typename undirected_graph::vertex_id_t v) { get_vertex(get_opposite_vertex(v, e)).remove_associated_edge(e); edge_repository.remove_stock(e); } - edge_id get_edge_impl(vertex_id x, vertex_id y) const + typename undirected_graph::edge_id_t get_edge_impl(typename undirected_graph::vertex_id_t x, typename undirected_graph::vertex_id_t y) const { - std::set const& edges = get_vertex(x).get_associated_edges(); - for(edge_id e : edges) + auto const& edges = get_vertex(x).get_associated_edges(); + for(auto e : edges) { if(get_edge(e).is_associated(y)) return e; } - exceptions::throw_exception_require_edge_that_does_not_exist(x, y); + exceptions::throw_exception_require_edge_that_does_not_exist(); } - repository vertex_repository; - repository edge_repository; + repository vertex_repository; + repository edge_repository; }; } diff --git a/include/repository.h b/include/repository.h index 6c83036..e6b3def 100644 --- a/include/repository.h +++ b/include/repository.h @@ -2,130 +2,71 @@ #define REPOSITORY_H_INCLUDED #include #include -#include -#include +#include +#include namespace lzhlib { - namespace traits + template + class id { - template - struct stock_traits + template + friend class repository; + public: + using stock_t = StockT; + + bool operator==(id rhs) const { - }; - } + return pointer == rhs.pointer; + } + bool operator!=(id rhs) const + { + return !(*this::iterator pointer; + }; template class repository { public: using stock_t = StockT; - using id_t = typename traits::stock_traits::id_t; + using id_t = id; - class attempt_to_use_unassigned_stock: public std::out_of_range - { - public: - attempt_to_use_unassigned_stock(id_t id) - : out_of_range(std::string("Attempt to use unassigned stock which id is ") + std::to_string(id.id) + "!") - { - - } - }; - class attempt_to_reuse_unreusable_stock: public std::out_of_range - { - public: - attempt_to_reuse_unreusable_stock(id_t id) - : out_of_range(std::string("Attempt to reuse unreusable stock which id is ") + std::to_string(id.id) + "!") - { - } - }; stock_t& get_stock(id_t id) { -#ifndef NDEBUG - if(ids_of_living_stocks.find(id) == ids_of_living_stocks.end()) - { - throw attempt_to_use_unassigned_stock(id); - } -#endif // NDEBUG - return stocks[id.id]; + return *id.pointer; } stock_t const& get_stock(id_t id) const { -#ifndef NDEBUG - if(ids_of_living_stocks.find(id) == ids_of_living_stocks.end()) - { - throw attempt_to_use_unassigned_stock(id); - } -#endif // NDEBUG - return stocks[id.id]; + return *id.pointer; } template id_t add_stock(Args&&... args) { - if(ids_of_reusable_stocks.empty()) - { - return allocate_stock(std::forward(args)...); - } - else - { - return reuse_stock(std::forward(args)...); - } + return {stocks.emplace(stocks.begin(),std::forward(args)...)}; } void remove_stock(id_t id) { - get_stock(id).~stock_t(); - ids_of_living_stocks.erase(id); - ids_of_reusable_stocks.insert(id); + stocks.erase(id.pointer); } id_t first_stock() const { - return *ids_of_living_stocks.begin(); + return {stocks.begin()}; } bool is_last_stock(id_t current_stock) const { - return ++(ids_of_living_stocks.find(current_stock)) == ids_of_living_stocks.end(); + return current_stock == stocks.end(); } id_t next_stock(id_t current_stock) const { - return *++(ids_of_living_stocks.find(current_stock)); - } - private: - template - id_t allocate_stock(Args&&... args) - { - stocks.emplace_back(std::forward(args)...); - id_t ret{stocks.size() - 1}; - ids_of_living_stocks.insert(ret); - return ret; - } - template - id_t reuse_stock(Args&&... args) - { - typename std::set::iterator i = iterator_of_a_reusable_stock(); - id_t ret = *i; - new(&get_reusable_stock(ret)) stock_t(std::forward(args)...); - ids_of_reusable_stocks.erase(i); - ids_of_living_stocks.insert(ret); - return ret; - } - typename std::set::iterator iterator_of_a_reusable_stock() const - { - return ids_of_reusable_stocks.begin(); - } - stock_t& get_reusable_stock(id_t id) - { -#ifndef NDEBUG - if(ids_of_reusable_stocks.find(id) == ids_of_reusable_stocks.end()) - { - throw attempt_to_reuse_unreusable_stock(id); - } -#endif // NDEBUG - return stocks[id.id]; + id_t next = current_stock; + ++next.pointer; + return next; } private: - std::vector stocks; - std::set ids_of_living_stocks; - std::set ids_of_reusable_stocks; + std::list stocks; }; } diff --git a/test/test.cpp b/test/test.cpp index 97cc030..bbf39b4 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -6,99 +6,93 @@ using namespace std; using namespace lzhlib; using namespace lzhlib::detail; -void test_vertex() -{ - vertex v{}; - edge_id e1{1}, e2{2}; - v.add_associated_edge(e1); - std::set s = v.get_associated_edges(); - assert(s.size() == 1); - assert(s.find(e1) != s.end() && s.find(e2) == s.end()); - - v.add_associated_edge(e2); - s = v.get_associated_edges(); - assert(s.size() == 2); - assert(s.find(e1) != s.end() && s.find(e2) != s.end()); - - v.remove_associated_edge(e1); - s = v.get_associated_edges(); - assert(s.size() == 1); - assert(s.find(e1) == s.end() && s.find(e2) != s.end()); - - v.remove_associated_edge(e2); - s = v.get_associated_edges(); - assert(s.empty()); - assert(s.find(e1) == s.end() && s.find(e2) == s.end()); - - v.vertex_value() = 1; - assert(v.vertex_value() == 1); - v.vertex_value() = 2; - assert(v.vertex_value() == 2); -} -void test_edge() -{ - edge e; - vertex_id v1{0}, v2{1}; - e.set_associated_vertices(v1, v2); - assert(e.is_associated(v1) && e.is_associated(v2)); - - edge::pair_t p1 = e.get_associated_vertices(); - assert((p1.first == v1 && p1.second == v2) || (p1.first == v2 && p1.first == v1)); - - e.detach(); - p1 = e.get_associated_vertices(); - assert(p1.first == invalid_vertex_id && p1.second == invalid_vertex_id); - assert(!e.is_associated(v1) && !e.is_associated(v2)); - - e.edge_value() = 1; - assert(e.edge_value() == 1); - e.edge_value() = 2; - assert(e.edge_value() == 2); -} +//void test_vertex() +//{ +// vertex v{}; +// edge_id e1{1}, e2{2}; +// v.add_associated_edge(e1); +// std::set s = v.get_associated_edges(); +// assert(s.size() == 1); +// assert(s.find(e1) != s.end() && s.find(e2) == s.end()); +// +// v.add_associated_edge(e2); +// s = v.get_associated_edges(); +// assert(s.size() == 2); +// assert(s.find(e1) != s.end() && s.find(e2) != s.end()); +// +// v.remove_associated_edge(e1); +// s = v.get_associated_edges(); +// assert(s.size() == 1); +// assert(s.find(e1) == s.end() && s.find(e2) != s.end()); +// +// v.remove_associated_edge(e2); +// s = v.get_associated_edges(); +// assert(s.empty()); +// assert(s.find(e1) == s.end() && s.find(e2) == s.end()); +// +// v.vertex_value() = 1; +// assert(v.vertex_value() == 1); +// v.vertex_value() = 2; +// assert(v.vertex_value() == 2); +//} +//void test_edge() +//{ +// edge e; +// vertex_id v1{0}, v2{1}; +// e.set_associated_vertices(v1, v2); +// assert(e.is_associated(v1) && e.is_associated(v2)); +// +// edge::pair_t p1 = e.get_associated_vertices(); +// assert((p1.first == v1 && p1.second == v2) || (p1.first == v2 && p1.first == v1)); +// +// e.detach(); +// p1 = e.get_associated_vertices(); +// assert(p1.first == invalid_vertex_id && p1.second == invalid_vertex_id); +// assert(!e.is_associated(v1) && !e.is_associated(v2)); +// +// e.edge_value() = 1; +// assert(e.edge_value() == 1); +// e.edge_value() = 2; +// assert(e.edge_value() == 2); +//} void test_vertex_repository() { - repository> r0; - vertex_id i0 = r0.add_stock(1); + using t = traits; + repository> r0; + auto i0 = r0.add_stock(1); assert(r0.get_stock(i0).vertex_value() == 1); r0.remove_stock(i0); - try - { - r0.get_stock(i0); - } - catch(repository>::attempt_to_use_unassigned_stock e) - { - assert(e.what() == std::string("Attempt to use unassigned stock which id is ") + std::to_string(i0.id) + "!"); - } - vertex_id i1 = r0.add_stock(2); - assert(r0.ids_of_living_stocks.size() == 1); - assert(r0.ids_of_reusable_stocks.size() == 0); + auto i1 = r0.add_stock(2); +// assert(r0.ids_of_living_stocks.size() == 1); +// assert(r0.ids_of_reusable_stocks.size() == 0); assert(r0.get_stock(i1).vertex_value() == 2); - vertex_id i2 = r0.add_stock(3); - assert(r0.ids_of_living_stocks.size() == 2); - assert(r0.ids_of_reusable_stocks.size() == 0); + auto i2 = r0.add_stock(3); +// assert(r0.ids_of_living_stocks.size() == 2); +// assert(r0.ids_of_reusable_stocks.size() == 0); assert(r0.get_stock(i1).vertex_value() == 2); assert(r0.get_stock(i2).vertex_value() == 3); r0.remove_stock(i1); - assert(r0.ids_of_living_stocks.size() == 1); - assert(r0.ids_of_reusable_stocks.size() == 1); +// assert(r0.ids_of_living_stocks.size() == 1); +// assert(r0.ids_of_reusable_stocks.size() == 1); assert(r0.get_stock(i2).vertex_value() == 3); r0.remove_stock(i2); - assert(r0.ids_of_living_stocks.size() == 0); - assert(r0.ids_of_reusable_stocks.size() == 2); +// assert(r0.ids_of_living_stocks.size() == 0); +// assert(r0.ids_of_reusable_stocks.size() == 2); } void test_vertex_edge_and_repository() { - repository> rv; - repository> re; + using t = traits; + repository> rv; + repository> re; - vertex_id v0 = rv.add_stock("a"); - vertex_id v1 = rv.add_stock("b"); - edge_id e0 = re.add_stock("a and b"); + auto v0 = rv.add_stock("a"); + auto v1 = rv.add_stock("b"); + auto e0 = re.add_stock("a and b"); rv.get_stock(v0).add_associated_edge(e0); rv.get_stock(v1).add_associated_edge(e0); @@ -107,11 +101,12 @@ void test_vertex_edge_and_repository() assert(re.get_stock(e0).is_associated(v0)); assert(re.get_stock(e0).is_associated(v1)); - std::setconst& s0 = rv.get_stock(v0).get_associated_edges(); - assert(s0.find(e0) != s0.end()); + auto const& vector0 = rv.get_stock(v0).get_associated_edges(); + cout<const& s1 = rv.get_stock(v1).get_associated_edges(); - assert(s1.find(e0) != s1.end()); + auto const& vector1 = rv.get_stock(v1).get_associated_edges(); + assert(find(vector1.begin(),vector1.end(),e0) != vector1.end()); } class test_undirected_graph { @@ -119,11 +114,12 @@ class test_undirected_graph void operator()() { { - undirected_graph g; - vertex_id v0 = g.add_vertex("v0"); - vertex_id v1 = g.add_vertex("v1"); + using t = traits; + undirected_graph g; + auto v0 = g.add_vertex("v0"); + auto v1 = g.add_vertex("v1"); { - edge_id e0 = g.add_edge(v0, v1); + auto e0 = g.add_edge(v0, v1); assert(g.value(e0) == ""); g.value(e0) = "e0"; assert(g.adjacent(v0, v1)); @@ -139,7 +135,7 @@ class test_undirected_graph assert(!g.adjacent(v1, v0)); } { - edge_id e1 = g.add_edge(v0, v1, "e1"); + auto e1 = g.add_edge(v0, v1, "e1"); assert(g.value(e1) == "e1"); assert(g.adjacent(v0, v1)); assert(g.adjacent(v1, v0)); @@ -156,8 +152,6 @@ class test_undirected_graph }; int main() { - test_vertex(); - test_edge(); test_vertex_repository(); test_vertex_edge_and_repository(); test_undirected_graph()(); From 720b6e95c395a2e667cf450b73a8b93fdda65f43 Mon Sep 17 00:00:00 2001 From: LiZhenhuan1019 Date: Fri, 5 Aug 2016 00:27:02 +0800 Subject: [PATCH 2/2] update TODO.txt --- TODO.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/TODO.txt b/TODO.txt index 137166f..648d9e0 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,2 +1,3 @@ 1.将图的repository换成只存边的数据,顶点中存边的数据的id以及对面顶点的id. -2.repository中,在删除stock之后若repository被析构,stock将可能被析构两次.考虑用链表或vector>实现repository \ No newline at end of file +2.repository中,在删除stock之后若repository被析构,stock将可能被析构两次.考虑用链表或vector>实现repository + ^done. \ No newline at end of file