Chris@16
|
1 //
|
Chris@16
|
2 // Copyright (c) 2000-2010
|
Chris@16
|
3 // Joerg Walter, Mathias Koch, David Bellot
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0. (See
|
Chris@16
|
6 // accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 //
|
Chris@16
|
9 // The authors gratefully acknowledge the support of
|
Chris@16
|
10 // GeNeSys mbH & Co. KG in producing this work.
|
Chris@16
|
11 //
|
Chris@16
|
12 // And we acknowledge the support from all contributors.
|
Chris@16
|
13
|
Chris@16
|
14 /// \file vector.hpp Definition for the class vector and its derivative
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef _BOOST_UBLAS_VECTOR_
|
Chris@16
|
17 #define _BOOST_UBLAS_VECTOR_
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/numeric/ublas/storage.hpp>
|
Chris@16
|
20 #include <boost/numeric/ublas/vector_expression.hpp>
|
Chris@16
|
21 #include <boost/numeric/ublas/detail/vector_assign.hpp>
|
Chris@16
|
22 #include <boost/serialization/collection_size_type.hpp>
|
Chris@16
|
23 #include <boost/serialization/nvp.hpp>
|
Chris@16
|
24
|
Chris@16
|
25
|
Chris@16
|
26 // Iterators based on ideas of Jeremy Siek
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost { namespace numeric { namespace ublas {
|
Chris@16
|
29
|
Chris@16
|
30 /** \brief A dense vector of values of type \c T.
|
Chris@16
|
31 *
|
Chris@16
|
32 * For a \f$n\f$-dimensional vector \f$v\f$ and \f$0\leq i < n\f$ every element \f$v_i\f$ is mapped
|
Chris@16
|
33 * to the \f$i\f$-th element of the container. A storage type \c A can be specified which defaults to \c unbounded_array.
|
Chris@16
|
34 * Elements are constructed by \c A, which need not initialise their value.
|
Chris@16
|
35 *
|
Chris@16
|
36 * \tparam T type of the objects stored in the vector (like int, double, complex,...)
|
Chris@16
|
37 * \tparam A The type of the storage array of the vector. Default is \c unbounded_array<T>. \c <bounded_array<T> and \c std::vector<T> can also be used
|
Chris@16
|
38 */
|
Chris@16
|
39 template<class T, class A>
|
Chris@16
|
40 class vector:
|
Chris@16
|
41 public vector_container<vector<T, A> > {
|
Chris@16
|
42
|
Chris@16
|
43 typedef vector<T, A> self_type;
|
Chris@16
|
44 public:
|
Chris@16
|
45 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
|
Chris@16
|
46 using vector_container<self_type>::operator ();
|
Chris@16
|
47 #endif
|
Chris@16
|
48
|
Chris@16
|
49 typedef typename A::size_type size_type;
|
Chris@16
|
50 typedef typename A::difference_type difference_type;
|
Chris@16
|
51 typedef T value_type;
|
Chris@16
|
52 typedef typename type_traits<T>::const_reference const_reference;
|
Chris@16
|
53 typedef T &reference;
|
Chris@16
|
54 typedef T *pointer;
|
Chris@16
|
55 typedef const T *const_pointer;
|
Chris@16
|
56 typedef A array_type;
|
Chris@16
|
57 typedef const vector_reference<const self_type> const_closure_type;
|
Chris@16
|
58 typedef vector_reference<self_type> closure_type;
|
Chris@16
|
59 typedef self_type vector_temporary_type;
|
Chris@16
|
60 typedef dense_tag storage_category;
|
Chris@16
|
61
|
Chris@16
|
62 // Construction and destruction
|
Chris@16
|
63
|
Chris@16
|
64 /// \brief Constructor of a vector
|
Chris@16
|
65 /// By default it is empty, i.e. \c size()==0.
|
Chris@16
|
66 BOOST_UBLAS_INLINE
|
Chris@16
|
67 vector ():
|
Chris@16
|
68 vector_container<self_type> (),
|
Chris@16
|
69 data_ () {}
|
Chris@16
|
70
|
Chris@16
|
71 /// \brief Constructor of a vector with a predefined size
|
Chris@16
|
72 /// By default, its elements are initialized to 0.
|
Chris@16
|
73 /// \param size initial size of the vector
|
Chris@16
|
74 explicit BOOST_UBLAS_INLINE
|
Chris@16
|
75 vector (size_type size):
|
Chris@16
|
76 vector_container<self_type> (),
|
Chris@16
|
77 data_ (size) {
|
Chris@16
|
78 }
|
Chris@16
|
79
|
Chris@16
|
80 /// \brief Constructor of a vector by copying from another container
|
Chris@16
|
81 /// This type has the generic name \c array_typ within the vector definition.
|
Chris@16
|
82 /// \param size initial size of the vector \bug this value is not used
|
Chris@16
|
83 /// \param data container of type \c A
|
Chris@16
|
84 /// \todo remove this definition because \c size is not used
|
Chris@16
|
85 BOOST_UBLAS_INLINE
|
Chris@16
|
86 vector (size_type size, const array_type &data):
|
Chris@16
|
87 vector_container<self_type> (),
|
Chris@16
|
88 data_ (data) {}
|
Chris@16
|
89
|
Chris@16
|
90 /// \brief Constructor of a vector by copying from another container
|
Chris@16
|
91 /// This type has the generic name \c array_typ within the vector definition.
|
Chris@16
|
92 /// \param data container of type \c A
|
Chris@16
|
93 BOOST_UBLAS_INLINE
|
Chris@16
|
94 vector (const array_type &data):
|
Chris@16
|
95 vector_container<self_type> (),
|
Chris@16
|
96 data_ (data) {}
|
Chris@16
|
97
|
Chris@16
|
98 /// \brief Constructor of a vector with a predefined size and a unique initial value
|
Chris@16
|
99 /// \param size of the vector
|
Chris@16
|
100 /// \param init value to assign to each element of the vector
|
Chris@16
|
101 BOOST_UBLAS_INLINE
|
Chris@16
|
102 vector (size_type size, const value_type &init):
|
Chris@16
|
103 vector_container<self_type> (),
|
Chris@16
|
104 data_ (size, init) {}
|
Chris@16
|
105
|
Chris@16
|
106 /// \brief Copy-constructor of a vector
|
Chris@16
|
107 /// \param v is the vector to be duplicated
|
Chris@16
|
108 BOOST_UBLAS_INLINE
|
Chris@16
|
109 vector (const vector &v):
|
Chris@16
|
110 vector_container<self_type> (),
|
Chris@16
|
111 data_ (v.data_) {}
|
Chris@16
|
112
|
Chris@16
|
113 /// \brief Copy-constructor of a vector from a vector_expression
|
Chris@16
|
114 /// Depending on the vector_expression, this constructor can have the cost of the computations
|
Chris@16
|
115 /// of the expression (trivial to say it, but it is to take into account in your complexity calculations).
|
Chris@16
|
116 /// \param ae the vector_expression which values will be duplicated into the vector
|
Chris@16
|
117 template<class AE>
|
Chris@16
|
118 BOOST_UBLAS_INLINE
|
Chris@16
|
119 vector (const vector_expression<AE> &ae):
|
Chris@16
|
120 vector_container<self_type> (),
|
Chris@16
|
121 data_ (ae ().size ()) {
|
Chris@16
|
122 vector_assign<scalar_assign> (*this, ae);
|
Chris@16
|
123 }
|
Chris@16
|
124
|
Chris@16
|
125 // -----------------------
|
Chris@16
|
126 // Random Access Container
|
Chris@16
|
127 // -----------------------
|
Chris@16
|
128
|
Chris@16
|
129 /// \brief Return the maximum size of the data container.
|
Chris@16
|
130 /// Return the upper bound (maximum size) on the data container. Depending on the container, it can be bigger than the current size of the vector.
|
Chris@16
|
131 BOOST_UBLAS_INLINE
|
Chris@16
|
132 size_type max_size () const {
|
Chris@16
|
133 return data_.max_size ();
|
Chris@16
|
134 }
|
Chris@16
|
135
|
Chris@16
|
136 /// \brief Return true if the vector is empty (\c size==0)
|
Chris@16
|
137 /// \return \c true if empty, \c false otherwise
|
Chris@16
|
138 BOOST_UBLAS_INLINE
|
Chris@16
|
139 bool empty () const {
|
Chris@16
|
140 return data_.size () == 0;
|
Chris@16
|
141 }
|
Chris@16
|
142
|
Chris@16
|
143 // ---------
|
Chris@16
|
144 // Accessors
|
Chris@16
|
145 // ---------
|
Chris@16
|
146
|
Chris@16
|
147 /// \brief Return the size of the vector
|
Chris@16
|
148 BOOST_UBLAS_INLINE
|
Chris@16
|
149 size_type size () const {
|
Chris@16
|
150 return data_.size ();
|
Chris@16
|
151 }
|
Chris@16
|
152
|
Chris@16
|
153 // -----------------
|
Chris@16
|
154 // Storage accessors
|
Chris@16
|
155 // -----------------
|
Chris@16
|
156
|
Chris@16
|
157 /// \brief Return a \c const reference to the container. Useful to access data directly for specific type of container.
|
Chris@16
|
158 BOOST_UBLAS_INLINE
|
Chris@16
|
159 const array_type &data () const {
|
Chris@16
|
160 return data_;
|
Chris@16
|
161 }
|
Chris@16
|
162
|
Chris@16
|
163 /// \brief Return a reference to the container. Useful to speed-up write operations to the data in very specific case.
|
Chris@16
|
164 BOOST_UBLAS_INLINE
|
Chris@16
|
165 array_type &data () {
|
Chris@16
|
166 return data_;
|
Chris@16
|
167 }
|
Chris@16
|
168
|
Chris@16
|
169 // --------
|
Chris@16
|
170 // Resizing
|
Chris@16
|
171 // --------
|
Chris@16
|
172
|
Chris@16
|
173 /// \brief Resize the vector
|
Chris@16
|
174 /// Resize the vector to a new size. If \c preserve is true, data are copied otherwise data are lost. If the new size is bigger, the remaining values are filled in with the initial value (0 by default) in the case of \c unbounded_array, which is the container by default. If the new size is smaller, last values are lost. This behaviour can be different if you explicitely specify another type of container.
|
Chris@16
|
175 /// \param size new size of the vector
|
Chris@16
|
176 /// \param preserve if true, keep values
|
Chris@16
|
177 BOOST_UBLAS_INLINE
|
Chris@16
|
178 void resize (size_type size, bool preserve = true) {
|
Chris@16
|
179 if (preserve)
|
Chris@16
|
180 data ().resize (size, typename A::value_type ());
|
Chris@16
|
181 else
|
Chris@16
|
182 data ().resize (size);
|
Chris@16
|
183 }
|
Chris@16
|
184
|
Chris@16
|
185 // ---------------
|
Chris@16
|
186 // Element support
|
Chris@16
|
187 // ---------------
|
Chris@16
|
188
|
Chris@16
|
189 /// \brief Return a pointer to the element \f$i\f$
|
Chris@16
|
190 /// \param i index of the element
|
Chris@16
|
191 // XXX this semantic is not the one expected by the name of this method
|
Chris@16
|
192 BOOST_UBLAS_INLINE
|
Chris@16
|
193 pointer find_element (size_type i) {
|
Chris@16
|
194 return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i));
|
Chris@16
|
195 }
|
Chris@16
|
196
|
Chris@16
|
197 /// \brief Return a const pointer to the element \f$i\f$
|
Chris@16
|
198 /// \param i index of the element
|
Chris@16
|
199 // XXX this semantic is not the one expected by the name of this method
|
Chris@16
|
200 BOOST_UBLAS_INLINE
|
Chris@16
|
201 const_pointer find_element (size_type i) const {
|
Chris@16
|
202 return & (data () [i]);
|
Chris@16
|
203 }
|
Chris@16
|
204
|
Chris@16
|
205 // --------------
|
Chris@16
|
206 // Element access
|
Chris@16
|
207 // --------------
|
Chris@16
|
208
|
Chris@16
|
209 /// \brief Return a const reference to the element \f$i\f$
|
Chris@16
|
210 /// Return a const reference to the element \f$i\f$. With some compilers, this notation will be faster than \c[i]
|
Chris@16
|
211 /// \param i index of the element
|
Chris@16
|
212 BOOST_UBLAS_INLINE
|
Chris@16
|
213 const_reference operator () (size_type i) const {
|
Chris@16
|
214 return data () [i];
|
Chris@16
|
215 }
|
Chris@16
|
216
|
Chris@16
|
217 /// \brief Return a reference to the element \f$i\f$
|
Chris@16
|
218 /// Return a reference to the element \f$i\f$. With some compilers, this notation will be faster than \c[i]
|
Chris@16
|
219 /// \param i index of the element
|
Chris@16
|
220 BOOST_UBLAS_INLINE
|
Chris@16
|
221 reference operator () (size_type i) {
|
Chris@16
|
222 return data () [i];
|
Chris@16
|
223 }
|
Chris@16
|
224
|
Chris@16
|
225 /// \brief Return a const reference to the element \f$i\f$
|
Chris@16
|
226 /// \param i index of the element
|
Chris@16
|
227 BOOST_UBLAS_INLINE
|
Chris@16
|
228 const_reference operator [] (size_type i) const {
|
Chris@16
|
229 return (*this) (i);
|
Chris@16
|
230 }
|
Chris@16
|
231
|
Chris@16
|
232 /// \brief Return a reference to the element \f$i\f$
|
Chris@16
|
233 /// \param i index of the element
|
Chris@16
|
234 BOOST_UBLAS_INLINE
|
Chris@16
|
235 reference operator [] (size_type i) {
|
Chris@16
|
236 return (*this) (i);
|
Chris@16
|
237 }
|
Chris@16
|
238
|
Chris@16
|
239 // ------------------
|
Chris@16
|
240 // Element assignment
|
Chris@16
|
241 // ------------------
|
Chris@16
|
242
|
Chris@16
|
243 /// \brief Set element \f$i\f$ to the value \c t
|
Chris@16
|
244 /// \param i index of the element
|
Chris@16
|
245 /// \param t reference to the value to be set
|
Chris@16
|
246 // XXX semantic of this is to insert a new element and therefore size=size+1 ?
|
Chris@16
|
247 BOOST_UBLAS_INLINE
|
Chris@16
|
248 reference insert_element (size_type i, const_reference t) {
|
Chris@16
|
249 return (data () [i] = t);
|
Chris@16
|
250 }
|
Chris@16
|
251
|
Chris@16
|
252 /// \brief Set element \f$i\f$ to the \e zero value
|
Chris@16
|
253 /// \param i index of the element
|
Chris@16
|
254 BOOST_UBLAS_INLINE
|
Chris@16
|
255 void erase_element (size_type i) {
|
Chris@16
|
256 data () [i] = value_type/*zero*/();
|
Chris@16
|
257 }
|
Chris@16
|
258
|
Chris@16
|
259 // -------
|
Chris@16
|
260 // Zeroing
|
Chris@16
|
261 // -------
|
Chris@16
|
262
|
Chris@16
|
263 /// \brief Clear the vector, i.e. set all values to the \c zero value.
|
Chris@16
|
264 BOOST_UBLAS_INLINE
|
Chris@16
|
265 void clear () {
|
Chris@16
|
266 std::fill (data ().begin (), data ().end (), value_type/*zero*/());
|
Chris@16
|
267 }
|
Chris@16
|
268
|
Chris@16
|
269 // Assignment
|
Chris@16
|
270 #ifdef BOOST_UBLAS_MOVE_SEMANTICS
|
Chris@16
|
271
|
Chris@16
|
272 /// \brief Assign a full vector (\e RHS-vector) to the current vector (\e LHS-vector)
|
Chris@16
|
273 /// \param v is the source vector
|
Chris@16
|
274 /// \return a reference to a vector (i.e. the destination vector)
|
Chris@16
|
275 /*! @note "pass by value" the key idea to enable move semantics */
|
Chris@16
|
276 BOOST_UBLAS_INLINE
|
Chris@16
|
277 vector &operator = (vector v) {
|
Chris@16
|
278 assign_temporary(v);
|
Chris@16
|
279 return *this;
|
Chris@16
|
280 }
|
Chris@16
|
281 #else
|
Chris@16
|
282 /// \brief Assign a full vector (\e RHS-vector) to the current vector (\e LHS-vector)
|
Chris@16
|
283 /// \param v is the source vector
|
Chris@16
|
284 /// \return a reference to a vector (i.e. the destination vector)
|
Chris@16
|
285 BOOST_UBLAS_INLINE
|
Chris@16
|
286 vector &operator = (const vector &v) {
|
Chris@16
|
287 data () = v.data ();
|
Chris@16
|
288 return *this;
|
Chris@16
|
289 }
|
Chris@16
|
290 #endif
|
Chris@16
|
291
|
Chris@16
|
292 /// \brief Assign a full vector (\e RHS-vector) to the current vector (\e LHS-vector)
|
Chris@16
|
293 /// Assign a full vector (\e RHS-vector) to the current vector (\e LHS-vector). This method does not create any temporary.
|
Chris@16
|
294 /// \param v is the source vector container
|
Chris@16
|
295 /// \return a reference to a vector (i.e. the destination vector)
|
Chris@16
|
296 template<class C> // Container assignment without temporary
|
Chris@16
|
297 BOOST_UBLAS_INLINE
|
Chris@16
|
298 vector &operator = (const vector_container<C> &v) {
|
Chris@16
|
299 resize (v ().size (), false);
|
Chris@16
|
300 assign (v);
|
Chris@16
|
301 return *this;
|
Chris@16
|
302 }
|
Chris@16
|
303
|
Chris@16
|
304 /// \brief Assign a full vector (\e RHS-vector) to the current vector (\e LHS-vector)
|
Chris@16
|
305 /// \param v is the source vector
|
Chris@16
|
306 /// \return a reference to a vector (i.e. the destination vector)
|
Chris@16
|
307 BOOST_UBLAS_INLINE
|
Chris@16
|
308 vector &assign_temporary (vector &v) {
|
Chris@16
|
309 swap (v);
|
Chris@16
|
310 return *this;
|
Chris@16
|
311 }
|
Chris@16
|
312
|
Chris@16
|
313 /// \brief Assign the result of a vector_expression to the vector
|
Chris@16
|
314 /// Assign the result of a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression.
|
Chris@16
|
315 /// \tparam AE is the type of the vector_expression
|
Chris@16
|
316 /// \param ae is a const reference to the vector_expression
|
Chris@16
|
317 /// \return a reference to the resulting vector
|
Chris@16
|
318 template<class AE>
|
Chris@16
|
319 BOOST_UBLAS_INLINE
|
Chris@16
|
320 vector &operator = (const vector_expression<AE> &ae) {
|
Chris@16
|
321 self_type temporary (ae);
|
Chris@16
|
322 return assign_temporary (temporary);
|
Chris@16
|
323 }
|
Chris@16
|
324
|
Chris@16
|
325 /// \brief Assign the result of a vector_expression to the vector
|
Chris@16
|
326 /// Assign the result of a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression.
|
Chris@16
|
327 /// \tparam AE is the type of the vector_expression
|
Chris@16
|
328 /// \param ae is a const reference to the vector_expression
|
Chris@16
|
329 /// \return a reference to the resulting vector
|
Chris@16
|
330 template<class AE>
|
Chris@16
|
331 BOOST_UBLAS_INLINE
|
Chris@16
|
332 vector &assign (const vector_expression<AE> &ae) {
|
Chris@16
|
333 vector_assign<scalar_assign> (*this, ae);
|
Chris@16
|
334 return *this;
|
Chris@16
|
335 }
|
Chris@16
|
336
|
Chris@16
|
337 // -------------------
|
Chris@16
|
338 // Computed assignment
|
Chris@16
|
339 // -------------------
|
Chris@16
|
340
|
Chris@16
|
341 /// \brief Assign the sum of the vector and a vector_expression to the vector
|
Chris@16
|
342 /// Assign the sum of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression.
|
Chris@16
|
343 /// A temporary is created for the computations.
|
Chris@16
|
344 /// \tparam AE is the type of the vector_expression
|
Chris@16
|
345 /// \param ae is a const reference to the vector_expression
|
Chris@16
|
346 /// \return a reference to the resulting vector
|
Chris@16
|
347 template<class AE>
|
Chris@16
|
348 BOOST_UBLAS_INLINE
|
Chris@16
|
349 vector &operator += (const vector_expression<AE> &ae) {
|
Chris@16
|
350 self_type temporary (*this + ae);
|
Chris@16
|
351 return assign_temporary (temporary);
|
Chris@16
|
352 }
|
Chris@16
|
353
|
Chris@16
|
354 /// \brief Assign the sum of the vector and a vector_expression to the vector
|
Chris@16
|
355 /// Assign the sum of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression.
|
Chris@16
|
356 /// No temporary is created. Computations are done and stored directly into the resulting vector.
|
Chris@16
|
357 /// \tparam AE is the type of the vector_expression
|
Chris@16
|
358 /// \param ae is a const reference to the vector_expression
|
Chris@16
|
359 /// \return a reference to the resulting vector
|
Chris@16
|
360 template<class C> // Container assignment without temporary
|
Chris@16
|
361 BOOST_UBLAS_INLINE
|
Chris@16
|
362 vector &operator += (const vector_container<C> &v) {
|
Chris@16
|
363 plus_assign (v);
|
Chris@16
|
364 return *this;
|
Chris@16
|
365 }
|
Chris@16
|
366
|
Chris@16
|
367 /// \brief Assign the sum of the vector and a vector_expression to the vector
|
Chris@16
|
368 /// Assign the sum of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression.
|
Chris@16
|
369 /// No temporary is created. Computations are done and stored directly into the resulting vector.
|
Chris@16
|
370 /// \tparam AE is the type of the vector_expression
|
Chris@16
|
371 /// \param ae is a const reference to the vector_expression
|
Chris@16
|
372 /// \return a reference to the resulting vector
|
Chris@16
|
373 template<class AE>
|
Chris@16
|
374 BOOST_UBLAS_INLINE
|
Chris@16
|
375 vector &plus_assign (const vector_expression<AE> &ae) {
|
Chris@16
|
376 vector_assign<scalar_plus_assign> (*this, ae);
|
Chris@16
|
377 return *this;
|
Chris@16
|
378 }
|
Chris@16
|
379
|
Chris@16
|
380 /// \brief Assign the difference of the vector and a vector_expression to the vector
|
Chris@16
|
381 /// Assign the difference of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression.
|
Chris@16
|
382 /// A temporary is created for the computations.
|
Chris@16
|
383 /// \tparam AE is the type of the vector_expression
|
Chris@16
|
384 /// \param ae is a const reference to the vector_expression
|
Chris@16
|
385 template<class AE>
|
Chris@16
|
386 BOOST_UBLAS_INLINE
|
Chris@16
|
387 vector &operator -= (const vector_expression<AE> &ae) {
|
Chris@16
|
388 self_type temporary (*this - ae);
|
Chris@16
|
389 return assign_temporary (temporary);
|
Chris@16
|
390 }
|
Chris@16
|
391
|
Chris@16
|
392 /// \brief Assign the difference of the vector and a vector_expression to the vector
|
Chris@16
|
393 /// Assign the difference of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression.
|
Chris@16
|
394 /// No temporary is created. Computations are done and stored directly into the resulting vector.
|
Chris@16
|
395 /// \tparam AE is the type of the vector_expression
|
Chris@16
|
396 /// \param ae is a const reference to the vector_expression
|
Chris@16
|
397 /// \return a reference to the resulting vector
|
Chris@16
|
398 template<class C> // Container assignment without temporary
|
Chris@16
|
399 BOOST_UBLAS_INLINE
|
Chris@16
|
400 vector &operator -= (const vector_container<C> &v) {
|
Chris@16
|
401 minus_assign (v);
|
Chris@16
|
402 return *this;
|
Chris@16
|
403 }
|
Chris@16
|
404
|
Chris@16
|
405 /// \brief Assign the difference of the vector and a vector_expression to the vector
|
Chris@16
|
406 /// Assign the difference of the vector and a vector_expression to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression.
|
Chris@16
|
407 /// No temporary is created. Computations are done and stored directly into the resulting vector.
|
Chris@16
|
408 /// \tparam AE is the type of the vector_expression
|
Chris@16
|
409 /// \param ae is a const reference to the vector_expression
|
Chris@16
|
410 /// \return a reference to the resulting vector
|
Chris@16
|
411 template<class AE>
|
Chris@16
|
412 BOOST_UBLAS_INLINE
|
Chris@16
|
413 vector &minus_assign (const vector_expression<AE> &ae) {
|
Chris@16
|
414 vector_assign<scalar_minus_assign> (*this, ae);
|
Chris@16
|
415 return *this;
|
Chris@16
|
416 }
|
Chris@16
|
417
|
Chris@16
|
418 /// \brief Assign the product of the vector and a scalar to the vector
|
Chris@16
|
419 /// Assign the product of the vector and a scalar to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression.
|
Chris@16
|
420 /// No temporary is created. Computations are done and stored directly into the resulting vector.
|
Chris@16
|
421 /// \tparam AE is the type of the vector_expression
|
Chris@16
|
422 /// \param at is a const reference to the scalar
|
Chris@16
|
423 /// \return a reference to the resulting vector
|
Chris@16
|
424 template<class AT>
|
Chris@16
|
425 BOOST_UBLAS_INLINE
|
Chris@16
|
426 vector &operator *= (const AT &at) {
|
Chris@16
|
427 vector_assign_scalar<scalar_multiplies_assign> (*this, at);
|
Chris@16
|
428 return *this;
|
Chris@16
|
429 }
|
Chris@16
|
430
|
Chris@16
|
431 /// \brief Assign the division of the vector by a scalar to the vector
|
Chris@16
|
432 /// Assign the division of the vector by a scalar to the vector. This is lazy-compiled and will be optimized out by the compiler on any type of expression.
|
Chris@16
|
433 /// No temporary is created. Computations are done and stored directly into the resulting vector.
|
Chris@16
|
434 /// \tparam AE is the type of the vector_expression
|
Chris@16
|
435 /// \param at is a const reference to the scalar
|
Chris@16
|
436 /// \return a reference to the resulting vector
|
Chris@16
|
437 template<class AT>
|
Chris@16
|
438 BOOST_UBLAS_INLINE
|
Chris@16
|
439 vector &operator /= (const AT &at) {
|
Chris@16
|
440 vector_assign_scalar<scalar_divides_assign> (*this, at);
|
Chris@16
|
441 return *this;
|
Chris@16
|
442 }
|
Chris@16
|
443
|
Chris@16
|
444 // --------
|
Chris@16
|
445 // Swapping
|
Chris@16
|
446 // --------
|
Chris@16
|
447
|
Chris@16
|
448 /// \brief Swap the content of the vector with another vector
|
Chris@16
|
449 /// \param v is the vector to be swapped with
|
Chris@16
|
450 BOOST_UBLAS_INLINE
|
Chris@16
|
451 void swap (vector &v) {
|
Chris@16
|
452 if (this != &v) {
|
Chris@16
|
453 data ().swap (v.data ());
|
Chris@16
|
454 }
|
Chris@16
|
455 }
|
Chris@16
|
456
|
Chris@16
|
457 /// \brief Swap the content of two vectors
|
Chris@16
|
458 /// \param v1 is the first vector. It takes values from v2
|
Chris@16
|
459 /// \param v2 is the second vector It takes values from v1
|
Chris@16
|
460 BOOST_UBLAS_INLINE
|
Chris@16
|
461 friend void swap (vector &v1, vector &v2) {
|
Chris@16
|
462 v1.swap (v2);
|
Chris@16
|
463 }
|
Chris@16
|
464
|
Chris@16
|
465 // Iterator types
|
Chris@16
|
466 private:
|
Chris@16
|
467 // Use the storage array iterator
|
Chris@16
|
468 typedef typename A::const_iterator const_subiterator_type;
|
Chris@16
|
469 typedef typename A::iterator subiterator_type;
|
Chris@16
|
470
|
Chris@16
|
471 public:
|
Chris@16
|
472 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
473 typedef indexed_iterator<self_type, dense_random_access_iterator_tag> iterator;
|
Chris@16
|
474 typedef indexed_const_iterator<self_type, dense_random_access_iterator_tag> const_iterator;
|
Chris@16
|
475 #else
|
Chris@16
|
476 class const_iterator;
|
Chris@16
|
477 class iterator;
|
Chris@16
|
478 #endif
|
Chris@16
|
479
|
Chris@16
|
480 // --------------
|
Chris@16
|
481 // Element lookup
|
Chris@16
|
482 // --------------
|
Chris@16
|
483
|
Chris@16
|
484 /// \brief Return a const iterator to the element \e i
|
Chris@16
|
485 /// \param i index of the element
|
Chris@16
|
486 BOOST_UBLAS_INLINE
|
Chris@16
|
487 const_iterator find (size_type i) const {
|
Chris@16
|
488 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
489 return const_iterator (*this, data ().begin () + i);
|
Chris@16
|
490 #else
|
Chris@16
|
491 return const_iterator (*this, i);
|
Chris@16
|
492 #endif
|
Chris@16
|
493 }
|
Chris@16
|
494
|
Chris@16
|
495 /// \brief Return an iterator to the element \e i
|
Chris@16
|
496 /// \param i index of the element
|
Chris@16
|
497 BOOST_UBLAS_INLINE
|
Chris@16
|
498 iterator find (size_type i) {
|
Chris@16
|
499 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
500 return iterator (*this, data ().begin () + i);
|
Chris@16
|
501 #else
|
Chris@16
|
502 return iterator (*this, i);
|
Chris@16
|
503 #endif
|
Chris@16
|
504 }
|
Chris@16
|
505
|
Chris@16
|
506 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
507 class const_iterator:
|
Chris@16
|
508 public container_const_reference<vector>,
|
Chris@16
|
509 public random_access_iterator_base<dense_random_access_iterator_tag,
|
Chris@16
|
510 const_iterator, value_type, difference_type> {
|
Chris@16
|
511 public:
|
Chris@16
|
512 typedef typename vector::difference_type difference_type;
|
Chris@16
|
513 typedef typename vector::value_type value_type;
|
Chris@16
|
514 typedef typename vector::const_reference reference;
|
Chris@16
|
515 typedef const typename vector::pointer pointer;
|
Chris@16
|
516
|
Chris@16
|
517 // ----------------------------
|
Chris@16
|
518 // Construction and destruction
|
Chris@16
|
519 // ----------------------------
|
Chris@16
|
520
|
Chris@16
|
521
|
Chris@16
|
522 BOOST_UBLAS_INLINE
|
Chris@16
|
523 const_iterator ():
|
Chris@16
|
524 container_const_reference<self_type> (), it_ () {}
|
Chris@16
|
525 BOOST_UBLAS_INLINE
|
Chris@16
|
526 const_iterator (const self_type &v, const const_subiterator_type &it):
|
Chris@16
|
527 container_const_reference<self_type> (v), it_ (it) {}
|
Chris@16
|
528 BOOST_UBLAS_INLINE
|
Chris@16
|
529 const_iterator (const typename self_type::iterator &it): // ISSUE vector:: stops VC8 using std::iterator here
|
Chris@16
|
530 container_const_reference<self_type> (it ()), it_ (it.it_) {}
|
Chris@16
|
531
|
Chris@16
|
532 // ----------
|
Chris@16
|
533 // Arithmetic
|
Chris@16
|
534 // ----------
|
Chris@16
|
535
|
Chris@16
|
536 /// \brief Increment by 1 the position of the iterator
|
Chris@16
|
537 /// \return a reference to the const iterator
|
Chris@16
|
538 BOOST_UBLAS_INLINE
|
Chris@16
|
539 const_iterator &operator ++ () {
|
Chris@16
|
540 ++ it_;
|
Chris@16
|
541 return *this;
|
Chris@16
|
542 }
|
Chris@16
|
543
|
Chris@16
|
544 /// \brief Decrement by 1 the position of the iterator
|
Chris@16
|
545 /// \return a reference to the const iterator
|
Chris@16
|
546 BOOST_UBLAS_INLINE
|
Chris@16
|
547 const_iterator &operator -- () {
|
Chris@16
|
548 -- it_;
|
Chris@16
|
549 return *this;
|
Chris@16
|
550 }
|
Chris@16
|
551
|
Chris@16
|
552 /// \brief Increment by \e n the position of the iterator
|
Chris@16
|
553 /// \return a reference to the const iterator
|
Chris@16
|
554 BOOST_UBLAS_INLINE
|
Chris@16
|
555 const_iterator &operator += (difference_type n) {
|
Chris@16
|
556 it_ += n;
|
Chris@16
|
557 return *this;
|
Chris@16
|
558 }
|
Chris@16
|
559
|
Chris@16
|
560 /// \brief Decrement by \e n the position of the iterator
|
Chris@16
|
561 /// \return a reference to the const iterator
|
Chris@16
|
562 BOOST_UBLAS_INLINE
|
Chris@16
|
563 const_iterator &operator -= (difference_type n) {
|
Chris@16
|
564 it_ -= n;
|
Chris@16
|
565 return *this;
|
Chris@16
|
566 }
|
Chris@16
|
567
|
Chris@16
|
568 /// \brief Return the different in number of positions between 2 iterators
|
Chris@16
|
569 BOOST_UBLAS_INLINE
|
Chris@16
|
570 difference_type operator - (const const_iterator &it) const {
|
Chris@16
|
571 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
572 return it_ - it.it_;
|
Chris@16
|
573 }
|
Chris@16
|
574
|
Chris@16
|
575 /// \brief Dereference an iterator
|
Chris@16
|
576 /// Dereference an iterator: a bounds' check is done before returning the value. A bad_index() expection is returned if out of bounds.
|
Chris@16
|
577 /// \return a const reference to the value pointed by the iterator
|
Chris@16
|
578 BOOST_UBLAS_INLINE
|
Chris@16
|
579 const_reference operator * () const {
|
Chris@16
|
580 BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_, bad_index ());
|
Chris@16
|
581 return *it_;
|
Chris@16
|
582 }
|
Chris@16
|
583
|
Chris@16
|
584 /// \brief Dereference an iterator at the n-th forward value
|
Chris@16
|
585 /// Dereference an iterator at the n-th forward value, that is the value pointed by iterator+n.
|
Chris@16
|
586 /// A bounds' check is done before returning the value. A bad_index() expection is returned if out of bounds.
|
Chris@16
|
587 /// \return a const reference
|
Chris@16
|
588 BOOST_UBLAS_INLINE
|
Chris@16
|
589 const_reference operator [] (difference_type n) const {
|
Chris@16
|
590 return *(it_ + n);
|
Chris@16
|
591 }
|
Chris@16
|
592
|
Chris@16
|
593 // Index
|
Chris@16
|
594 /// \brief return the index of the element referenced by the iterator
|
Chris@16
|
595 BOOST_UBLAS_INLINE
|
Chris@16
|
596 size_type index () const {
|
Chris@16
|
597 BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_, bad_index ());
|
Chris@16
|
598 return it_ - (*this) ().begin ().it_;
|
Chris@16
|
599 }
|
Chris@16
|
600
|
Chris@16
|
601 // Assignment
|
Chris@16
|
602 BOOST_UBLAS_INLINE
|
Chris@16
|
603 /// \brief assign the value of an iterator to the iterator
|
Chris@16
|
604 const_iterator &operator = (const const_iterator &it) {
|
Chris@16
|
605 container_const_reference<self_type>::assign (&it ());
|
Chris@16
|
606 it_ = it.it_;
|
Chris@16
|
607 return *this;
|
Chris@16
|
608 }
|
Chris@16
|
609
|
Chris@16
|
610 // Comparison
|
Chris@16
|
611 /// \brief compare the value of two itetarors
|
Chris@16
|
612 /// \return true if they reference the same element
|
Chris@16
|
613 BOOST_UBLAS_INLINE
|
Chris@16
|
614 bool operator == (const const_iterator &it) const {
|
Chris@16
|
615 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
616 return it_ == it.it_;
|
Chris@16
|
617 }
|
Chris@16
|
618
|
Chris@16
|
619
|
Chris@16
|
620 /// \brief compare the value of two iterators
|
Chris@16
|
621 /// \return return true if the left-hand-side iterator refers to a value placed before the right-hand-side iterator
|
Chris@16
|
622 BOOST_UBLAS_INLINE
|
Chris@16
|
623 bool operator < (const const_iterator &it) const {
|
Chris@16
|
624 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
625 return it_ < it.it_;
|
Chris@16
|
626 }
|
Chris@16
|
627
|
Chris@16
|
628 private:
|
Chris@16
|
629 const_subiterator_type it_;
|
Chris@16
|
630
|
Chris@16
|
631 friend class iterator;
|
Chris@16
|
632 };
|
Chris@16
|
633 #endif
|
Chris@16
|
634
|
Chris@16
|
635 /// \brief return an iterator on the first element of the vector
|
Chris@16
|
636 BOOST_UBLAS_INLINE
|
Chris@16
|
637 const_iterator begin () const {
|
Chris@16
|
638 return find (0);
|
Chris@16
|
639 }
|
Chris@16
|
640
|
Chris@16
|
641 /// \brief return an iterator after the last element of the vector
|
Chris@16
|
642 BOOST_UBLAS_INLINE
|
Chris@16
|
643 const_iterator end () const {
|
Chris@16
|
644 return find (data_.size ());
|
Chris@16
|
645 }
|
Chris@16
|
646
|
Chris@16
|
647 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
648 class iterator:
|
Chris@16
|
649 public container_reference<vector>,
|
Chris@16
|
650 public random_access_iterator_base<dense_random_access_iterator_tag,
|
Chris@16
|
651 iterator, value_type, difference_type> {
|
Chris@16
|
652 public:
|
Chris@16
|
653 typedef typename vector::difference_type difference_type;
|
Chris@16
|
654 typedef typename vector::value_type value_type;
|
Chris@16
|
655 typedef typename vector::reference reference;
|
Chris@16
|
656 typedef typename vector::pointer pointer;
|
Chris@16
|
657
|
Chris@16
|
658
|
Chris@16
|
659 // Construction and destruction
|
Chris@16
|
660 BOOST_UBLAS_INLINE
|
Chris@16
|
661 iterator ():
|
Chris@16
|
662 container_reference<self_type> (), it_ () {}
|
Chris@16
|
663 BOOST_UBLAS_INLINE
|
Chris@16
|
664 iterator (self_type &v, const subiterator_type &it):
|
Chris@16
|
665 container_reference<self_type> (v), it_ (it) {}
|
Chris@16
|
666
|
Chris@16
|
667 // Arithmetic
|
Chris@16
|
668 BOOST_UBLAS_INLINE
|
Chris@16
|
669 iterator &operator ++ () {
|
Chris@16
|
670 ++ it_;
|
Chris@16
|
671 return *this;
|
Chris@16
|
672 }
|
Chris@16
|
673 BOOST_UBLAS_INLINE
|
Chris@16
|
674 iterator &operator -- () {
|
Chris@16
|
675 -- it_;
|
Chris@16
|
676 return *this;
|
Chris@16
|
677 }
|
Chris@16
|
678 BOOST_UBLAS_INLINE
|
Chris@16
|
679 iterator &operator += (difference_type n) {
|
Chris@16
|
680 it_ += n;
|
Chris@16
|
681 return *this;
|
Chris@16
|
682 }
|
Chris@16
|
683 BOOST_UBLAS_INLINE
|
Chris@16
|
684 iterator &operator -= (difference_type n) {
|
Chris@16
|
685 it_ -= n;
|
Chris@16
|
686 return *this;
|
Chris@16
|
687 }
|
Chris@16
|
688 BOOST_UBLAS_INLINE
|
Chris@16
|
689 difference_type operator - (const iterator &it) const {
|
Chris@16
|
690 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
691 return it_ - it.it_;
|
Chris@16
|
692 }
|
Chris@16
|
693
|
Chris@16
|
694 // Dereference
|
Chris@16
|
695 BOOST_UBLAS_INLINE
|
Chris@16
|
696 reference operator * () const {
|
Chris@16
|
697 BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_ , bad_index ());
|
Chris@16
|
698 return *it_;
|
Chris@16
|
699 }
|
Chris@16
|
700 BOOST_UBLAS_INLINE
|
Chris@16
|
701 reference operator [] (difference_type n) const {
|
Chris@16
|
702 return *(it_ + n);
|
Chris@16
|
703 }
|
Chris@16
|
704
|
Chris@16
|
705 // Index
|
Chris@16
|
706 BOOST_UBLAS_INLINE
|
Chris@16
|
707 size_type index () const {
|
Chris@16
|
708 BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_ , bad_index ());
|
Chris@16
|
709 return it_ - (*this) ().begin ().it_;
|
Chris@16
|
710 }
|
Chris@16
|
711
|
Chris@16
|
712 // Assignment
|
Chris@16
|
713 BOOST_UBLAS_INLINE
|
Chris@16
|
714 iterator &operator = (const iterator &it) {
|
Chris@16
|
715 container_reference<self_type>::assign (&it ());
|
Chris@16
|
716 it_ = it.it_;
|
Chris@16
|
717 return *this;
|
Chris@16
|
718 }
|
Chris@16
|
719
|
Chris@16
|
720 // Comparison
|
Chris@16
|
721 BOOST_UBLAS_INLINE
|
Chris@16
|
722 bool operator == (const iterator &it) const {
|
Chris@16
|
723 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
724 return it_ == it.it_;
|
Chris@16
|
725 }
|
Chris@16
|
726 BOOST_UBLAS_INLINE
|
Chris@16
|
727 bool operator < (const iterator &it) const {
|
Chris@16
|
728 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
729 return it_ < it.it_;
|
Chris@16
|
730 }
|
Chris@16
|
731
|
Chris@16
|
732 private:
|
Chris@16
|
733 subiterator_type it_;
|
Chris@16
|
734
|
Chris@16
|
735 friend class const_iterator;
|
Chris@16
|
736 };
|
Chris@16
|
737 #endif
|
Chris@16
|
738
|
Chris@16
|
739 /// \brief Return an iterator on the first element of the vector
|
Chris@16
|
740 BOOST_UBLAS_INLINE
|
Chris@16
|
741 iterator begin () {
|
Chris@16
|
742 return find (0);
|
Chris@16
|
743 }
|
Chris@16
|
744
|
Chris@16
|
745 /// \brief Return an iterator at the end of the vector
|
Chris@16
|
746 BOOST_UBLAS_INLINE
|
Chris@16
|
747 iterator end () {
|
Chris@16
|
748 return find (data_.size ());
|
Chris@16
|
749 }
|
Chris@16
|
750
|
Chris@16
|
751 // Reverse iterator
|
Chris@16
|
752 typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
|
Chris@16
|
753 typedef reverse_iterator_base<iterator> reverse_iterator;
|
Chris@16
|
754
|
Chris@16
|
755 /// \brief Return a const reverse iterator before the first element of the reversed vector (i.e. end() of normal vector)
|
Chris@16
|
756 BOOST_UBLAS_INLINE
|
Chris@16
|
757 const_reverse_iterator rbegin () const {
|
Chris@16
|
758 return const_reverse_iterator (end ());
|
Chris@16
|
759 }
|
Chris@16
|
760
|
Chris@16
|
761 /// \brief Return a const reverse iterator on the end of the reverse vector (i.e. first element of the normal vector)
|
Chris@16
|
762 BOOST_UBLAS_INLINE
|
Chris@16
|
763 const_reverse_iterator rend () const {
|
Chris@16
|
764 return const_reverse_iterator (begin ());
|
Chris@16
|
765 }
|
Chris@16
|
766
|
Chris@16
|
767 /// \brief Return a const reverse iterator before the first element of the reversed vector (i.e. end() of normal vector)
|
Chris@16
|
768 BOOST_UBLAS_INLINE
|
Chris@16
|
769 reverse_iterator rbegin () {
|
Chris@16
|
770 return reverse_iterator (end ());
|
Chris@16
|
771 }
|
Chris@16
|
772
|
Chris@16
|
773 /// \brief Return a const reverse iterator on the end of the reverse vector (i.e. first element of the normal vector)
|
Chris@16
|
774 BOOST_UBLAS_INLINE
|
Chris@16
|
775 reverse_iterator rend () {
|
Chris@16
|
776 return reverse_iterator (begin ());
|
Chris@16
|
777 }
|
Chris@16
|
778
|
Chris@16
|
779 // -------------
|
Chris@16
|
780 // Serialization
|
Chris@16
|
781 // -------------
|
Chris@16
|
782
|
Chris@16
|
783 /// Serialize a vector into and archive as defined in Boost
|
Chris@16
|
784 /// \param ar Archive object. Can be a flat file, an XML file or any other stream
|
Chris@16
|
785 /// \param file_version Optional file version (not yet used)
|
Chris@16
|
786 template<class Archive>
|
Chris@16
|
787 void serialize(Archive & ar, const unsigned int /* file_version */){
|
Chris@16
|
788 ar & serialization::make_nvp("data",data_);
|
Chris@16
|
789 }
|
Chris@16
|
790
|
Chris@16
|
791 private:
|
Chris@16
|
792 array_type data_;
|
Chris@16
|
793 };
|
Chris@16
|
794
|
Chris@16
|
795
|
Chris@16
|
796 // --------------------
|
Chris@16
|
797 // Bounded vector class
|
Chris@16
|
798 // --------------------
|
Chris@16
|
799
|
Chris@16
|
800 /// \brief a dense vector of values of type \c T, of variable size but with maximum \f$N\f$.
|
Chris@16
|
801 /// A dense vector of values of type \c T, of variable size but with maximum \f$N\f$. The default constructor
|
Chris@16
|
802 /// creates the vector with size \f$N\f$. Elements are constructed by the storage type \c bounded_array, which \b need \b not \b initialise their value.
|
Chris@16
|
803 template<class T, std::size_t N>
|
Chris@16
|
804 class bounded_vector:
|
Chris@16
|
805 public vector<T, bounded_array<T, N> > {
|
Chris@16
|
806
|
Chris@16
|
807 typedef vector<T, bounded_array<T, N> > vector_type;
|
Chris@16
|
808 public:
|
Chris@16
|
809 typedef typename vector_type::size_type size_type;
|
Chris@16
|
810 static const size_type max_size = N;
|
Chris@16
|
811
|
Chris@16
|
812 // Construction and destruction
|
Chris@16
|
813 BOOST_UBLAS_INLINE
|
Chris@16
|
814 bounded_vector ():
|
Chris@16
|
815 vector_type (N) {}
|
Chris@16
|
816 BOOST_UBLAS_INLINE
|
Chris@16
|
817 bounded_vector (size_type size):
|
Chris@16
|
818 vector_type (size) {}
|
Chris@16
|
819 BOOST_UBLAS_INLINE
|
Chris@16
|
820 bounded_vector (const bounded_vector &v):
|
Chris@16
|
821 vector_type (v) {}
|
Chris@16
|
822 template<class A2> // Allow vector<T,bounded_array<N> construction
|
Chris@16
|
823 BOOST_UBLAS_INLINE
|
Chris@16
|
824 bounded_vector (const vector<T, A2> &v):
|
Chris@16
|
825 vector_type (v) {}
|
Chris@16
|
826 template<class AE>
|
Chris@16
|
827 BOOST_UBLAS_INLINE
|
Chris@16
|
828 bounded_vector (const vector_expression<AE> &ae):
|
Chris@16
|
829 vector_type (ae) {}
|
Chris@16
|
830 BOOST_UBLAS_INLINE
|
Chris@16
|
831 ~bounded_vector () {}
|
Chris@16
|
832
|
Chris@16
|
833 // Assignment
|
Chris@16
|
834 #ifdef BOOST_UBLAS_MOVE_SEMANTICS
|
Chris@16
|
835
|
Chris@16
|
836 /*! @note "pass by value" the key idea to enable move semantics */
|
Chris@16
|
837 BOOST_UBLAS_INLINE
|
Chris@16
|
838 bounded_vector &operator = (bounded_vector v) {
|
Chris@16
|
839 vector_type::operator = (v);
|
Chris@16
|
840 return *this;
|
Chris@16
|
841 }
|
Chris@16
|
842 #else
|
Chris@16
|
843 BOOST_UBLAS_INLINE
|
Chris@16
|
844 bounded_vector &operator = (const bounded_vector &v) {
|
Chris@16
|
845 vector_type::operator = (v);
|
Chris@16
|
846 return *this;
|
Chris@16
|
847 }
|
Chris@16
|
848 #endif
|
Chris@16
|
849 template<class A2> // Generic vector assignment
|
Chris@16
|
850 BOOST_UBLAS_INLINE
|
Chris@16
|
851 bounded_vector &operator = (const vector<T, A2> &v) {
|
Chris@16
|
852 vector_type::operator = (v);
|
Chris@16
|
853 return *this;
|
Chris@16
|
854 }
|
Chris@16
|
855 template<class C> // Container assignment without temporary
|
Chris@16
|
856 BOOST_UBLAS_INLINE
|
Chris@16
|
857 bounded_vector &operator = (const vector_container<C> &v) {
|
Chris@16
|
858 vector_type::operator = (v);
|
Chris@16
|
859 return *this;
|
Chris@16
|
860 }
|
Chris@16
|
861 template<class AE>
|
Chris@16
|
862 BOOST_UBLAS_INLINE
|
Chris@16
|
863 bounded_vector &operator = (const vector_expression<AE> &ae) {
|
Chris@16
|
864 vector_type::operator = (ae);
|
Chris@16
|
865 return *this;
|
Chris@16
|
866 }
|
Chris@16
|
867 };
|
Chris@16
|
868
|
Chris@16
|
869
|
Chris@16
|
870 // -----------------
|
Chris@16
|
871 // Zero vector class
|
Chris@16
|
872 // -----------------
|
Chris@16
|
873
|
Chris@16
|
874 /// \brief A zero vector of type \c T and a given \c size
|
Chris@16
|
875 /// A zero vector of type \c T and a given \c size. This is a virtual vector in the sense that no memory is allocated
|
Chris@16
|
876 /// for storing the zero values: it still acts like any other vector. However assigning values to it will not change the zero
|
Chris@16
|
877 /// vector into a normal vector. It must first be assigned to another normal vector by any suitable means. Its memory footprint is constant.
|
Chris@16
|
878 template<class T, class ALLOC>
|
Chris@16
|
879 class zero_vector:
|
Chris@16
|
880 public vector_container<zero_vector<T, ALLOC> > {
|
Chris@16
|
881
|
Chris@16
|
882 typedef const T *const_pointer;
|
Chris@16
|
883 typedef zero_vector<T, ALLOC> self_type;
|
Chris@16
|
884 public:
|
Chris@16
|
885 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
|
Chris@16
|
886 using vector_container<self_type>::operator ();
|
Chris@16
|
887 #endif
|
Chris@16
|
888 typedef typename ALLOC::size_type size_type;
|
Chris@16
|
889 typedef typename ALLOC::difference_type difference_type;
|
Chris@16
|
890 typedef T value_type;
|
Chris@16
|
891 typedef const T &const_reference;
|
Chris@16
|
892 typedef T &reference;
|
Chris@16
|
893 typedef const vector_reference<const self_type> const_closure_type;
|
Chris@16
|
894 typedef vector_reference<self_type> closure_type;
|
Chris@16
|
895 typedef sparse_tag storage_category;
|
Chris@16
|
896
|
Chris@16
|
897 // Construction and destruction
|
Chris@16
|
898 BOOST_UBLAS_INLINE
|
Chris@16
|
899 zero_vector ():
|
Chris@16
|
900 vector_container<self_type> (),
|
Chris@16
|
901 size_ (0) {}
|
Chris@16
|
902 explicit BOOST_UBLAS_INLINE
|
Chris@16
|
903 zero_vector (size_type size):
|
Chris@16
|
904 vector_container<self_type> (),
|
Chris@16
|
905 size_ (size) {}
|
Chris@16
|
906 BOOST_UBLAS_INLINE
|
Chris@16
|
907 zero_vector (const zero_vector &v):
|
Chris@16
|
908 vector_container<self_type> (),
|
Chris@16
|
909 size_ (v.size_) {}
|
Chris@16
|
910
|
Chris@16
|
911 // Accessors
|
Chris@16
|
912 BOOST_UBLAS_INLINE
|
Chris@16
|
913 size_type size () const {
|
Chris@16
|
914 return size_;
|
Chris@16
|
915 }
|
Chris@16
|
916
|
Chris@16
|
917 // Resizing
|
Chris@16
|
918 BOOST_UBLAS_INLINE
|
Chris@16
|
919 void resize (size_type size, bool /*preserve*/ = true) {
|
Chris@16
|
920 size_ = size;
|
Chris@16
|
921 }
|
Chris@16
|
922
|
Chris@16
|
923 // Element support
|
Chris@16
|
924 BOOST_UBLAS_INLINE
|
Chris@16
|
925 const_pointer find_element (size_type i) const {
|
Chris@16
|
926 return & zero_;
|
Chris@16
|
927 }
|
Chris@16
|
928
|
Chris@16
|
929 // Element access
|
Chris@16
|
930 BOOST_UBLAS_INLINE
|
Chris@16
|
931 const_reference operator () (size_type /* i */) const {
|
Chris@16
|
932 return zero_;
|
Chris@16
|
933 }
|
Chris@16
|
934
|
Chris@16
|
935 BOOST_UBLAS_INLINE
|
Chris@16
|
936 const_reference operator [] (size_type i) const {
|
Chris@16
|
937 return (*this) (i);
|
Chris@16
|
938 }
|
Chris@16
|
939
|
Chris@16
|
940 // Assignment
|
Chris@16
|
941 BOOST_UBLAS_INLINE
|
Chris@16
|
942 zero_vector &operator = (const zero_vector &v) {
|
Chris@16
|
943 size_ = v.size_;
|
Chris@16
|
944 return *this;
|
Chris@16
|
945 }
|
Chris@16
|
946 BOOST_UBLAS_INLINE
|
Chris@16
|
947 zero_vector &assign_temporary (zero_vector &v) {
|
Chris@16
|
948 swap (v);
|
Chris@16
|
949 return *this;
|
Chris@16
|
950 }
|
Chris@16
|
951
|
Chris@16
|
952 // Swapping
|
Chris@16
|
953 BOOST_UBLAS_INLINE
|
Chris@16
|
954 void swap (zero_vector &v) {
|
Chris@16
|
955 if (this != &v) {
|
Chris@16
|
956 std::swap (size_, v.size_);
|
Chris@16
|
957 }
|
Chris@16
|
958 }
|
Chris@16
|
959 BOOST_UBLAS_INLINE
|
Chris@16
|
960 friend void swap (zero_vector &v1, zero_vector &v2) {
|
Chris@16
|
961 v1.swap (v2);
|
Chris@16
|
962 }
|
Chris@16
|
963
|
Chris@16
|
964 // Iterator types
|
Chris@16
|
965 public:
|
Chris@16
|
966 class const_iterator;
|
Chris@16
|
967
|
Chris@16
|
968 // Element lookup
|
Chris@16
|
969 BOOST_UBLAS_INLINE
|
Chris@16
|
970 const_iterator find (size_type /*i*/) const {
|
Chris@16
|
971 return const_iterator (*this);
|
Chris@16
|
972 }
|
Chris@16
|
973
|
Chris@16
|
974 class const_iterator:
|
Chris@16
|
975 public container_const_reference<zero_vector>,
|
Chris@16
|
976 public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
|
Chris@16
|
977 const_iterator, value_type> {
|
Chris@16
|
978 public:
|
Chris@16
|
979 typedef typename zero_vector::difference_type difference_type;
|
Chris@16
|
980 typedef typename zero_vector::value_type value_type;
|
Chris@16
|
981 typedef typename zero_vector::const_reference reference;
|
Chris@16
|
982 typedef typename zero_vector::const_pointer pointer;
|
Chris@16
|
983
|
Chris@16
|
984 // Construction and destruction
|
Chris@16
|
985 BOOST_UBLAS_INLINE
|
Chris@16
|
986 const_iterator ():
|
Chris@16
|
987 container_const_reference<self_type> () {}
|
Chris@16
|
988 BOOST_UBLAS_INLINE
|
Chris@16
|
989 const_iterator (const self_type &v):
|
Chris@16
|
990 container_const_reference<self_type> (v) {}
|
Chris@16
|
991
|
Chris@16
|
992 // Arithmetic
|
Chris@16
|
993 BOOST_UBLAS_INLINE
|
Chris@16
|
994 const_iterator &operator ++ () {
|
Chris@16
|
995 BOOST_UBLAS_CHECK_FALSE (bad_index ());
|
Chris@16
|
996 return *this;
|
Chris@16
|
997 }
|
Chris@16
|
998 BOOST_UBLAS_INLINE
|
Chris@16
|
999 const_iterator &operator -- () {
|
Chris@16
|
1000 BOOST_UBLAS_CHECK_FALSE (bad_index ());
|
Chris@16
|
1001 return *this;
|
Chris@16
|
1002 }
|
Chris@16
|
1003
|
Chris@16
|
1004 // Dereference
|
Chris@16
|
1005 BOOST_UBLAS_INLINE
|
Chris@16
|
1006 const_reference operator * () const {
|
Chris@16
|
1007 BOOST_UBLAS_CHECK_FALSE (bad_index ());
|
Chris@16
|
1008 return zero_; // arbitary return value
|
Chris@16
|
1009 }
|
Chris@16
|
1010
|
Chris@16
|
1011 // Index
|
Chris@16
|
1012 BOOST_UBLAS_INLINE
|
Chris@16
|
1013 size_type index () const {
|
Chris@16
|
1014 BOOST_UBLAS_CHECK_FALSE (bad_index ());
|
Chris@16
|
1015 return 0; // arbitary return value
|
Chris@16
|
1016 }
|
Chris@16
|
1017
|
Chris@16
|
1018 // Assignment
|
Chris@16
|
1019 BOOST_UBLAS_INLINE
|
Chris@16
|
1020 const_iterator &operator = (const const_iterator &it) {
|
Chris@16
|
1021 container_const_reference<self_type>::assign (&it ());
|
Chris@16
|
1022 return *this;
|
Chris@16
|
1023 }
|
Chris@16
|
1024
|
Chris@16
|
1025 // Comparison
|
Chris@16
|
1026 BOOST_UBLAS_INLINE
|
Chris@16
|
1027 bool operator == (const const_iterator &it) const {
|
Chris@16
|
1028 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
1029 detail::ignore_unused_variable_warning(it);
|
Chris@16
|
1030 return true;
|
Chris@16
|
1031 }
|
Chris@16
|
1032 };
|
Chris@16
|
1033
|
Chris@16
|
1034 typedef const_iterator iterator;
|
Chris@16
|
1035
|
Chris@16
|
1036 BOOST_UBLAS_INLINE
|
Chris@16
|
1037 const_iterator begin () const {
|
Chris@16
|
1038 return const_iterator (*this);
|
Chris@16
|
1039 }
|
Chris@16
|
1040 BOOST_UBLAS_INLINE
|
Chris@16
|
1041 const_iterator end () const {
|
Chris@16
|
1042 return const_iterator (*this);
|
Chris@16
|
1043 }
|
Chris@16
|
1044
|
Chris@16
|
1045 // Reverse iterator
|
Chris@16
|
1046 typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
|
Chris@16
|
1047
|
Chris@16
|
1048 BOOST_UBLAS_INLINE
|
Chris@16
|
1049 const_reverse_iterator rbegin () const {
|
Chris@16
|
1050 return const_reverse_iterator (end ());
|
Chris@16
|
1051 }
|
Chris@16
|
1052 BOOST_UBLAS_INLINE
|
Chris@16
|
1053 const_reverse_iterator rend () const {
|
Chris@16
|
1054 return const_reverse_iterator (begin ());
|
Chris@16
|
1055 }
|
Chris@16
|
1056
|
Chris@16
|
1057 // Serialization
|
Chris@16
|
1058 template<class Archive>
|
Chris@16
|
1059 void serialize(Archive & ar, const unsigned int /* file_version */){
|
Chris@16
|
1060 serialization::collection_size_type s (size_);
|
Chris@16
|
1061 ar & serialization::make_nvp("size",s);
|
Chris@16
|
1062 if (Archive::is_loading::value) {
|
Chris@16
|
1063 size_ = s;
|
Chris@16
|
1064 }
|
Chris@16
|
1065 }
|
Chris@16
|
1066
|
Chris@16
|
1067 private:
|
Chris@16
|
1068 size_type size_;
|
Chris@16
|
1069 typedef const value_type const_value_type;
|
Chris@16
|
1070 static const_value_type zero_;
|
Chris@16
|
1071 };
|
Chris@16
|
1072
|
Chris@16
|
1073 template<class T, class ALLOC>
|
Chris@16
|
1074 typename zero_vector<T, ALLOC>::const_value_type zero_vector<T, ALLOC>::zero_ = T(/*zero*/);
|
Chris@16
|
1075
|
Chris@16
|
1076
|
Chris@16
|
1077 // Unit vector class
|
Chris@16
|
1078 /// \brief unit_vector represents a canonical unit vector
|
Chris@16
|
1079 /// unit_vector represents a canonical unit vector. The \e k-th unit vector of dimension \f$n\f$ holds 0 for every value \f$u_i\f$ s.t. \f$i \neq k\f$ and 1 when \f$i=k\f$.
|
Chris@16
|
1080 /// At construction, the value \e k is given after the dimension of the vector.
|
Chris@16
|
1081 /// \tparam T is the type of elements in the vector. They must be 0 and 1 assignable in order for the vector to have its unit-vector semantic.
|
Chris@16
|
1082 /// \tparam ALLOC a specific allocator can be specified if needed. Most of the time this parameter is omited.
|
Chris@16
|
1083 template<class T, class ALLOC>
|
Chris@16
|
1084 class unit_vector:
|
Chris@16
|
1085 public vector_container<unit_vector<T, ALLOC> > {
|
Chris@16
|
1086
|
Chris@16
|
1087 typedef const T *const_pointer;
|
Chris@16
|
1088 typedef unit_vector<T, ALLOC> self_type;
|
Chris@16
|
1089 public:
|
Chris@16
|
1090 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
|
Chris@16
|
1091 using vector_container<self_type>::operator ();
|
Chris@16
|
1092 #endif
|
Chris@16
|
1093 typedef typename ALLOC::size_type size_type;
|
Chris@16
|
1094 typedef typename ALLOC::difference_type difference_type;
|
Chris@16
|
1095 typedef T value_type;
|
Chris@16
|
1096 typedef const T &const_reference;
|
Chris@16
|
1097 typedef T &reference;
|
Chris@16
|
1098 typedef const vector_reference<const self_type> const_closure_type;
|
Chris@16
|
1099 typedef vector_reference<self_type> closure_type;
|
Chris@16
|
1100 typedef sparse_tag storage_category;
|
Chris@16
|
1101
|
Chris@16
|
1102 // Construction and destruction
|
Chris@16
|
1103 /// \brief Simple constructor with dimension and index 0
|
Chris@16
|
1104 BOOST_UBLAS_INLINE
|
Chris@16
|
1105 unit_vector ():
|
Chris@16
|
1106 vector_container<self_type> (),
|
Chris@16
|
1107 size_ (0), index_ (0) {}
|
Chris@16
|
1108
|
Chris@16
|
1109 /// \brief Constructor of unit_vector
|
Chris@16
|
1110 /// \param size is the dimension of the vector
|
Chris@16
|
1111 /// \param index is the order of the vector
|
Chris@16
|
1112 BOOST_UBLAS_INLINE
|
Chris@16
|
1113 explicit unit_vector (size_type size, size_type index = 0):
|
Chris@16
|
1114 vector_container<self_type> (),
|
Chris@16
|
1115 size_ (size), index_ (index) {}
|
Chris@16
|
1116
|
Chris@16
|
1117 /// \brief Copy-constructor
|
Chris@16
|
1118 BOOST_UBLAS_INLINE
|
Chris@16
|
1119 unit_vector (const unit_vector &v):
|
Chris@16
|
1120 vector_container<self_type> (),
|
Chris@16
|
1121 size_ (v.size_), index_ (v.index_) {}
|
Chris@16
|
1122
|
Chris@16
|
1123 // Accessors
|
Chris@16
|
1124 //----------
|
Chris@16
|
1125
|
Chris@16
|
1126 /// \brief Return the size (dimension) of the vector
|
Chris@16
|
1127 BOOST_UBLAS_INLINE
|
Chris@16
|
1128 size_type size () const {
|
Chris@16
|
1129 return size_;
|
Chris@16
|
1130 }
|
Chris@16
|
1131
|
Chris@16
|
1132 /// \brief Return the order of the unit vector
|
Chris@16
|
1133 BOOST_UBLAS_INLINE
|
Chris@16
|
1134 size_type index () const {
|
Chris@16
|
1135 return index_;
|
Chris@16
|
1136 }
|
Chris@16
|
1137
|
Chris@16
|
1138 // Resizing
|
Chris@16
|
1139 // --------
|
Chris@16
|
1140
|
Chris@16
|
1141 /// \brief Resize the vector. The values are preserved by default (i.e. the index does not change)
|
Chris@16
|
1142 /// \param size is the new size of the vector
|
Chris@16
|
1143 BOOST_UBLAS_INLINE
|
Chris@16
|
1144 void resize (size_type size, bool /*preserve*/ = true) {
|
Chris@16
|
1145 size_ = size;
|
Chris@16
|
1146 }
|
Chris@16
|
1147
|
Chris@16
|
1148 // Element support
|
Chris@16
|
1149 // ---------------
|
Chris@16
|
1150
|
Chris@16
|
1151 /// \brief Return a const pointer to the element of index i
|
Chris@16
|
1152 BOOST_UBLAS_INLINE
|
Chris@16
|
1153 const_pointer find_element (size_type i) const {
|
Chris@16
|
1154 if (i == index_)
|
Chris@16
|
1155 return & one_;
|
Chris@16
|
1156 else
|
Chris@16
|
1157 return & zero_;
|
Chris@16
|
1158 }
|
Chris@16
|
1159
|
Chris@16
|
1160 // Element access
|
Chris@16
|
1161 BOOST_UBLAS_INLINE
|
Chris@16
|
1162 const_reference operator () (size_type i) const {
|
Chris@16
|
1163 if (i == index_)
|
Chris@16
|
1164 return one_;
|
Chris@16
|
1165 else
|
Chris@16
|
1166 return zero_;
|
Chris@16
|
1167 }
|
Chris@16
|
1168
|
Chris@16
|
1169 BOOST_UBLAS_INLINE
|
Chris@16
|
1170 const_reference operator [] (size_type i) const {
|
Chris@16
|
1171 return (*this) (i);
|
Chris@16
|
1172 }
|
Chris@16
|
1173
|
Chris@16
|
1174 // Assignment
|
Chris@16
|
1175 BOOST_UBLAS_INLINE
|
Chris@16
|
1176 unit_vector &operator = (const unit_vector &v) {
|
Chris@16
|
1177 size_ = v.size_;
|
Chris@16
|
1178 index_ = v.index_;
|
Chris@16
|
1179 return *this;
|
Chris@16
|
1180 }
|
Chris@16
|
1181 BOOST_UBLAS_INLINE
|
Chris@16
|
1182 unit_vector &assign_temporary (unit_vector &v) {
|
Chris@16
|
1183 swap (v);
|
Chris@16
|
1184 return *this;
|
Chris@16
|
1185 }
|
Chris@16
|
1186
|
Chris@16
|
1187 // Swapping
|
Chris@16
|
1188 BOOST_UBLAS_INLINE
|
Chris@16
|
1189 void swap (unit_vector &v) {
|
Chris@16
|
1190 if (this != &v) {
|
Chris@16
|
1191 std::swap (size_, v.size_);
|
Chris@16
|
1192 std::swap (index_, v.index_);
|
Chris@16
|
1193 }
|
Chris@16
|
1194 }
|
Chris@16
|
1195 BOOST_UBLAS_INLINE
|
Chris@16
|
1196 friend void swap (unit_vector &v1, unit_vector &v2) {
|
Chris@16
|
1197 v1.swap (v2);
|
Chris@16
|
1198 }
|
Chris@16
|
1199
|
Chris@16
|
1200 // Iterator types
|
Chris@16
|
1201 private:
|
Chris@16
|
1202 // Use bool to indicate begin (one_ as value)
|
Chris@16
|
1203 typedef bool const_subiterator_type;
|
Chris@16
|
1204 public:
|
Chris@16
|
1205 class const_iterator;
|
Chris@16
|
1206
|
Chris@16
|
1207 // Element lookup
|
Chris@16
|
1208 BOOST_UBLAS_INLINE
|
Chris@16
|
1209 const_iterator find (size_type i) const {
|
Chris@16
|
1210 return const_iterator (*this, i <= index_);
|
Chris@16
|
1211 }
|
Chris@16
|
1212
|
Chris@16
|
1213 class const_iterator:
|
Chris@16
|
1214 public container_const_reference<unit_vector>,
|
Chris@16
|
1215 public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
|
Chris@16
|
1216 const_iterator, value_type> {
|
Chris@16
|
1217 public:
|
Chris@16
|
1218 typedef typename unit_vector::difference_type difference_type;
|
Chris@16
|
1219 typedef typename unit_vector::value_type value_type;
|
Chris@16
|
1220 typedef typename unit_vector::const_reference reference;
|
Chris@16
|
1221 typedef typename unit_vector::const_pointer pointer;
|
Chris@16
|
1222
|
Chris@16
|
1223 // Construction and destruction
|
Chris@16
|
1224 BOOST_UBLAS_INLINE
|
Chris@16
|
1225 const_iterator ():
|
Chris@16
|
1226 container_const_reference<unit_vector> (), it_ () {}
|
Chris@16
|
1227 BOOST_UBLAS_INLINE
|
Chris@16
|
1228 const_iterator (const unit_vector &v, const const_subiterator_type &it):
|
Chris@16
|
1229 container_const_reference<unit_vector> (v), it_ (it) {}
|
Chris@16
|
1230
|
Chris@16
|
1231 // Arithmetic
|
Chris@16
|
1232 BOOST_UBLAS_INLINE
|
Chris@16
|
1233 const_iterator &operator ++ () {
|
Chris@16
|
1234 BOOST_UBLAS_CHECK (it_, bad_index ());
|
Chris@16
|
1235 it_ = !it_;
|
Chris@16
|
1236 return *this;
|
Chris@16
|
1237 }
|
Chris@16
|
1238 BOOST_UBLAS_INLINE
|
Chris@16
|
1239 const_iterator &operator -- () {
|
Chris@16
|
1240 BOOST_UBLAS_CHECK (!it_, bad_index ());
|
Chris@16
|
1241 it_ = !it_;
|
Chris@16
|
1242 return *this;
|
Chris@16
|
1243 }
|
Chris@16
|
1244
|
Chris@16
|
1245 // Dereference
|
Chris@16
|
1246 BOOST_UBLAS_INLINE
|
Chris@16
|
1247 const_reference operator * () const {
|
Chris@16
|
1248 BOOST_UBLAS_CHECK (it_, bad_index ());
|
Chris@16
|
1249 return one_;
|
Chris@16
|
1250 }
|
Chris@16
|
1251
|
Chris@16
|
1252 // Index
|
Chris@16
|
1253 BOOST_UBLAS_INLINE
|
Chris@16
|
1254 size_type index () const {
|
Chris@16
|
1255 BOOST_UBLAS_CHECK (it_, bad_index ());
|
Chris@16
|
1256 return (*this) ().index_;
|
Chris@16
|
1257 }
|
Chris@16
|
1258
|
Chris@16
|
1259 // Assignment
|
Chris@16
|
1260 BOOST_UBLAS_INLINE
|
Chris@16
|
1261 const_iterator &operator = (const const_iterator &it) {
|
Chris@16
|
1262 container_const_reference<unit_vector>::assign (&it ());
|
Chris@16
|
1263 it_ = it.it_;
|
Chris@16
|
1264 return *this;
|
Chris@16
|
1265 }
|
Chris@16
|
1266
|
Chris@16
|
1267 // Comparison
|
Chris@16
|
1268 BOOST_UBLAS_INLINE
|
Chris@16
|
1269 bool operator == (const const_iterator &it) const {
|
Chris@16
|
1270 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
1271 return it_ == it.it_;
|
Chris@16
|
1272 }
|
Chris@16
|
1273
|
Chris@16
|
1274 private:
|
Chris@16
|
1275 const_subiterator_type it_;
|
Chris@16
|
1276 };
|
Chris@16
|
1277
|
Chris@16
|
1278 typedef const_iterator iterator;
|
Chris@16
|
1279
|
Chris@16
|
1280 BOOST_UBLAS_INLINE
|
Chris@16
|
1281 const_iterator begin () const {
|
Chris@16
|
1282 return const_iterator (*this, true);
|
Chris@16
|
1283 }
|
Chris@16
|
1284 BOOST_UBLAS_INLINE
|
Chris@16
|
1285 const_iterator end () const {
|
Chris@16
|
1286 return const_iterator (*this, false);
|
Chris@16
|
1287 }
|
Chris@16
|
1288
|
Chris@16
|
1289 // Reverse iterator
|
Chris@16
|
1290 typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
|
Chris@16
|
1291
|
Chris@16
|
1292 BOOST_UBLAS_INLINE
|
Chris@16
|
1293 const_reverse_iterator rbegin () const {
|
Chris@16
|
1294 return const_reverse_iterator (end ());
|
Chris@16
|
1295 }
|
Chris@16
|
1296 BOOST_UBLAS_INLINE
|
Chris@16
|
1297 const_reverse_iterator rend () const {
|
Chris@16
|
1298 return const_reverse_iterator (begin ());
|
Chris@16
|
1299 }
|
Chris@16
|
1300
|
Chris@16
|
1301 // Serialization
|
Chris@16
|
1302 template<class Archive>
|
Chris@16
|
1303 void serialize(Archive & ar, const unsigned int /* file_version */){
|
Chris@16
|
1304 serialization::collection_size_type s (size_);
|
Chris@16
|
1305 ar & serialization::make_nvp("size",s);
|
Chris@16
|
1306 if (Archive::is_loading::value) {
|
Chris@16
|
1307 size_ = s;
|
Chris@16
|
1308 }
|
Chris@16
|
1309 ar & serialization::make_nvp("index", index_);
|
Chris@16
|
1310 }
|
Chris@16
|
1311
|
Chris@16
|
1312 private:
|
Chris@16
|
1313 size_type size_;
|
Chris@16
|
1314 size_type index_;
|
Chris@16
|
1315 typedef const value_type const_value_type;
|
Chris@16
|
1316 static const_value_type zero_;
|
Chris@16
|
1317 static const_value_type one_;
|
Chris@16
|
1318 };
|
Chris@16
|
1319
|
Chris@16
|
1320 template<class T, class ALLOC>
|
Chris@16
|
1321 typename unit_vector<T, ALLOC>::const_value_type unit_vector<T, ALLOC>::zero_ = T(/*zero*/);
|
Chris@16
|
1322 template<class T, class ALLOC>
|
Chris@16
|
1323 typename unit_vector<T, ALLOC>::const_value_type unit_vector<T, ALLOC>::one_ (1); // ISSUE: need 'one'-traits here
|
Chris@16
|
1324
|
Chris@16
|
1325 /// \brief A scalar (i.e. unique value) vector of type \c T and a given \c size
|
Chris@16
|
1326 /// A scalar (i.e. unique value) vector of type \c T and a given \c size. This is a virtual vector in the sense that no memory is allocated
|
Chris@16
|
1327 /// for storing the unique value more than once: it still acts like any other vector. However assigning a new value will change all the value at once.
|
Chris@16
|
1328 /// vector into a normal vector. It must first be assigned to another normal vector by any suitable means. Its memory footprint is constant.
|
Chris@16
|
1329 /// \tparam T type of the objects stored in the vector: it can be anything even if most of the time, scalar types will be used like \c double or \c int. Complex types can be used, or even classes like boost::interval.
|
Chris@16
|
1330 template<class T, class ALLOC>
|
Chris@16
|
1331 class scalar_vector:
|
Chris@16
|
1332 public vector_container<scalar_vector<T, ALLOC> > {
|
Chris@16
|
1333
|
Chris@16
|
1334 typedef const T *const_pointer;
|
Chris@16
|
1335 typedef scalar_vector<T, ALLOC> self_type;
|
Chris@16
|
1336 public:
|
Chris@16
|
1337 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
|
Chris@16
|
1338 using vector_container<self_type>::operator ();
|
Chris@16
|
1339 #endif
|
Chris@16
|
1340 typedef typename ALLOC::size_type size_type;
|
Chris@16
|
1341 typedef typename ALLOC::difference_type difference_type;
|
Chris@16
|
1342 typedef T value_type;
|
Chris@16
|
1343 typedef const T &const_reference;
|
Chris@16
|
1344 typedef T &reference;
|
Chris@16
|
1345 typedef const vector_reference<const self_type> const_closure_type;
|
Chris@16
|
1346 typedef vector_reference<self_type> closure_type;
|
Chris@16
|
1347 typedef dense_tag storage_category;
|
Chris@16
|
1348
|
Chris@16
|
1349 // Construction and destruction
|
Chris@16
|
1350 BOOST_UBLAS_INLINE
|
Chris@16
|
1351 scalar_vector ():
|
Chris@16
|
1352 vector_container<self_type> (),
|
Chris@16
|
1353 size_ (0), value_ () {}
|
Chris@16
|
1354 BOOST_UBLAS_INLINE
|
Chris@16
|
1355 explicit scalar_vector (size_type size, const value_type &value = value_type(1)):
|
Chris@16
|
1356 vector_container<self_type> (),
|
Chris@16
|
1357 size_ (size), value_ (value) {}
|
Chris@16
|
1358 BOOST_UBLAS_INLINE
|
Chris@16
|
1359 scalar_vector (const scalar_vector &v):
|
Chris@16
|
1360 vector_container<self_type> (),
|
Chris@16
|
1361 size_ (v.size_), value_ (v.value_) {}
|
Chris@16
|
1362
|
Chris@16
|
1363 // Accessors
|
Chris@16
|
1364 BOOST_UBLAS_INLINE
|
Chris@16
|
1365 size_type size () const {
|
Chris@16
|
1366 return size_;
|
Chris@16
|
1367 }
|
Chris@16
|
1368
|
Chris@16
|
1369 // Resizing
|
Chris@16
|
1370 BOOST_UBLAS_INLINE
|
Chris@16
|
1371 void resize (size_type size, bool /*preserve*/ = true) {
|
Chris@16
|
1372 size_ = size;
|
Chris@16
|
1373 }
|
Chris@16
|
1374
|
Chris@16
|
1375 // Element support
|
Chris@16
|
1376 BOOST_UBLAS_INLINE
|
Chris@16
|
1377 const_pointer find_element (size_type /*i*/) const {
|
Chris@16
|
1378 return & value_;
|
Chris@16
|
1379 }
|
Chris@16
|
1380
|
Chris@16
|
1381 // Element access
|
Chris@16
|
1382 BOOST_UBLAS_INLINE
|
Chris@16
|
1383 const_reference operator () (size_type /*i*/) const {
|
Chris@16
|
1384 return value_;
|
Chris@16
|
1385 }
|
Chris@16
|
1386
|
Chris@16
|
1387 BOOST_UBLAS_INLINE
|
Chris@16
|
1388 const_reference operator [] (size_type /*i*/) const {
|
Chris@16
|
1389 return value_;
|
Chris@16
|
1390 }
|
Chris@16
|
1391
|
Chris@16
|
1392 // Assignment
|
Chris@16
|
1393 BOOST_UBLAS_INLINE
|
Chris@16
|
1394 scalar_vector &operator = (const scalar_vector &v) {
|
Chris@16
|
1395 size_ = v.size_;
|
Chris@16
|
1396 value_ = v.value_;
|
Chris@16
|
1397 return *this;
|
Chris@16
|
1398 }
|
Chris@16
|
1399 BOOST_UBLAS_INLINE
|
Chris@16
|
1400 scalar_vector &assign_temporary (scalar_vector &v) {
|
Chris@16
|
1401 swap (v);
|
Chris@16
|
1402 return *this;
|
Chris@16
|
1403 }
|
Chris@16
|
1404
|
Chris@16
|
1405 // Swapping
|
Chris@16
|
1406 BOOST_UBLAS_INLINE
|
Chris@16
|
1407 void swap (scalar_vector &v) {
|
Chris@16
|
1408 if (this != &v) {
|
Chris@16
|
1409 std::swap (size_, v.size_);
|
Chris@16
|
1410 std::swap (value_, v.value_);
|
Chris@16
|
1411 }
|
Chris@16
|
1412 }
|
Chris@16
|
1413 BOOST_UBLAS_INLINE
|
Chris@16
|
1414 friend void swap (scalar_vector &v1, scalar_vector &v2) {
|
Chris@16
|
1415 v1.swap (v2);
|
Chris@16
|
1416 }
|
Chris@16
|
1417
|
Chris@16
|
1418 // Iterator types
|
Chris@16
|
1419 private:
|
Chris@16
|
1420 // Use an index
|
Chris@16
|
1421 typedef size_type const_subiterator_type;
|
Chris@16
|
1422
|
Chris@16
|
1423 public:
|
Chris@16
|
1424 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
1425 typedef indexed_const_iterator<self_type, dense_random_access_iterator_tag> iterator;
|
Chris@16
|
1426 typedef indexed_const_iterator<self_type, dense_random_access_iterator_tag> const_iterator;
|
Chris@16
|
1427 #else
|
Chris@16
|
1428 class const_iterator;
|
Chris@16
|
1429 #endif
|
Chris@16
|
1430
|
Chris@16
|
1431 // Element lookup
|
Chris@16
|
1432 BOOST_UBLAS_INLINE
|
Chris@16
|
1433 const_iterator find (size_type i) const {
|
Chris@16
|
1434 return const_iterator (*this, i);
|
Chris@16
|
1435 }
|
Chris@16
|
1436
|
Chris@16
|
1437 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
1438 class const_iterator:
|
Chris@16
|
1439 public container_const_reference<scalar_vector>,
|
Chris@16
|
1440 public random_access_iterator_base<dense_random_access_iterator_tag,
|
Chris@16
|
1441 const_iterator, value_type> {
|
Chris@16
|
1442 public:
|
Chris@16
|
1443 typedef typename scalar_vector::difference_type difference_type;
|
Chris@16
|
1444 typedef typename scalar_vector::value_type value_type;
|
Chris@16
|
1445 typedef typename scalar_vector::const_reference reference;
|
Chris@16
|
1446 typedef typename scalar_vector::const_pointer pointer;
|
Chris@16
|
1447
|
Chris@16
|
1448 // Construction and destruction
|
Chris@16
|
1449 BOOST_UBLAS_INLINE
|
Chris@16
|
1450 const_iterator ():
|
Chris@16
|
1451 container_const_reference<scalar_vector> (), it_ () {}
|
Chris@16
|
1452 BOOST_UBLAS_INLINE
|
Chris@16
|
1453 const_iterator (const scalar_vector &v, const const_subiterator_type &it):
|
Chris@16
|
1454 container_const_reference<scalar_vector> (v), it_ (it) {}
|
Chris@16
|
1455
|
Chris@16
|
1456 // Arithmetic
|
Chris@16
|
1457 BOOST_UBLAS_INLINE
|
Chris@16
|
1458 const_iterator &operator ++ () {
|
Chris@16
|
1459 ++ it_;
|
Chris@16
|
1460 return *this;
|
Chris@16
|
1461 }
|
Chris@16
|
1462 BOOST_UBLAS_INLINE
|
Chris@16
|
1463 const_iterator &operator -- () {
|
Chris@16
|
1464 -- it_;
|
Chris@16
|
1465 return *this;
|
Chris@16
|
1466 }
|
Chris@16
|
1467 BOOST_UBLAS_INLINE
|
Chris@16
|
1468 const_iterator &operator += (difference_type n) {
|
Chris@16
|
1469 it_ += n;
|
Chris@16
|
1470 return *this;
|
Chris@16
|
1471 }
|
Chris@16
|
1472 BOOST_UBLAS_INLINE
|
Chris@16
|
1473 const_iterator &operator -= (difference_type n) {
|
Chris@16
|
1474 it_ -= n;
|
Chris@16
|
1475 return *this;
|
Chris@16
|
1476 }
|
Chris@16
|
1477 BOOST_UBLAS_INLINE
|
Chris@16
|
1478 difference_type operator - (const const_iterator &it) const {
|
Chris@16
|
1479 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
1480 return it_ - it.it_;
|
Chris@16
|
1481 }
|
Chris@16
|
1482
|
Chris@16
|
1483 // Dereference
|
Chris@16
|
1484 BOOST_UBLAS_INLINE
|
Chris@16
|
1485 const_reference operator * () const {
|
Chris@16
|
1486 BOOST_UBLAS_CHECK (it_ < (*this) ().size (), bad_index ());
|
Chris@16
|
1487 return (*this) () (index ());
|
Chris@16
|
1488 }
|
Chris@16
|
1489 BOOST_UBLAS_INLINE
|
Chris@16
|
1490 const_reference operator [] (difference_type n) const {
|
Chris@16
|
1491 return *(*this + n);
|
Chris@16
|
1492 }
|
Chris@16
|
1493
|
Chris@16
|
1494 // Index
|
Chris@16
|
1495 BOOST_UBLAS_INLINE
|
Chris@16
|
1496 size_type index () const {
|
Chris@16
|
1497 BOOST_UBLAS_CHECK (it_ < (*this) ().size (), bad_index ());
|
Chris@16
|
1498 return it_;
|
Chris@16
|
1499 }
|
Chris@16
|
1500
|
Chris@16
|
1501 // Assignment
|
Chris@16
|
1502 BOOST_UBLAS_INLINE
|
Chris@16
|
1503 const_iterator &operator = (const const_iterator &it) {
|
Chris@16
|
1504 container_const_reference<scalar_vector>::assign (&it ());
|
Chris@16
|
1505 it_ = it.it_;
|
Chris@16
|
1506 return *this;
|
Chris@16
|
1507 }
|
Chris@16
|
1508
|
Chris@16
|
1509 // Comparison
|
Chris@16
|
1510 BOOST_UBLAS_INLINE
|
Chris@16
|
1511 bool operator == (const const_iterator &it) const {
|
Chris@16
|
1512 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
1513 return it_ == it.it_;
|
Chris@16
|
1514 }
|
Chris@16
|
1515 BOOST_UBLAS_INLINE
|
Chris@16
|
1516 bool operator < (const const_iterator &it) const {
|
Chris@16
|
1517 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
1518 return it_ < it.it_;
|
Chris@16
|
1519 }
|
Chris@16
|
1520
|
Chris@16
|
1521 private:
|
Chris@16
|
1522 const_subiterator_type it_;
|
Chris@16
|
1523 };
|
Chris@16
|
1524
|
Chris@16
|
1525 typedef const_iterator iterator;
|
Chris@16
|
1526 #endif
|
Chris@16
|
1527
|
Chris@16
|
1528 BOOST_UBLAS_INLINE
|
Chris@16
|
1529 const_iterator begin () const {
|
Chris@16
|
1530 return find (0);
|
Chris@16
|
1531 }
|
Chris@16
|
1532 BOOST_UBLAS_INLINE
|
Chris@16
|
1533 const_iterator end () const {
|
Chris@16
|
1534 return find (size_);
|
Chris@16
|
1535 }
|
Chris@16
|
1536
|
Chris@16
|
1537 // Reverse iterator
|
Chris@16
|
1538 typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
|
Chris@16
|
1539
|
Chris@16
|
1540 BOOST_UBLAS_INLINE
|
Chris@16
|
1541 const_reverse_iterator rbegin () const {
|
Chris@16
|
1542 return const_reverse_iterator (end ());
|
Chris@16
|
1543 }
|
Chris@16
|
1544 BOOST_UBLAS_INLINE
|
Chris@16
|
1545 const_reverse_iterator rend () const {
|
Chris@16
|
1546 return const_reverse_iterator (begin ());
|
Chris@16
|
1547 }
|
Chris@16
|
1548
|
Chris@16
|
1549 // Serialization
|
Chris@16
|
1550 template<class Archive>
|
Chris@16
|
1551 void serialize(Archive & ar, const unsigned int /* file_version */){
|
Chris@16
|
1552 serialization::collection_size_type s (size_);
|
Chris@16
|
1553 ar & serialization::make_nvp("size",s);
|
Chris@16
|
1554 if (Archive::is_loading::value) {
|
Chris@16
|
1555 size_ = s;
|
Chris@16
|
1556 }
|
Chris@16
|
1557 ar & serialization::make_nvp("value", value_);
|
Chris@16
|
1558 }
|
Chris@16
|
1559
|
Chris@16
|
1560 private:
|
Chris@16
|
1561 size_type size_;
|
Chris@16
|
1562 value_type value_;
|
Chris@16
|
1563 };
|
Chris@16
|
1564
|
Chris@16
|
1565 // ------------------------
|
Chris@16
|
1566 // Array based vector class
|
Chris@16
|
1567 // ------------------------
|
Chris@16
|
1568
|
Chris@16
|
1569 /// \brief A dense vector of values of type \c T with the given \c size. The data is stored as an ordinary C++ array \c T \c data_[M]
|
Chris@16
|
1570 template<class T, std::size_t N>
|
Chris@16
|
1571 class c_vector:
|
Chris@16
|
1572 public vector_container<c_vector<T, N> > {
|
Chris@16
|
1573
|
Chris@16
|
1574 typedef c_vector<T, N> self_type;
|
Chris@16
|
1575 public:
|
Chris@16
|
1576 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
|
Chris@16
|
1577 using vector_container<self_type>::operator ();
|
Chris@16
|
1578 #endif
|
Chris@16
|
1579 typedef std::size_t size_type;
|
Chris@16
|
1580 typedef std::ptrdiff_t difference_type;
|
Chris@16
|
1581 typedef T value_type;
|
Chris@16
|
1582 typedef const T &const_reference;
|
Chris@16
|
1583 typedef T &reference;
|
Chris@16
|
1584 typedef value_type array_type[N];
|
Chris@16
|
1585 typedef T *pointer;
|
Chris@16
|
1586 typedef const T *const_pointer;
|
Chris@16
|
1587 typedef const vector_reference<const self_type> const_closure_type;
|
Chris@16
|
1588 typedef vector_reference<self_type> closure_type;
|
Chris@16
|
1589 typedef self_type vector_temporary_type;
|
Chris@16
|
1590 typedef dense_tag storage_category;
|
Chris@16
|
1591
|
Chris@16
|
1592 // Construction and destruction
|
Chris@16
|
1593 BOOST_UBLAS_INLINE
|
Chris@16
|
1594 c_vector ():
|
Chris@16
|
1595 size_ (N) /* , data_ () */ {}
|
Chris@16
|
1596 explicit BOOST_UBLAS_INLINE
|
Chris@16
|
1597 c_vector (size_type size):
|
Chris@16
|
1598 size_ (size) /* , data_ () */ {
|
Chris@16
|
1599 if (size_ > N)
|
Chris@16
|
1600 bad_size ().raise ();
|
Chris@16
|
1601 }
|
Chris@16
|
1602 BOOST_UBLAS_INLINE
|
Chris@16
|
1603 c_vector (const c_vector &v):
|
Chris@16
|
1604 size_ (v.size_) /* , data_ () */ {
|
Chris@16
|
1605 if (size_ > N)
|
Chris@16
|
1606 bad_size ().raise ();
|
Chris@16
|
1607 assign(v);
|
Chris@16
|
1608 }
|
Chris@16
|
1609 template<class AE>
|
Chris@16
|
1610 BOOST_UBLAS_INLINE
|
Chris@16
|
1611 c_vector (const vector_expression<AE> &ae):
|
Chris@16
|
1612 size_ (ae ().size ()) /* , data_ () */ {
|
Chris@16
|
1613 if (size_ > N)
|
Chris@16
|
1614 bad_size ().raise ();
|
Chris@16
|
1615 vector_assign<scalar_assign> (*this, ae);
|
Chris@16
|
1616 }
|
Chris@16
|
1617
|
Chris@16
|
1618 // Accessors
|
Chris@16
|
1619 BOOST_UBLAS_INLINE
|
Chris@16
|
1620 size_type size () const {
|
Chris@16
|
1621 return size_;
|
Chris@16
|
1622 }
|
Chris@16
|
1623 BOOST_UBLAS_INLINE
|
Chris@16
|
1624 const_pointer data () const {
|
Chris@16
|
1625 return data_;
|
Chris@16
|
1626 }
|
Chris@16
|
1627 BOOST_UBLAS_INLINE
|
Chris@16
|
1628 pointer data () {
|
Chris@16
|
1629 return data_;
|
Chris@16
|
1630 }
|
Chris@16
|
1631
|
Chris@16
|
1632 // Resizing
|
Chris@16
|
1633 BOOST_UBLAS_INLINE
|
Chris@16
|
1634 void resize (size_type size, bool preserve = true) {
|
Chris@16
|
1635 if (size > N)
|
Chris@16
|
1636 bad_size ().raise ();
|
Chris@16
|
1637 size_ = size;
|
Chris@16
|
1638 }
|
Chris@16
|
1639
|
Chris@16
|
1640 // Element support
|
Chris@16
|
1641 BOOST_UBLAS_INLINE
|
Chris@16
|
1642 pointer find_element (size_type i) {
|
Chris@16
|
1643 return const_cast<pointer> (const_cast<const self_type&>(*this).find_element (i));
|
Chris@16
|
1644 }
|
Chris@16
|
1645 BOOST_UBLAS_INLINE
|
Chris@16
|
1646 const_pointer find_element (size_type i) const {
|
Chris@16
|
1647 return & data_ [i];
|
Chris@16
|
1648 }
|
Chris@16
|
1649
|
Chris@16
|
1650 // Element access
|
Chris@16
|
1651 BOOST_UBLAS_INLINE
|
Chris@16
|
1652 const_reference operator () (size_type i) const {
|
Chris@16
|
1653 BOOST_UBLAS_CHECK (i < size_, bad_index ());
|
Chris@16
|
1654 return data_ [i];
|
Chris@16
|
1655 }
|
Chris@16
|
1656 BOOST_UBLAS_INLINE
|
Chris@16
|
1657 reference operator () (size_type i) {
|
Chris@16
|
1658 BOOST_UBLAS_CHECK (i < size_, bad_index ());
|
Chris@16
|
1659 return data_ [i];
|
Chris@16
|
1660 }
|
Chris@16
|
1661
|
Chris@16
|
1662 BOOST_UBLAS_INLINE
|
Chris@16
|
1663 const_reference operator [] (size_type i) const {
|
Chris@16
|
1664 return (*this) (i);
|
Chris@16
|
1665 }
|
Chris@16
|
1666 BOOST_UBLAS_INLINE
|
Chris@16
|
1667 reference operator [] (size_type i) {
|
Chris@16
|
1668 return (*this) (i);
|
Chris@16
|
1669 }
|
Chris@16
|
1670
|
Chris@16
|
1671 // Element assignment
|
Chris@16
|
1672 BOOST_UBLAS_INLINE
|
Chris@16
|
1673 reference insert_element (size_type i, const_reference t) {
|
Chris@16
|
1674 BOOST_UBLAS_CHECK (i < size_, bad_index ());
|
Chris@16
|
1675 return (data_ [i] = t);
|
Chris@16
|
1676 }
|
Chris@16
|
1677 BOOST_UBLAS_INLINE
|
Chris@16
|
1678 void erase_element (size_type i) {
|
Chris@16
|
1679 BOOST_UBLAS_CHECK (i < size_, bad_index ());
|
Chris@16
|
1680 data_ [i] = value_type/*zero*/();
|
Chris@16
|
1681 }
|
Chris@16
|
1682
|
Chris@16
|
1683 // Zeroing
|
Chris@16
|
1684 BOOST_UBLAS_INLINE
|
Chris@16
|
1685 void clear () {
|
Chris@16
|
1686 std::fill (data_, data_ + size_, value_type/*zero*/());
|
Chris@16
|
1687 }
|
Chris@16
|
1688
|
Chris@16
|
1689 // Assignment
|
Chris@16
|
1690 #ifdef BOOST_UBLAS_MOVE_SEMANTICS
|
Chris@16
|
1691
|
Chris@16
|
1692 /*! @note "pass by value" the key idea to enable move semantics */
|
Chris@16
|
1693 BOOST_UBLAS_INLINE
|
Chris@16
|
1694 c_vector &operator = (c_vector v) {
|
Chris@16
|
1695 assign_temporary(v);
|
Chris@16
|
1696 return *this;
|
Chris@16
|
1697 }
|
Chris@16
|
1698 #else
|
Chris@16
|
1699 BOOST_UBLAS_INLINE
|
Chris@16
|
1700 c_vector &operator = (const c_vector &v) {
|
Chris@16
|
1701 size_ = v.size_;
|
Chris@16
|
1702 std::copy (v.data_, v.data_ + v.size_, data_);
|
Chris@16
|
1703 return *this;
|
Chris@16
|
1704 }
|
Chris@16
|
1705 #endif
|
Chris@16
|
1706 template<class C> // Container assignment without temporary
|
Chris@16
|
1707 BOOST_UBLAS_INLINE
|
Chris@16
|
1708 c_vector &operator = (const vector_container<C> &v) {
|
Chris@16
|
1709 resize (v ().size (), false);
|
Chris@16
|
1710 assign (v);
|
Chris@16
|
1711 return *this;
|
Chris@16
|
1712 }
|
Chris@16
|
1713 BOOST_UBLAS_INLINE
|
Chris@16
|
1714 c_vector &assign_temporary (c_vector &v) {
|
Chris@16
|
1715 swap (v);
|
Chris@16
|
1716 return *this;
|
Chris@16
|
1717 }
|
Chris@16
|
1718 template<class AE>
|
Chris@16
|
1719 BOOST_UBLAS_INLINE
|
Chris@16
|
1720 c_vector &operator = (const vector_expression<AE> &ae) {
|
Chris@16
|
1721 self_type temporary (ae);
|
Chris@16
|
1722 return assign_temporary (temporary);
|
Chris@16
|
1723 }
|
Chris@16
|
1724 template<class AE>
|
Chris@16
|
1725 BOOST_UBLAS_INLINE
|
Chris@16
|
1726 c_vector &assign (const vector_expression<AE> &ae) {
|
Chris@16
|
1727 vector_assign<scalar_assign> (*this, ae);
|
Chris@16
|
1728 return *this;
|
Chris@16
|
1729 }
|
Chris@16
|
1730
|
Chris@16
|
1731 // Computed assignment
|
Chris@16
|
1732 template<class AE>
|
Chris@16
|
1733 BOOST_UBLAS_INLINE
|
Chris@16
|
1734 c_vector &operator += (const vector_expression<AE> &ae) {
|
Chris@16
|
1735 self_type temporary (*this + ae);
|
Chris@16
|
1736 return assign_temporary (temporary);
|
Chris@16
|
1737 }
|
Chris@16
|
1738 template<class C> // Container assignment without temporary
|
Chris@16
|
1739 BOOST_UBLAS_INLINE
|
Chris@16
|
1740 c_vector &operator += (const vector_container<C> &v) {
|
Chris@16
|
1741 plus_assign (v);
|
Chris@16
|
1742 return *this;
|
Chris@16
|
1743 }
|
Chris@16
|
1744 template<class AE>
|
Chris@16
|
1745 BOOST_UBLAS_INLINE
|
Chris@16
|
1746 c_vector &plus_assign (const vector_expression<AE> &ae) {
|
Chris@16
|
1747 vector_assign<scalar_plus_assign> ( *this, ae);
|
Chris@16
|
1748 return *this;
|
Chris@16
|
1749 }
|
Chris@16
|
1750 template<class AE>
|
Chris@16
|
1751 BOOST_UBLAS_INLINE
|
Chris@16
|
1752 c_vector &operator -= (const vector_expression<AE> &ae) {
|
Chris@16
|
1753 self_type temporary (*this - ae);
|
Chris@16
|
1754 return assign_temporary (temporary);
|
Chris@16
|
1755 }
|
Chris@16
|
1756 template<class C> // Container assignment without temporary
|
Chris@16
|
1757 BOOST_UBLAS_INLINE
|
Chris@16
|
1758 c_vector &operator -= (const vector_container<C> &v) {
|
Chris@16
|
1759 minus_assign (v);
|
Chris@16
|
1760 return *this;
|
Chris@16
|
1761 }
|
Chris@16
|
1762 template<class AE>
|
Chris@16
|
1763 BOOST_UBLAS_INLINE
|
Chris@16
|
1764 c_vector &minus_assign (const vector_expression<AE> &ae) {
|
Chris@16
|
1765 vector_assign<scalar_minus_assign> (*this, ae);
|
Chris@16
|
1766 return *this;
|
Chris@16
|
1767 }
|
Chris@16
|
1768 template<class AT>
|
Chris@16
|
1769 BOOST_UBLAS_INLINE
|
Chris@16
|
1770 c_vector &operator *= (const AT &at) {
|
Chris@16
|
1771 vector_assign_scalar<scalar_multiplies_assign> (*this, at);
|
Chris@16
|
1772 return *this;
|
Chris@16
|
1773 }
|
Chris@16
|
1774 template<class AT>
|
Chris@16
|
1775 BOOST_UBLAS_INLINE
|
Chris@16
|
1776 c_vector &operator /= (const AT &at) {
|
Chris@16
|
1777 vector_assign_scalar<scalar_divides_assign> (*this, at);
|
Chris@16
|
1778 return *this;
|
Chris@16
|
1779 }
|
Chris@16
|
1780
|
Chris@16
|
1781 // Swapping
|
Chris@16
|
1782 BOOST_UBLAS_INLINE
|
Chris@16
|
1783 void swap (c_vector &v) {
|
Chris@16
|
1784 if (this != &v) {
|
Chris@16
|
1785 BOOST_UBLAS_CHECK (size_ == v.size_, bad_size ());
|
Chris@16
|
1786 std::swap (size_, v.size_);
|
Chris@16
|
1787 std::swap_ranges (data_, data_ + size_, v.data_);
|
Chris@16
|
1788 }
|
Chris@16
|
1789 }
|
Chris@16
|
1790 BOOST_UBLAS_INLINE
|
Chris@16
|
1791 friend void swap (c_vector &v1, c_vector &v2) {
|
Chris@16
|
1792 v1.swap (v2);
|
Chris@16
|
1793 }
|
Chris@16
|
1794
|
Chris@16
|
1795 // Iterator types
|
Chris@16
|
1796 private:
|
Chris@16
|
1797 // Use pointers for iterator
|
Chris@16
|
1798 typedef const_pointer const_subiterator_type;
|
Chris@16
|
1799 typedef pointer subiterator_type;
|
Chris@16
|
1800
|
Chris@16
|
1801 public:
|
Chris@16
|
1802 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
1803 typedef indexed_iterator<self_type, dense_random_access_iterator_tag> iterator;
|
Chris@16
|
1804 typedef indexed_const_iterator<self_type, dense_random_access_iterator_tag> const_iterator;
|
Chris@16
|
1805 #else
|
Chris@16
|
1806 class const_iterator;
|
Chris@16
|
1807 class iterator;
|
Chris@16
|
1808 #endif
|
Chris@16
|
1809
|
Chris@16
|
1810 // Element lookup
|
Chris@16
|
1811 BOOST_UBLAS_INLINE
|
Chris@16
|
1812 const_iterator find (size_type i) const {
|
Chris@16
|
1813 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
1814 return const_iterator (*this, &data_ [i]);
|
Chris@16
|
1815 #else
|
Chris@16
|
1816 return const_iterator (*this, i);
|
Chris@16
|
1817 #endif
|
Chris@16
|
1818 }
|
Chris@16
|
1819 BOOST_UBLAS_INLINE
|
Chris@16
|
1820 iterator find (size_type i) {
|
Chris@16
|
1821 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
1822 return iterator (*this, &data_ [i]);
|
Chris@16
|
1823 #else
|
Chris@16
|
1824 return iterator (*this, i);
|
Chris@16
|
1825 #endif
|
Chris@16
|
1826 }
|
Chris@16
|
1827
|
Chris@16
|
1828 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
1829 class const_iterator:
|
Chris@16
|
1830 public container_const_reference<c_vector>,
|
Chris@16
|
1831 public random_access_iterator_base<dense_random_access_iterator_tag,
|
Chris@16
|
1832 const_iterator, value_type> {
|
Chris@16
|
1833 public:
|
Chris@16
|
1834 typedef typename c_vector::difference_type difference_type;
|
Chris@16
|
1835 typedef typename c_vector::value_type value_type;
|
Chris@16
|
1836 typedef typename c_vector::const_reference reference;
|
Chris@16
|
1837 typedef typename c_vector::const_pointer pointer;
|
Chris@16
|
1838
|
Chris@16
|
1839 // Construction and destruction
|
Chris@16
|
1840 BOOST_UBLAS_INLINE
|
Chris@16
|
1841 const_iterator ():
|
Chris@16
|
1842 container_const_reference<self_type> (), it_ () {}
|
Chris@16
|
1843 BOOST_UBLAS_INLINE
|
Chris@16
|
1844 const_iterator (const self_type &v, const const_subiterator_type &it):
|
Chris@16
|
1845 container_const_reference<self_type> (v), it_ (it) {}
|
Chris@16
|
1846 BOOST_UBLAS_INLINE
|
Chris@16
|
1847 const_iterator (const typename self_type::iterator &it): // ISSUE self_type:: stops VC8 using std::iterator here
|
Chris@16
|
1848 container_const_reference<self_type> (it ()), it_ (it.it_) {}
|
Chris@16
|
1849
|
Chris@16
|
1850 // Arithmetic
|
Chris@16
|
1851 BOOST_UBLAS_INLINE
|
Chris@16
|
1852 const_iterator &operator ++ () {
|
Chris@16
|
1853 ++ it_;
|
Chris@16
|
1854 return *this;
|
Chris@16
|
1855 }
|
Chris@16
|
1856 BOOST_UBLAS_INLINE
|
Chris@16
|
1857 const_iterator &operator -- () {
|
Chris@16
|
1858 -- it_;
|
Chris@16
|
1859 return *this;
|
Chris@16
|
1860 }
|
Chris@16
|
1861 BOOST_UBLAS_INLINE
|
Chris@16
|
1862 const_iterator &operator += (difference_type n) {
|
Chris@16
|
1863 it_ += n;
|
Chris@16
|
1864 return *this;
|
Chris@16
|
1865 }
|
Chris@16
|
1866 BOOST_UBLAS_INLINE
|
Chris@16
|
1867 const_iterator &operator -= (difference_type n) {
|
Chris@16
|
1868 it_ -= n;
|
Chris@16
|
1869 return *this;
|
Chris@16
|
1870 }
|
Chris@16
|
1871 BOOST_UBLAS_INLINE
|
Chris@16
|
1872 difference_type operator - (const const_iterator &it) const {
|
Chris@16
|
1873 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
1874 return it_ - it.it_;
|
Chris@16
|
1875 }
|
Chris@16
|
1876
|
Chris@16
|
1877 // Dereference
|
Chris@16
|
1878 BOOST_UBLAS_INLINE
|
Chris@16
|
1879 const_reference operator * () const {
|
Chris@16
|
1880 BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_, bad_index ());
|
Chris@16
|
1881 return *it_;
|
Chris@16
|
1882 }
|
Chris@16
|
1883 BOOST_UBLAS_INLINE
|
Chris@16
|
1884 const_reference operator [] (difference_type n) const {
|
Chris@16
|
1885 return *(it_ + n);
|
Chris@16
|
1886 }
|
Chris@16
|
1887
|
Chris@16
|
1888 // Index
|
Chris@16
|
1889 BOOST_UBLAS_INLINE
|
Chris@16
|
1890 size_type index () const {
|
Chris@16
|
1891 BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_, bad_index ());
|
Chris@16
|
1892 const self_type &v = (*this) ();
|
Chris@16
|
1893 return it_ - v.begin ().it_;
|
Chris@16
|
1894 }
|
Chris@16
|
1895
|
Chris@16
|
1896 // Assignment
|
Chris@16
|
1897 BOOST_UBLAS_INLINE
|
Chris@16
|
1898 const_iterator &operator = (const const_iterator &it) {
|
Chris@16
|
1899 container_const_reference<self_type>::assign (&it ());
|
Chris@16
|
1900 it_ = it.it_;
|
Chris@16
|
1901 return *this;
|
Chris@16
|
1902 }
|
Chris@16
|
1903
|
Chris@16
|
1904 // Comparison
|
Chris@16
|
1905 BOOST_UBLAS_INLINE
|
Chris@16
|
1906 bool operator == (const const_iterator &it) const {
|
Chris@16
|
1907 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
1908 return it_ == it.it_;
|
Chris@16
|
1909 }
|
Chris@16
|
1910 BOOST_UBLAS_INLINE
|
Chris@16
|
1911 bool operator < (const const_iterator &it) const {
|
Chris@16
|
1912 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
1913 return it_ < it.it_;
|
Chris@16
|
1914 }
|
Chris@16
|
1915
|
Chris@16
|
1916 private:
|
Chris@16
|
1917 const_subiterator_type it_;
|
Chris@16
|
1918
|
Chris@16
|
1919 friend class iterator;
|
Chris@16
|
1920 };
|
Chris@16
|
1921 #endif
|
Chris@16
|
1922
|
Chris@16
|
1923 BOOST_UBLAS_INLINE
|
Chris@16
|
1924 const_iterator begin () const {
|
Chris@16
|
1925 return find (0);
|
Chris@16
|
1926 }
|
Chris@16
|
1927 BOOST_UBLAS_INLINE
|
Chris@16
|
1928 const_iterator end () const {
|
Chris@16
|
1929 return find (size_);
|
Chris@16
|
1930 }
|
Chris@16
|
1931
|
Chris@16
|
1932 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
|
Chris@16
|
1933 class iterator:
|
Chris@16
|
1934 public container_reference<c_vector>,
|
Chris@16
|
1935 public random_access_iterator_base<dense_random_access_iterator_tag,
|
Chris@16
|
1936 iterator, value_type> {
|
Chris@16
|
1937 public:
|
Chris@16
|
1938 typedef typename c_vector::difference_type difference_type;
|
Chris@16
|
1939 typedef typename c_vector::value_type value_type;
|
Chris@16
|
1940 typedef typename c_vector::reference reference;
|
Chris@16
|
1941 typedef typename c_vector::pointer pointer;
|
Chris@16
|
1942
|
Chris@16
|
1943 // Construction and destruction
|
Chris@16
|
1944 BOOST_UBLAS_INLINE
|
Chris@16
|
1945 iterator ():
|
Chris@16
|
1946 container_reference<self_type> (), it_ () {}
|
Chris@16
|
1947 BOOST_UBLAS_INLINE
|
Chris@16
|
1948 iterator (self_type &v, const subiterator_type &it):
|
Chris@16
|
1949 container_reference<self_type> (v), it_ (it) {}
|
Chris@16
|
1950
|
Chris@16
|
1951 // Arithmetic
|
Chris@16
|
1952 BOOST_UBLAS_INLINE
|
Chris@16
|
1953 iterator &operator ++ () {
|
Chris@16
|
1954 ++ it_;
|
Chris@16
|
1955 return *this;
|
Chris@16
|
1956 }
|
Chris@16
|
1957 BOOST_UBLAS_INLINE
|
Chris@16
|
1958 iterator &operator -- () {
|
Chris@16
|
1959 -- it_;
|
Chris@16
|
1960 return *this;
|
Chris@16
|
1961 }
|
Chris@16
|
1962 BOOST_UBLAS_INLINE
|
Chris@16
|
1963 iterator &operator += (difference_type n) {
|
Chris@16
|
1964 it_ += n;
|
Chris@16
|
1965 return *this;
|
Chris@16
|
1966 }
|
Chris@16
|
1967 BOOST_UBLAS_INLINE
|
Chris@16
|
1968 iterator &operator -= (difference_type n) {
|
Chris@16
|
1969 it_ -= n;
|
Chris@16
|
1970 return *this;
|
Chris@16
|
1971 }
|
Chris@16
|
1972 BOOST_UBLAS_INLINE
|
Chris@16
|
1973 difference_type operator - (const iterator &it) const {
|
Chris@16
|
1974 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
1975 return it_ - it.it_;
|
Chris@16
|
1976 }
|
Chris@16
|
1977
|
Chris@16
|
1978 // Dereference
|
Chris@16
|
1979 BOOST_UBLAS_INLINE
|
Chris@16
|
1980 reference operator * () const {
|
Chris@16
|
1981 BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_, bad_index ());
|
Chris@16
|
1982 return *it_;
|
Chris@16
|
1983 }
|
Chris@16
|
1984 BOOST_UBLAS_INLINE
|
Chris@16
|
1985 reference operator [] (difference_type n) const {
|
Chris@16
|
1986 return *(it_ + n);
|
Chris@16
|
1987 }
|
Chris@16
|
1988
|
Chris@16
|
1989 // Index
|
Chris@16
|
1990 BOOST_UBLAS_INLINE
|
Chris@16
|
1991 size_type index () const {
|
Chris@16
|
1992 BOOST_UBLAS_CHECK (it_ >= (*this) ().begin ().it_ && it_ < (*this) ().end ().it_, bad_index ());
|
Chris@16
|
1993 // EDG won't allow const self_type it doesn't allow friend access to it_
|
Chris@16
|
1994 self_type &v = (*this) ();
|
Chris@16
|
1995 return it_ - v.begin ().it_;
|
Chris@16
|
1996 }
|
Chris@16
|
1997
|
Chris@16
|
1998 // Assignment
|
Chris@16
|
1999 BOOST_UBLAS_INLINE
|
Chris@16
|
2000 iterator &operator = (const iterator &it) {
|
Chris@16
|
2001 container_reference<self_type>::assign (&it ());
|
Chris@16
|
2002 it_ = it.it_;
|
Chris@16
|
2003 return *this;
|
Chris@16
|
2004 }
|
Chris@16
|
2005
|
Chris@16
|
2006 // Comparison
|
Chris@16
|
2007 BOOST_UBLAS_INLINE
|
Chris@16
|
2008 bool operator == (const iterator &it) const {
|
Chris@16
|
2009 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
2010 return it_ == it.it_;
|
Chris@16
|
2011 }
|
Chris@16
|
2012 BOOST_UBLAS_INLINE
|
Chris@16
|
2013 bool operator < (const iterator &it) const {
|
Chris@16
|
2014 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
|
Chris@16
|
2015 return it_ < it.it_;
|
Chris@16
|
2016 }
|
Chris@16
|
2017
|
Chris@16
|
2018 private:
|
Chris@16
|
2019 subiterator_type it_;
|
Chris@16
|
2020
|
Chris@16
|
2021 friend class const_iterator;
|
Chris@16
|
2022 };
|
Chris@16
|
2023 #endif
|
Chris@16
|
2024
|
Chris@16
|
2025 BOOST_UBLAS_INLINE
|
Chris@16
|
2026 iterator begin () {
|
Chris@16
|
2027 return find (0);
|
Chris@16
|
2028 }
|
Chris@16
|
2029 BOOST_UBLAS_INLINE
|
Chris@16
|
2030 iterator end () {
|
Chris@16
|
2031 return find (size_);
|
Chris@16
|
2032 }
|
Chris@16
|
2033
|
Chris@16
|
2034 // Reverse iterator
|
Chris@16
|
2035 typedef reverse_iterator_base<const_iterator> const_reverse_iterator;
|
Chris@16
|
2036 typedef reverse_iterator_base<iterator> reverse_iterator;
|
Chris@16
|
2037
|
Chris@16
|
2038 BOOST_UBLAS_INLINE
|
Chris@16
|
2039 const_reverse_iterator rbegin () const {
|
Chris@16
|
2040 return const_reverse_iterator (end ());
|
Chris@16
|
2041 }
|
Chris@16
|
2042 BOOST_UBLAS_INLINE
|
Chris@16
|
2043 const_reverse_iterator rend () const {
|
Chris@16
|
2044 return const_reverse_iterator (begin ());
|
Chris@16
|
2045 }
|
Chris@16
|
2046 BOOST_UBLAS_INLINE
|
Chris@16
|
2047 reverse_iterator rbegin () {
|
Chris@16
|
2048 return reverse_iterator (end ());
|
Chris@16
|
2049 }
|
Chris@16
|
2050 BOOST_UBLAS_INLINE
|
Chris@16
|
2051 reverse_iterator rend () {
|
Chris@16
|
2052 return reverse_iterator (begin ());
|
Chris@16
|
2053 }
|
Chris@16
|
2054
|
Chris@16
|
2055 // Serialization
|
Chris@16
|
2056 template<class Archive>
|
Chris@16
|
2057 void serialize(Archive & ar, const unsigned int /* file_version */){
|
Chris@16
|
2058 serialization::collection_size_type s (size_);
|
Chris@16
|
2059 ar & serialization::make_nvp("size",s);
|
Chris@16
|
2060
|
Chris@16
|
2061 // copy the value back if loading
|
Chris@16
|
2062 if (Archive::is_loading::value) {
|
Chris@16
|
2063 if (s > N) bad_size("too large size in bounded_vector::load()\n").raise();
|
Chris@16
|
2064 size_ = s;
|
Chris@16
|
2065 }
|
Chris@16
|
2066 // ISSUE: this writes the full array
|
Chris@16
|
2067 ar & serialization::make_nvp("data",data_);
|
Chris@16
|
2068 }
|
Chris@16
|
2069
|
Chris@16
|
2070 private:
|
Chris@16
|
2071 size_type size_;
|
Chris@16
|
2072 array_type data_;
|
Chris@16
|
2073 };
|
Chris@16
|
2074
|
Chris@16
|
2075 }}}
|
Chris@16
|
2076
|
Chris@16
|
2077 #endif
|