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

itkFixedArray.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Insight Segmentation & Registration Toolkit
00004   Module:    $RCSfile: itkFixedArray.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-11-28 09:15:37 $
00007   Version:   $Revision: 1.46 $
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 __itkFixedArray_h
00018 #define __itkFixedArray_h
00019 
00020 #include "itkMacro.h"
00021 
00022 #ifdef _MSC_VER
00023 # pragma warning (push)
00024 # pragma warning (disable: 4284) // operator-> returning pointer to non-aggregate
00025 #endif
00026 
00027 namespace itk
00028 {
00029 
00036 template <typename TVector>
00037 struct GetVectorDimension
00038 {
00039   itkStaticConstMacro(VectorDimension, unsigned int, TVector::Dimension);
00040 }; 
00041 
00042 
00043   
00063 template <typename TValueType, unsigned int VLength=3>
00064 class FixedArray
00065 {
00066 public:
00068   itkStaticConstMacro(Length, unsigned int, VLength);
00069 
00071   itkStaticConstMacro(Dimension, unsigned int, VLength);
00072 
00074   typedef TValueType  ValueType;
00075 
00077   typedef ValueType         CArray[VLength];
00078 
00080   typedef ValueType*        Iterator;
00081 
00083   typedef const ValueType*  ConstIterator;
00084 
00085   class ConstReverseIterator;
00086 
00089   class ReverseIterator
00090     {
00091     public:
00092       explicit ReverseIterator(Iterator i): m_Iterator(i) {}
00093       Iterator operator++()        { return --m_Iterator; }
00094       Iterator operator++(int)     { return m_Iterator--; }
00095       Iterator operator--()        { return ++m_Iterator; }
00096       Iterator operator--(int)     { return m_Iterator++; }
00097       Iterator operator->() const  { return (m_Iterator-1); }
00098       ValueType& operator*() const { return *(m_Iterator-1); }
00099       bool operator!=(const ReverseIterator &rit) const {return m_Iterator != rit.m_Iterator;}
00100       bool operator==(const ReverseIterator &rit) const {return m_Iterator == rit.m_Iterator;}
00101     private:
00102       Iterator m_Iterator;
00103       friend class ConstReverseIterator;
00104     };
00105   
00108   class ConstReverseIterator
00109     {
00110     public:
00111       explicit ConstReverseIterator(ConstIterator i): m_Iterator(i) {}
00112       ConstReverseIterator(const ReverseIterator& rit) { m_Iterator = rit.m_Iterator; }
00113       ConstIterator operator++()         { return --m_Iterator; }
00114       ConstIterator operator++(int)      { return m_Iterator--; }
00115       ConstIterator operator--()         { return ++m_Iterator; }
00116       ConstIterator operator--(int)      { return m_Iterator++; }
00117       ConstIterator operator->() const   { return (m_Iterator-1); }
00118       const ValueType& operator*() const { return *(m_Iterator-1); }
00119       bool operator!=(const ConstReverseIterator &rit) const {return m_Iterator != rit.m_Iterator;}
00120       bool operator==(const ConstReverseIterator &rit) const {return m_Iterator == rit.m_Iterator;}
00121     private:
00122       ConstIterator m_Iterator;
00123     };  
00124   
00126   typedef ValueType *        pointer;
00127 
00129   typedef const ValueType*  const_pointer;
00130 
00132   typedef ValueType&        reference;
00133 
00135   typedef const ValueType&  const_reference;
00136 
00137   typedef unsigned int   SizeType;
00138   
00139 public:
00141   FixedArray();
00142   FixedArray(const ValueType r[VLength]);
00143   FixedArray(const ValueType& r);
00145 
00147   template< class TFixedArrayValueType >
00148   FixedArray(const FixedArray< TFixedArrayValueType, VLength >& r)
00149     {
00150     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00151     Iterator i = this->Begin();
00152     while( i != this->End() ) 
00153       {
00154       *i++ = static_cast< TValueType >(*input++);
00155       }
00156     }
00158 
00159 
00174   template< class TFixedArrayValueType >
00175   FixedArray& operator= (const FixedArray< TFixedArrayValueType, VLength > & r)
00176     {
00177     if((void *)r.Begin() == (void *)m_InternalArray) return *this;
00178     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00179     Iterator i = this->Begin();
00180     while( i != this->End() ) 
00181       {
00182       *i++ = static_cast< TValueType >(*input++);
00183       }
00184     return *this;
00185     }
00187 
00188   FixedArray& operator= (const ValueType r[VLength]);
00189     
00193   bool operator==(const FixedArray& r ) const;
00194   bool operator!=(const FixedArray& r ) const
00195     { return !operator==(r); }
00197 
00201         reference operator[](short index)                { return m_InternalArray[index]; }
00202   const_reference operator[](short index) const          { return m_InternalArray[index]; }
00203         reference operator[](unsigned short index)       { return m_InternalArray[index]; }
00204   const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
00205         reference operator[](int index)                  { return m_InternalArray[index]; }
00206   const_reference operator[](int index) const            { return m_InternalArray[index]; }
00207         reference operator[](unsigned int index)         { return m_InternalArray[index]; }
00208   const_reference operator[](unsigned int index) const   { return m_InternalArray[index]; }
00209         reference operator[](long index)                 { return m_InternalArray[index]; }
00210   const_reference operator[](long index) const           { return m_InternalArray[index]; }
00211         reference operator[](unsigned long index)        { return m_InternalArray[index]; }
00212   const_reference operator[](unsigned long index) const  { return m_InternalArray[index]; }
00214 
00216   void SetElement( unsigned short index, const_reference value )
00217                                   { m_InternalArray[ index ] = value; }
00218   const_reference GetElement( unsigned short index ) const { return m_InternalArray[index]; }
00220 
00222   ValueType* GetDataPointer() { return m_InternalArray; }
00223   const ValueType* GetDataPointer() const { return m_InternalArray; }
00225 
00227   Iterator      Begin();
00228   ConstIterator Begin() const;
00229   Iterator      End();
00230   ConstIterator End() const;
00231   ReverseIterator      rBegin();
00232   ConstReverseIterator rBegin() const;
00233   ReverseIterator      rEnd();
00234   ConstReverseIterator rEnd() const;
00235   SizeType      Size() const;
00236   void Fill(const ValueType&);
00238 
00239 private:
00241   CArray  m_InternalArray;
00242 
00243 public:
00244  
00245   static FixedArray Filled(const ValueType&);
00246 };
00247   
00248 template <typename TValueType, unsigned int VLength>
00249 std::ostream & operator<<(std::ostream &os, const FixedArray<TValueType,VLength> &arr);
00250 
00251 } // namespace itk
00252 
00253 #ifdef _MSC_VER
00254 # pragma warning (pop)
00255 #endif
00256 
00257 // Define instantiation macro for this template.
00258 #define ITK_TEMPLATE_FixedArray(_, EXPORT, x, y) namespace itk { \
00259   _(2(class EXPORT FixedArray< ITK_TEMPLATE_2 x >)) \
00260   _(1(EXPORT std::ostream& operator<<(std::ostream&, \
00261                                       const FixedArray< ITK_TEMPLATE_2 x >&))) \
00262   namespace Templates { typedef FixedArray< ITK_TEMPLATE_2 x > FixedArray##y; } \
00263   }
00264 
00265 #if ITK_TEMPLATE_EXPLICIT
00266 # include "Templates/itkFixedArray+-.h"
00267 #endif
00268 
00269 #if ITK_TEMPLATE_TXX
00270 # include "itkFixedArray.txx"
00271 #endif
00272 
00273 #include "itkNumericTraitsFixedArrayPixel.h"
00274 
00275 #endif
00276 

Generated at Mon Jul 12 2010 18:20:32 for ITK by doxygen 1.7.1 written by Dimitri van Heesch, © 1997-2000