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: 2005/08/08 21:36:16 $
00007   Version:   $Revision: 1.36 $
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 
00086   class ReverseIterator
00087   {
00088   public:
00089     explicit ReverseIterator(Iterator i): m_Iterator(i) {}
00090     Iterator operator++()        { return --m_Iterator; }
00091     Iterator operator++(int)     { return m_Iterator--; }
00092     Iterator operator--()        { return ++m_Iterator; }
00093     Iterator operator--(int)     { return m_Iterator++; }
00094     Iterator operator->() const  { return (m_Iterator-1); }
00095     ValueType& operator*() const { return *(m_Iterator-1); }
00096     bool operator!=(const ReverseIterator &rit) const {return m_Iterator != rit.m_Iterator;};
00097     bool operator==(const ReverseIterator &rit) const {return m_Iterator == rit.m_Iterator;};
00098   private:
00099     Iterator m_Iterator;
00100   };
00101   
00103   class ConstReverseIterator
00104   {
00105   public:
00106     explicit ConstReverseIterator(ConstIterator i): m_Iterator(i) {}
00107     ConstIterator operator++()         { return --m_Iterator; }
00108     ConstIterator operator++(int)      { return m_Iterator--; }
00109     ConstIterator operator--()         { return ++m_Iterator; }
00110     ConstIterator operator--(int)      { return m_Iterator++; }
00111     ConstIterator operator->() const   { return (m_Iterator-1); }
00112     const ValueType& operator*() const { return *(m_Iterator-1); }
00113     bool operator!=(const ConstReverseIterator &rit) const {return m_Iterator != rit.m_Iterator;};
00114     bool operator==(const ConstReverseIterator &rit) const {return m_Iterator == rit.m_Iterator;};
00115   private:
00116     ConstIterator m_Iterator;
00117   };  
00118   
00120   typedef ValueType*        pointer;
00121 
00123   typedef const ValueType*  const_pointer;
00124 
00126   typedef ValueType&        reference;
00127 
00129   typedef const ValueType&  const_reference;
00130   
00131   typedef unsigned int   SizeType;
00132   
00133 public:
00135   FixedArray();
00136   FixedArray(const ValueType r[VLength]);
00137 
00139   template< class TFixedArrayValueType >
00140   FixedArray(const FixedArray< TFixedArrayValueType, VLength >& r)
00141     {
00142     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00143     for(Iterator i = this->Begin() ; i != this->End() ;) 
00144       *i++ = static_cast< TValueType >(*input++);
00145     }
00146   
00147 
00150   ~FixedArray();
00151   
00153   template< class TFixedArrayValueType >
00154   FixedArray& operator= (const FixedArray< TFixedArrayValueType, VLength > & r)
00155     {
00156     if((void *)r.Begin() == (void *)m_InternalArray) return *this;
00157     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00158     for(Iterator i = this->Begin() ; i != this->End() ;) 
00159       *i++ = static_cast< TValueType >(*input++);
00160     return *this;
00161     }
00162 
00163   FixedArray& operator= (const ValueType r[VLength]);
00164     
00168   bool operator==(const FixedArray& r ) const;
00169   bool operator!=(const FixedArray& r ) const
00170     { return !operator==(r); }
00171   
00175         reference operator[](short index)                { return m_InternalArray[index]; }
00176   const_reference operator[](short index) const          { return m_InternalArray[index]; }
00177         reference operator[](unsigned short index)       { return m_InternalArray[index]; }
00178   const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
00179         reference operator[](int index)                  { return m_InternalArray[index]; }
00180   const_reference operator[](int index) const            { return m_InternalArray[index]; }
00181         reference operator[](unsigned int index)         { return m_InternalArray[index]; }
00182   const_reference operator[](unsigned int index) const   { return m_InternalArray[index]; }
00183         reference operator[](long index)                 { return m_InternalArray[index]; }
00184   const_reference operator[](long index) const           { return m_InternalArray[index]; }
00185         reference operator[](unsigned long index)        { return m_InternalArray[index]; }
00186   const_reference operator[](unsigned long index) const  { return m_InternalArray[index]; }
00187   
00189   void SetElement( unsigned short index, const_reference value )
00190                                   { m_InternalArray[ index ] = value; }
00191   const_reference GetElement( unsigned short index ) const { return m_InternalArray[index]; }
00192   
00194   ValueType* GetDataPointer() { return m_InternalArray; }
00195   const ValueType* GetDataPointer() const { return m_InternalArray; }
00196     
00198   Iterator      Begin();
00199   ConstIterator Begin() const;
00200   Iterator      End();
00201   ConstIterator End() const;
00202   ReverseIterator      rBegin();
00203   ConstReverseIterator rBegin() const;
00204   ReverseIterator      rEnd();
00205   ConstReverseIterator rEnd() const;
00206   SizeType      Size() const;
00207   void Fill(const ValueType&);
00208     
00209 private:
00211   CArray  m_InternalArray;
00212   
00213 public:
00214  
00215   static FixedArray Filled(const ValueType&);
00216 };
00217   
00218 template <typename TValueType, unsigned int VLength>
00219 std::ostream & operator<<(std::ostream &os, const FixedArray<TValueType,VLength> &arr)
00220 {
00221   os << "[";
00222   if ( VLength == 1 )
00223     {
00224     os << arr[0] ;
00225     }
00226   else
00227     {
00228     for (int i=0; i < static_cast<int>(VLength) - 1; ++i)
00229       {
00230       os << arr[i] << ", ";
00231       }
00232     os << arr[VLength-1];
00233     }
00234   os << "]";
00235   return os;
00236 }
00237 
00238 } // namespace itk
00239 
00240 #ifdef _MSC_VER
00241 # pragma warning (pop)
00242 #endif
00243 
00244 #ifndef ITK_MANUAL_INSTANTIATION
00245 #include "itkFixedArray.txx"
00246 #endif
00247 
00248 #endif

Generated at Wed May 24 23:08:29 2006 for ITK by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2000