Orfeo Toolbox  3.16
itkVariableLengthVector.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkVariableLengthVector.h,v $
5  Language: C++
6  Date: $Date: 2009-05-25 08:34:20 $
7  Version: $Revision: 1.16 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkVariableLengthVector_h
18 #define __itkVariableLengthVector_h
19 
20 #include "itkMacro.h"
21 #include "itkNumericTraits.h"
22 
23 namespace itk
24 {
67 template <typename TValueType >
69 {
70 public:
71 
73  typedef TValueType ValueType;
74  typedef TValueType ComponentType;
77 
79  typedef unsigned int ElementIdentifier;
80 
84 
86  VariableLengthVector(unsigned int dimension);
87 
94  VariableLengthVector( ValueType* data, unsigned int sz,
95  bool LetArrayManageMemory = false);
96 
103  VariableLengthVector( const ValueType* data, unsigned int sz,
104  bool LetArrayManageMemory = false);
105 
106 
116  template< class T >
118  {
119  m_NumElements = v.Size();
121  m_LetArrayManageMemory = true;
122  for( ElementIdentifier i=0; i< v.Size(); i++ )
123  {
124  this->m_Data[i] = static_cast< ValueType >( v[i] );
125  }
126  }
127 
131 
133  void Fill (TValueType const& v);
134 
136  template< class T >
137  const VariableLengthVector< TValueType > & operator=
139  {
140  if( m_Data == static_cast< void * >(const_cast< T * >
141  ((const_cast< VariableLengthVector< T > & >(v)).GetDataPointer())) )
142  {
143  return *this;
144  }
145  this->SetSize( v.Size() );
146  for( ElementIdentifier i=0; i< v.Size(); i++ )
147  {
148  this->m_Data[i] = static_cast< ValueType >( v[i] );
149  }
150  return *this;
151  }
152 
154  const Self & operator=(const Self & v);
155 
157  inline unsigned int Size (void ) const
158  { return m_NumElements; }
159  inline unsigned int GetNumberOfElements(void) const
160  { return m_NumElements; }
161 
163  TValueType & operator[](unsigned int i) { return this->m_Data[i]; }
165  TValueType const & operator[](unsigned int i) const { return this->m_Data[i]; }
166 
168  inline const TValueType & GetElement( unsigned int i ) const
169  { return m_Data[i]; }
170 
172  void SetElement( unsigned int i, const TValueType & value )
173  { m_Data[i] = value; }
174 
185  void SetSize(unsigned int sz, bool destroyExistingData=true);
186  inline unsigned int GetSize(void) const
187  { return m_NumElements; }
188 
194  void SetData(TValueType* data,bool LetArrayManageMemory = false);
195 
205  void SetData(TValueType* data, unsigned int sz, bool LetArrayManageMemory = false);
206 
207 
211 
212 
219  void Reserve( ElementIdentifier );
220 
222  TValueType * AllocateElements( ElementIdentifier size ) const;
223 
224  const TValueType* GetDataPointer() const { return m_Data; }
225 
230  template< class T >
231  inline Self operator+(const VariableLengthVector< T > &v) const
232  {
233  // if( m_NumElements != v.GetSize() )
234  // {
235  // itkGenericExceptionMacro( << "Cannot add VariableLengthVector of length "
236  // << m_NumElements " and " << v.GetSize() );
237  // }
238  const ElementIdentifier length = v.Size();
239  Self result( length );
240  for( ElementIdentifier i=0; i< length; i++ )
241  {
242  result[i] = (*this)[i] + static_cast< ValueType >( v[i] );
243  }
244  return result;
245  }
246  template< class T >
247  inline Self operator-(const VariableLengthVector< T > &v) const
248  {
249  // if( m_NumElements != v.GetSize() )
250  // {
251  // itkGenericExceptionMacro( << "Cannot add VariableLengthVector of length "
252  // << m_NumElements " and " << v.GetSize() );
253  // }
254  const ElementIdentifier length = v.Size();
255  Self result( length );
256  for( ElementIdentifier i=0; i< length; i++ )
257  {
258  result[i] = (*this)[i] - static_cast< ValueType >( v[i] );
259  }
260  return result;
261  }
262  template< class T > inline Self operator*( T s ) const
263  {
264  Self result( m_NumElements );
265  for( ElementIdentifier i=0; i< m_NumElements; i++ )
266  {
267  result[i] = m_Data[i] * static_cast< ValueType >( s );
268  }
269  return result;
270  }
271  template< class T > inline Self operator/( T s ) const
272  {
273  Self result( m_NumElements );
274  for( ElementIdentifier i=0; i< m_NumElements; i++ )
275  {
276  result[i] = static_cast< ValueType >(
277  static_cast< RealValueType >(m_Data[i]) /
278  static_cast< RealValueType >( s ));
279  }
280  return result;
281  }
282  inline Self operator+( TValueType s ) const
283  {
284  Self result( m_NumElements );
285  for( ElementIdentifier i=0; i< m_NumElements; i++ )
286  {
287  result[i] = m_Data[i] + s;
288  }
289  return result;
290  }
291  inline Self operator-( TValueType s ) const
292  {
293  Self result( m_NumElements );
294  for( ElementIdentifier i=0; i< m_NumElements; i++ )
295  {
296  result[i] = m_Data[i] - s;
297  }
298  return result;
299  }
300  inline Self& operator--()
301  {
302  for( ElementIdentifier i=0; i< m_NumElements; i++ )
303  {
304  this->m_Data[i] -= static_cast< ValueType >( 1.0 );
305  }
306  return *this;
307  }
308  inline Self& operator++() // prefix operator ++v;
309  {
310  for( ElementIdentifier i=0; i< m_NumElements; i++ )
311  {
312  this->m_Data[i] += static_cast< ValueType >( 1.0 );
313  }
314  return *this;
315  }
316  inline Self operator--(int) // postfix operator v--;
317  {
318  Self tmp(*this);
319  --tmp;
320  return tmp;
321  }
322  inline Self operator++(int) // postfix operator v++;
323  {
324  Self tmp(*this);
325  ++tmp;
326  return tmp;
327  }
328  template< class T > inline Self& operator-=
330  {
331  for( ElementIdentifier i=0; i< m_NumElements; i++ )
332  {
333  m_Data[i] -= static_cast< ValueType >( v[i] );
334  }
335  return *this;
336  }
337  inline Self& operator-=( TValueType s )
338  {
339  for( ElementIdentifier i=0; i< m_NumElements; i++ )
340  {
341  m_Data[i] -= s;
342  }
343  return *this;
344  }
345  template< class T > inline Self& operator+=
347  {
348  for( ElementIdentifier i=0; i< m_NumElements; i++ )
349  {
350  m_Data[i] += static_cast< ValueType >( v[i] );
351  }
352  return *this;
353  }
354  inline Self& operator+=( TValueType s )
355  {
356  for( ElementIdentifier i=0; i< m_NumElements; i++ )
357  {
358  m_Data[i] += s;
359  }
360  return *this;
361  }
362  template< class T > inline Self& operator*=( T s )
363  {
364  for( ElementIdentifier i=0; i< m_NumElements; i++ )
365  {
366  m_Data[i] *= (static_cast< ValueType >( s ));
367  }
368  return *this;
369  }
370  template< class T > inline Self& operator/=( T s )
371  {
372  for( ElementIdentifier i=0; i< m_NumElements; i++ )
373  {
374  m_Data[i] = static_cast< ValueType >(
375  static_cast< RealValueType >(m_Data[i]) /
376  static_cast< RealValueType >( s ));
377  }
378  return *this;
379  }
380  Self & operator- (); // negation operator
381  bool operator==( const Self &v) const;
382  bool operator!=( const Self &v) const;
383 
385  RealValueType GetNorm() const;
386 
389 
390 private:
391 
392  bool m_LetArrayManageMemory; // if true, the array is responsible for memory of data
393  TValueType * m_Data; // Array to hold data
395 };
396 
400 template< class TValueType, class T >
401 inline
403 operator*(const T &scalar, const VariableLengthVector<TValueType> &v)
404 {
405  return v * scalar;
406 }
407 
408 
409 template <typename TValueType >
410 std::ostream & operator<<(std::ostream &os, const VariableLengthVector<TValueType> &arr)
411 {
412  const unsigned int length = arr.Size();
413  const signed int last = (unsigned int) length - 1;
414 
415  os << "[";
416  for (signed int i=0; i < last; ++i)
417  {
418  os << arr[i] << ", ";
419  }
420  if (length >= 1)
421  {
422  os << arr[last];
423  }
424  os << "]";
425  return os;
426 }
427 
428 } // namespace itk
429 
430 #include "itkNumericTraitsVariableLengthVectorPixel.h"
431 
432 // Define instantiation macro for this template.
433 #define ITK_TEMPLATE_VariableLengthVector(_, EXPORT, x, y) namespace itk { \
434  _(1(class EXPORT VariableLengthVector< ITK_TEMPLATE_1 x >)) \
435  namespace Templates { typedef VariableLengthVector< ITK_TEMPLATE_1 x > VariableLengthVector##y; } \
436  }
437 
438 #if ITK_TEMPLATE_EXPLICIT
439 # include "Templates/itkVariableLengthVector+-.h"
440 #endif
441 
442 #if ITK_TEMPLATE_TXX
443 # include "itkVariableLengthVector.txx"
444 #endif
445 
446 #endif

Generated at Sun May 19 2013 00:11:49 for Orfeo Toolbox with doxygen 1.8.3.1