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: 2006/10/26 23:19:17 $
00007   Version:   $Revision: 1.11 $
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] = 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++() // prefix operator ++v;
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) // postfix operator v--;
00320     {
00321       Self tmp(*this);
00322       --tmp;
00323       return tmp;
00324     }
00325   inline Self operator++(int) // postfix operator v++;
00326     {
00327       Self tmp(*this);
00328       ++tmp;
00329       return tmp;
00330     }
00331    template< class T > inline Self& operator-=
00332                   ( const VariableLengthVector< T > &v )
00333     {
00334     for( ElementIdentifier i=0; i< m_NumElements; i++ )
00335       {
00336       m_Data[i] -= static_cast< ValueType >( v[i] );
00337       }
00338     return *this;
00339     }
00340   inline Self& operator-=( TValueType s )
00341     {
00342     for( ElementIdentifier i=0; i< m_NumElements; i++ )
00343       {
00344       m_Data[i] -= s ;
00345       }
00346     return *this;
00347     }
00348   template< class T > inline Self& operator+=
00349                   ( const VariableLengthVector< T > &v )
00350     {
00351     for( ElementIdentifier i=0; i< m_NumElements; i++ )
00352       {
00353       m_Data[i] += static_cast< ValueType >( v[i] );
00354       }
00355     return *this;
00356     }
00357   inline Self& operator+=( TValueType s )
00358     {
00359     for( ElementIdentifier i=0; i< m_NumElements; i++ )
00360       {
00361       m_Data[i] += s;
00362       }
00363     return *this;
00364     }
00365   template< class T > inline Self& operator*=( T s )
00366     {
00367     for( ElementIdentifier i=0; i< m_NumElements; i++ )
00368       {
00369       m_Data[i] *= (static_cast< ValueType >( s ));
00370       }
00371     return *this;
00372     }
00373   template< class T > inline Self& operator/=( T s )
00374     {
00375     for( ElementIdentifier i=0; i< m_NumElements; i++ )
00376       {
00377       m_Data[i] /= (static_cast< ValueType >( s ));
00378       }
00379     return *this;
00380     }
00381   Self & operator- (); // negation operator
00382   bool operator==( const Self &v) const;
00383   bool operator!=( const Self &v) const;
00385 
00387   RealValueType GetSquaredNorm() const;
00388 
00389 private:
00390 
00391   bool m_LetArrayManageMemory; // if true, the array is responsible for memory 
00392                                // of data
00393   TValueType *m_Data; // Array to hold data
00394   ElementIdentifier m_NumElements;
00395 };
00396 
00400 template< class TValueType, class T >
00401 inline
00402 VariableLengthVector<TValueType>
00403 operator*(const T &scalar, const VariableLengthVector<TValueType> &v)
00404 {
00405   return v * scalar;
00406 }
00407 
00408   
00409 template <typename TValueType >
00410 std::ostream & operator<<(std::ostream &os, const VariableLengthVector<TValueType> &arr)
00411 {
00412   const unsigned int length = arr.Size();
00413   const signed int last   = (unsigned int) length - 1;
00414 
00415   os << "[";
00416   for (signed int i=0; i < last; ++i)
00417     {
00418     os << arr[i] << ", ";
00419     }
00420   if (length >= 1)
00421     {
00422     os << arr[last];
00423     }
00424   os << "]";
00425   return os;
00426 }
00427 
00428 } // namespace itk
00429 
00430 #include "itkNumericTraitsVariableLengthVectorPixel.h"
00431 
00432 // Define instantiation macro for this template.
00433 #define ITK_TEMPLATE_VariableLengthVector(_, EXPORT, x, y) namespace itk { \
00434   _(1(class EXPORT VariableLengthVector< ITK_TEMPLATE_1 x >)) \
00435   namespace Templates { typedef VariableLengthVector< ITK_TEMPLATE_1 x > VariableLengthVector##y; } \
00436   }
00437 
00438 #if ITK_TEMPLATE_EXPLICIT
00439 # include "Templates/itkVariableLengthVector+-.h"
00440 #endif
00441 
00442 #if ITK_TEMPLATE_TXX
00443 # include "itkVariableLengthVector.txx"
00444 #endif
00445 
00446 #endif
00447 

Generated at Sun Sep 23 14:30:44 2007 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000