indexed_properties.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Copyright 2005 The Trustees of Indiana University.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Jeremiah Willcock
  6. // Douglas Gregor
  7. // Andrew Lumsdaine
  8. // Indexed properties -- used for CSR and CSR-like graphs
  9. #ifndef BOOST_GRAPH_INDEXED_PROPERTIES_HPP
  10. #define BOOST_GRAPH_INDEXED_PROPERTIES_HPP
  11. #include <vector>
  12. #include <utility>
  13. #include <algorithm>
  14. #include <climits>
  15. #include <iterator>
  16. #include <boost/graph/graph_traits.hpp>
  17. #include <boost/graph/properties.hpp>
  18. #include <boost/iterator/counting_iterator.hpp>
  19. #include <boost/integer.hpp>
  20. #include <boost/iterator/iterator_facade.hpp>
  21. #include <boost/property_map/property_map.hpp>
  22. #include <boost/mpl/if.hpp>
  23. namespace boost
  24. {
  25. namespace detail
  26. {
  27. template < typename Derived, typename Property, typename Descriptor,
  28. typename IndexMap >
  29. class indexed_vertex_properties
  30. {
  31. public:
  32. typedef no_property vertex_property_type;
  33. typedef Property vertex_bundled;
  34. typedef iterator_property_map<
  35. typename std::vector< Property >::iterator, IndexMap >
  36. vertex_map_type;
  37. typedef iterator_property_map<
  38. typename std::vector< Property >::const_iterator, IndexMap >
  39. const_vertex_map_type;
  40. // Directly access a vertex or edge bundle
  41. Property& operator[](Descriptor v)
  42. {
  43. return m_vertex_properties[get(vertex_index, derived(), v)];
  44. }
  45. const Property& operator[](Descriptor v) const
  46. {
  47. return m_vertex_properties[get(vertex_index, derived(), v)];
  48. }
  49. vertex_map_type get_vertex_bundle(
  50. const IndexMap& index_map = IndexMap())
  51. {
  52. return vertex_map_type(m_vertex_properties.begin(), index_map);
  53. }
  54. const_vertex_map_type get_vertex_bundle(
  55. const IndexMap& index_map = IndexMap()) const
  56. {
  57. return const_vertex_map_type(
  58. m_vertex_properties.begin(), index_map);
  59. }
  60. protected:
  61. // Default-construct with no property values
  62. indexed_vertex_properties() {}
  63. // Initialize with n default-constructed property values
  64. indexed_vertex_properties(std::size_t n) : m_vertex_properties(n) {}
  65. public:
  66. // Clear the properties vector
  67. void clear() { m_vertex_properties.clear(); }
  68. // Resize the properties vector
  69. void resize(std::size_t n) { m_vertex_properties.resize(n); }
  70. // Reserve space in the vector of properties
  71. void reserve(std::size_t n) { m_vertex_properties.reserve(n); }
  72. // Add a new property value to the back
  73. void push_back(const Property& prop)
  74. {
  75. m_vertex_properties.push_back(prop);
  76. }
  77. // Write an element by raw index
  78. void write_by_index(std::size_t idx, const Property& prop)
  79. {
  80. m_vertex_properties[idx] = prop;
  81. }
  82. // Access to the derived object
  83. Derived& derived() { return *static_cast< Derived* >(this); }
  84. const Derived& derived() const
  85. {
  86. return *static_cast< const Derived* >(this);
  87. }
  88. public: // should be private, but friend templates not portable
  89. std::vector< Property > m_vertex_properties;
  90. };
  91. template < typename Derived, typename Descriptor, typename IndexMap >
  92. class indexed_vertex_properties< Derived, void, Descriptor, IndexMap >
  93. {
  94. struct secret
  95. {
  96. };
  97. public:
  98. typedef no_property vertex_property_type;
  99. typedef void vertex_bundled;
  100. typedef secret vertex_map_type;
  101. typedef secret const_vertex_map_type;
  102. secret operator[](secret) { return secret(); }
  103. vertex_map_type get_vertex_bundle() const { return vertex_map_type(); }
  104. protected:
  105. // All operations do nothing.
  106. indexed_vertex_properties() {}
  107. indexed_vertex_properties(std::size_t) {}
  108. public:
  109. void clear() {}
  110. void resize(std::size_t) {}
  111. void reserve(std::size_t) {}
  112. };
  113. template < typename Derived, typename Property, typename Descriptor,
  114. typename IndexMap >
  115. class indexed_edge_properties
  116. {
  117. public:
  118. typedef no_property edge_property_type;
  119. typedef Property edge_bundled;
  120. typedef Property edge_push_back_type;
  121. typedef iterator_property_map<
  122. typename std::vector< Property >::iterator, IndexMap >
  123. edge_map_type;
  124. typedef iterator_property_map<
  125. typename std::vector< Property >::const_iterator, IndexMap >
  126. const_edge_map_type;
  127. // Directly access a edge or edge bundle
  128. Property& operator[](Descriptor v)
  129. {
  130. return m_edge_properties[get(edge_index, derived(), v)];
  131. }
  132. const Property& operator[](Descriptor v) const
  133. {
  134. return m_edge_properties[get(edge_index, derived(), v)];
  135. }
  136. edge_map_type get_edge_bundle(const IndexMap& index_map = IndexMap())
  137. {
  138. return edge_map_type(m_edge_properties.begin(), index_map);
  139. }
  140. const_edge_map_type get_edge_bundle(
  141. const IndexMap& index_map = IndexMap()) const
  142. {
  143. return const_edge_map_type(m_edge_properties.begin(), index_map);
  144. }
  145. protected:
  146. // Default-construct with no property values
  147. indexed_edge_properties() {}
  148. // Initialize with n default-constructed property values
  149. indexed_edge_properties(std::size_t n) : m_edge_properties(n) {}
  150. // Get the size of the properties vector
  151. std::size_t size() const { return m_edge_properties.size(); }
  152. // Clear the properties vector
  153. void clear() { m_edge_properties.clear(); }
  154. // Resize the properties vector
  155. void resize(std::size_t n) { m_edge_properties.resize(n); }
  156. // Reserve space in the vector of properties
  157. void reserve(std::size_t n) { m_edge_properties.reserve(n); }
  158. // Write an element by raw index
  159. void write_by_index(std::size_t idx, const Property& prop)
  160. {
  161. m_edge_properties[idx] = prop;
  162. }
  163. public:
  164. // Add a new property value to the back
  165. void push_back(const Property& prop)
  166. {
  167. m_edge_properties.push_back(prop);
  168. }
  169. // Move range of properties backwards
  170. void move_range(
  171. std::size_t src_begin, std::size_t src_end, std::size_t dest_begin)
  172. {
  173. std::copy_backward(m_edge_properties.begin() + src_begin,
  174. m_edge_properties.begin() + src_end,
  175. m_edge_properties.begin() + dest_begin + (src_end - src_begin));
  176. }
  177. typedef typename std::vector< Property >::iterator iterator;
  178. iterator begin() { return m_edge_properties.begin(); }
  179. iterator end() { return m_edge_properties.end(); }
  180. private:
  181. // Access to the derived object
  182. Derived& derived() { return *static_cast< Derived* >(this); }
  183. const Derived& derived() const
  184. {
  185. return *static_cast< const Derived* >(this);
  186. }
  187. public: // should be private, but friend templates not portable
  188. std::vector< Property > m_edge_properties;
  189. };
  190. struct dummy_no_property_iterator
  191. : public boost::iterator_facade< dummy_no_property_iterator, no_property,
  192. std::random_access_iterator_tag >
  193. {
  194. mutable no_property prop;
  195. no_property& dereference() const { return prop; }
  196. bool equal(const dummy_no_property_iterator&) const { return true; }
  197. void increment() {}
  198. void decrement() {}
  199. void advance(std::ptrdiff_t) {}
  200. std::ptrdiff_t distance_to(const dummy_no_property_iterator) const
  201. {
  202. return 0;
  203. }
  204. };
  205. template < typename Derived, typename Descriptor, typename IndexMap >
  206. class indexed_edge_properties< Derived, void, Descriptor, IndexMap >
  207. {
  208. struct secret
  209. {
  210. };
  211. public:
  212. typedef no_property edge_property_type;
  213. typedef void edge_bundled;
  214. typedef void* edge_push_back_type;
  215. typedef secret edge_map_type;
  216. typedef secret const_edge_map_type;
  217. secret operator[](secret) { return secret(); }
  218. void write_by_index(std::size_t /*idx*/, const no_property& /*prop*/) {}
  219. edge_map_type get_edge_bundle(const IndexMap& = IndexMap()) const
  220. {
  221. return edge_map_type();
  222. }
  223. protected:
  224. // All operations do nothing.
  225. indexed_edge_properties() {}
  226. indexed_edge_properties(std::size_t) {}
  227. std::size_t size() const { return 0; }
  228. void clear() {}
  229. void resize(std::size_t) {}
  230. void reserve(std::size_t) {}
  231. public:
  232. void push_back(const edge_push_back_type&) {}
  233. void move_range(std::size_t /*src_begin*/, std::size_t /*src_end*/,
  234. std::size_t /*dest_begin*/)
  235. {
  236. }
  237. typedef dummy_no_property_iterator iterator;
  238. iterator begin() { return dummy_no_property_iterator(); }
  239. iterator end() { return dummy_no_property_iterator(); }
  240. };
  241. }
  242. }
  243. #endif // BOOST_GRAPH_INDEXED_PROPERTIES_HPP