]>
Commit | Line | Data |
---|---|---|
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/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 | IndexMap& get_index_map() { return index; } | |
62 | const IndexMap& get_index_map() const { return index; } | |
63 | ||
64 | public: | |
65 | // Copy ctor absent, default semantics is OK. | |
66 | // Assignment operator absent, default semantics is OK. | |
67 | // CONSIDER: not sure that assignment to 'index' is correct. | |
68 | ||
69 | reference operator[](const key_type& v) const { | |
70 | typename property_traits<IndexMap>::value_type i = get(index, v); | |
71 | if (static_cast<unsigned>(i) >= store->size()) { | |
72 | store->resize(i + 1, T()); | |
73 | } | |
74 | return (*store)[i]; | |
75 | } | |
76 | private: | |
77 | // Conceptually, we have a vector of infinite size. For practical | |
78 | // purposes, we start with an empty vector and grow it as needed. | |
79 | // Note that we cannot store pointer to vector here -- we cannot | |
80 | // store pointer to data, because if copy of property map resizes | |
81 | // the vector, the pointer to data will be invalidated. | |
82 | // I wonder if class 'pmap_ref' is simply needed. | |
83 | shared_ptr< std::vector<T> > store; | |
84 | IndexMap index; | |
85 | }; | |
86 | ||
87 | template<typename T, typename IndexMap> | |
88 | vector_property_map<T, IndexMap> | |
89 | make_vector_property_map(IndexMap index) | |
90 | { | |
91 | return vector_property_map<T, IndexMap>(index); | |
92 | } | |
93 | } | |
94 | ||
95 | #ifdef BOOST_GRAPH_USE_MPI | |
96 | #include <boost/property_map/parallel/vector_property_map.hpp> | |
97 | #endif | |
98 | ||
99 | #endif |