fix doc example typo
[boost.git] / boost / property_map / vector_property_map.hpp
bloba97fb8105b2072ce95c44238e36b064dd33bbfef
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 //
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/property_map.hpp>
14 #include <boost/shared_ptr.hpp>
15 #include <vector>
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> >
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;
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()
48 return store->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
58 return store->end();
61 IndexMap& get_index_map() { return index; }
62 const IndexMap& get_index_map() const { return index; }
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.
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());
74 return (*store)[i];
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;
87 template<typename T, typename IndexMap>
88 vector_property_map<T, IndexMap>
89 make_vector_property_map(IndexMap index)
91 return vector_property_map<T, IndexMap>(index);
95 #ifdef BOOST_GRAPH_USE_MPI
96 #include <boost/property_map/parallel/distributed_property_map.hpp>
97 #include <boost/property_map/parallel/local_property_map.hpp>
99 namespace boost {
101 /** Distributed vector property map.
103 * This specialization of @ref vector_property_map builds a
104 * distributed vector property map given the local index maps
105 * generated by distributed graph types that automatically have index
106 * properties.
108 * This specialization is useful when creating external distributed
109 * property maps via the same syntax used to create external
110 * sequential property maps.
112 template<typename T, typename ProcessGroup, typename GlobalMap,
113 typename StorageMap>
114 class vector_property_map<T,
115 local_property_map<ProcessGroup, GlobalMap,
116 StorageMap> >
117 : public parallel::distributed_property_map<
118 ProcessGroup, GlobalMap, vector_property_map<T, StorageMap> >
120 typedef vector_property_map<T, StorageMap> local_iterator_map;
122 typedef parallel::distributed_property_map<ProcessGroup, GlobalMap,
123 local_iterator_map> inherited;
125 typedef local_property_map<ProcessGroup, GlobalMap, StorageMap> index_map_type;
127 public:
128 vector_property_map(const index_map_type& index = index_map_type())
129 : inherited(index.process_group(), index.global(),
130 local_iterator_map(index.base())) { }
132 vector_property_map(unsigned inital_size,
133 const index_map_type& index = index_map_type())
134 : inherited(index.process_group(), index.global(),
135 local_iterator_map(inital_size, index.base())) { }
138 /** Distributed vector property map.
140 * This specialization of @ref vector_property_map builds a
141 * distributed vector property map given the local index maps
142 * generated by distributed graph types that automatically have index
143 * properties.
145 * This specialization is useful when creating external distributed
146 * property maps via the same syntax used to create external
147 * sequential property maps.
149 template<typename T, typename ProcessGroup, typename GlobalMap,
150 typename StorageMap>
151 class vector_property_map<
153 parallel::distributed_property_map<
154 ProcessGroup,
155 GlobalMap,
156 StorageMap
159 : public parallel::distributed_property_map<
160 ProcessGroup, GlobalMap, vector_property_map<T, StorageMap> >
162 typedef vector_property_map<T, StorageMap> local_iterator_map;
164 typedef parallel::distributed_property_map<ProcessGroup, GlobalMap,
165 local_iterator_map> inherited;
167 typedef parallel::distributed_property_map<ProcessGroup, GlobalMap,
168 StorageMap>
169 index_map_type;
171 public:
172 vector_property_map(const index_map_type& index = index_map_type())
173 : inherited(index.process_group(), index.global(),
174 local_iterator_map(index.base())) { }
176 vector_property_map(unsigned inital_size,
177 const index_map_type& index = index_map_type())
178 : inherited(index.process_group(), index.global(),
179 local_iterator_map(inital_size, index.base())) { }
183 #endif // BOOST_GRAPH_USE_MPI
185 #endif