Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkVariableLengthVector_h
00018 #define __itkVariableLengthVector_h
00019
00020 #include "itkMacro.h"
00021 #include "itkNumericTraits.h"
00022
00023 namespace itk
00024 {
00067 template <typename TValueType >
00068 class VariableLengthVector
00069 {
00070 public:
00071
00073 typedef TValueType ValueType;
00074 typedef TValueType ComponentType;
00075 typedef typename NumericTraits< ValueType >::RealType RealValueType;
00076 typedef VariableLengthVector Self;
00077
00079 typedef unsigned int ElementIdentifier;
00080
00083 VariableLengthVector();
00084
00086 VariableLengthVector(unsigned int dimension);
00087
00094 VariableLengthVector( ValueType* data, unsigned int sz,
00095 bool LetArrayManageMemory = false);
00096
00103 VariableLengthVector( const ValueType* data, unsigned int sz,
00104 bool LetArrayManageMemory = false);
00105
00106
00116 template< class T >
00117 VariableLengthVector(const VariableLengthVector< T > & v)
00118 {
00119 m_NumElements = v.Size();
00120 m_Data = this->AllocateElements(m_NumElements);
00121 m_LetArrayManageMemory = true;
00122 for( ElementIdentifier i=0; i< v.Size(); i++ )
00123 {
00124 this->m_Data[i] = static_cast< ValueType >( v[i] );
00125 }
00126 }
00128
00131 VariableLengthVector(const VariableLengthVector< TValueType > & v);
00132
00134 void Fill (TValueType const& v);
00135
00137 template< class T >
00138 const VariableLengthVector< TValueType > & operator=
00139 (const VariableLengthVector< T > & v)
00140 {
00141 if( m_Data == static_cast< void * >(const_cast< T * >
00142 ((const_cast< VariableLengthVector< T > & >(v)).GetDataPointer())) )
00143 {
00144 return *this;
00145 }
00146 this->SetSize( v.Size() );
00147 for( ElementIdentifier i=0; i< v.Size(); i++ )
00148 {
00149 this->m_Data[i] = static_cast< ValueType >( v[i] );
00150 }
00151 return *this;
00152 }
00154
00156 const Self & operator=(const Self & v);
00157
00159 inline unsigned int Size (void ) const
00160 { return m_NumElements; }
00161 inline unsigned int GetNumberOfElements(void) const
00162 { return m_NumElements; }
00164
00166 TValueType & operator[](unsigned int i) { return this->m_Data[i]; }
00167
00169 TValueType const & operator[](unsigned int i) const { return this->m_Data[i]; }
00170
00172 inline const TValueType & GetElement( unsigned int i ) const
00173 { return m_Data[i]; }
00174
00176 void SetElement( unsigned int i, const TValueType & value )
00177 { m_Data[i] = value; }
00178
00189 void SetSize(unsigned int sz, bool destroyExistingData=true);
00190 inline unsigned int GetSize(void) const
00191 { return m_NumElements; }
00193
00199 void SetData(TValueType* data,bool LetArrayManageMemory = false);
00200
00210 void SetData(TValueType* data, unsigned int sz, bool LetArrayManageMemory = false);
00211
00212
00215 ~VariableLengthVector();
00216
00217
00224 void Reserve( ElementIdentifier );
00225
00227 TValueType * AllocateElements( ElementIdentifier size ) const;
00228
00229 const TValueType* GetDataPointer() const { return m_Data; }
00230
00235 template< class T >
00236 inline Self operator+(const VariableLengthVector< T > &v) const
00237 {
00238
00239
00240
00241
00242
00243 const ElementIdentifier length = v.Size();
00244 Self result( length );
00245 for( ElementIdentifier i=0; i< length; i++ )
00246 {
00247 result[i] = (*this)[i] + static_cast< ValueType >( v[i] );
00248 }
00249 return result;
00250 }
00251 template< class T >
00252 inline Self operator-(const VariableLengthVector< T > &v) const
00253 {
00254
00255
00256
00257
00258
00259 const ElementIdentifier length = v.Size();
00260 Self result( length );
00261 for( ElementIdentifier i=0; i< length; i++ )
00262 {
00263 result[i] = (*this)[i] - static_cast< ValueType >( v[i] );
00264 }
00265 return result;
00266 }
00267 template< class T > inline Self operator*( T s ) const
00268 {
00269 Self result( m_NumElements );
00270 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00271 {
00272 result[i] = m_Data[i] * static_cast< ValueType >( s );
00273 }
00274 return result;
00275 }
00276 template< class T > inline Self operator/( T s ) const
00277 {
00278 Self result( m_NumElements );
00279 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00280 {
00281 result[i] = static_cast< ValueType >(
00282 static_cast< RealValueType >(m_Data[i]) /
00283 static_cast< RealValueType >( s ));
00284 }
00285 return result;
00286 }
00287 inline Self operator+( TValueType s ) const
00288 {
00289 Self result( m_NumElements );
00290 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00291 {
00292 result[i] = m_Data[i] + s;
00293 }
00294 return result;
00295 }
00296 inline Self operator-( TValueType s ) const
00297 {
00298 Self result( m_NumElements );
00299 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00300 {
00301 result[i] = m_Data[i] - s;
00302 }
00303 return result;
00304 }
00305 inline Self& operator--()
00306 {
00307 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00308 {
00309 this->m_Data[i] -= static_cast< ValueType >( 1.0 );
00310 }
00311 return *this;
00312 }
00313 inline Self& operator++()
00314 {
00315 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00316 {
00317 this->m_Data[i] += static_cast< ValueType >( 1.0 );
00318 }
00319 return *this;
00320 }
00321 inline Self operator--(int)
00322 {
00323 Self tmp(*this);
00324 --tmp;
00325 return tmp;
00326 }
00327 inline Self operator++(int)
00328 {
00329 Self tmp(*this);
00330 ++tmp;
00331 return tmp;
00332 }
00333 template< class T > inline Self& operator-=
00334 ( const VariableLengthVector< T > &v )
00335 {
00336 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00337 {
00338 m_Data[i] -= static_cast< ValueType >( v[i] );
00339 }
00340 return *this;
00341 }
00342 inline Self& operator-=( TValueType s )
00343 {
00344 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00345 {
00346 m_Data[i] -= s;
00347 }
00348 return *this;
00349 }
00350 template< class T > inline Self& operator+=
00351 ( const VariableLengthVector< T > &v )
00352 {
00353 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00354 {
00355 m_Data[i] += static_cast< ValueType >( v[i] );
00356 }
00357 return *this;
00358 }
00359 inline Self& operator+=( TValueType s )
00360 {
00361 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00362 {
00363 m_Data[i] += s;
00364 }
00365 return *this;
00366 }
00367 template< class T > inline Self& operator*=( T s )
00368 {
00369 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00370 {
00371 m_Data[i] *= (static_cast< ValueType >( s ));
00372 }
00373 return *this;
00374 }
00375 template< class T > inline Self& operator/=( T s )
00376 {
00377 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00378 {
00379 m_Data[i] = static_cast< ValueType >(
00380 static_cast< RealValueType >(m_Data[i]) /
00381 static_cast< RealValueType >( s ));
00382 }
00383 return *this;
00384 }
00385 Self & operator- ();
00386 bool operator==( const Self &v) const;
00387 bool operator!=( const Self &v) const;
00389
00391 RealValueType GetNorm() const;
00392
00394 RealValueType GetSquaredNorm() const;
00395
00396 private:
00397
00398 bool m_LetArrayManageMemory;
00399 TValueType * m_Data;
00400 ElementIdentifier m_NumElements;
00401 };
00402
00406 template< class TValueType, class T >
00407 inline
00408 VariableLengthVector<TValueType>
00409 operator*(const T &scalar, const VariableLengthVector<TValueType> &v)
00410 {
00411 return v * scalar;
00412 }
00413
00414
00415 template <typename TValueType >
00416 std::ostream & operator<<(std::ostream &os, const VariableLengthVector<TValueType> &arr)
00417 {
00418 const unsigned int length = arr.Size();
00419 const signed int last = (unsigned int) length - 1;
00420
00421 os << "[";
00422 for (signed int i=0; i < last; ++i)
00423 {
00424 os << arr[i] << ", ";
00425 }
00426 if (length >= 1)
00427 {
00428 os << arr[last];
00429 }
00430 os << "]";
00431 return os;
00432 }
00433
00434 }
00435
00436 #include "itkNumericTraitsVariableLengthVectorPixel.h"
00437
00438
00439 #define ITK_TEMPLATE_VariableLengthVector(_, EXPORT, x, y) namespace itk { \
00440 _(1(class EXPORT VariableLengthVector< ITK_TEMPLATE_1 x >)) \
00441 namespace Templates { typedef VariableLengthVector< ITK_TEMPLATE_1 x > VariableLengthVector##y; } \
00442 }
00443
00444 #if ITK_TEMPLATE_EXPLICIT
00445 # include "Templates/itkVariableLengthVector+-.h"
00446 #endif
00447
00448 #if ITK_TEMPLATE_TXX
00449 # include "itkVariableLengthVector.txx"
00450 #endif
00451
00452 #endif
00453