grid_graph.hpp 33 KB

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