vector_property_map.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/vector_property_map.hpp>
  83. #endif
  84. #endif