grid_graph.hpp 32 KB

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