Orocos Real-Time Toolkit  2.9.0
MultiVector.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Thu Oct 10 16:21:19 CEST 2002 MultiVector.hpp
3 
4  MultiVector.hpp - description
5  -------------------
6  begin : Thu October 10 2002
7  copyright : (C) 2002 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #ifndef MULTIVECTOR_HPP
40 #define MULTIVECTOR_HPP
41 
42 #include "../rtt-config.h"
43 
44 #include <ostream>
45 #ifdef OS_HAVE_STREAMS
46 #include <sstream>
47 #endif
48 
49 #include <vector>
50 
51 #ifdef ORO_PRAGMA_INTERFACE
52 #pragma interface
53 #endif
54 
55 namespace RTT
56 { namespace extras {
57 
58 
71  template <unsigned S = 6, class T = double>
72  struct MultiVector
73  {
77  enum Size {size = S};
78 
83  typedef T DataType[ S ];
84 
89  DataType data;
90 
94  operator const T* () const
95  {
96  return data;
97  }
98 
102  operator T&()
103  {
104  return *data;
105  }
106 
113  MultiVector( const T d )
114  {
115  for ( unsigned int i = 0; i < S;++i )
116  data[ i ] = d;
117  }
118 
123  {
124  for ( unsigned int i = 0; i < S;++i )
125  data[ i ] = 0;
126  }
127 
134  template<class Alloc>
135  MultiVector(const std::vector<T,Alloc>& vect)
136  {
137  for ( unsigned int i = 0; i < S;++i )
138  if ( i < vect.size() )
139  data[ i ] = vect[i];
140  else
141  data[ i ] = 0;
142  }
143 
151  {
152  for ( unsigned int i = 0; i < S; ++i )
153  data[ i ] += d.data[ i ];
154 
155  return *this;
156  }
157 
165  {
166  for ( unsigned int i = 0; i < S; ++i )
167  data[ i ] *= d.data[ i ];
168 
169  return *this;
170  }
171 
178  MultiVector& operator *= ( const T d )
179  {
180  for ( unsigned int i = 0; i < S; ++i )
181  data[ i ] *= d;
182 
183  return *this;
184  }
185 
193  {
194  MultiVector tmp;
195 
196  for ( unsigned int i = 0; i < S; ++i )
197  tmp.data[ i ] = data[ i ] - d.data[ i ];
198 
199  return tmp;
200  }
201 
203  {
204  MultiVector tmp;
205 
206  for ( unsigned int i = 0; i < S; ++i )
207  tmp.data[ i ] = - data[ i ];
208 
209  return tmp;
210  }
211 
219  {
220  MultiVector tmp;
221 
222  for ( unsigned int i = 0; i < S; ++i )
223  tmp[ i ] = data[ i ] + d.data[ i ];
224 
225  return tmp;
226  }
227 
235  {
236  MultiVector tmp;
237 
238  for ( unsigned int i = 0; i < S; ++i )
239  tmp[ i ] = data[ i ] * d.data[ i ];
240 
241  return tmp;
242  }
243 
244  MultiVector operator / ( const T d ) const
245  {
246  MultiVector tmp;
247 
248  for ( unsigned int i = 0; i < S; ++i )
249  tmp[ i ] = data[ i ] / d;
250 
251  return tmp;
252  }
253 
260  MultiVector operator * ( const T d ) const
261  {
262  MultiVector tmp;
263 
264  for ( unsigned int i = 0; i < S; ++i )
265  tmp[ i ] = d * data[ i ];
266 
267  return tmp;
268  }
269 
278  {
279  for ( unsigned int i = 0; i < S; ++i )
280  data[ i ] = d.data[ i ];
281 
282  return *this;
283  }
284 
292  bool operator == ( const MultiVector& d )
293  {
294  for ( unsigned int i = 0; i < S; ++i )
295  if (data[ i ] != d.data[ i ])
296  return false;
297  return true;
298  }
299 
307  bool operator != ( const MultiVector& d )
308  {
309  for ( unsigned int i = 0; i < S; ++i )
310  if (data[ i ] != d.data[ i ])
311  return true;
312  return false;
313  }
314 
319  template<class Alloc>
320  void getVector( std::vector<T, Alloc>& vect ) const
321  {
322  vect.resize(S);
323  for ( unsigned int i = 0; i < S; ++i )
324  vect[i] = data[i];
325  }
326 
331  template<class Alloc>
332  bool setVector(const std::vector<T, Alloc>& vect )
333  {
334  if ( vect.size() != S )
335  return false;
336  for ( unsigned int i = 0; i < S; ++i )
337  data[i] = vect[i];
338  return true;
339  }
340 
341  /*
342  * This is just plain wrong since assignment with integer
343  * would call this method.
344  * Actually, it's the standard that's wrong for doing
345  * int == pointer
346  *
347  MultiVector& operator = (const DataType d)
348  {
349  // if (d != 0)
350  for (int i=0; i<S; ++i)
351  data[i] = d[i];
352  return *this;
353  }
354  *
355  */
356 
364  MultiVector& operator = ( const T d )
365  {
366  for ( unsigned int i = 0; i < S; ++i )
367  data[ i ] = d;
368 
369  return *this;
370  }
371 
378  //replaced int parameter with unsigned int to unconfuse MSVC
379  T& operator[] ( unsigned int i )
380  {
381  return data[ i ];
382  }
383  };
384 
385  template<unsigned S, typename D>
387  {
388  return v*d;
389  }
390 
391  template<unsigned S, typename D>
393  {
394  return v+d;
395  }
396 
397  template<unsigned S, typename D>
399  {
400  return v-d;
401  }
402 
403 
408 
413 
418 
423 
427  template <unsigned int S, class T>
428  std::ostream &operator<<(std::ostream &os, MultiVector<S,T> &q)
429  {
430 #ifdef OS_HAVE_STREAMS
431  std::stringstream ss;
432  ss << "(";
433  for (unsigned int i = 0; i < (S - 1) ; i++) {
434  ss << q[i] << ", ";
435  }
436  ss << q[ S - 1 ] << ")";
437  os << ss.str();
438 #endif
439  return os;
440  }
441 
445  template <unsigned int S, class T>
446  std::istream &operator>>(std::istream &os, MultiVector<S,T> &q)
447  {
448 #ifdef OS_HAVE_STREAMS
450  char c;
451  os >> c; // '('
452  for (unsigned int i = 0; i < (S - 1) ; i++) {
453  os >> p[i];
454  os >> c; // ','
455  os >> c; // ' '
456  }
457  os >> p[ S - 1 ];
458  os >> c; // ')';
459  if ( os )
460  q = p;
461 #endif
462  return os;
463  }
464 
465 }}
466 
467 #endif
MultiVector operator*(const MultiVector &d) const
Returns the vector product of this and another MultiVector.
T DataType[S]
You can use DataType if you want to refer to a pointer holding S elements.
Definition: MultiVector.hpp:83
MultiVector(const std::vector< T, Alloc > &vect)
Creates a vector from a std::vector.
bool operator!=(const MultiVector &d)
Compares elements of this MultiVector are different from elements of another MultiVector.
bool operator==(const MultiVector &d)
Compares all elements of this MultiVector to all elements of another MultiVector. ...
MultiVector operator-() const
MultiVector< 6, int > Int6D
A MultiVector consisting of 6 integers.
MultiVector()
Creates a vector with all elements initialized to zero.
void getVector(std::vector< T, Alloc > &vect) const
Return the contents of this MultiVector as an std::vector Resizes vect to the size of this MultiVecto...
MultiVector operator/(const T d) const
MultiVector & operator*=(const MultiVector &d)
Vector product of another MultiVector with this MultiVector.
MultiVector< 6, double > Double6D
A MultiVector consisting of 6 doubles.
MultiVector< 6, long > Long6D
A MultiVector consisting of 6 longs.
Size
The size of this MultiVector is stored in this enum.
Definition: MultiVector.hpp:77
MultiVector & operator=(const MultiVector &d)
Assigns all elements of this MultiVector to all elements of another MultiVector.
MultiVector operator+(const MultiVector &d) const
Returns the addition of this and another MultiVector.
std::istream & operator>>(std::istream &os, MultiVector< S, T > &q)
Output to standard C++ input stream.
MultiVector(const T d)
Creates a vector with all elements initialized to d.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
DataType data
The data contained in the MultiVector.
Definition: MultiVector.hpp:89
MultiVector & operator+=(const MultiVector &d)
Adds another MultiVector to this MultiVector.
A static allocated Vector.
Definition: MultiVector.hpp:72
bool setVector(const std::vector< T, Alloc > &vect)
Set the contents of this MultiVector from an std::vector.
MultiVector< 6, bool > Bool6D
A MultiVector consisting of 6 booleans.
T & operator[](unsigned int i)
Not checked index operator to the internal data.