vector_property_map.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 BOOST_PROPERTY_MAP_VECTOR_PROPERTY_MAP_HPP
  10. #define BOOST_PROPERTY_MAP_VECTOR_PROPERTY_MAP_HPP
  11. #include <boost/property_map/property_map.hpp>
  12. #include <boost/smart_ptr/shared_ptr.hpp>
  13. #include <iterator>
  14. #include <vector>
  15. namespace boost {
  16. template<typename T, typename IndexMap = identity_property_map>
  17. class vector_property_map
  18. : public boost::put_get_helper<
  19. typename std::iterator_traits<
  20. typename std::vector<T>::iterator >::reference,
  21. vector_property_map<T, IndexMap> >
  22. {
  23. public:
  24. typedef typename property_traits<IndexMap>::key_type key_type;
  25. typedef T value_type;
  26. typedef typename std::iterator_traits<
  27. typename std::vector<T>::iterator >::reference reference;
  28. typedef boost::lvalue_property_map_tag category;
  29. vector_property_map(const IndexMap& index = IndexMap())
  30. : store(new std::vector<T>()), index(index)
  31. {}
  32. vector_property_map(unsigned initial_size,
  33. const IndexMap& index = IndexMap())
  34. : store(new std::vector<T>(initial_size)), index(index)
  35. {}
  36. typename std::vector<T>::iterator storage_begin()
  37. {
  38. return store->begin();
  39. }
  40. typename std::vector<T>::iterator storage_end()
  41. {
  42. return store->end();
  43. }
  44. typename std::vector<T>::const_iterator storage_begin() const
  45. {
  46. return store->begin();
  47. }
  48. typename std::vector<T>::const_iterator storage_end() const
  49. {
  50. return store->end();
  51. }
  52. IndexMap& get_index_map() { return index; }
  53. const IndexMap& get_index_map() const { return index; }
  54. public:
  55. // Copy ctor absent, default semantics is OK.
  56. // Assignment operator absent, default semantics is OK.
  57. // CONSIDER: not sure that assignment to 'index' is correct.
  58. reference operator[](const key_type& v) const {
  59. typename property_traits<IndexMap>::value_type i = get(index, v);
  60. if (static_cast<unsigned>(i) >= store->size()) {
  61. store->resize(i + 1, T());
  62. }
  63. return (*store)[i];
  64. }
  65. private:
  66. // Conceptually, we have a vector of infinite size. For practical
  67. // purposes, we start with an empty vector and grow it as needed.
  68. // Note that we cannot store pointer to vector here -- we cannot
  69. // store pointer to data, because if copy of property map resizes
  70. // the vector, the pointer to data will be invalidated.
  71. // I wonder if class 'pmap_ref' is simply needed.
  72. shared_ptr< std::vector<T> > store;
  73. IndexMap index;
  74. };
  75. template<typename T, typename IndexMap>
  76. vector_property_map<T, IndexMap>
  77. make_vector_property_map(IndexMap index)
  78. {
  79. return vector_property_map<T, IndexMap>(index);
  80. }
  81. }
  82. #endif