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 |
|
---|
10 | #ifndef VECTOR_PROPERTY_MAP_HPP_VP_2003_03_04
|
---|
11 | #define VECTOR_PROPERTY_MAP_HPP_VP_2003_03_04
|
---|
12 |
|
---|
13 | #include <boost/property_map.hpp>
|
---|
14 | #include <boost/shared_ptr.hpp>
|
---|
15 | #include <vector>
|
---|
16 |
|
---|
17 | namespace boost {
|
---|
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> >
|
---|
24 | {
|
---|
25 | public:
|
---|
26 | typedef typename property_traits<IndexMap>::key_type key_type;
|
---|
27 | typedef T value_type;
|
---|
28 | typedef typename std::iterator_traits<
|
---|
29 | typename std::vector<T>::iterator >::reference reference;
|
---|
30 | typedef boost::lvalue_property_map_tag category;
|
---|
31 |
|
---|
32 | vector_property_map(const IndexMap& index = IndexMap())
|
---|
33 | : store(new std::vector<T>()), index(index)
|
---|
34 | {}
|
---|
35 |
|
---|
36 | vector_property_map(unsigned initial_size,
|
---|
37 | const IndexMap& index = IndexMap())
|
---|
38 | : store(new std::vector<T>(initial_size)), index(index)
|
---|
39 | {}
|
---|
40 |
|
---|
41 | typename std::vector<T>::iterator storage_begin()
|
---|
42 | {
|
---|
43 | return store->begin();
|
---|
44 | }
|
---|
45 |
|
---|
46 | typename std::vector<T>::iterator storage_end()
|
---|
47 | {
|
---|
48 | return store->end();
|
---|
49 | }
|
---|
50 |
|
---|
51 | typename std::vector<T>::const_iterator storage_begin() const
|
---|
52 | {
|
---|
53 | return store->begin();
|
---|
54 | }
|
---|
55 |
|
---|
56 | typename std::vector<T>::const_iterator storage_end() const
|
---|
57 | {
|
---|
58 | return store->end();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public:
|
---|
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.
|
---|
65 |
|
---|
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());
|
---|
70 | }
|
---|
71 | return (*store)[i];
|
---|
72 | }
|
---|
73 | private:
|
---|
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;
|
---|
81 | IndexMap index;
|
---|
82 | };
|
---|
83 |
|
---|
84 | template<typename T, typename IndexMap>
|
---|
85 | vector_property_map<T, IndexMap>
|
---|
86 | make_vector_property_map(IndexMap index)
|
---|
87 | {
|
---|
88 | return vector_property_map<T, IndexMap>(index);
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | #endif
|
---|