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)
6 // See http://www.boost.org/libs/graph/vector_property_map.html for
10 #ifndef VECTOR_PROPERTY_MAP_HPP_VP_2003_03_04
11 #define VECTOR_PROPERTY_MAP_HPP_VP_2003_03_04
13 #include <boost/property_map.hpp>
14 #include <boost/shared_ptr.hpp>
18 template<typename T, typename IndexMap = identity_property_map>
19 class vector_property_map
20 : public boost::put_get_helper<
21 typename std::iterator_traits<
22 typename std::vector<T>::iterator >::reference,
23 vector_property_map<T, IndexMap> >
26 typedef typename property_traits<IndexMap>::key_type key_type;
28 typedef typename std::iterator_traits<
29 typename std::vector<T>::iterator >::reference reference;
30 typedef boost::lvalue_property_map_tag category;
32 vector_property_map(const IndexMap& index = IndexMap())
33 : store(new std::vector<T>()), index(index)
36 vector_property_map(unsigned initial_size,
37 const IndexMap& index = IndexMap())
38 : store(new std::vector<T>(initial_size)), index(index)
41 typename std::vector<T>::iterator storage_begin()
43 return store->begin();
46 typename std::vector<T>::iterator storage_end()
51 typename std::vector<T>::const_iterator storage_begin() const
53 return store->begin();
56 typename std::vector<T>::const_iterator storage_end() const
62 // Copy ctor absent, default semantics is OK.
63 // Assignment operator absent, default semantics is OK.
64 // CONSIDER: not sure that assignment to 'index' is correct.
66 reference operator[](const key_type& v) const {
67 typename property_traits<IndexMap>::value_type i = get(index, v);
68 if (static_cast<unsigned>(i) >= store->size()) {
69 store->resize(i + 1, T());
74 // Conceptually, we have a vector of infinite size. For practical
75 // purposes, we start with an empty vector and grow it as needed.
76 // Note that we cannot store pointer to vector here -- we cannot
77 // store pointer to data, because if copy of property map resizes
78 // the vector, the pointer to data will be invalidated.
79 // I wonder if class 'pmap_ref' is simply needed.
80 shared_ptr< std::vector<T> > store;
84 template<typename T, typename IndexMap>
85 vector_property_map<T, IndexMap>
86 make_vector_property_map(IndexMap index)
88 return vector_property_map<T, IndexMap>(index);