TooN
vector.hh
1 //-*- c++ -*-
2 //
3 // Copyright (C) 2009 Tom Drummond (twd20@cam.ac.uk),
4 // Ed Rosten (er258@cam.ac.uk)
5 
6 //All rights reserved.
7 //
8 //Redistribution and use in source and binary forms, with or without
9 //modification, are permitted provided that the following conditions
10 //are met:
11 //1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 //
17 //THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
18 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 //ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
21 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 //POSSIBILITY OF SUCH DAMAGE.
28 
29 
30 
31 namespace TooN {
32 
33 
125 template<int Size=Dynamic, typename Precision=DefaultPrecision, typename Base=Internal::VBase>
126 struct Vector : public Base::template VLayout<Size, Precision> {
127 protected:
128 public:
129 
133  static const int SizeParameter = Size;
134 
135  typedef typename Base::template VLayout<Size, Precision>::PointerType PointerType;
136  // sneaky hack: only one of these constructors will work with any given base
137  // class but they don't generate errors unless the user tries to use one of them
138  // although the error message may be less than helpful - maybe this can be changed?
139 
141 
142 
148  inline Vector(){}
149 
150  Vector(Vector&&) = default;
151  Vector(const Vector&) = default;
152 
156  explicit inline Vector(int size_in) : Base::template VLayout<Size, Precision>(size_in) {}
157 
164  explicit inline Vector(PointerType data) : Base::template VLayout<Size, Precision> (data) {}
165 
170  template<typename X=Precision>
171  inline Vector(std::initializer_list<Precision> init, typename std::enable_if<sizeof(X)&&(Size <0)>::type* = 0)
172  :Base::template VLayout<Size, Precision> (init)
173  {}
174 
175 
180  template<typename Precision2, int Size2>
181  inline Vector(const Precision2(&init)[Size2])
182  :Base::template VLayout<Size, Precision> (init)
183  {}
184 
191  inline Vector(PointerType data, int size_in) : Base::template VLayout<Size, Precision> (data, size_in) {}
192 
194  inline Vector(PointerType data_in, int size_in, int stride_in, Internal::Slicing)
195  : Base::template VLayout<Size, Precision>(data_in, size_in, stride_in) {}
196 
197  using Base::template VLayout<Size, Precision>::size;
198  using Base::template VLayout<Size, Precision>::try_destructive_resize;
199 
205  template <class Op>
206  inline Vector(const Operator<Op>& op)
207  : Base::template VLayout<Size, Precision> (op)
208  {
209  op.eval(*this);
210  }
211 
212  // Copy construction is a very special case. Copy construction goes all the
213  // way down to the bottom. GenericVBase has no idea how to copy itself.
214  // However, the underlying allocator objects do. In the case of static sized
215  // objects, C++ automatically copies the data. For slice objects, C++ copies
216  // all parts (pointer and size), which is correct. For dynamically sized
217  // non-slice objects the copying has to be done by hand.
218 
219  // inline Vector(const Vector&from);
220 
222  template<int Size2, typename Precision2, typename Base2>
224  Base::template VLayout<Size, Precision>(from.size()) {
225  operator=(from);
226  }
227 
229 
230 #ifdef DOXYGEN_INCLUDE_ONLY_FOR_DOCS
231 
234 
245  Precision& operator[] (int i);
246 
250  const Precision& operator[] (int i) const;
251 
253 
254 #endif
255 
256 
259 
263  inline Vector& operator= (const Vector& from){
264  try_destructive_resize(from.size());
265  SizeMismatch<Size,Size>::test(size(), from.size());
266  const int s=size();
267  for(int i=0; i<s; i++){
268  (*this)[i]=from[i];
269  }
270  return *this;
271  }
272 
276  template<int Size2, typename Precision2, typename Base2>
278  try_destructive_resize(from.size());
279  SizeMismatch<Size,Size2>::test(size(), from.size());
280  const int s=size();
281  for(int i=0; i<s; i++){
282  (*this)[i]=from[i];
283  }
284  return *this;
285  }
286 
291  template <class Op>
292  inline Vector & operator=(const Operator<Op>& op){
293  try_destructive_resize(op);
294  op.eval(*this);
295  return *this;
296  }
298 
301 
303  Vector& operator/=(const Precision rhs) {
304  for(int i=0; i<size(); i++)
305  (*this)[i]/=rhs;
306  return *this;
307  }
308 
310  Vector& operator*=(const Precision rhs) {
311  for(int i=0; i<size(); i++)
312  (*this)[i]*=rhs;
313  return *this;
314  }
315 
317  template<int Size2, class Precision2, class Base2>
319  SizeMismatch<Size,Size2>::test(size(),rhs.size());
320  for(int i=0; i<size(); i++)
321  (*this)[i]+=rhs[i];
322  return *this;
323  }
324 
332  template<class Op>
334  {
335  op.plusequals(*this);
336  return *this;
337  }
338 
339  template<class Op>
340  Vector& operator-=(const Operator<Op>& op)
341  {
342  op.minusequals(*this);
343  return *this;
344  }
345 
347  template<int Size2, class Precision2, class Base2>
349  SizeMismatch<Size,Size2>::test(size(),rhs.size());
350  for(int i=0; i<size(); i++)
351  (*this)[i]-=rhs[i];
352  return *this;
353  }
354 
356 
359 
361  template<int Size2, class Precision2, class Base2>
363  SizeMismatch<Size,Size2>::test(size(),rhs.size());
364  for(int i=0; i<size(); i++)
365  if((*this)[i]!=rhs[i])
366  return 0;
367  return 1;
368  }
369 
371  template<int Size2, class Precision2, class Base2>
373  SizeMismatch<Size,Size2>::test(size(),rhs.size());
374  for(int i=0; i<size(); i++)
375  if((*this)[i]!=rhs[i])
376  return 1;
377  return 0;
378  }
379 
380 
381  template<class Op>
382  bool operator!=(const Operator<Op>& op)
383  {
384  return op.notequal(*this);
385  }
386 
388 
391 
394  {
395  return *this;
396  }
397 
398 #ifdef DOXYGEN_INCLUDE_ONLY_FOR_DOCS
399 
401  int size() const;
402 
411  void resize(int s);
412 
419  Precision* get_data_ptr();
420 
421 
422 
424 
426 
427 
438 
450 
462  DiagonalMatrix<Size,Precision> as_diagonal();
463 
475  template<Start, Length>
476  const Vector<Length,Precision>& slice() const;
477 
491  template<Start, Length>
492  Vector<Length,Precision>& slice();
493 
506  template<Start, Length>
507  const Vector<Length,Precision>& slice() const;
508 
522  template<Start, Length>
523  Vector<Length,Precision>& slice();
525 
526 #endif
527 
528 };
529 
530 }
Vector & operator*=(const Precision rhs)
multiply this vector by a constant
Definition: vector.hh:310
Vector & operator+=(const Vector< Size2, Precision2, Base2 > &rhs)
add another vector onto this one
Definition: vector.hh:318
bool operator==(const Vector< Size2, Precision2, Base2 > &rhs) const
Test for equality with another vector.
Definition: vector.hh:362
Vector()
Default constructor for vectors.
Definition: vector.hh:148
static const int SizeParameter
Value of template Size parameter.
Definition: vector.hh:133
Vector & operator-=(const Vector< Size2, Precision2, Base2 > &rhs)
subtract another vector from this one
Definition: vector.hh:348
Vector(const Vector< Size2, Precision2, Base2 > &from)
constructor from arbitrary vector
Definition: vector.hh:223
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
A vector.
Definition: vector.hh:126
Vector & operator=(const Vector &from)
operator = from copy A size mismatch is a fatal error, unless the destination is resizable.
Definition: vector.hh:263
A matrix.
Definition: matrix.hh:105
Vector(const Precision2(&init)[Size2])
Construct from an array by reference.
Definition: vector.hh:181
Vector(PointerType data_in, int size_in, int stride_in, Internal::Slicing)
internal constructor
Definition: vector.hh:194
Vector(std::initializer_list< Precision > init, typename std::enable_if< sizeof(X)&&(Size< 0)>::type *=0)
Construct from an initializer list.
Definition: vector.hh:171
A diagonal matrix.
Definition: diagmatrix.h:108
Definition: operators.hh:119
Vector(const Operator< Op > &op)
construction from Operator object
Definition: vector.hh:206
Vector(int size_in)
Constructor for dynamically-size vectors.
Definition: vector.hh:156
Vector & operator/=(const Precision rhs)
divide this vector by a constant
Definition: vector.hh:303
Vector & operator+=(const Operator< Op > &op)
add an Operator object onto this vector
Definition: vector.hh:333
bool operator!=(const Vector< Size2, Precision2, Base2 > &rhs) const
Test for inequality with another vector.
Definition: vector.hh:372
Vector(PointerType data)
Constructor used when constructing a vector which references other data, e.g.
Definition: vector.hh:164
Vector & ref()
return me as a non const reference - useful for temporaries
Definition: vector.hh:393
Definition: size_mismatch.hh:103
Vector & operator=(const Operator< Op > &op)
assignment from an Operator object Assignment from sized operators causes a resize of Resizable Vecto...
Definition: vector.hh:292
Vector(PointerType data, int size_in)
Constructor used when constructing a dynamic vector which references other data, e.g.
Definition: vector.hh:191
Definition: TooN.h:145