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
00022 namespace itk
00023 {
00066 template <typename TValueType >
00067 class VariableLengthVector
00068 {
00069 public:
00070
00072 typedef TValueType ValueType;
00073 typedef TValueType ComponentType;
00074 typedef VariableLengthVector Self;
00075
00077 typedef unsigned int ElementIdentifier;
00078
00081 VariableLengthVector();
00082
00084 VariableLengthVector(unsigned int dimension);
00085
00092 VariableLengthVector( ValueType* data, unsigned int sz,
00093 bool LetArrayManageMemory = false);
00094
00101 VariableLengthVector( const ValueType* data, unsigned int sz,
00102 bool LetArrayManageMemory = false);
00103
00104
00114 template< class T >
00115 VariableLengthVector(const VariableLengthVector< T > & v)
00116 {
00117 m_NumElements = v.Size();
00118 m_Data = this->AllocateElements(m_NumElements);
00119 m_LetArrayManageMemory = true;
00120 for( ElementIdentifier i=0; i< v.Size(); i++ )
00121 {
00122 this->m_Data[i] = static_cast< ValueType >( v[i] );
00123 }
00124 }
00125
00128 VariableLengthVector(const VariableLengthVector< TValueType > & v);
00129
00131 void Fill (TValueType const& v);
00132
00134 template< class T >
00135 const VariableLengthVector< TValueType > & operator=
00136 (const VariableLengthVector< T > & v)
00137 {
00138 if( m_Data == static_cast< void * >(const_cast< T * >
00139 ((const_cast< VariableLengthVector< T > & >(v)).GetDataPointer())) )
00140 {
00141 return *this;
00142 }
00143 this->SetSize( v.Size() );
00144 for( ElementIdentifier i=0; i< v.Size(); i++ )
00145 {
00146 this->m_Data[i] = static_cast< ValueType >( v[i] );
00147 }
00148 return *this;
00149 }
00150
00152 const Self & operator=(const Self & v);
00153
00155 inline unsigned int Size (void ) const
00156 { return m_NumElements; }
00157 inline unsigned int GetNumberOfElements(void) const
00158 { return m_NumElements; }
00159
00161 TValueType & operator[](unsigned int i) { return this->m_Data[i]; }
00163 TValueType const & operator[](unsigned int i) const { return this->m_Data[i]; }
00164
00166 inline const TValueType & GetElement( unsigned int i ) const
00167 { return m_Data[i]; }
00168
00170 void SetElement( unsigned int i, const TValueType & value )
00171 { m_Data[i] = value; }
00172
00183 void SetSize(unsigned int sz, bool destroyExistingData=true);
00184 inline unsigned int GetSize(void) const
00185 { return m_NumElements; }
00186
00192 void SetData(TValueType* data,bool LetArrayManageMemory = false);
00193
00203 void SetData(TValueType* data, unsigned int sz, bool LetArrayManageMemory = false);
00204
00205
00208 ~VariableLengthVector();
00209
00210
00217 void Reserve( ElementIdentifier );
00218
00220 TValueType * AllocateElements( ElementIdentifier size ) const;
00221
00222 const TValueType* GetDataPointer() { return m_Data; }
00223
00228 template< class T >
00229 inline Self operator+(const VariableLengthVector< T > &v) const
00230 {
00231
00232
00233
00234
00235
00236 const ElementIdentifier length = v.Size();
00237 Self result( length );
00238 for( ElementIdentifier i=0; i< length; i++ )
00239 {
00240 result[i] = (*this)[i] + static_cast< ValueType >( v[i] );
00241 }
00242 return result;
00243 }
00244 template< class T >
00245 inline Self operator-(const VariableLengthVector< T > &v) const
00246 {
00247
00248
00249
00250
00251
00252 const ElementIdentifier length = v.Size();
00253 Self result( length );
00254 for( ElementIdentifier i=0; i< length; i++ )
00255 {
00256 result[i] = (*this)[i] - static_cast< ValueType >( v[i] );
00257 }
00258 return result;
00259 }
00260 template< class T > inline Self operator*( T s ) const
00261 {
00262 Self result( m_NumElements );
00263 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00264 {
00265 result[i] = m_Data[i] * static_cast< ValueType >( s );
00266 }
00267 return result;
00268 }
00269 template< class T > inline Self operator/( T s ) const
00270 {
00271 Self result( m_NumElements );
00272 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00273 {
00274 result[i] = m_Data[i] / (static_cast< ValueType >( s ));
00275 }
00276 return result;
00277 }
00278 inline Self operator+( TValueType s ) const
00279 {
00280 Self result( m_NumElements );
00281 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00282 {
00283 result[i] = m_Data[i] + 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--()
00297 {
00298 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00299 {
00300 this->m_Data[i] -= static_cast< ValueType >( 1.0 );
00301 }
00302 return *this;
00303 }
00304 inline Self& operator++()
00305 {
00306 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00307 {
00308 this->m_Data[i] += static_cast< ValueType >( 1.0 );
00309 }
00310 return *this;
00311 }
00312 inline Self& operator--(int)
00313 {
00314 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00315 {
00316 this->m_Data[i] -= static_cast< ValueType >( 1.0 );
00317 }
00318 return *this;
00319 }
00320 inline Self& operator++(int)
00321 {
00322 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00323 {
00324 this->m_Data[i] += static_cast< ValueType >( 1.0 );
00325 }
00326 return *this;
00327 }
00328 template< class T > inline Self& operator-=
00329 ( const VariableLengthVector< T > &v )
00330 {
00331 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00332 {
00333 m_Data[i] -= static_cast< ValueType >( v[i] );
00334 }
00335 return *this;
00336 }
00337 inline Self& operator-=( TValueType s )
00338 {
00339 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00340 {
00341 m_Data[i] -= s ;
00342 }
00343 return *this;
00344 }
00345 template< class T > inline Self& operator+=
00346 ( const VariableLengthVector< T > &v )
00347 {
00348 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00349 {
00350 m_Data[i] += static_cast< ValueType >( v[i] );
00351 }
00352 return *this;
00353 }
00354 inline Self& operator+=( TValueType s )
00355 {
00356 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00357 {
00358 m_Data[i] += s;
00359 }
00360 return *this;
00361 }
00362 template< class T > inline Self& operator*=( T s )
00363 {
00364 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00365 {
00366 m_Data[i] *= (static_cast< ValueType >( s ));
00367 }
00368 return *this;
00369 }
00370 template< class T > inline Self& operator/=( T s )
00371 {
00372 for( ElementIdentifier i=0; i< m_NumElements; i++ )
00373 {
00374 m_Data[i] /= (static_cast< ValueType >( s ));
00375 }
00376 return *this;
00377 }
00378 Self & operator- ();
00379 bool operator==( const Self &v) const;
00380 bool operator!=( const Self &v) const;
00381
00382
00383
00384 private:
00385
00386 bool m_LetArrayManageMemory;
00387
00388 TValueType *m_Data;
00389 ElementIdentifier m_NumElements;
00390 };
00391
00392
00393
00394 template <typename TValueType >
00395 std::ostream & operator<<(std::ostream &os, const VariableLengthVector<TValueType> &arr)
00396 {
00397 const unsigned int length = arr.Size();
00398 const signed int last = (unsigned int) length - 1;
00399
00400 os << "[";
00401 for (signed int i=0; i < last; ++i)
00402 {
00403 os << arr[i] << ", ";
00404 }
00405 if (length >= 1)
00406 {
00407 os << arr[last];
00408 }
00409 os << "]";
00410 return os;
00411 }
00412
00413 }
00414
00415
00416 #ifndef ITK_MANUAL_INSTANTIATION
00417 #include "itkVariableLengthVector.txx"
00418 #endif
00419
00420 #endif