1 //=======================================================================
2 // Copyright 2009 Trustees of Indiana University.
3 // Authors: Michael Hansen, Andrew Lumsdaine
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
10 #ifndef BOOST_GRAPH_GRID_GRAPH_HPP
11 #define BOOST_GRAPH_GRID_GRAPH_HPP
17 #include <boost/array.hpp>
18 #include <boost/bind.hpp>
19 #include <boost/limits.hpp>
20 #include <boost/graph/graph_traits.hpp>
21 #include <boost/graph/properties.hpp>
22 #include <boost/iterator/counting_iterator.hpp>
23 #include <boost/iterator/transform_iterator.hpp>
24 #include <boost/property_map/property_map.hpp>
26 #define BOOST_GRID_GRAPH_TEMPLATE_PARAMS \
27 std::size_t DimensionsT, typename VertexIndexT, \
30 #define BOOST_GRID_GRAPH_TYPE \
31 grid_graph<DimensionsT, VertexIndexT, EdgeIndexT>
33 #define BOOST_GRID_GRAPH_TRAITS_T \
34 typename graph_traits<BOOST_GRID_GRAPH_TYPE >
38 // Class prototype for grid_graph
39 template<BOOST_GRID_GRAPH_TEMPLATE_PARAMS>
46 template <typename Graph,
49 struct grid_graph_index_map {
51 typedef Index value_type;
52 typedef Index reference_type;
53 typedef reference_type reference;
54 typedef Descriptor key_type;
55 typedef readable_property_map_tag category;
57 grid_graph_index_map() { }
59 grid_graph_index_map(const Graph& graph) :
62 value_type operator[](key_type key) const {
63 return (m_graph->index_of(key));
67 get(const grid_graph_index_map<Graph, Descriptor, Index>& index_map,
68 const typename grid_graph_index_map<Graph, Descriptor, Index>::key_type& key)
70 return (index_map[key]);
77 template<BOOST_GRID_GRAPH_TEMPLATE_PARAMS>
78 struct property_map<BOOST_GRID_GRAPH_TYPE, vertex_index_t> {
79 typedef grid_graph_index_map<BOOST_GRID_GRAPH_TYPE,
80 BOOST_GRID_GRAPH_TRAITS_T::vertex_descriptor,
81 BOOST_GRID_GRAPH_TRAITS_T::vertices_size_type> type;
82 typedef type const_type;
85 template<BOOST_GRID_GRAPH_TEMPLATE_PARAMS>
86 struct property_map<BOOST_GRID_GRAPH_TYPE, edge_index_t> {
87 typedef grid_graph_index_map<BOOST_GRID_GRAPH_TYPE,
88 BOOST_GRID_GRAPH_TRAITS_T::edge_descriptor,
89 BOOST_GRID_GRAPH_TRAITS_T::edges_size_type> type;
90 typedef type const_type;
93 //==========================
94 // Reverse Edge Property Map
95 //==========================
97 template <typename Descriptor>
98 struct grid_graph_reverse_edge_map {
100 typedef Descriptor value_type;
101 typedef Descriptor reference_type;
102 typedef reference_type reference;
103 typedef Descriptor key_type;
104 typedef readable_property_map_tag category;
106 grid_graph_reverse_edge_map() { }
108 value_type operator[](const key_type& key) const {
109 return (value_type(key.second, key.first));
112 friend inline Descriptor
113 get(const grid_graph_reverse_edge_map<Descriptor>& rev_map,
114 const typename grid_graph_reverse_edge_map<Descriptor>::key_type& key)
116 return (rev_map[key]);
120 template<BOOST_GRID_GRAPH_TEMPLATE_PARAMS>
121 struct property_map<BOOST_GRID_GRAPH_TYPE, edge_reverse_t> {
122 typedef grid_graph_reverse_edge_map<BOOST_GRID_GRAPH_TRAITS_T::edge_descriptor> type;
123 typedef type const_type;
133 template <typename Graph>
134 struct grid_graph_vertex_at {
136 typedef typename graph_traits<Graph>::vertex_descriptor result_type;
138 grid_graph_vertex_at() : m_graph(0) {}
140 grid_graph_vertex_at(const Graph* graph) :
145 (typename graph_traits<Graph>::vertices_size_type vertex_index) const {
146 return (vertex(vertex_index, *m_graph));
150 const Graph* m_graph;
154 template <typename Graph>
155 struct grid_graph_out_edge_at {
158 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
161 typedef typename graph_traits<Graph>::edge_descriptor result_type;
163 grid_graph_out_edge_at() : m_vertex(), m_graph(0) {}
165 grid_graph_out_edge_at(vertex_descriptor source_vertex,
166 const Graph* graph) :
167 m_vertex(source_vertex),
172 (typename graph_traits<Graph>::degree_size_type out_edge_index) const {
173 return (out_edge_at(m_vertex, out_edge_index, *m_graph));
177 vertex_descriptor m_vertex;
178 const Graph* m_graph;
182 template <typename Graph>
183 struct grid_graph_in_edge_at {
186 typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
189 typedef typename graph_traits<Graph>::edge_descriptor result_type;
191 grid_graph_in_edge_at() : m_vertex(), m_graph(0) {}
193 grid_graph_in_edge_at(vertex_descriptor target_vertex,
194 const Graph* graph) :
195 m_vertex(target_vertex),
200 (typename graph_traits<Graph>::degree_size_type in_edge_index) const {
201 return (in_edge_at(m_vertex, in_edge_index, *m_graph));
205 vertex_descriptor m_vertex;
206 const Graph* m_graph;
210 template <typename Graph>
211 struct grid_graph_edge_at {
213 typedef typename graph_traits<Graph>::edge_descriptor result_type;
215 grid_graph_edge_at() : m_graph(0) {}
217 grid_graph_edge_at(const Graph* graph) :
222 (typename graph_traits<Graph>::edges_size_type edge_index) const {
223 return (edge_at(edge_index, *m_graph));
227 const Graph* m_graph;
230 // adjacent_vertex_at
231 template <typename Graph>
232 struct grid_graph_adjacent_vertex_at {
235 typedef typename graph_traits<Graph>::vertex_descriptor result_type;
237 grid_graph_adjacent_vertex_at(result_type source_vertex,
238 const Graph* graph) :
239 m_vertex(source_vertex),
244 (typename graph_traits<Graph>::degree_size_type adjacent_index) const {
245 return (target(out_edge_at(m_vertex, adjacent_index, *m_graph), *m_graph));
249 result_type m_vertex;
250 const Graph* m_graph;
253 } // namespace detail
259 template <std::size_t Dimensions,
260 typename VertexIndex = std::size_t,
261 typename EdgeIndex = VertexIndex>
265 typedef boost::array<bool, Dimensions> WrapDimensionArray;
270 typedef grid_graph<Dimensions, VertexIndex, EdgeIndex> type;
273 typedef VertexIndex vertices_size_type;
274 typedef EdgeIndex edges_size_type;
275 typedef EdgeIndex degree_size_type;
278 typedef boost::array<VertexIndex, Dimensions> vertex_descriptor;
279 typedef std::pair<vertex_descriptor, vertex_descriptor> edge_descriptor;
282 typedef counting_iterator<vertices_size_type> vertex_index_iterator;
283 typedef detail::grid_graph_vertex_at<type> vertex_function;
284 typedef transform_iterator<vertex_function, vertex_index_iterator> vertex_iterator;
287 typedef counting_iterator<edges_size_type> edge_index_iterator;
288 typedef detail::grid_graph_edge_at<type> edge_function;
289 typedef transform_iterator<edge_function, edge_index_iterator> edge_iterator;
292 typedef counting_iterator<degree_size_type> degree_iterator;
293 typedef detail::grid_graph_out_edge_at<type> out_edge_function;
294 typedef transform_iterator<out_edge_function, degree_iterator> out_edge_iterator;
297 typedef detail::grid_graph_in_edge_at<type> in_edge_function;
298 typedef transform_iterator<in_edge_function, degree_iterator> in_edge_iterator;
300 // adjacency_iterator
301 typedef detail::grid_graph_adjacent_vertex_at<type> adjacent_vertex_function;
302 typedef transform_iterator<adjacent_vertex_function, degree_iterator> adjacency_iterator;
305 typedef directed_tag directed_category;
306 typedef disallow_parallel_edge_tag edge_parallel_category;
307 struct traversal_category : virtual public incidence_graph_tag,
308 virtual public adjacency_graph_tag,
309 virtual public vertex_list_graph_tag,
310 virtual public edge_list_graph_tag,
311 virtual public bidirectional_graph_tag,
312 virtual public adjacency_matrix_tag { };
314 static inline vertex_descriptor null_vertex()
316 vertex_descriptor maxed_out_vertex;
317 std::fill(maxed_out_vertex.begin(), maxed_out_vertex.end(),
318 (std::numeric_limits<vertices_size_type>::max)());
320 return (maxed_out_vertex);
323 // Constructor that defaults to no wrapping for all dimensions.
324 grid_graph(vertex_descriptor dimension_lengths) :
325 m_dimension_lengths(dimension_lengths) {
327 std::fill(m_wrap_dimension.begin(),
328 m_wrap_dimension.end(), false);
333 // Constructor that allows for wrapping to be specified for all
334 // dimensions at once.
335 grid_graph(vertex_descriptor dimension_lengths,
336 bool wrap_all_dimensions) :
337 m_dimension_lengths(dimension_lengths) {
339 std::fill(m_wrap_dimension.begin(),
340 m_wrap_dimension.end(),
341 wrap_all_dimensions);
346 // Constructor that allows for individual dimension wrapping to be
348 grid_graph(vertex_descriptor dimension_lengths,
349 WrapDimensionArray wrap_dimension) :
350 m_dimension_lengths(dimension_lengths),
351 m_wrap_dimension(wrap_dimension) {
356 // Returns the number of dimensions in the graph
357 inline std::size_t dimensions() const {
361 // Returns the length of dimension [dimension_index]
362 inline vertices_size_type length(std::size_t dimension) const {
363 return (m_dimension_lengths[dimension]);
366 // Returns a value indicating if dimension [dimension_index] wraps
367 inline bool wrapped(std::size_t dimension) const {
368 return (m_wrap_dimension[dimension]);
371 // Gets the vertex that is [distance] units ahead of [vertex] in
372 // dimension [dimension_index].
373 vertex_descriptor next
374 (vertex_descriptor vertex,
375 std::size_t dimension_index,
376 vertices_size_type distance = 1) const {
378 vertices_size_type new_position =
379 vertex[dimension_index] + distance;
381 if (wrapped(dimension_index)) {
382 new_position %= length(dimension_index);
385 // Stop at the end of this dimension if necessary.
387 (std::min)(new_position,
388 vertices_size_type(length(dimension_index) - 1));
391 vertex[dimension_index] = new_position;
396 // Gets the vertex that is [distance] units behind [vertex] in
397 // dimension [dimension_index].
398 vertex_descriptor previous
399 (vertex_descriptor vertex,
400 std::size_t dimension_index,
401 vertices_size_type distance = 1) const {
403 // We're assuming that vertices_size_type is unsigned, so we
404 // need to be careful about the math.
405 vertex[dimension_index] =
406 (distance > vertex[dimension_index]) ?
407 (wrapped(dimension_index) ?
408 (length(dimension_index) - (distance % length(dimension_index))) : 0) :
409 vertex[dimension_index] - distance;
416 // Returns the number of vertices in the graph
417 inline vertices_size_type num_vertices() const {
418 return (m_num_vertices);
421 // Returns the number of edges in the graph
422 inline edges_size_type num_edges() const {
423 return (m_num_edges);
426 // Returns the number of edges in dimension [dimension_index]
427 inline edges_size_type num_edges
428 (std::size_t dimension_index) const {
429 return (m_edge_count[dimension_index]);
432 // Returns the index of [vertex] (See also vertex_at)
433 vertices_size_type index_of(vertex_descriptor vertex) const {
435 vertices_size_type vertex_index = 0;
436 vertices_size_type index_multiplier = 1;
438 for (std::size_t dimension_index = 0;
439 dimension_index < Dimensions;
442 vertex_index += (vertex[dimension_index] * index_multiplier);
443 index_multiplier *= length(dimension_index);
446 return (vertex_index);
449 // Returns the vertex whose index is [vertex_index] (See also
450 // index_of(vertex_descriptor))
451 vertex_descriptor vertex_at
452 (vertices_size_type vertex_index) const {
454 boost::array<vertices_size_type, Dimensions> vertex;
455 vertices_size_type index_divider = 1;
457 for (std::size_t dimension_index = 0;
458 dimension_index < Dimensions;
461 vertex[dimension_index] = (vertex_index / index_divider) %
462 length(dimension_index);
464 index_divider *= length(dimension_index);
470 // Returns the edge whose index is [edge_index] (See also
471 // index_of(edge_descriptor)). NOTE: The index mapping is
472 // dependent upon dimension wrapping.
473 edge_descriptor edge_at(edges_size_type edge_index) const {
475 // Edge indices are sorted into bins by dimension
476 std::size_t dimension_index = 0;
477 edges_size_type dimension_edges = num_edges(0);
479 while (edge_index >= dimension_edges) {
480 edge_index -= dimension_edges;
482 dimension_edges = num_edges(dimension_index);
485 vertex_descriptor vertex_source, vertex_target;
486 bool is_forward = ((edge_index / (num_edges(dimension_index) / 2)) == 0);
488 if (wrapped(dimension_index)) {
489 vertex_source = vertex_at(edge_index % num_vertices());
490 vertex_target = is_forward ?
491 next(vertex_source, dimension_index) :
492 previous(vertex_source, dimension_index);
496 // Dimensions can wrap arbitrarily, so an index needs to be
497 // computed in a more complex manner. This is done by
498 // grouping the edges for each dimension together into "bins"
499 // and considering [edge_index] as an offset into the bin.
500 // Each bin consists of two parts: the "forward" looking edges
501 // and the "backward" looking edges for the dimension.
503 edges_size_type vertex_offset = edge_index % num_edges(dimension_index);
505 // Consider vertex_offset an index into the graph's vertex
506 // space but with the dimension [dimension_index] reduced in
508 vertices_size_type index_divider = 1;
510 for (std::size_t dimension_index_iter = 0;
511 dimension_index_iter < Dimensions;
512 ++dimension_index_iter) {
514 std::size_t dimension_length = (dimension_index_iter == dimension_index) ?
515 length(dimension_index_iter) - 1 :
516 length(dimension_index_iter);
518 vertex_source[dimension_index_iter] = (vertex_offset / index_divider) %
521 index_divider *= dimension_length;
525 vertex_target = next(vertex_source, dimension_index);
528 // Shift forward one more unit in the dimension for backward
529 // edges since the algorithm above will leave us one behind.
530 vertex_target = vertex_source;
531 ++vertex_source[dimension_index];
534 } // if (wrapped(dimension_index))
536 return (std::make_pair(vertex_source, vertex_target));
539 // Returns the index for [edge] (See also edge_at)
540 edges_size_type index_of(edge_descriptor edge) const {
541 vertex_descriptor source_vertex = source(edge, *this);
542 vertex_descriptor target_vertex = target(edge, *this);
544 BOOST_ASSERT (source_vertex != target_vertex);
546 // Determine the dimension where the source and target vertices
547 // differ (should only be one if this is a valid edge).
548 std::size_t different_dimension_index = 0;
550 while (source_vertex[different_dimension_index] ==
551 target_vertex[different_dimension_index]) {
553 ++different_dimension_index;
556 edges_size_type edge_index = 0;
558 // Offset the edge index into the appropriate "bin" (see edge_at
559 // for a more in-depth description).
560 for (std::size_t dimension_index = 0;
561 dimension_index < different_dimension_index;
564 edge_index += num_edges(dimension_index);
567 // Get the position of both vertices in the differing dimension.
568 vertices_size_type source_position = source_vertex[different_dimension_index];
569 vertices_size_type target_position = target_vertex[different_dimension_index];
571 // Determine if edge is forward or backward
572 bool is_forward = true;
574 if (wrapped(different_dimension_index)) {
576 // If the dimension is wrapped, an edge is going backward if
577 // either A: its target precedes the source in the differing
578 // dimension and the vertices are adjacent or B: its source
579 // precedes the target and they're not adjacent.
580 if (((target_position < source_position) &&
581 ((source_position - target_position) == 1)) ||
582 ((source_position < target_position) &&
583 ((target_position - source_position) > 1))) {
588 else if (target_position < source_position) {
592 // "Backward" edges are in the second half of the bin.
594 edge_index += (num_edges(different_dimension_index) / 2);
597 // Finally, apply the vertex offset
598 if (wrapped(different_dimension_index)) {
599 edge_index += index_of(source_vertex);
602 vertices_size_type index_multiplier = 1;
605 --source_vertex[different_dimension_index];
608 for (std::size_t dimension_index = 0;
609 dimension_index < Dimensions;
612 edge_index += (source_vertex[dimension_index] * index_multiplier);
613 index_multiplier *= (dimension_index == different_dimension_index) ?
614 length(dimension_index) - 1 :
615 length(dimension_index);
622 // Returns the number of out-edges for [vertex]
623 degree_size_type out_degree(vertex_descriptor vertex) const {
625 degree_size_type out_edge_count = 0;
627 for (std::size_t dimension_index = 0;
628 dimension_index < Dimensions;
631 // If the vertex is on the edge of this dimension, then its
632 // number of out edges is dependent upon whether the dimension
634 if ((vertex[dimension_index] == 0) ||
635 (vertex[dimension_index] == (length(dimension_index) - 1))) {
636 out_edge_count += (wrapped(dimension_index) ? 2 : 1);
639 // Next and previous edges, regardless or wrapping
644 return (out_edge_count);
647 // Returns an out-edge for [vertex] by index. Indices are in the
648 // range [0, out_degree(vertex)).
649 edge_descriptor out_edge_at
650 (vertex_descriptor vertex,
651 degree_size_type out_edge_index) const {
653 edges_size_type edges_left = out_edge_index + 1;
654 std::size_t dimension_index = 0;
655 bool is_forward = false;
657 // Walks the out edges of [vertex] and accommodates for dimension
659 while (edges_left > 0) {
661 if (!wrapped(dimension_index)) {
662 if (!is_forward && (vertex[dimension_index] == 0)) {
666 else if (is_forward &&
667 (vertex[dimension_index] == (length(dimension_index) - 1))) {
676 if (edges_left > 0) {
677 is_forward = !is_forward;
685 return (std::make_pair(vertex, is_forward ?
686 next(vertex, dimension_index) :
687 previous(vertex, dimension_index)));
690 // Returns the number of in-edges for [vertex]
691 inline degree_size_type in_degree(vertex_descriptor vertex) const {
692 return (out_degree(vertex));
695 // Returns an in-edge for [vertex] by index. Indices are in the
696 // range [0, in_degree(vertex)).
697 edge_descriptor in_edge_at
698 (vertex_descriptor vertex,
699 edges_size_type in_edge_index) const {
701 edge_descriptor out_edge = out_edge_at(vertex, in_edge_index);
702 return (std::make_pair(target(out_edge, *this), source(out_edge, *this)));
706 // Pre-computes the number of vertices and edges
707 void precalculate() {
709 std::accumulate(m_dimension_lengths.begin(),
710 m_dimension_lengths.end(),
711 vertices_size_type(1),
712 std::multiplies<vertices_size_type>());
714 // Calculate number of edges in each dimension
717 for (std::size_t dimension_index = 0;
718 dimension_index < Dimensions;
721 if (wrapped(dimension_index)) {
722 m_edge_count[dimension_index] = num_vertices() * 2;
725 m_edge_count[dimension_index] =
726 (num_vertices() - (num_vertices() / length(dimension_index))) * 2;
729 m_num_edges += num_edges(dimension_index);
733 const vertex_descriptor m_dimension_lengths;
734 WrapDimensionArray m_wrap_dimension;
735 vertices_size_type m_num_vertices;
737 boost::array<edges_size_type, Dimensions> m_edge_count;
738 edges_size_type m_num_edges;
746 friend inline std::pair<typename type::vertex_iterator,
747 typename type::vertex_iterator>
748 vertices(const type& graph) {
749 typedef typename type::vertex_iterator vertex_iterator;
750 typedef typename type::vertex_function vertex_function;
751 typedef typename type::vertex_index_iterator vertex_index_iterator;
753 return (std::make_pair
754 (vertex_iterator(vertex_index_iterator(0),
755 vertex_function(&graph)),
756 vertex_iterator(vertex_index_iterator(graph.num_vertices()),
757 vertex_function(&graph))));
760 friend inline typename type::vertices_size_type
761 num_vertices(const type& graph) {
762 return (graph.num_vertices());
765 friend inline typename type::vertex_descriptor
766 vertex(typename type::vertices_size_type vertex_index,
769 return (graph.vertex_at(vertex_index));
776 friend inline std::pair<typename type::out_edge_iterator,
777 typename type::out_edge_iterator>
778 out_edges(typename type::vertex_descriptor vertex,
780 typedef typename type::degree_iterator degree_iterator;
781 typedef typename type::out_edge_function out_edge_function;
782 typedef typename type::out_edge_iterator out_edge_iterator;
784 return (std::make_pair
785 (out_edge_iterator(degree_iterator(0),
786 out_edge_function(vertex, &graph)),
787 out_edge_iterator(degree_iterator(graph.out_degree(vertex)),
788 out_edge_function(vertex, &graph))));
791 friend inline typename type::degree_size_type
793 (typename type::vertex_descriptor vertex,
795 return (graph.out_degree(vertex));
798 friend inline typename type::edge_descriptor
799 out_edge_at(typename type::vertex_descriptor vertex,
800 typename type::degree_size_type out_edge_index,
802 return (graph.out_edge_at(vertex, out_edge_index));
809 friend typename std::pair<typename type::adjacency_iterator,
810 typename type::adjacency_iterator>
811 adjacent_vertices (typename type::vertex_descriptor vertex,
813 typedef typename type::degree_iterator degree_iterator;
814 typedef typename type::adjacent_vertex_function adjacent_vertex_function;
815 typedef typename type::adjacency_iterator adjacency_iterator;
817 return (std::make_pair
818 (adjacency_iterator(degree_iterator(0),
819 adjacent_vertex_function(vertex, &graph)),
820 adjacency_iterator(degree_iterator(graph.out_degree(vertex)),
821 adjacent_vertex_function(vertex, &graph))));
828 friend inline typename type::edges_size_type
829 num_edges(const type& graph) {
830 return (graph.num_edges());
833 friend inline typename type::edge_descriptor
834 edge_at(typename type::edges_size_type edge_index,
836 return (graph.edge_at(edge_index));
839 friend inline std::pair<typename type::edge_iterator,
840 typename type::edge_iterator>
841 edges(const type& graph) {
842 typedef typename type::edge_index_iterator edge_index_iterator;
843 typedef typename type::edge_function edge_function;
844 typedef typename type::edge_iterator edge_iterator;
846 return (std::make_pair
847 (edge_iterator(edge_index_iterator(0),
848 edge_function(&graph)),
849 edge_iterator(edge_index_iterator(graph.num_edges()),
850 edge_function(&graph))));
853 //===================
854 // BiDirectionalGraph
855 //===================
857 friend inline std::pair<typename type::in_edge_iterator,
858 typename type::in_edge_iterator>
859 in_edges(typename type::vertex_descriptor vertex,
861 typedef typename type::in_edge_function in_edge_function;
862 typedef typename type::degree_iterator degree_iterator;
863 typedef typename type::in_edge_iterator in_edge_iterator;
865 return (std::make_pair
866 (in_edge_iterator(degree_iterator(0),
867 in_edge_function(vertex, &graph)),
868 in_edge_iterator(degree_iterator(graph.in_degree(vertex)),
869 in_edge_function(vertex, &graph))));
872 friend inline typename type::degree_size_type
873 in_degree (typename type::vertex_descriptor vertex,
875 return (graph.in_degree(vertex));
878 friend inline typename type::degree_size_type
879 degree (typename type::vertex_descriptor vertex,
881 return (graph.out_degree(vertex) * 2);
884 friend inline typename type::edge_descriptor
885 in_edge_at(typename type::vertex_descriptor vertex,
886 typename type::degree_size_type in_edge_index,
888 return (graph.in_edge_at(vertex, in_edge_index));
896 friend std::pair<typename type::edge_descriptor, bool>
897 edge (typename type::vertex_descriptor source_vertex,
898 typename type::vertex_descriptor destination_vertex,
901 std::pair<typename type::edge_descriptor, bool> edge_exists =
902 std::make_pair(std::make_pair(source_vertex, destination_vertex), false);
904 for (std::size_t dimension_index = 0;
905 dimension_index < Dimensions;
908 typename type::vertices_size_type dim_difference = 0;
909 typename type::vertices_size_type
910 source_dim = source_vertex[dimension_index],
911 dest_dim = destination_vertex[dimension_index];
913 dim_difference = (source_dim > dest_dim) ?
914 (source_dim - dest_dim) : (dest_dim - source_dim);
916 if (dim_difference > 0) {
918 // If we've already found a valid edge, this would mean that
919 // the vertices are really diagonal across dimensions and
920 // therefore not connected.
921 if (edge_exists.second) {
922 edge_exists.second = false;
926 // If the difference is one, the vertices are right next to
927 // each other and the edge is valid. The edge is still
928 // valid, though, if the dimension wraps and the vertices
929 // are on opposite ends.
930 if ((dim_difference == 1) ||
931 (graph.wrapped(dimension_index) &&
932 (((source_dim == 0) && (dest_dim == (graph.length(dimension_index) - 1))) ||
933 ((dest_dim == 0) && (source_dim == (graph.length(dimension_index) - 1)))))) {
935 edge_exists.second = true;
936 // Stay in the loop to check for diagonal vertices.
940 // Stop checking - the vertices are too far apart.
941 edge_exists.second = false;
946 } // for dimension_index
948 return (edge_exists);
952 //=============================
953 // Index Property Map Functions
954 //=============================
956 friend inline typename type::vertices_size_type
959 typename type::vertex_descriptor vertex) {
960 return (graph.index_of(vertex));
963 friend inline typename type::edges_size_type
966 typename type::edge_descriptor edge) {
967 return (graph.index_of(edge));
970 friend inline grid_graph_index_map<
972 typename type::vertex_descriptor,
973 typename type::vertices_size_type>
974 get(vertex_index_t, const type& graph) {
975 return (grid_graph_index_map<
977 typename type::vertex_descriptor,
978 typename type::vertices_size_type>(graph));
981 friend inline grid_graph_index_map<
983 typename type::edge_descriptor,
984 typename type::edges_size_type>
985 get(edge_index_t, const type& graph) {
986 return (grid_graph_index_map<
988 typename type::edge_descriptor,
989 typename type::edges_size_type>(graph));
992 friend inline grid_graph_reverse_edge_map<
993 typename type::edge_descriptor>
994 get(edge_reverse_t, const type& graph) {
995 return (grid_graph_reverse_edge_map<
996 typename type::edge_descriptor>());
999 template<typename Graph,
1000 typename Descriptor,
1002 friend struct grid_graph_index_map;
1004 template<typename Descriptor>
1005 friend struct grid_graph_reverse_edge_map;
1009 } // namespace boost
1011 #undef BOOST_GRID_GRAPH_TYPE
1012 #undef BOOST_GRID_GRAPH_TEMPLATE_PARAMS
1013 #undef BOOST_GRID_GRAPH_TRAITS_T
1015 #endif // BOOST_GRAPH_GRID_GRAPH_HPP