|
| 1 | +#include <catch2/catch.hpp> |
| 2 | +//#include "csv_routes.hpp" |
| 3 | +#include <tuple> |
| 4 | +#include <vector> |
| 5 | +#include <string> |
| 6 | +#include <iostream> |
| 7 | +#include "graph/graph.hpp" |
| 8 | +#include "graph/views/breadth_first_search.hpp" |
| 9 | +#include "graph/container/compressed_graph.hpp" |
| 10 | + |
| 11 | +using std::vector; |
| 12 | +using std::string; |
| 13 | +using std::tuple; |
| 14 | +using std::get; |
| 15 | +using std::declval; |
| 16 | + |
| 17 | +using std::is_const_v; |
| 18 | +using std::is_reference_v; |
| 19 | +using std::is_lvalue_reference_v; |
| 20 | +using std::is_same_v; |
| 21 | +using std::remove_reference_t; |
| 22 | +using std::add_lvalue_reference_t; |
| 23 | + |
| 24 | +using std::integral; |
| 25 | +using std::ranges::forward_range; |
| 26 | +using std::ranges::random_access_range; |
| 27 | + |
| 28 | +using std::graph::vertex_range_t; |
| 29 | +using std::graph::vertex_iterator_t; |
| 30 | +using std::graph::vertex_reference_t; |
| 31 | +using std::graph::vertex_edge_range_t; |
| 32 | +using std::graph::edge_reference_t; |
| 33 | +using std::graph::vertex_id_t; |
| 34 | +using namespace std::graph::views; |
| 35 | + |
| 36 | +#if 0 |
| 37 | +#if 0 |
| 38 | +template <class G1a> |
| 39 | +auto check1(G1a&& g) { |
| 40 | + static_assert(!is_const_v<G1a>); |
| 41 | +} |
| 42 | +template <class G2b> |
| 43 | +auto check2(G2b&& g) { |
| 44 | + static_assert(is_const_v<std::remove_reference_t<G2b>>); |
| 45 | + //static_assert(is_const_v<decltype(g)>); |
| 46 | + //static_assert(is_const_v<G2b>); |
| 47 | +} |
| 48 | +template <class G3c> |
| 49 | +auto check3(G3c&& g) { |
| 50 | + static_assert(is_const_v<std::remove_reference_t<G3c>>); |
| 51 | + static_assert(std::is_volatile_v<std::remove_reference_t<G3c>>); |
| 52 | + //static_assert(is_const_v<decltype(g)>); |
| 53 | + //static_assert(is_const_v<G3c>); |
| 54 | +} |
| 55 | +void check_test() { |
| 56 | + using G = vector<vector<tuple<int>>>; |
| 57 | + { |
| 58 | + using G1 = G; |
| 59 | + G1 g; |
| 60 | + check1(g); |
| 61 | + } |
| 62 | + { |
| 63 | + using G2 = const G; |
| 64 | + static_assert(is_const_v<G2>); |
| 65 | + G2 g; |
| 66 | + G2& g2 = g; |
| 67 | + static_assert(is_const_v<decltype(g)>); |
| 68 | + check2(g); |
| 69 | + static_assert(is_const_v<std::remove_reference_t<decltype(g2)>>); |
| 70 | + check2(g2); |
| 71 | + } |
| 72 | + { |
| 73 | + using G3 = const volatile G; |
| 74 | + static_assert(is_const_v<G3>); |
| 75 | + static_assert(std::is_volatile_v<G3>); |
| 76 | + G3 g; |
| 77 | + G3& g3 = g; |
| 78 | + static_assert(is_const_v<decltype(g)>); |
| 79 | + check3(g); |
| 80 | + static_assert(is_const_v<std::remove_reference_t<decltype(g3)>>); |
| 81 | + check3(g3); |
| 82 | + } |
| 83 | +} |
| 84 | +#endif |
| 85 | + |
| 86 | +namespace mygraph { |
| 87 | +using vertex_id_type = int; |
| 88 | +using edge_type = tuple<vertex_id_type>; |
| 89 | +using edges_type = vector<edge_type>; |
| 90 | +using vertices_type = vector<edges_type>; |
| 91 | +using Graph = vertices_type; |
| 92 | + |
| 93 | +vertex_id_type vertex_id(const Graph& g, vertex_iterator_t<const Graph> it) { return static_cast<vertex_id_type>(it - std::ranges::begin(std::graph::vertices(g))); } |
| 94 | +vertex_id_type target_id(const Graph& g, const edge_type& uv) noexcept { return get<0>(uv); } |
| 95 | + |
| 96 | +//using Graph = vector<vector<tuple<int>>>; |
| 97 | +//vertex_id_t<const Graph> target_id(const Graph& g, edge_reference_t<const Graph> uv) noexcept { return get<0>(uv); } |
| 98 | +static_assert(is_same_v<edge_reference_t<const Graph>, const tuple<int>&>); |
| 99 | +} // namespace mygraph |
| 100 | + |
| 101 | +vector<string> actors{"Tom Cruise", "Kevin Bacon", "Hugo Weaving", "Carrie-Anne Moss", "Natalie Portman", |
| 102 | + "Jack Nicholson", "Kelly McGillis", "Harrison Ford", "Sebastian Stan", "Mila Kunis", |
| 103 | + "Michelle Pfeiffer", "Keanu Reeves", "Julia Roberts"}; |
| 104 | + |
| 105 | +mygraph::Graph costar_adjacency_list{{1, 5, 6}, {7, 10, 0, 5, 12}, {4, 3, 11}, {2, 11}, {8, 9, 2, 12}, {0, 1}, |
| 106 | + {7, 0}, {6, 1, 10}, {4, 9}, {4, 8}, {7, 1}, {2, 3}, |
| 107 | + {1, 4}}; |
| 108 | + |
| 109 | +// in case the previous def of target_id(g,uv) isn't found... |
| 110 | +vertex_id_t<const mygraph::Graph> target_id(const mygraph::Graph& g, |
| 111 | + edge_reference_t<const mygraph::Graph> uv) noexcept { |
| 112 | + return get<0>(uv); |
| 113 | +} |
| 114 | + |
| 115 | +#if 1 |
| 116 | +TEST_CASE("const vertex-vertex-tuple types", "[compressed][bfs][example][bacon]") { |
| 117 | + using G = const mygraph::Graph; |
| 118 | + using _UnCV = std::remove_cvref_t<G>; |
| 119 | + |
| 120 | + static_assert(random_access_range<vertex_range_t<G>>, "vertices range is random access"); |
| 121 | + static_assert(is_lvalue_reference_v<vertex_range_t<G>>, "vertices range is an lvalue reference"); |
| 122 | + static_assert(is_const_v<remove_reference_t<vertex_range_t<G>>>, "vertices ranges is const"); |
| 123 | + static_assert(is_same_v<vertex_range_t<G>, const vector<vector<tuple<int>>>&>, |
| 124 | + "vertices range is a const vector<vector<tuple<int>>> reference"); |
| 125 | + |
| 126 | + static_assert(integral<vertex_id_t<G>>, "Vertex id is integral"); |
| 127 | + static_assert(is_lvalue_reference_v<vertex_range_t<G>>, "vertices range is an lvalue reference"); |
| 128 | + static_assert(!is_const_v<remove_reference_t<vertex_id_t<G>>>, "Vertex id is not const"); |
| 129 | + |
| 130 | + static_assert(forward_range<vertex_range_t<G>>, "vertex edges range is forward range"); |
| 131 | + static_assert(std::is_lvalue_reference_v<vertex_edge_range_t<G>>, "vertex edges range is an lvalue reference"); |
| 132 | + static_assert(is_const_v<remove_reference_t<vertex_edge_range_t<G>>>, "vertex edges range is const"); |
| 133 | + static_assert(std::is_same_v<vertex_edge_range_t<G>, const vector<tuple<int>>&>, |
| 134 | + "vertex edges range is a const vertex<tuple<int>> reference"); |
| 135 | + |
| 136 | + static_assert(std::is_lvalue_reference_v<edge_reference_t<G>>, "Edge is a reference"); |
| 137 | + static_assert(is_const_v<remove_reference_t<edge_reference_t<G>>>, "Edge is const"); |
| 138 | + static_assert(std::is_same_v<edge_reference_t<G>, const tuple<int>&>, "Edge is a const tuple<int> reference"); |
| 139 | + static_assert(std::is_same_v<edge_reference_t<G>, std::add_lvalue_reference_t<const tuple<int>>>, "Edge is a const tuple<int> reference"); |
| 140 | + |
| 141 | + static_assert(std::graph::_Edges::_Can_ref_eval<G, _UnCV>, "Can auto evaluate edges(g,u)"); |
| 142 | + static_assert(std::graph::_Edges::_Can_id_eval<G, _UnCV>, "Can auto evaluate edges(g,uid)"); |
| 143 | + |
| 144 | + static_assert(!std::graph::_Target_id::_Has_ref_member<G, _UnCV>, "Has no member target_id(g,uv)"); |
| 145 | + static_assert(std::graph::_Target_id::_Has_ref_ADL<G, _UnCV>, "Has ADL target_id(g,uv)"); |
| 146 | + |
| 147 | + static_assert(std::graph::_Target::_Can_ref_eval<G, _UnCV>, "Can auto evaluate target(g,uv)"); |
| 148 | + //static_assert(std::is_same_v<vertex_reference_t<G>, |
| 149 | + // decltype(std::graph::target(declval<G>(), declval<edge_reference_t<G>>()))>); |
| 150 | + |
| 151 | + for (auto&& u : std::graph::vertices(costar_adjacency_list)) { |
| 152 | + for (auto&& uv : std::graph::edges(costar_adjacency_list, u)) { |
| 153 | + auto vid = std::graph::target_id(costar_adjacency_list, uv); |
| 154 | + static_assert(std::integral<decltype(vid)>); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
| 158 | +#endif |
| 159 | + |
| 160 | +TEST_CASE("non-const vertex-vertex-tuple types", "[compressed][bfs][example][bacon]") { |
| 161 | + using G = mygraph::Graph; |
| 162 | + using _UnCV = std::remove_cvref_t<G>; |
| 163 | + static_assert(std::ranges::random_access_range<std::graph::vertex_range_t<G>>, "vertices range is a reference"); |
| 164 | + static_assert(std::is_same_v<vertex_range_t<G>, vector<vector<tuple<int>>>&>, |
| 165 | + "vertices range is a vector<vector<tuple<int>>> reference"); |
| 166 | + |
| 167 | + static_assert(std::integral<vertex_id_t<G>>, "Vertex id is integral"); |
| 168 | + |
| 169 | + static_assert(std::is_lvalue_reference_v<vertex_edge_range_t<G>>, "edges range is a reference"); |
| 170 | + static_assert(std::is_same_v<vertex_edge_range_t<G>, vector<tuple<int>>&>, |
| 171 | + "edges range is a vertex<tuple<int>> reference"); |
| 172 | + |
| 173 | + static_assert(std::is_lvalue_reference_v<edge_reference_t<G>>, "Edge is a reference"); |
| 174 | + static_assert(std::is_same_v<edge_reference_t<G>, tuple<int>&>, "Edge is a tuple<int> reference"); |
| 175 | + //static_assert(std::is_same_v<edge_reference_t<G>, std::add_lvalue_reference<tuple<int>>>, "Edge is a tuple<int> reference"); |
| 176 | + |
| 177 | + static_assert(std::graph::_Edges::_Can_ref_eval<G, _UnCV>, "Can auto evaluate edges(g,u)"); |
| 178 | + static_assert(std::graph::_Edges::_Can_id_eval<G, _UnCV>, "Can auto evaluate edges(g,uid)"); |
| 179 | + |
| 180 | + static_assert(!std::graph::_Target_id::_Has_ref_member<G, _UnCV>, "Has no member target_id(g,uv)"); |
| 181 | + static_assert(std::graph::_Target_id::_Has_ref_ADL<G, _UnCV>, "Has ADL target_id(g,uv)"); |
| 182 | + |
| 183 | + static_assert(std::graph::_Target::_Can_ref_eval<G, _UnCV>, "Can auto evaluate target(g,uv)"); |
| 184 | + static_assert(std::is_same_v<vertex_reference_t<G>, |
| 185 | + decltype(std::graph::target(declval<G>(), declval<edge_reference_t<G>>()))>); |
| 186 | + |
| 187 | + for (auto&& u : std::graph::vertices(costar_adjacency_list)) { |
| 188 | + for (auto&& uv : std::graph::edges(costar_adjacency_list, u)) { |
| 189 | + auto vid = std::graph::target_id(costar_adjacency_list, uv); |
| 190 | + static_assert(std::integral<decltype(vid)>); |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | + |
| 196 | +TEST_CASE("Kevin Bacon example", "[compressed][bfs][example][bacon]") { |
| 197 | + using G = mygraph::Graph; |
| 198 | + |
| 199 | + vector<int> bacon_number(size(actors)); |
| 200 | + //auto evf = [](edge_reference_t<G> uv) { return uv; }; |
| 201 | + |
| 202 | + // 1 -> Kevin Bacon |
| 203 | + for (auto&& [uid, vid, uv] : sourced_edges_breadth_first_search(costar_adjacency_list, 1)) { |
| 204 | + bacon_number[vid] = bacon_number[uid] + 1; |
| 205 | + } |
| 206 | + |
| 207 | + for (int i = 0; i < size(actors); ++i) { |
| 208 | + std::cout << actors[i] << " has Bacon number " << bacon_number[i] << std::endl; |
| 209 | + } |
| 210 | +} |
| 211 | +#endif //0 |
0 commit comments