grid_graph.hpp 33 KB

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