face_iterators.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //=======================================================================
  2. // Copyright (c) Aaron Windsor 2007
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. #ifndef __FACE_ITERATORS_HPP__
  9. #define __FACE_ITERATORS_HPP__
  10. #include <boost/iterator/iterator_facade.hpp>
  11. #include <boost/mpl/bool.hpp>
  12. #include <boost/graph/graph_traits.hpp>
  13. namespace boost
  14. {
  15. // tags for defining traversal properties
  16. // VisitorType
  17. struct lead_visitor
  18. {
  19. };
  20. struct follow_visitor
  21. {
  22. };
  23. // TraversalType
  24. struct single_side
  25. {
  26. };
  27. struct both_sides
  28. {
  29. };
  30. // TraversalSubType
  31. struct first_side
  32. {
  33. }; // for single_side
  34. struct second_side
  35. {
  36. }; // for single_side
  37. struct alternating
  38. {
  39. }; // for both_sides
  40. // Time
  41. struct current_iteration
  42. {
  43. };
  44. struct previous_iteration
  45. {
  46. };
  47. // Why TraversalType AND TraversalSubType? TraversalSubType is a function
  48. // template parameter passed in to the constructor of the face iterator,
  49. // whereas TraversalType is a class template parameter. This lets us decide
  50. // at runtime whether to move along the first or second side of a bicomp (by
  51. // assigning a face_iterator that has been constructed with TraversalSubType
  52. // = first_side or second_side to a face_iterator variable) without any of
  53. // the virtual function overhead that comes with implementing this
  54. // functionality as a more structured form of type erasure. It also allows
  55. // a single face_iterator to be the end iterator of two iterators traversing
  56. // both sides of a bicomp.
  57. // ValueType is either graph_traits<Graph>::vertex_descriptor
  58. // or graph_traits<Graph>::edge_descriptor
  59. // forward declaration (defining defaults)
  60. template < typename Graph, typename FaceHandlesMap, typename ValueType,
  61. typename BicompSideToTraverse = single_side,
  62. typename VisitorType = lead_visitor, typename Time = current_iteration >
  63. class face_iterator;
  64. template < typename Graph, bool StoreEdge > struct edge_storage
  65. {
  66. };
  67. template < typename Graph > struct edge_storage< Graph, true >
  68. {
  69. typename graph_traits< Graph >::edge_descriptor value;
  70. };
  71. // specialization for TraversalType = traverse_vertices
  72. template < typename Graph, typename FaceHandlesMap, typename ValueType,
  73. typename TraversalType, typename VisitorType, typename Time >
  74. class face_iterator : public boost::iterator_facade<
  75. face_iterator< Graph, FaceHandlesMap, ValueType,
  76. TraversalType, VisitorType, Time >,
  77. ValueType, boost::forward_traversal_tag, ValueType >
  78. {
  79. public:
  80. typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
  81. typedef typename graph_traits< Graph >::edge_descriptor edge_t;
  82. typedef face_iterator< Graph, FaceHandlesMap, ValueType, TraversalType,
  83. VisitorType, Time >
  84. self;
  85. typedef typename FaceHandlesMap::value_type face_handle_t;
  86. face_iterator()
  87. : m_lead(graph_traits< Graph >::null_vertex())
  88. , m_follow(graph_traits< Graph >::null_vertex())
  89. {
  90. }
  91. template < typename TraversalSubType >
  92. face_iterator(face_handle_t anchor_handle, FaceHandlesMap face_handles,
  93. TraversalSubType traversal_type)
  94. : m_follow(anchor_handle.get_anchor()), m_face_handles(face_handles)
  95. {
  96. set_lead_dispatch(anchor_handle, traversal_type);
  97. }
  98. template < typename TraversalSubType >
  99. face_iterator(vertex_t anchor, FaceHandlesMap face_handles,
  100. TraversalSubType traversal_type)
  101. : m_follow(anchor), m_face_handles(face_handles)
  102. {
  103. set_lead_dispatch(m_face_handles[anchor], traversal_type);
  104. }
  105. private:
  106. friend class boost::iterator_core_access;
  107. inline vertex_t get_first_vertex(
  108. face_handle_t anchor_handle, current_iteration)
  109. {
  110. return anchor_handle.first_vertex();
  111. }
  112. inline vertex_t get_second_vertex(
  113. face_handle_t anchor_handle, current_iteration)
  114. {
  115. return anchor_handle.second_vertex();
  116. }
  117. inline vertex_t get_first_vertex(
  118. face_handle_t anchor_handle, previous_iteration)
  119. {
  120. return anchor_handle.old_first_vertex();
  121. }
  122. inline vertex_t get_second_vertex(
  123. face_handle_t anchor_handle, previous_iteration)
  124. {
  125. return anchor_handle.old_second_vertex();
  126. }
  127. inline void set_lead_dispatch(face_handle_t anchor_handle, first_side)
  128. {
  129. m_lead = get_first_vertex(anchor_handle, Time());
  130. set_edge_to_first_dispatch(anchor_handle, ValueType(), Time());
  131. }
  132. inline void set_lead_dispatch(face_handle_t anchor_handle, second_side)
  133. {
  134. m_lead = get_second_vertex(anchor_handle, Time());
  135. set_edge_to_second_dispatch(anchor_handle, ValueType(), Time());
  136. }
  137. inline void set_edge_to_first_dispatch(
  138. face_handle_t anchor_handle, edge_t, current_iteration)
  139. {
  140. m_edge.value = anchor_handle.first_edge();
  141. }
  142. inline void set_edge_to_second_dispatch(
  143. face_handle_t anchor_handle, edge_t, current_iteration)
  144. {
  145. m_edge.value = anchor_handle.second_edge();
  146. }
  147. inline void set_edge_to_first_dispatch(
  148. face_handle_t anchor_handle, edge_t, previous_iteration)
  149. {
  150. m_edge.value = anchor_handle.old_first_edge();
  151. }
  152. inline void set_edge_to_second_dispatch(
  153. face_handle_t anchor_handle, edge_t, previous_iteration)
  154. {
  155. m_edge.value = anchor_handle.old_second_edge();
  156. }
  157. template < typename T >
  158. inline void set_edge_to_first_dispatch(face_handle_t, vertex_t, T)
  159. {
  160. }
  161. template < typename T >
  162. inline void set_edge_to_second_dispatch(face_handle_t, vertex_t, T)
  163. {
  164. }
  165. void increment()
  166. {
  167. face_handle_t curr_face_handle(m_face_handles[m_lead]);
  168. vertex_t first = get_first_vertex(curr_face_handle, Time());
  169. vertex_t second = get_second_vertex(curr_face_handle, Time());
  170. if (first == m_follow)
  171. {
  172. m_follow = m_lead;
  173. set_edge_to_second_dispatch(curr_face_handle, ValueType(), Time());
  174. m_lead = second;
  175. }
  176. else if (second == m_follow)
  177. {
  178. m_follow = m_lead;
  179. set_edge_to_first_dispatch(curr_face_handle, ValueType(), Time());
  180. m_lead = first;
  181. }
  182. else
  183. m_lead = m_follow = graph_traits< Graph >::null_vertex();
  184. }
  185. bool equal(self const& other) const
  186. {
  187. return m_lead == other.m_lead && m_follow == other.m_follow;
  188. }
  189. ValueType dereference() const
  190. {
  191. return dereference_dispatch(VisitorType(), ValueType());
  192. }
  193. inline ValueType dereference_dispatch(lead_visitor, vertex_t) const
  194. {
  195. return m_lead;
  196. }
  197. inline ValueType dereference_dispatch(follow_visitor, vertex_t) const
  198. {
  199. return m_follow;
  200. }
  201. inline ValueType dereference_dispatch(lead_visitor, edge_t) const
  202. {
  203. return m_edge.value;
  204. }
  205. inline ValueType dereference_dispatch(follow_visitor, edge_t) const
  206. {
  207. return m_edge.value;
  208. }
  209. vertex_t m_lead;
  210. vertex_t m_follow;
  211. edge_storage< Graph, boost::is_same< ValueType, edge_t >::value > m_edge;
  212. FaceHandlesMap m_face_handles;
  213. };
  214. template < typename Graph, typename FaceHandlesMap, typename ValueType,
  215. typename VisitorType, typename Time >
  216. class face_iterator< Graph, FaceHandlesMap, ValueType, both_sides, VisitorType,
  217. Time >
  218. : public boost::iterator_facade< face_iterator< Graph, FaceHandlesMap,
  219. ValueType, both_sides, VisitorType, Time >,
  220. ValueType, boost::forward_traversal_tag, ValueType >
  221. {
  222. public:
  223. typedef face_iterator< Graph, FaceHandlesMap, ValueType, both_sides,
  224. VisitorType, Time >
  225. self;
  226. typedef typename graph_traits< Graph >::vertex_descriptor vertex_t;
  227. typedef typename FaceHandlesMap::value_type face_handle_t;
  228. face_iterator() {}
  229. face_iterator(face_handle_t anchor_handle, FaceHandlesMap face_handles)
  230. : first_itr(anchor_handle, face_handles, first_side())
  231. , second_itr(anchor_handle, face_handles, second_side())
  232. , first_is_active(true)
  233. , first_increment(true)
  234. {
  235. }
  236. face_iterator(vertex_t anchor, FaceHandlesMap face_handles)
  237. : first_itr(face_handles[anchor], face_handles, first_side())
  238. , second_itr(face_handles[anchor], face_handles, second_side())
  239. , first_is_active(true)
  240. , first_increment(true)
  241. {
  242. }
  243. private:
  244. friend class boost::iterator_core_access;
  245. typedef face_iterator< Graph, FaceHandlesMap, ValueType, single_side,
  246. follow_visitor, Time >
  247. inner_itr_t;
  248. void increment()
  249. {
  250. if (first_increment)
  251. {
  252. ++first_itr;
  253. ++second_itr;
  254. first_increment = false;
  255. }
  256. else if (first_is_active)
  257. ++first_itr;
  258. else
  259. ++second_itr;
  260. first_is_active = !first_is_active;
  261. }
  262. bool equal(self const& other) const
  263. {
  264. // Want this iterator to be equal to the "end" iterator when at least
  265. // one of the iterators has reached the root of the current bicomp.
  266. // This isn't ideal, but it works.
  267. return (first_itr == other.first_itr || second_itr == other.second_itr);
  268. }
  269. ValueType dereference() const
  270. {
  271. return first_is_active ? *first_itr : *second_itr;
  272. }
  273. inner_itr_t first_itr;
  274. inner_itr_t second_itr;
  275. inner_itr_t face_end;
  276. bool first_is_active;
  277. bool first_increment;
  278. };
  279. } /* namespace boost */
  280. #endif //__FACE_ITERATORS_HPP__