Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

itkVariableLengthVector.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkVariableLengthVector.h,v $
00005   Language:  C++
00006   Date:      $Date: 2008-07-17 11:50:28 $
00007   Version:   $Revision: 1.14 $
00008 
00009   Copyright (c) Insight Software Consortium. All rights reserved.
00010   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even 
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00014      PURPOSE.  See the above copyright notices for more information.
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     // if( m_NumElements != v.GetSize() ) 
00239     //   { 
00240     //   itkGenericExceptionMacro( << "Cannot add VariableLengthVector of length " 
00241     //                             << m_NumElements " and " << v.GetSize() );
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     // if( m_NumElements != v.GetSize() ) 
00255     //   { 
00256     //   itkGenericExceptionMacro( << "Cannot add VariableLengthVector of length " 
00257     //                             << m_NumElements " and " << v.GetSize() );
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++() // prefix operator ++v;
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) // postfix operator v--;
00322     {
00323       Self tmp(*this);
00324       --tmp;
00325       return tmp;
00326     }
00327   inline Self operator++(int) // postfix operator v++;
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- (); // negation 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; // if true, the array is responsible for memory 
00399                                // of data
00400   TValueType *m_Data; // Array to hold data
00401   ElementIdentifier m_NumElements;
00402 };
00403 
00407 template< class TValueType, class T >
00408 inline
00409 VariableLengthVector<TValueType>
00410 operator*(const T &scalar, const VariableLengthVector<TValueType> &v)
00411 {
00412   return v * scalar;
00413 }
00414 
00415   
00416 template <typename TValueType >
00417 std::ostream & operator<<(std::ostream &os, const VariableLengthVector<TValueType> &arr)
00418 {
00419   const unsigned int length = arr.Size();
00420   const signed int last   = (unsigned int) length - 1;
00421 
00422   os << "[";
00423   for (signed int i=0; i < last; ++i)
00424     {
00425     os << arr[i] << ", ";
00426     }
00427   if (length >= 1)
00428     {
00429     os << arr[last];
00430     }
00431   os << "]";
00432   return os;
00433 }
00434 
00435 } // namespace itk
00436 
00437 #include "itkNumericTraitsVariableLengthVectorPixel.h"
00438 
00439 // Define instantiation macro for this template.
00440 #define ITK_TEMPLATE_VariableLengthVector(_, EXPORT, x, y) namespace itk { \
00441   _(1(class EXPORT VariableLengthVector< ITK_TEMPLATE_1 x >)) \
00442   namespace Templates { typedef VariableLengthVector< ITK_TEMPLATE_1 x > VariableLengthVector##y; } \
00443   }
00444 
00445 #if ITK_TEMPLATE_EXPLICIT
00446 # include "Templates/itkVariableLengthVector+-.h"
00447 #endif
00448 
00449 #if ITK_TEMPLATE_TXX
00450 # include "itkVariableLengthVector.txx"
00451 #endif
00452 
00453 #endif
00454 

Generated at Sat Feb 28 13:43:26 2009 for ITK by doxygen 1.5.6 written by Dimitri van Heesch, © 1997-2000