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() { 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] = m_Data[i] / (static_cast< ValueType >( s ));
00282 }
00283 return result;
00284 }
00285 inline Self operator+( TValueType s ) const
00286 {
00287 Self result( m_NumElements );
00288 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00289 {
00290 result[i] = m_Data[i] + s;
00291 }
00292 return result;
00293 }
00294 inline Self operator-( TValueType s ) const
00295 {
00296 Self result( m_NumElements );
00297 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00298 {
00299 result[i] = m_Data[i] - s;
00300 }
00301 return result;
00302 }
00303 inline Self& operator--()
00304 {
00305 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00306 {
00307 this->m_Data[i] -= static_cast< ValueType >( 1.0 );
00308 }
00309 return *this;
00310 }
00311 inline Self& operator++()
00312 {
00313 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00314 {
00315 this->m_Data[i] += static_cast< ValueType >( 1.0 );
00316 }
00317 return *this;
00318 }
00319 inline Self& operator--(int)
00320 {
00321 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00322 {
00323 this->m_Data[i] -= static_cast< ValueType >( 1.0 );
00324 }
00325 return *this;
00326 }
00327 inline Self& operator++(int)
00328 {
00329 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00330 {
00331 this->m_Data[i] += static_cast< ValueType >( 1.0 );
00332 }
00333 return *this;
00334 }
00335 template< class T > inline Self& operator-=
00336 ( const VariableLengthVector< T > &v )
00337 {
00338 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00339 {
00340 m_Data[i] -= static_cast< ValueType >( v[i] );
00341 }
00342 return *this;
00343 }
00344 inline Self& operator-=( TValueType s )
00345 {
00346 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00347 {
00348 m_Data[i] -= s ;
00349 }
00350 return *this;
00351 }
00352 template< class T > inline Self& operator+=
00353 ( const VariableLengthVector< T > &v )
00354 {
00355 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00356 {
00357 m_Data[i] += static_cast< ValueType >( v[i] );
00358 }
00359 return *this;
00360 }
00361 inline Self& operator+=( TValueType s )
00362 {
00363 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00364 {
00365 m_Data[i] += s;
00366 }
00367 return *this;
00368 }
00369 template< class T > inline Self& operator*=( T s )
00370 {
00371 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00372 {
00373 m_Data[i] *= (static_cast< ValueType >( s ));
00374 }
00375 return *this;
00376 }
00377 template< class T > inline Self& operator/=( T s )
00378 {
00379 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00380 {
00381 m_Data[i] /= (static_cast< ValueType >( 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 GetSquaredNorm() const;
00392
00393 private:
00394
00395 bool m_LetArrayManageMemory;
00396
00397 TValueType *m_Data;
00398 ElementIdentifier m_NumElements;
00399 };
00400
00404 template< class TValueType, class T >
00405 inline
00406 VariableLengthVector<TValueType>
00407 operator*(const T &scalar, const VariableLengthVector<TValueType> &v)
00408 {
00409 return v * scalar;
00410 }
00411
00412
00413 template <typename TValueType >
00414 std::ostream & operator<<(std::ostream &os, const VariableLengthVector<TValueType> &arr)
00415 {
00416 const unsigned int length = arr.Size();
00417 const signed int last = (unsigned int) length - 1;
00418
00419 os << "[";
00420 for (signed int i=0; i < last; ++i)
00421 {
00422 os << arr[i] << ", ";
00423 }
00424 if (length >= 1)
00425 {
00426 os << arr[last];
00427 }
00428 os << "]";
00429 return os;
00430 }
00431
00432 }
00433
00434 #include "itkNumericTraitsVariableLengthVectorPixel.h"
00435
00436 #ifndef ITK_MANUAL_INSTANTIATION
00437 #include "itkVariableLengthVector.txx"
00438 #endif
00439
00440 #endif
00441