indexed_properties.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. namespace detail {
  25. template<typename Derived, typename Property, typename Descriptor, typename IndexMap>
  26. class indexed_vertex_properties
  27. {
  28. public:
  29. typedef no_property vertex_property_type;
  30. typedef Property vertex_bundled;
  31. typedef iterator_property_map<
  32. typename std::vector<Property>::iterator,
  33. IndexMap> vertex_map_type;
  34. typedef iterator_property_map<
  35. typename std::vector<Property>::const_iterator,
  36. IndexMap> const_vertex_map_type;
  37. // Directly access a vertex or edge bundle
  38. Property& operator[](Descriptor v)
  39. { return m_vertex_properties[get(vertex_index, derived(), v)]; }
  40. const Property& operator[](Descriptor v) const
  41. { return m_vertex_properties[get(vertex_index, derived(), v)]; }
  42. vertex_map_type get_vertex_bundle(const IndexMap& index_map = IndexMap()) {
  43. return vertex_map_type(m_vertex_properties.begin(), index_map);
  44. }
  45. const_vertex_map_type get_vertex_bundle(const IndexMap& index_map = IndexMap()) const {
  46. return const_vertex_map_type(m_vertex_properties.begin(), index_map);
  47. }
  48. protected:
  49. // Default-construct with no property values
  50. indexed_vertex_properties() {}
  51. // Initialize with n default-constructed property values
  52. indexed_vertex_properties(std::size_t n) : m_vertex_properties(n) { }
  53. public:
  54. // Clear the properties vector
  55. void clear()
  56. {
  57. m_vertex_properties.clear();
  58. }
  59. // Resize the properties vector
  60. void resize(std::size_t n)
  61. {
  62. m_vertex_properties.resize(n);
  63. }
  64. // Reserve space in the vector of properties
  65. void reserve(std::size_t n)
  66. {
  67. m_vertex_properties.reserve(n);
  68. }
  69. // Add a new property value to the back
  70. void push_back(const Property& prop)
  71. {
  72. m_vertex_properties.push_back(prop);
  73. }
  74. // Write an element by raw index
  75. void write_by_index(std::size_t idx, const Property& prop)
  76. {
  77. m_vertex_properties[idx] = prop;
  78. }
  79. // Access to the derived object
  80. Derived& derived() { return *static_cast<Derived*>(this); }
  81. const Derived& derived() const
  82. { return *static_cast<const Derived*>(this); }
  83. public: // should be private, but friend templates not portable
  84. std::vector<Property> m_vertex_properties;
  85. };
  86. template<typename Derived, typename Descriptor, typename IndexMap>
  87. class indexed_vertex_properties<Derived, void, Descriptor, IndexMap>
  88. {
  89. struct secret {};
  90. public:
  91. typedef no_property vertex_property_type;
  92. typedef void vertex_bundled;
  93. typedef secret vertex_map_type;
  94. typedef secret const_vertex_map_type;
  95. secret operator[](secret) { return secret(); }
  96. vertex_map_type get_vertex_bundle() const {
  97. return vertex_map_type();
  98. }
  99. protected:
  100. // All operations do nothing.
  101. indexed_vertex_properties() { }
  102. indexed_vertex_properties(std::size_t) { }
  103. public:
  104. void clear() { }
  105. void resize(std::size_t) { }
  106. void reserve(std::size_t) { }
  107. };
  108. template<typename Derived, typename Property, typename Descriptor, typename IndexMap>
  109. class indexed_edge_properties
  110. {
  111. public:
  112. typedef no_property edge_property_type;
  113. typedef Property edge_bundled;
  114. typedef Property edge_push_back_type;
  115. typedef iterator_property_map<
  116. typename std::vector<Property>::iterator,
  117. IndexMap> edge_map_type;
  118. typedef iterator_property_map<
  119. typename std::vector<Property>::const_iterator,
  120. IndexMap> const_edge_map_type;
  121. // Directly access a edge or edge bundle
  122. Property& operator[](Descriptor v)
  123. { return m_edge_properties[get(edge_index, derived(), v)]; }
  124. const Property& operator[](Descriptor v) const
  125. { return m_edge_properties[get(edge_index, derived(), v)]; }
  126. edge_map_type get_edge_bundle(const IndexMap& index_map = IndexMap()) {
  127. return edge_map_type(m_edge_properties.begin(), index_map);
  128. }
  129. const_edge_map_type get_edge_bundle(const IndexMap& index_map = IndexMap()) const {
  130. return const_edge_map_type(m_edge_properties.begin(), index_map);
  131. }
  132. protected:
  133. // Default-construct with no property values
  134. indexed_edge_properties() {}
  135. // Initialize with n default-constructed property values
  136. indexed_edge_properties(std::size_t n) : m_edge_properties(n) { }
  137. // Get the size of the properties vector
  138. std::size_t size() const
  139. {
  140. return m_edge_properties.size();
  141. }
  142. // Clear the properties vector
  143. void clear()
  144. {
  145. m_edge_properties.clear();
  146. }
  147. // Resize the properties vector
  148. void resize(std::size_t n)
  149. {
  150. m_edge_properties.resize(n);
  151. }
  152. // Reserve space in the vector of properties
  153. void reserve(std::size_t n)
  154. {
  155. m_edge_properties.reserve(n);
  156. }
  157. // Write an element by raw index
  158. void write_by_index(std::size_t idx, const Property& prop)
  159. {
  160. m_edge_properties[idx] = prop;
  161. }
  162. public:
  163. // Add a new property value to the back
  164. void push_back(const Property& prop)
  165. {
  166. m_edge_properties.push_back(prop);
  167. }
  168. // Move range of properties backwards
  169. void move_range(std::size_t src_begin, std::size_t src_end, std::size_t dest_begin) {
  170. std::copy_backward(
  171. m_edge_properties.begin() + src_begin,
  172. m_edge_properties.begin() + src_end,
  173. m_edge_properties.begin() + dest_begin + (src_end - src_begin));
  174. }
  175. typedef typename std::vector<Property>::iterator iterator;
  176. iterator begin() {return m_edge_properties.begin();}
  177. iterator end() {return m_edge_properties.end();}
  178. private:
  179. // Access to the derived object
  180. Derived& derived() { return *static_cast<Derived*>(this); }
  181. const Derived& derived() const
  182. { return *static_cast<const Derived*>(this); }
  183. public: // should be private, but friend templates not portable
  184. std::vector<Property> m_edge_properties;
  185. };
  186. struct dummy_no_property_iterator
  187. : public boost::iterator_facade<dummy_no_property_iterator, no_property, std::random_access_iterator_tag> {
  188. mutable no_property prop;
  189. no_property& dereference() const {return prop;}
  190. bool equal(const dummy_no_property_iterator&) const {return true;}
  191. void increment() {}
  192. void decrement() {}
  193. void advance(std::ptrdiff_t) {}
  194. std::ptrdiff_t distance_to(const dummy_no_property_iterator) const {return 0;}
  195. };
  196. template<typename Derived, typename Descriptor, typename IndexMap>
  197. class indexed_edge_properties<Derived, void, Descriptor, IndexMap>
  198. {
  199. struct secret {};
  200. public:
  201. typedef no_property edge_property_type;
  202. typedef void edge_bundled;
  203. typedef void* edge_push_back_type;
  204. typedef secret edge_map_type;
  205. typedef secret const_edge_map_type;
  206. secret operator[](secret) { return secret(); }
  207. void write_by_index(std::size_t /*idx*/, const no_property& /*prop*/) {}
  208. edge_map_type get_edge_bundle(const IndexMap& = IndexMap()) const {
  209. return edge_map_type();
  210. }
  211. protected:
  212. // All operations do nothing.
  213. indexed_edge_properties() { }
  214. indexed_edge_properties(std::size_t) { }
  215. std::size_t size() const {return 0;}
  216. void clear() { }
  217. void resize(std::size_t) { }
  218. void reserve(std::size_t) { }
  219. public:
  220. void push_back(const edge_push_back_type&) { }
  221. void move_range(std::size_t /*src_begin*/, std::size_t /*src_end*/, std::size_t /*dest_begin*/) {}
  222. typedef dummy_no_property_iterator iterator;
  223. iterator begin() {return dummy_no_property_iterator();}
  224. iterator end() {return dummy_no_property_iterator();}
  225. };
  226. }
  227. }
  228. #endif // BOOST_GRAPH_INDEXED_PROPERTIES_HPP