vector_property_map.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (C) Vladimir Prus 2003.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. //
  6. // See http://www.boost.org/libs/graph/vector_property_map.html for
  7. // documentation.
  8. //
  9. #ifndef VECTOR_PROPERTY_MAP_HPP_VP_2003_03_04
  10. #define VECTOR_PROPERTY_MAP_HPP_VP_2003_03_04
  11. #include <boost/property_map/property_map.hpp>
  12. #include <boost/shared_ptr.hpp>
  13. #include <vector>
  14. namespace boost {
  15. template<typename T, typename IndexMap = identity_property_map>
  16. class vector_property_map
  17. : public boost::put_get_helper<
  18. typename std::iterator_traits<
  19. typename std::vector<T>::iterator >::reference,
  20. vector_property_map<T, IndexMap> >
  21. {
  22. public:
  23. typedef typename property_traits<IndexMap>::key_type key_type;
  24. typedef T value_type;
  25. typedef typename std::iterator_traits<
  26. typename std::vector<T>::iterator >::reference reference;
  27. typedef boost::lvalue_property_map_tag category;
  28. vector_property_map(const IndexMap& index = IndexMap())
  29. : store(new std::vector<T>()), index(index)
  30. {}
  31. vector_property_map(unsigned initial_size,
  32. const IndexMap& index = IndexMap())
  33. : store(new std::vector<T>(initial_size)), index(index)
  34. {}
  35. typename std::vector<T>::iterator storage_begin()
  36. {
  37. return store->begin();
  38. }
  39. typename std::vector<T>::iterator storage_end()
  40. {
  41. return store->end();
  42. }
  43. typename std::vector<T>::const_iterator storage_begin() const
  44. {
  45. return store->begin();
  46. }
  47. typename std::vector<T>::const_iterator storage_end() const
  48. {
  49. return store->end();
  50. }
  51. IndexMap& get_index_map() { return index; }
  52. const IndexMap& get_index_map() const { return index; }
  53. public:
  54. // Copy ctor absent, default semantics is OK.
  55. // Assignment operator absent, default semantics is OK.
  56. // CONSIDER: not sure that assignment to 'index' is correct.
  57. reference operator[](const key_type& v) const {
  58. typename property_traits<IndexMap>::value_type i = get(index, v);
  59. if (static_cast<unsigned>(i) >= store->size()) {
  60. store->resize(i + 1, T());
  61. }
  62. return (*store)[i];
  63. }
  64. private:
  65. // Conceptually, we have a vector of infinite size. For practical
  66. // purposes, we start with an empty vector and grow it as needed.
  67. // Note that we cannot store pointer to vector here -- we cannot
  68. // store pointer to data, because if copy of property map resizes
  69. // the vector, the pointer to data will be invalidated.
  70. // I wonder if class 'pmap_ref' is simply needed.
  71. shared_ptr< std::vector<T> > store;
  72. IndexMap index;
  73. };
  74. template<typename T, typename IndexMap>
  75. vector_property_map<T, IndexMap>
  76. make_vector_property_map(IndexMap index)
  77. {
  78. return vector_property_map<T, IndexMap>(index);
  79. }
  80. }
  81. #ifdef BOOST_GRAPH_USE_MPI
  82. #include <boost/property_map/parallel/distributed_property_map.hpp>
  83. #include <boost/property_map/parallel/local_property_map.hpp>
  84. namespace boost {
  85. /** Distributed vector property map.
  86. *
  87. * This specialization of @ref vector_property_map builds a
  88. * distributed vector property map given the local index maps
  89. * generated by distributed graph types that automatically have index
  90. * properties.
  91. *
  92. * This specialization is useful when creating external distributed
  93. * property maps via the same syntax used to create external
  94. * sequential property maps.
  95. */
  96. template<typename T, typename ProcessGroup, typename GlobalMap,
  97. typename StorageMap>
  98. class vector_property_map<T,
  99. local_property_map<ProcessGroup, GlobalMap,
  100. StorageMap> >
  101. : public parallel::distributed_property_map<
  102. ProcessGroup, GlobalMap, vector_property_map<T, StorageMap> >
  103. {
  104. typedef vector_property_map<T, StorageMap> local_iterator_map;
  105. typedef parallel::distributed_property_map<ProcessGroup, GlobalMap,
  106. local_iterator_map> inherited;
  107. typedef local_property_map<ProcessGroup, GlobalMap, StorageMap> index_map_type;
  108. public:
  109. vector_property_map(const index_map_type& index = index_map_type())
  110. : inherited(index.process_group(), index.global(),
  111. local_iterator_map(index.base())) { }
  112. vector_property_map(unsigned inital_size,
  113. const index_map_type& index = index_map_type())
  114. : inherited(index.process_group(), index.global(),
  115. local_iterator_map(inital_size, index.base())) { }
  116. };
  117. /** Distributed vector property map.
  118. *
  119. * This specialization of @ref vector_property_map builds a
  120. * distributed vector property map given the local index maps
  121. * generated by distributed graph types that automatically have index
  122. * properties.
  123. *
  124. * This specialization is useful when creating external distributed
  125. * property maps via the same syntax used to create external
  126. * sequential property maps.
  127. */
  128. template<typename T, typename ProcessGroup, typename GlobalMap,
  129. typename StorageMap>
  130. class vector_property_map<
  131. T,
  132. parallel::distributed_property_map<
  133. ProcessGroup,
  134. GlobalMap,
  135. StorageMap
  136. >
  137. >
  138. : public parallel::distributed_property_map<
  139. ProcessGroup, GlobalMap, vector_property_map<T, StorageMap> >
  140. {
  141. typedef vector_property_map<T, StorageMap> local_iterator_map;
  142. typedef parallel::distributed_property_map<ProcessGroup, GlobalMap,
  143. local_iterator_map> inherited;
  144. typedef parallel::distributed_property_map<ProcessGroup, GlobalMap,
  145. StorageMap>
  146. index_map_type;
  147. public:
  148. vector_property_map(const index_map_type& index = index_map_type())
  149. : inherited(index.process_group(), index.global(),
  150. local_iterator_map(index.base())) { }
  151. vector_property_map(unsigned inital_size,
  152. const index_map_type& index = index_map_type())
  153. : inherited(index.process_group(), index.global(),
  154. local_iterator_map(inital_size, index.base())) { }
  155. };
  156. }
  157. #endif // BOOST_GRAPH_USE_MPI
  158. #endif