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-07-09 18:06:39 $
00007   Version:   $Revision: 1.45 $
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]);
00144 
00146   template< class TFixedArrayValueType >
00147   FixedArray(const FixedArray< TFixedArrayValueType, VLength >& r)
00148     {
00149     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00150     Iterator i = this->Begin();
00151     while( i != this->End() ) 
00152       {
00153       *i++ = static_cast< TValueType >(*input++);
00154       }
00155     }
00157 
00158 
00173   template< class TFixedArrayValueType >
00174   FixedArray& operator= (const FixedArray< TFixedArrayValueType, VLength > & r)
00175     {
00176     if((void *)r.Begin() == (void *)m_InternalArray) return *this;
00177     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00178     Iterator i = this->Begin();
00179     while( i != this->End() ) 
00180       {
00181       *i++ = static_cast< TValueType >(*input++);
00182       }
00183     return *this;
00184     }
00186 
00187   FixedArray& operator= (const ValueType r[VLength]);
00188     
00192   bool operator==(const FixedArray& r ) const;
00193   bool operator!=(const FixedArray& r ) const
00194     { return !operator==(r); }
00196 
00200         reference operator[](short index)                { return m_InternalArray[index]; }
00201   const_reference operator[](short index) const          { return m_InternalArray[index]; }
00202         reference operator[](unsigned short index)       { return m_InternalArray[index]; }
00203   const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
00204         reference operator[](int index)                  { return m_InternalArray[index]; }
00205   const_reference operator[](int index) const            { return m_InternalArray[index]; }
00206         reference operator[](unsigned int index)         { return m_InternalArray[index]; }
00207   const_reference operator[](unsigned int index) const   { return m_InternalArray[index]; }
00208         reference operator[](long index)                 { return m_InternalArray[index]; }
00209   const_reference operator[](long index) const           { return m_InternalArray[index]; }
00210         reference operator[](unsigned long index)        { return m_InternalArray[index]; }
00211   const_reference operator[](unsigned long index) const  { return m_InternalArray[index]; }
00213 
00215   void SetElement( unsigned short index, const_reference value )
00216                                   { m_InternalArray[ index ] = value; }
00217   const_reference GetElement( unsigned short index ) const { return m_InternalArray[index]; }
00219 
00221   ValueType* GetDataPointer() { return m_InternalArray; }
00222   const ValueType* GetDataPointer() const { return m_InternalArray; }
00224 
00226   Iterator      Begin();
00227   ConstIterator Begin() const;
00228   Iterator      End();
00229   ConstIterator End() const;
00230   ReverseIterator      rBegin();
00231   ConstReverseIterator rBegin() const;
00232   ReverseIterator      rEnd();
00233   ConstReverseIterator rEnd() const;
00234   SizeType      Size() const;
00235   void Fill(const ValueType&);
00237 
00238 private:
00240   CArray  m_InternalArray;
00241 
00242 public:
00243  
00244   static FixedArray Filled(const ValueType&);
00245 };
00246   
00247 template <typename TValueType, unsigned int VLength>
00248 std::ostream & operator<<(std::ostream &os, const FixedArray<TValueType,VLength> &arr);
00249 
00250 } // namespace itk
00251 
00252 #ifdef _MSC_VER
00253 # pragma warning (pop)
00254 #endif
00255 
00256 // Define instantiation macro for this template.
00257 #define ITK_TEMPLATE_FixedArray(_, EXPORT, x, y) namespace itk { \
00258   _(2(class EXPORT FixedArray< ITK_TEMPLATE_2 x >)) \
00259   _(1(EXPORT std::ostream& operator<<(std::ostream&, \
00260                                       const FixedArray< ITK_TEMPLATE_2 x >&))) \
00261   namespace Templates { typedef FixedArray< ITK_TEMPLATE_2 x > FixedArray##y; } \
00262   }
00263 
00264 #if ITK_TEMPLATE_EXPLICIT
00265 # include "Templates/itkFixedArray+-.h"
00266 #endif
00267 
00268 #if ITK_TEMPLATE_TXX
00269 # include "itkFixedArray.txx"
00270 #endif
00271 
00272 #include "itkNumericTraitsFixedArrayPixel.h"
00273 
00274 #endif
00275 

Generated at Tue Sep 15 02:57:33 2009 for ITK by doxygen 1.5.8 written by Dimitri van Heesch, © 1997-2000