annotate DEPENDENCIES/generic/include/boost/graph/grid_graph.hpp @ 133:4acb5d8d80b6 tip

Don't fail environmental check if README.md exists (but .txt and no-suffix don't)
author Chris Cannam
date Tue, 30 Jul 2019 12:25:44 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 //=======================================================================
Chris@16 2 // Copyright 2009 Trustees of Indiana University.
Chris@16 3 // Authors: Michael Hansen, Andrew Lumsdaine
Chris@16 4 //
Chris@16 5 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 6 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 //=======================================================================
Chris@16 9
Chris@16 10 #ifndef BOOST_GRAPH_GRID_GRAPH_HPP
Chris@16 11 #define BOOST_GRAPH_GRID_GRAPH_HPP
Chris@16 12
Chris@16 13 #include <cmath>
Chris@16 14 #include <functional>
Chris@16 15 #include <numeric>
Chris@16 16
Chris@16 17 #include <boost/array.hpp>
Chris@16 18 #include <boost/bind.hpp>
Chris@16 19 #include <boost/limits.hpp>
Chris@16 20 #include <boost/graph/graph_traits.hpp>
Chris@16 21 #include <boost/graph/properties.hpp>
Chris@16 22 #include <boost/iterator/counting_iterator.hpp>
Chris@16 23 #include <boost/iterator/transform_iterator.hpp>
Chris@16 24 #include <boost/property_map/property_map.hpp>
Chris@16 25
Chris@16 26 #define BOOST_GRID_GRAPH_TEMPLATE_PARAMS \
Chris@16 27 std::size_t DimensionsT, typename VertexIndexT, \
Chris@16 28 typename EdgeIndexT
Chris@16 29
Chris@16 30 #define BOOST_GRID_GRAPH_TYPE \
Chris@16 31 grid_graph<DimensionsT, VertexIndexT, EdgeIndexT>
Chris@16 32
Chris@16 33 #define BOOST_GRID_GRAPH_TRAITS_T \
Chris@16 34 typename graph_traits<BOOST_GRID_GRAPH_TYPE >
Chris@16 35
Chris@16 36 namespace boost {
Chris@16 37
Chris@16 38 // Class prototype for grid_graph
Chris@16 39 template<BOOST_GRID_GRAPH_TEMPLATE_PARAMS>
Chris@16 40 class grid_graph;
Chris@16 41
Chris@16 42 //===================
Chris@16 43 // Index Property Map
Chris@16 44 //===================
Chris@16 45
Chris@16 46 template <typename Graph,
Chris@16 47 typename Descriptor,
Chris@16 48 typename Index>
Chris@16 49 struct grid_graph_index_map {
Chris@16 50 public:
Chris@16 51 typedef Index value_type;
Chris@16 52 typedef Index reference_type;
Chris@16 53 typedef reference_type reference;
Chris@16 54 typedef Descriptor key_type;
Chris@16 55 typedef readable_property_map_tag category;
Chris@16 56
Chris@16 57 grid_graph_index_map() { }
Chris@16 58
Chris@16 59 grid_graph_index_map(const Graph& graph) :
Chris@16 60 m_graph(&graph) { }
Chris@16 61
Chris@16 62 value_type operator[](key_type key) const {
Chris@16 63 return (m_graph->index_of(key));
Chris@16 64 }
Chris@16 65
Chris@16 66 friend inline Index
Chris@16 67 get(const grid_graph_index_map<Graph, Descriptor, Index>& index_map,
Chris@16 68 const typename grid_graph_index_map<Graph, Descriptor, Index>::key_type& key)
Chris@16 69 {
Chris@16 70 return (index_map[key]);
Chris@16 71 }
Chris@16 72
Chris@16 73 protected:
Chris@16 74 const Graph* m_graph;
Chris@16 75 };
Chris@16 76
Chris@16 77 template<BOOST_GRID_GRAPH_TEMPLATE_PARAMS>
Chris@16 78 struct property_map<BOOST_GRID_GRAPH_TYPE, vertex_index_t> {
Chris@16 79 typedef grid_graph_index_map<BOOST_GRID_GRAPH_TYPE,
Chris@16 80 BOOST_GRID_GRAPH_TRAITS_T::vertex_descriptor,
Chris@16 81 BOOST_GRID_GRAPH_TRAITS_T::vertices_size_type> type;
Chris@16 82 typedef type const_type;
Chris@16 83 };
Chris@16 84
Chris@16 85 template<BOOST_GRID_GRAPH_TEMPLATE_PARAMS>
Chris@16 86 struct property_map<BOOST_GRID_GRAPH_TYPE, edge_index_t> {
Chris@16 87 typedef grid_graph_index_map<BOOST_GRID_GRAPH_TYPE,
Chris@16 88 BOOST_GRID_GRAPH_TRAITS_T::edge_descriptor,
Chris@16 89 BOOST_GRID_GRAPH_TRAITS_T::edges_size_type> type;
Chris@16 90 typedef type const_type;
Chris@16 91 };
Chris@16 92
Chris@16 93 //==========================
Chris@16 94 // Reverse Edge Property Map
Chris@16 95 //==========================
Chris@16 96
Chris@16 97 template <typename Descriptor>
Chris@16 98 struct grid_graph_reverse_edge_map {
Chris@16 99 public:
Chris@16 100 typedef Descriptor value_type;
Chris@16 101 typedef Descriptor reference_type;
Chris@16 102 typedef reference_type reference;
Chris@16 103 typedef Descriptor key_type;
Chris@16 104 typedef readable_property_map_tag category;
Chris@16 105
Chris@16 106 grid_graph_reverse_edge_map() { }
Chris@16 107
Chris@16 108 value_type operator[](const key_type& key) const {
Chris@16 109 return (value_type(key.second, key.first));
Chris@16 110 }
Chris@16 111
Chris@16 112 friend inline Descriptor
Chris@16 113 get(const grid_graph_reverse_edge_map<Descriptor>& rev_map,
Chris@16 114 const typename grid_graph_reverse_edge_map<Descriptor>::key_type& key)
Chris@16 115 {
Chris@16 116 return (rev_map[key]);
Chris@16 117 }
Chris@16 118 };
Chris@16 119
Chris@16 120 template<BOOST_GRID_GRAPH_TEMPLATE_PARAMS>
Chris@16 121 struct property_map<BOOST_GRID_GRAPH_TYPE, edge_reverse_t> {
Chris@16 122 typedef grid_graph_reverse_edge_map<BOOST_GRID_GRAPH_TRAITS_T::edge_descriptor> type;
Chris@16 123 typedef type const_type;
Chris@16 124 };
Chris@16 125
Chris@16 126 //=================
Chris@16 127 // Function Objects
Chris@16 128 //=================
Chris@16 129
Chris@16 130 namespace detail {
Chris@16 131
Chris@16 132 // vertex_at
Chris@16 133 template <typename Graph>
Chris@16 134 struct grid_graph_vertex_at {
Chris@16 135
Chris@16 136 typedef typename graph_traits<Graph>::vertex_descriptor result_type;
Chris@16 137
Chris@16 138 grid_graph_vertex_at() : m_graph(0) {}
Chris@16 139
Chris@16 140 grid_graph_vertex_at(const Graph* graph) :
Chris@16 141 m_graph(graph) { }
Chris@16 142
Chris@16 143 result_type
Chris@16 144 operator()
Chris@16 145 (typename graph_traits<Graph>::vertices_size_type vertex_index) const {
Chris@16 146 return (vertex(vertex_index, *m_graph));
Chris@16 147 }
Chris@16 148
Chris@16 149 private:
Chris@16 150 const Graph* m_graph;
Chris@16 151 };
Chris@16 152
Chris@16 153 // out_edge_at
Chris@16 154 template <typename Graph>
Chris@16 155 struct grid_graph_out_edge_at {
Chris@16 156
Chris@16 157 private:
Chris@16 158 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
Chris@16 159
Chris@16 160 public:
Chris@16 161 typedef typename graph_traits<Graph>::edge_descriptor result_type;
Chris@16 162
Chris@16 163 grid_graph_out_edge_at() : m_vertex(), m_graph(0) {}
Chris@16 164
Chris@16 165 grid_graph_out_edge_at(vertex_descriptor source_vertex,
Chris@16 166 const Graph* graph) :
Chris@16 167 m_vertex(source_vertex),
Chris@16 168 m_graph(graph) { }
Chris@16 169
Chris@16 170 result_type
Chris@16 171 operator()
Chris@16 172 (typename graph_traits<Graph>::degree_size_type out_edge_index) const {
Chris@16 173 return (out_edge_at(m_vertex, out_edge_index, *m_graph));
Chris@16 174 }
Chris@16 175
Chris@16 176 private:
Chris@16 177 vertex_descriptor m_vertex;
Chris@16 178 const Graph* m_graph;
Chris@16 179 };
Chris@16 180
Chris@16 181 // in_edge_at
Chris@16 182 template <typename Graph>
Chris@16 183 struct grid_graph_in_edge_at {
Chris@16 184
Chris@16 185 private:
Chris@16 186 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
Chris@16 187
Chris@16 188 public:
Chris@16 189 typedef typename graph_traits<Graph>::edge_descriptor result_type;
Chris@16 190
Chris@16 191 grid_graph_in_edge_at() : m_vertex(), m_graph(0) {}
Chris@16 192
Chris@16 193 grid_graph_in_edge_at(vertex_descriptor target_vertex,
Chris@16 194 const Graph* graph) :
Chris@16 195 m_vertex(target_vertex),
Chris@16 196 m_graph(graph) { }
Chris@16 197
Chris@16 198 result_type
Chris@16 199 operator()
Chris@16 200 (typename graph_traits<Graph>::degree_size_type in_edge_index) const {
Chris@16 201 return (in_edge_at(m_vertex, in_edge_index, *m_graph));
Chris@16 202 }
Chris@16 203
Chris@16 204 private:
Chris@16 205 vertex_descriptor m_vertex;
Chris@16 206 const Graph* m_graph;
Chris@16 207 };
Chris@16 208
Chris@16 209 // edge_at
Chris@16 210 template <typename Graph>
Chris@16 211 struct grid_graph_edge_at {
Chris@16 212
Chris@16 213 typedef typename graph_traits<Graph>::edge_descriptor result_type;
Chris@16 214
Chris@16 215 grid_graph_edge_at() : m_graph(0) {}
Chris@16 216
Chris@16 217 grid_graph_edge_at(const Graph* graph) :
Chris@16 218 m_graph(graph) { }
Chris@16 219
Chris@16 220 result_type
Chris@16 221 operator()
Chris@16 222 (typename graph_traits<Graph>::edges_size_type edge_index) const {
Chris@16 223 return (edge_at(edge_index, *m_graph));
Chris@16 224 }
Chris@16 225
Chris@16 226 private:
Chris@16 227 const Graph* m_graph;
Chris@16 228 };
Chris@16 229
Chris@16 230 // adjacent_vertex_at
Chris@16 231 template <typename Graph>
Chris@16 232 struct grid_graph_adjacent_vertex_at {
Chris@16 233
Chris@16 234 public:
Chris@16 235 typedef typename graph_traits<Graph>::vertex_descriptor result_type;
Chris@16 236
Chris@16 237 grid_graph_adjacent_vertex_at(result_type source_vertex,
Chris@16 238 const Graph* graph) :
Chris@16 239 m_vertex(source_vertex),
Chris@16 240 m_graph(graph) { }
Chris@16 241
Chris@16 242 result_type
Chris@16 243 operator()
Chris@16 244 (typename graph_traits<Graph>::degree_size_type adjacent_index) const {
Chris@16 245 return (target(out_edge_at(m_vertex, adjacent_index, *m_graph), *m_graph));
Chris@16 246 }
Chris@16 247
Chris@16 248 private:
Chris@16 249 result_type m_vertex;
Chris@16 250 const Graph* m_graph;
Chris@16 251 };
Chris@16 252
Chris@16 253 } // namespace detail
Chris@16 254
Chris@16 255 //===========
Chris@16 256 // Grid Graph
Chris@16 257 //===========
Chris@16 258
Chris@16 259 template <std::size_t Dimensions,
Chris@16 260 typename VertexIndex = std::size_t,
Chris@16 261 typename EdgeIndex = VertexIndex>
Chris@16 262 class grid_graph {
Chris@16 263
Chris@16 264 private:
Chris@16 265 typedef boost::array<bool, Dimensions> WrapDimensionArray;
Chris@16 266 grid_graph() { };
Chris@16 267
Chris@16 268 public:
Chris@16 269
Chris@16 270 typedef grid_graph<Dimensions, VertexIndex, EdgeIndex> type;
Chris@16 271
Chris@16 272 // sizes
Chris@16 273 typedef VertexIndex vertices_size_type;
Chris@16 274 typedef EdgeIndex edges_size_type;
Chris@16 275 typedef EdgeIndex degree_size_type;
Chris@16 276
Chris@16 277 // descriptors
Chris@16 278 typedef boost::array<VertexIndex, Dimensions> vertex_descriptor;
Chris@16 279 typedef std::pair<vertex_descriptor, vertex_descriptor> edge_descriptor;
Chris@16 280
Chris@16 281 // vertex_iterator
Chris@16 282 typedef counting_iterator<vertices_size_type> vertex_index_iterator;
Chris@16 283 typedef detail::grid_graph_vertex_at<type> vertex_function;
Chris@16 284 typedef transform_iterator<vertex_function, vertex_index_iterator> vertex_iterator;
Chris@16 285
Chris@16 286 // edge_iterator
Chris@16 287 typedef counting_iterator<edges_size_type> edge_index_iterator;
Chris@16 288 typedef detail::grid_graph_edge_at<type> edge_function;
Chris@16 289 typedef transform_iterator<edge_function, edge_index_iterator> edge_iterator;
Chris@16 290
Chris@16 291 // out_edge_iterator
Chris@16 292 typedef counting_iterator<degree_size_type> degree_iterator;
Chris@16 293 typedef detail::grid_graph_out_edge_at<type> out_edge_function;
Chris@16 294 typedef transform_iterator<out_edge_function, degree_iterator> out_edge_iterator;
Chris@16 295
Chris@16 296 // in_edge_iterator
Chris@16 297 typedef detail::grid_graph_in_edge_at<type> in_edge_function;
Chris@16 298 typedef transform_iterator<in_edge_function, degree_iterator> in_edge_iterator;
Chris@16 299
Chris@16 300 // adjacency_iterator
Chris@16 301 typedef detail::grid_graph_adjacent_vertex_at<type> adjacent_vertex_function;
Chris@16 302 typedef transform_iterator<adjacent_vertex_function, degree_iterator> adjacency_iterator;
Chris@16 303
Chris@16 304 // categories
Chris@16 305 typedef directed_tag directed_category;
Chris@16 306 typedef disallow_parallel_edge_tag edge_parallel_category;
Chris@16 307 struct traversal_category : virtual public incidence_graph_tag,
Chris@16 308 virtual public adjacency_graph_tag,
Chris@16 309 virtual public vertex_list_graph_tag,
Chris@16 310 virtual public edge_list_graph_tag,
Chris@16 311 virtual public bidirectional_graph_tag,
Chris@16 312 virtual public adjacency_matrix_tag { };
Chris@16 313
Chris@16 314 static inline vertex_descriptor null_vertex()
Chris@16 315 {
Chris@16 316 vertex_descriptor maxed_out_vertex;
Chris@16 317 std::fill(maxed_out_vertex.begin(), maxed_out_vertex.end(),
Chris@16 318 (std::numeric_limits<vertices_size_type>::max)());
Chris@16 319
Chris@16 320 return (maxed_out_vertex);
Chris@16 321 }
Chris@16 322
Chris@16 323 // Constructor that defaults to no wrapping for all dimensions.
Chris@16 324 grid_graph(vertex_descriptor dimension_lengths) :
Chris@16 325 m_dimension_lengths(dimension_lengths) {
Chris@16 326
Chris@16 327 std::fill(m_wrap_dimension.begin(),
Chris@16 328 m_wrap_dimension.end(), false);
Chris@16 329
Chris@16 330 precalculate();
Chris@16 331 }
Chris@16 332
Chris@16 333 // Constructor that allows for wrapping to be specified for all
Chris@16 334 // dimensions at once.
Chris@16 335 grid_graph(vertex_descriptor dimension_lengths,
Chris@16 336 bool wrap_all_dimensions) :
Chris@16 337 m_dimension_lengths(dimension_lengths) {
Chris@16 338
Chris@16 339 std::fill(m_wrap_dimension.begin(),
Chris@16 340 m_wrap_dimension.end(),
Chris@16 341 wrap_all_dimensions);
Chris@16 342
Chris@16 343 precalculate();
Chris@16 344 }
Chris@16 345
Chris@16 346 // Constructor that allows for individual dimension wrapping to be
Chris@16 347 // specified.
Chris@16 348 grid_graph(vertex_descriptor dimension_lengths,
Chris@16 349 WrapDimensionArray wrap_dimension) :
Chris@16 350 m_dimension_lengths(dimension_lengths),
Chris@16 351 m_wrap_dimension(wrap_dimension) {
Chris@16 352
Chris@16 353 precalculate();
Chris@16 354 }
Chris@16 355
Chris@16 356 // Returns the number of dimensions in the graph
Chris@16 357 inline std::size_t dimensions() const {
Chris@16 358 return (Dimensions);
Chris@16 359 }
Chris@16 360
Chris@16 361 // Returns the length of dimension [dimension_index]
Chris@16 362 inline vertices_size_type length(std::size_t dimension) const {
Chris@16 363 return (m_dimension_lengths[dimension]);
Chris@16 364 }
Chris@16 365
Chris@16 366 // Returns a value indicating if dimension [dimension_index] wraps
Chris@16 367 inline bool wrapped(std::size_t dimension) const {
Chris@16 368 return (m_wrap_dimension[dimension]);
Chris@16 369 }
Chris@16 370
Chris@16 371 // Gets the vertex that is [distance] units ahead of [vertex] in
Chris@16 372 // dimension [dimension_index].
Chris@16 373 vertex_descriptor next
Chris@16 374 (vertex_descriptor vertex,
Chris@16 375 std::size_t dimension_index,
Chris@16 376 vertices_size_type distance = 1) const {
Chris@16 377
Chris@16 378 vertices_size_type new_position =
Chris@16 379 vertex[dimension_index] + distance;
Chris@16 380
Chris@16 381 if (wrapped(dimension_index)) {
Chris@16 382 new_position %= length(dimension_index);
Chris@16 383 }
Chris@16 384 else {
Chris@16 385 // Stop at the end of this dimension if necessary.
Chris@16 386 new_position =
Chris@16 387 (std::min)(new_position,
Chris@16 388 vertices_size_type(length(dimension_index) - 1));
Chris@16 389 }
Chris@16 390
Chris@16 391 vertex[dimension_index] = new_position;
Chris@16 392
Chris@16 393 return (vertex);
Chris@16 394 }
Chris@16 395
Chris@16 396 // Gets the vertex that is [distance] units behind [vertex] in
Chris@16 397 // dimension [dimension_index].
Chris@16 398 vertex_descriptor previous
Chris@16 399 (vertex_descriptor vertex,
Chris@16 400 std::size_t dimension_index,
Chris@16 401 vertices_size_type distance = 1) const {
Chris@16 402
Chris@16 403 // We're assuming that vertices_size_type is unsigned, so we
Chris@16 404 // need to be careful about the math.
Chris@16 405 vertex[dimension_index] =
Chris@16 406 (distance > vertex[dimension_index]) ?
Chris@16 407 (wrapped(dimension_index) ?
Chris@16 408 (length(dimension_index) - (distance % length(dimension_index))) : 0) :
Chris@16 409 vertex[dimension_index] - distance;
Chris@16 410
Chris@16 411 return (vertex);
Chris@16 412 }
Chris@16 413
Chris@16 414 protected:
Chris@16 415
Chris@16 416 // Returns the number of vertices in the graph
Chris@16 417 inline vertices_size_type num_vertices() const {
Chris@16 418 return (m_num_vertices);
Chris@16 419 }
Chris@16 420
Chris@16 421 // Returns the number of edges in the graph
Chris@16 422 inline edges_size_type num_edges() const {
Chris@16 423 return (m_num_edges);
Chris@16 424 }
Chris@16 425
Chris@16 426 // Returns the number of edges in dimension [dimension_index]
Chris@16 427 inline edges_size_type num_edges
Chris@16 428 (std::size_t dimension_index) const {
Chris@16 429 return (m_edge_count[dimension_index]);
Chris@16 430 }
Chris@16 431
Chris@16 432 // Returns the index of [vertex] (See also vertex_at)
Chris@16 433 vertices_size_type index_of(vertex_descriptor vertex) const {
Chris@16 434
Chris@16 435 vertices_size_type vertex_index = 0;
Chris@16 436 vertices_size_type index_multiplier = 1;
Chris@16 437
Chris@16 438 for (std::size_t dimension_index = 0;
Chris@16 439 dimension_index < Dimensions;
Chris@16 440 ++dimension_index) {
Chris@16 441
Chris@16 442 vertex_index += (vertex[dimension_index] * index_multiplier);
Chris@16 443 index_multiplier *= length(dimension_index);
Chris@16 444 }
Chris@16 445
Chris@16 446 return (vertex_index);
Chris@16 447 }
Chris@16 448
Chris@16 449 // Returns the vertex whose index is [vertex_index] (See also
Chris@16 450 // index_of(vertex_descriptor))
Chris@16 451 vertex_descriptor vertex_at
Chris@16 452 (vertices_size_type vertex_index) const {
Chris@16 453
Chris@16 454 boost::array<vertices_size_type, Dimensions> vertex;
Chris@16 455 vertices_size_type index_divider = 1;
Chris@16 456
Chris@16 457 for (std::size_t dimension_index = 0;
Chris@16 458 dimension_index < Dimensions;
Chris@16 459 ++dimension_index) {
Chris@16 460
Chris@16 461 vertex[dimension_index] = (vertex_index / index_divider) %
Chris@16 462 length(dimension_index);
Chris@16 463
Chris@16 464 index_divider *= length(dimension_index);
Chris@16 465 }
Chris@16 466
Chris@16 467 return (vertex);
Chris@16 468 }
Chris@16 469
Chris@16 470 // Returns the edge whose index is [edge_index] (See also
Chris@16 471 // index_of(edge_descriptor)). NOTE: The index mapping is
Chris@16 472 // dependent upon dimension wrapping.
Chris@16 473 edge_descriptor edge_at(edges_size_type edge_index) const {
Chris@16 474
Chris@16 475 // Edge indices are sorted into bins by dimension
Chris@16 476 std::size_t dimension_index = 0;
Chris@16 477 edges_size_type dimension_edges = num_edges(0);
Chris@16 478
Chris@16 479 while (edge_index >= dimension_edges) {
Chris@16 480 edge_index -= dimension_edges;
Chris@16 481 ++dimension_index;
Chris@16 482 dimension_edges = num_edges(dimension_index);
Chris@16 483 }
Chris@16 484
Chris@16 485 vertex_descriptor vertex_source, vertex_target;
Chris@16 486 bool is_forward = ((edge_index / (num_edges(dimension_index) / 2)) == 0);
Chris@16 487
Chris@16 488 if (wrapped(dimension_index)) {
Chris@16 489 vertex_source = vertex_at(edge_index % num_vertices());
Chris@16 490 vertex_target = is_forward ?
Chris@16 491 next(vertex_source, dimension_index) :
Chris@16 492 previous(vertex_source, dimension_index);
Chris@16 493 }
Chris@16 494 else {
Chris@16 495
Chris@16 496 // Dimensions can wrap arbitrarily, so an index needs to be
Chris@16 497 // computed in a more complex manner. This is done by
Chris@16 498 // grouping the edges for each dimension together into "bins"
Chris@16 499 // and considering [edge_index] as an offset into the bin.
Chris@16 500 // Each bin consists of two parts: the "forward" looking edges
Chris@16 501 // and the "backward" looking edges for the dimension.
Chris@16 502
Chris@16 503 edges_size_type vertex_offset = edge_index % num_edges(dimension_index);
Chris@16 504
Chris@16 505 // Consider vertex_offset an index into the graph's vertex
Chris@16 506 // space but with the dimension [dimension_index] reduced in
Chris@16 507 // size by one.
Chris@16 508 vertices_size_type index_divider = 1;
Chris@16 509
Chris@16 510 for (std::size_t dimension_index_iter = 0;
Chris@16 511 dimension_index_iter < Dimensions;
Chris@16 512 ++dimension_index_iter) {
Chris@16 513
Chris@16 514 std::size_t dimension_length = (dimension_index_iter == dimension_index) ?
Chris@16 515 length(dimension_index_iter) - 1 :
Chris@16 516 length(dimension_index_iter);
Chris@16 517
Chris@16 518 vertex_source[dimension_index_iter] = (vertex_offset / index_divider) %
Chris@16 519 dimension_length;
Chris@16 520
Chris@16 521 index_divider *= dimension_length;
Chris@16 522 }
Chris@16 523
Chris@16 524 if (is_forward) {
Chris@16 525 vertex_target = next(vertex_source, dimension_index);
Chris@16 526 }
Chris@16 527 else {
Chris@16 528 // Shift forward one more unit in the dimension for backward
Chris@16 529 // edges since the algorithm above will leave us one behind.
Chris@16 530 vertex_target = vertex_source;
Chris@16 531 ++vertex_source[dimension_index];
Chris@16 532 }
Chris@16 533
Chris@16 534 } // if (wrapped(dimension_index))
Chris@16 535
Chris@16 536 return (std::make_pair(vertex_source, vertex_target));
Chris@16 537 }
Chris@16 538
Chris@16 539 // Returns the index for [edge] (See also edge_at)
Chris@16 540 edges_size_type index_of(edge_descriptor edge) const {
Chris@16 541 vertex_descriptor source_vertex = source(edge, *this);
Chris@16 542 vertex_descriptor target_vertex = target(edge, *this);
Chris@16 543
Chris@16 544 BOOST_ASSERT (source_vertex != target_vertex);
Chris@16 545
Chris@16 546 // Determine the dimension where the source and target vertices
Chris@16 547 // differ (should only be one if this is a valid edge).
Chris@16 548 std::size_t different_dimension_index = 0;
Chris@16 549
Chris@16 550 while (source_vertex[different_dimension_index] ==
Chris@16 551 target_vertex[different_dimension_index]) {
Chris@16 552
Chris@16 553 ++different_dimension_index;
Chris@16 554 }
Chris@16 555
Chris@16 556 edges_size_type edge_index = 0;
Chris@16 557
Chris@16 558 // Offset the edge index into the appropriate "bin" (see edge_at
Chris@16 559 // for a more in-depth description).
Chris@16 560 for (std::size_t dimension_index = 0;
Chris@16 561 dimension_index < different_dimension_index;
Chris@16 562 ++dimension_index) {
Chris@16 563
Chris@16 564 edge_index += num_edges(dimension_index);
Chris@16 565 }
Chris@16 566
Chris@16 567 // Get the position of both vertices in the differing dimension.
Chris@16 568 vertices_size_type source_position = source_vertex[different_dimension_index];
Chris@16 569 vertices_size_type target_position = target_vertex[different_dimension_index];
Chris@16 570
Chris@16 571 // Determine if edge is forward or backward
Chris@16 572 bool is_forward = true;
Chris@16 573
Chris@16 574 if (wrapped(different_dimension_index)) {
Chris@16 575
Chris@16 576 // If the dimension is wrapped, an edge is going backward if
Chris@16 577 // either A: its target precedes the source in the differing
Chris@16 578 // dimension and the vertices are adjacent or B: its source
Chris@16 579 // precedes the target and they're not adjacent.
Chris@16 580 if (((target_position < source_position) &&
Chris@16 581 ((source_position - target_position) == 1)) ||
Chris@16 582 ((source_position < target_position) &&
Chris@16 583 ((target_position - source_position) > 1))) {
Chris@16 584
Chris@16 585 is_forward = false;
Chris@16 586 }
Chris@16 587 }
Chris@16 588 else if (target_position < source_position) {
Chris@16 589 is_forward = false;
Chris@16 590 }
Chris@16 591
Chris@16 592 // "Backward" edges are in the second half of the bin.
Chris@16 593 if (!is_forward) {
Chris@16 594 edge_index += (num_edges(different_dimension_index) / 2);
Chris@16 595 }
Chris@16 596
Chris@16 597 // Finally, apply the vertex offset
Chris@16 598 if (wrapped(different_dimension_index)) {
Chris@16 599 edge_index += index_of(source_vertex);
Chris@16 600 }
Chris@16 601 else {
Chris@16 602 vertices_size_type index_multiplier = 1;
Chris@16 603
Chris@16 604 if (!is_forward) {
Chris@16 605 --source_vertex[different_dimension_index];
Chris@16 606 }
Chris@16 607
Chris@16 608 for (std::size_t dimension_index = 0;
Chris@16 609 dimension_index < Dimensions;
Chris@16 610 ++dimension_index) {
Chris@16 611
Chris@16 612 edge_index += (source_vertex[dimension_index] * index_multiplier);
Chris@16 613 index_multiplier *= (dimension_index == different_dimension_index) ?
Chris@16 614 length(dimension_index) - 1 :
Chris@16 615 length(dimension_index);
Chris@16 616 }
Chris@16 617 }
Chris@16 618
Chris@16 619 return (edge_index);
Chris@16 620 }
Chris@16 621
Chris@16 622 // Returns the number of out-edges for [vertex]
Chris@16 623 degree_size_type out_degree(vertex_descriptor vertex) const {
Chris@16 624
Chris@16 625 degree_size_type out_edge_count = 0;
Chris@16 626
Chris@16 627 for (std::size_t dimension_index = 0;
Chris@16 628 dimension_index < Dimensions;
Chris@16 629 ++dimension_index) {
Chris@16 630
Chris@16 631 // If the vertex is on the edge of this dimension, then its
Chris@16 632 // number of out edges is dependent upon whether the dimension
Chris@16 633 // wraps or not.
Chris@16 634 if ((vertex[dimension_index] == 0) ||
Chris@16 635 (vertex[dimension_index] == (length(dimension_index) - 1))) {
Chris@16 636 out_edge_count += (wrapped(dimension_index) ? 2 : 1);
Chris@16 637 }
Chris@16 638 else {
Chris@16 639 // Next and previous edges, regardless or wrapping
Chris@16 640 out_edge_count += 2;
Chris@16 641 }
Chris@16 642 }
Chris@16 643
Chris@16 644 return (out_edge_count);
Chris@16 645 }
Chris@16 646
Chris@16 647 // Returns an out-edge for [vertex] by index. Indices are in the
Chris@16 648 // range [0, out_degree(vertex)).
Chris@16 649 edge_descriptor out_edge_at
Chris@16 650 (vertex_descriptor vertex,
Chris@16 651 degree_size_type out_edge_index) const {
Chris@16 652
Chris@16 653 edges_size_type edges_left = out_edge_index + 1;
Chris@16 654 std::size_t dimension_index = 0;
Chris@16 655 bool is_forward = false;
Chris@16 656
Chris@16 657 // Walks the out edges of [vertex] and accommodates for dimension
Chris@16 658 // wrapping.
Chris@16 659 while (edges_left > 0) {
Chris@16 660
Chris@16 661 if (!wrapped(dimension_index)) {
Chris@16 662 if (!is_forward && (vertex[dimension_index] == 0)) {
Chris@16 663 is_forward = true;
Chris@16 664 continue;
Chris@16 665 }
Chris@16 666 else if (is_forward &&
Chris@16 667 (vertex[dimension_index] == (length(dimension_index) - 1))) {
Chris@16 668 is_forward = false;
Chris@16 669 ++dimension_index;
Chris@16 670 continue;
Chris@16 671 }
Chris@16 672 }
Chris@16 673
Chris@16 674 --edges_left;
Chris@16 675
Chris@16 676 if (edges_left > 0) {
Chris@16 677 is_forward = !is_forward;
Chris@16 678
Chris@16 679 if (!is_forward) {
Chris@16 680 ++dimension_index;
Chris@16 681 }
Chris@16 682 }
Chris@16 683 }
Chris@16 684
Chris@16 685 return (std::make_pair(vertex, is_forward ?
Chris@16 686 next(vertex, dimension_index) :
Chris@16 687 previous(vertex, dimension_index)));
Chris@16 688 }
Chris@16 689
Chris@16 690 // Returns the number of in-edges for [vertex]
Chris@16 691 inline degree_size_type in_degree(vertex_descriptor vertex) const {
Chris@16 692 return (out_degree(vertex));
Chris@16 693 }
Chris@16 694
Chris@16 695 // Returns an in-edge for [vertex] by index. Indices are in the
Chris@16 696 // range [0, in_degree(vertex)).
Chris@16 697 edge_descriptor in_edge_at
Chris@16 698 (vertex_descriptor vertex,
Chris@16 699 edges_size_type in_edge_index) const {
Chris@16 700
Chris@16 701 edge_descriptor out_edge = out_edge_at(vertex, in_edge_index);
Chris@16 702 return (std::make_pair(target(out_edge, *this), source(out_edge, *this)));
Chris@16 703
Chris@16 704 }
Chris@16 705
Chris@16 706 // Pre-computes the number of vertices and edges
Chris@16 707 void precalculate() {
Chris@16 708 m_num_vertices =
Chris@16 709 std::accumulate(m_dimension_lengths.begin(),
Chris@16 710 m_dimension_lengths.end(),
Chris@16 711 vertices_size_type(1),
Chris@16 712 std::multiplies<vertices_size_type>());
Chris@16 713
Chris@16 714 // Calculate number of edges in each dimension
Chris@16 715 m_num_edges = 0;
Chris@16 716
Chris@16 717 for (std::size_t dimension_index = 0;
Chris@16 718 dimension_index < Dimensions;
Chris@16 719 ++dimension_index) {
Chris@16 720
Chris@16 721 if (wrapped(dimension_index)) {
Chris@16 722 m_edge_count[dimension_index] = num_vertices() * 2;
Chris@16 723 }
Chris@16 724 else {
Chris@16 725 m_edge_count[dimension_index] =
Chris@16 726 (num_vertices() - (num_vertices() / length(dimension_index))) * 2;
Chris@16 727 }
Chris@16 728
Chris@16 729 m_num_edges += num_edges(dimension_index);
Chris@16 730 }
Chris@16 731 }
Chris@16 732
Chris@16 733 const vertex_descriptor m_dimension_lengths;
Chris@16 734 WrapDimensionArray m_wrap_dimension;
Chris@16 735 vertices_size_type m_num_vertices;
Chris@16 736
Chris@16 737 boost::array<edges_size_type, Dimensions> m_edge_count;
Chris@16 738 edges_size_type m_num_edges;
Chris@16 739
Chris@16 740 public:
Chris@16 741
Chris@16 742 //================
Chris@16 743 // VertexListGraph
Chris@16 744 //================
Chris@16 745
Chris@16 746 friend inline std::pair<typename type::vertex_iterator,
Chris@16 747 typename type::vertex_iterator>
Chris@16 748 vertices(const type& graph) {
Chris@16 749 typedef typename type::vertex_iterator vertex_iterator;
Chris@16 750 typedef typename type::vertex_function vertex_function;
Chris@16 751 typedef typename type::vertex_index_iterator vertex_index_iterator;
Chris@16 752
Chris@16 753 return (std::make_pair
Chris@16 754 (vertex_iterator(vertex_index_iterator(0),
Chris@16 755 vertex_function(&graph)),
Chris@16 756 vertex_iterator(vertex_index_iterator(graph.num_vertices()),
Chris@16 757 vertex_function(&graph))));
Chris@16 758 }
Chris@16 759
Chris@16 760 friend inline typename type::vertices_size_type
Chris@16 761 num_vertices(const type& graph) {
Chris@16 762 return (graph.num_vertices());
Chris@16 763 }
Chris@16 764
Chris@16 765 friend inline typename type::vertex_descriptor
Chris@16 766 vertex(typename type::vertices_size_type vertex_index,
Chris@16 767 const type& graph) {
Chris@16 768
Chris@16 769 return (graph.vertex_at(vertex_index));
Chris@16 770 }
Chris@16 771
Chris@16 772 //===============
Chris@16 773 // IncidenceGraph
Chris@16 774 //===============
Chris@16 775
Chris@16 776 friend inline std::pair<typename type::out_edge_iterator,
Chris@16 777 typename type::out_edge_iterator>
Chris@16 778 out_edges(typename type::vertex_descriptor vertex,
Chris@16 779 const type& graph) {
Chris@16 780 typedef typename type::degree_iterator degree_iterator;
Chris@16 781 typedef typename type::out_edge_function out_edge_function;
Chris@16 782 typedef typename type::out_edge_iterator out_edge_iterator;
Chris@16 783
Chris@16 784 return (std::make_pair
Chris@16 785 (out_edge_iterator(degree_iterator(0),
Chris@16 786 out_edge_function(vertex, &graph)),
Chris@16 787 out_edge_iterator(degree_iterator(graph.out_degree(vertex)),
Chris@16 788 out_edge_function(vertex, &graph))));
Chris@16 789 }
Chris@16 790
Chris@16 791 friend inline typename type::degree_size_type
Chris@16 792 out_degree
Chris@16 793 (typename type::vertex_descriptor vertex,
Chris@16 794 const type& graph) {
Chris@16 795 return (graph.out_degree(vertex));
Chris@16 796 }
Chris@16 797
Chris@16 798 friend inline typename type::edge_descriptor
Chris@16 799 out_edge_at(typename type::vertex_descriptor vertex,
Chris@16 800 typename type::degree_size_type out_edge_index,
Chris@16 801 const type& graph) {
Chris@16 802 return (graph.out_edge_at(vertex, out_edge_index));
Chris@16 803 }
Chris@16 804
Chris@16 805 //===============
Chris@16 806 // AdjacencyGraph
Chris@16 807 //===============
Chris@16 808
Chris@16 809 friend typename std::pair<typename type::adjacency_iterator,
Chris@16 810 typename type::adjacency_iterator>
Chris@16 811 adjacent_vertices (typename type::vertex_descriptor vertex,
Chris@16 812 const type& graph) {
Chris@16 813 typedef typename type::degree_iterator degree_iterator;
Chris@16 814 typedef typename type::adjacent_vertex_function adjacent_vertex_function;
Chris@16 815 typedef typename type::adjacency_iterator adjacency_iterator;
Chris@16 816
Chris@16 817 return (std::make_pair
Chris@16 818 (adjacency_iterator(degree_iterator(0),
Chris@16 819 adjacent_vertex_function(vertex, &graph)),
Chris@16 820 adjacency_iterator(degree_iterator(graph.out_degree(vertex)),
Chris@16 821 adjacent_vertex_function(vertex, &graph))));
Chris@16 822 }
Chris@16 823
Chris@16 824 //==============
Chris@16 825 // EdgeListGraph
Chris@16 826 //==============
Chris@16 827
Chris@16 828 friend inline typename type::edges_size_type
Chris@16 829 num_edges(const type& graph) {
Chris@16 830 return (graph.num_edges());
Chris@16 831 }
Chris@16 832
Chris@16 833 friend inline typename type::edge_descriptor
Chris@16 834 edge_at(typename type::edges_size_type edge_index,
Chris@16 835 const type& graph) {
Chris@16 836 return (graph.edge_at(edge_index));
Chris@16 837 }
Chris@16 838
Chris@16 839 friend inline std::pair<typename type::edge_iterator,
Chris@16 840 typename type::edge_iterator>
Chris@16 841 edges(const type& graph) {
Chris@16 842 typedef typename type::edge_index_iterator edge_index_iterator;
Chris@16 843 typedef typename type::edge_function edge_function;
Chris@16 844 typedef typename type::edge_iterator edge_iterator;
Chris@16 845
Chris@16 846 return (std::make_pair
Chris@16 847 (edge_iterator(edge_index_iterator(0),
Chris@16 848 edge_function(&graph)),
Chris@16 849 edge_iterator(edge_index_iterator(graph.num_edges()),
Chris@16 850 edge_function(&graph))));
Chris@16 851 }
Chris@16 852
Chris@16 853 //===================
Chris@16 854 // BiDirectionalGraph
Chris@16 855 //===================
Chris@16 856
Chris@16 857 friend inline std::pair<typename type::in_edge_iterator,
Chris@16 858 typename type::in_edge_iterator>
Chris@16 859 in_edges(typename type::vertex_descriptor vertex,
Chris@16 860 const type& graph) {
Chris@16 861 typedef typename type::in_edge_function in_edge_function;
Chris@16 862 typedef typename type::degree_iterator degree_iterator;
Chris@16 863 typedef typename type::in_edge_iterator in_edge_iterator;
Chris@16 864
Chris@16 865 return (std::make_pair
Chris@16 866 (in_edge_iterator(degree_iterator(0),
Chris@16 867 in_edge_function(vertex, &graph)),
Chris@16 868 in_edge_iterator(degree_iterator(graph.in_degree(vertex)),
Chris@16 869 in_edge_function(vertex, &graph))));
Chris@16 870 }
Chris@16 871
Chris@16 872 friend inline typename type::degree_size_type
Chris@16 873 in_degree (typename type::vertex_descriptor vertex,
Chris@16 874 const type& graph) {
Chris@16 875 return (graph.in_degree(vertex));
Chris@16 876 }
Chris@16 877
Chris@16 878 friend inline typename type::degree_size_type
Chris@16 879 degree (typename type::vertex_descriptor vertex,
Chris@16 880 const type& graph) {
Chris@16 881 return (graph.out_degree(vertex) * 2);
Chris@16 882 }
Chris@16 883
Chris@16 884 friend inline typename type::edge_descriptor
Chris@16 885 in_edge_at(typename type::vertex_descriptor vertex,
Chris@16 886 typename type::degree_size_type in_edge_index,
Chris@16 887 const type& graph) {
Chris@16 888 return (graph.in_edge_at(vertex, in_edge_index));
Chris@16 889 }
Chris@16 890
Chris@16 891
Chris@16 892 //==================
Chris@16 893 // Adjacency Matrix
Chris@16 894 //==================
Chris@16 895
Chris@16 896 friend std::pair<typename type::edge_descriptor, bool>
Chris@16 897 edge (typename type::vertex_descriptor source_vertex,
Chris@16 898 typename type::vertex_descriptor destination_vertex,
Chris@16 899 const type& graph) {
Chris@16 900
Chris@16 901 std::pair<typename type::edge_descriptor, bool> edge_exists =
Chris@16 902 std::make_pair(std::make_pair(source_vertex, destination_vertex), false);
Chris@16 903
Chris@16 904 for (std::size_t dimension_index = 0;
Chris@16 905 dimension_index < Dimensions;
Chris@16 906 ++dimension_index) {
Chris@16 907
Chris@16 908 typename type::vertices_size_type dim_difference = 0;
Chris@16 909 typename type::vertices_size_type
Chris@16 910 source_dim = source_vertex[dimension_index],
Chris@16 911 dest_dim = destination_vertex[dimension_index];
Chris@16 912
Chris@16 913 dim_difference = (source_dim > dest_dim) ?
Chris@16 914 (source_dim - dest_dim) : (dest_dim - source_dim);
Chris@16 915
Chris@16 916 if (dim_difference > 0) {
Chris@16 917
Chris@16 918 // If we've already found a valid edge, this would mean that
Chris@16 919 // the vertices are really diagonal across dimensions and
Chris@16 920 // therefore not connected.
Chris@16 921 if (edge_exists.second) {
Chris@16 922 edge_exists.second = false;
Chris@16 923 break;
Chris@16 924 }
Chris@16 925
Chris@16 926 // If the difference is one, the vertices are right next to
Chris@16 927 // each other and the edge is valid. The edge is still
Chris@16 928 // valid, though, if the dimension wraps and the vertices
Chris@16 929 // are on opposite ends.
Chris@16 930 if ((dim_difference == 1) ||
Chris@16 931 (graph.wrapped(dimension_index) &&
Chris@16 932 (((source_dim == 0) && (dest_dim == (graph.length(dimension_index) - 1))) ||
Chris@16 933 ((dest_dim == 0) && (source_dim == (graph.length(dimension_index) - 1)))))) {
Chris@16 934
Chris@16 935 edge_exists.second = true;
Chris@16 936 // Stay in the loop to check for diagonal vertices.
Chris@16 937 }
Chris@16 938 else {
Chris@16 939
Chris@16 940 // Stop checking - the vertices are too far apart.
Chris@16 941 edge_exists.second = false;
Chris@16 942 break;
Chris@16 943 }
Chris@16 944 }
Chris@16 945
Chris@16 946 } // for dimension_index
Chris@16 947
Chris@16 948 return (edge_exists);
Chris@16 949 }
Chris@16 950
Chris@16 951
Chris@16 952 //=============================
Chris@16 953 // Index Property Map Functions
Chris@16 954 //=============================
Chris@16 955
Chris@16 956 friend inline typename type::vertices_size_type
Chris@16 957 get(vertex_index_t,
Chris@16 958 const type& graph,
Chris@16 959 typename type::vertex_descriptor vertex) {
Chris@16 960 return (graph.index_of(vertex));
Chris@16 961 }
Chris@16 962
Chris@16 963 friend inline typename type::edges_size_type
Chris@16 964 get(edge_index_t,
Chris@16 965 const type& graph,
Chris@16 966 typename type::edge_descriptor edge) {
Chris@16 967 return (graph.index_of(edge));
Chris@16 968 }
Chris@16 969
Chris@16 970 friend inline grid_graph_index_map<
Chris@16 971 type,
Chris@16 972 typename type::vertex_descriptor,
Chris@16 973 typename type::vertices_size_type>
Chris@16 974 get(vertex_index_t, const type& graph) {
Chris@16 975 return (grid_graph_index_map<
Chris@16 976 type,
Chris@16 977 typename type::vertex_descriptor,
Chris@16 978 typename type::vertices_size_type>(graph));
Chris@16 979 }
Chris@16 980
Chris@16 981 friend inline grid_graph_index_map<
Chris@16 982 type,
Chris@16 983 typename type::edge_descriptor,
Chris@16 984 typename type::edges_size_type>
Chris@16 985 get(edge_index_t, const type& graph) {
Chris@16 986 return (grid_graph_index_map<
Chris@16 987 type,
Chris@16 988 typename type::edge_descriptor,
Chris@16 989 typename type::edges_size_type>(graph));
Chris@16 990 }
Chris@16 991
Chris@16 992 friend inline grid_graph_reverse_edge_map<
Chris@16 993 typename type::edge_descriptor>
Chris@16 994 get(edge_reverse_t, const type& graph) {
Chris@16 995 return (grid_graph_reverse_edge_map<
Chris@16 996 typename type::edge_descriptor>());
Chris@16 997 }
Chris@16 998
Chris@16 999 template<typename Graph,
Chris@16 1000 typename Descriptor,
Chris@16 1001 typename Index>
Chris@16 1002 friend struct grid_graph_index_map;
Chris@16 1003
Chris@16 1004 template<typename Descriptor>
Chris@16 1005 friend struct grid_graph_reverse_edge_map;
Chris@16 1006
Chris@16 1007 }; // grid_graph
Chris@16 1008
Chris@16 1009 } // namespace boost
Chris@16 1010
Chris@16 1011 #undef BOOST_GRID_GRAPH_TYPE
Chris@16 1012 #undef BOOST_GRID_GRAPH_TEMPLATE_PARAMS
Chris@16 1013 #undef BOOST_GRID_GRAPH_TRAITS_T
Chris@16 1014
Chris@16 1015 #endif // BOOST_GRAPH_GRID_GRAPH_HPP