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: 2004/02/03 21:20:20 $
00007   Version:   $Revision: 1.33 $
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   private:
00097     Iterator m_Iterator;
00098   };
00099   
00101   class ConstReverseIterator
00102   {
00103   public:
00104     explicit ConstReverseIterator(ConstIterator i): m_Iterator(i) {}
00105     ConstIterator operator++()         { return --m_Iterator; }
00106     ConstIterator operator++(int)      { return m_Iterator--; }
00107     ConstIterator operator--()         { return ++m_Iterator; }
00108     ConstIterator operator--(int)      { return m_Iterator++; }
00109     ConstIterator operator->() const   { return (m_Iterator-1); }
00110     const ValueType& operator*() const { return *(m_Iterator-1); }
00111   private:
00112     ConstIterator m_Iterator;
00113   };  
00114   
00116   typedef ValueType*        pointer;
00117 
00119   typedef const ValueType*  const_pointer;
00120 
00122   typedef ValueType&        reference;
00123 
00125   typedef const ValueType&  const_reference;
00126   
00127   typedef unsigned int   SizeType;
00128   
00129 public:
00131   FixedArray();
00132   FixedArray(const FixedArray& r);
00133   FixedArray(const ValueType r[VLength]);
00134 
00137   ~FixedArray();
00138   
00140   FixedArray& operator= (const FixedArray& r);
00141   FixedArray& operator= (const ValueType r[VLength]);
00142     
00146   bool operator==(const FixedArray& r ) const;
00147   bool operator!=(const FixedArray& r ) const
00148     { return !operator==(r); }
00149   
00153         reference operator[](short index)                { return m_InternalArray[index]; }
00154   const_reference operator[](short index) const          { return m_InternalArray[index]; }
00155         reference operator[](unsigned short index)       { return m_InternalArray[index]; }
00156   const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
00157         reference operator[](int index)                  { return m_InternalArray[index]; }
00158   const_reference operator[](int index) const            { return m_InternalArray[index]; }
00159         reference operator[](unsigned int index)         { return m_InternalArray[index]; }
00160   const_reference operator[](unsigned int index) const   { return m_InternalArray[index]; }
00161         reference operator[](long index)                 { return m_InternalArray[index]; }
00162   const_reference operator[](long index) const           { return m_InternalArray[index]; }
00163         reference operator[](unsigned long index)        { return m_InternalArray[index]; }
00164   const_reference operator[](unsigned long index) const  { return m_InternalArray[index]; }
00165   
00167   void SetElement( unsigned short index, const_reference value )
00168                                   { m_InternalArray[ index ] = value; }
00169   const_reference GetElement( unsigned short index ) const { return m_InternalArray[index]; }
00170   
00172   ValueType* GetDataPointer() { return m_InternalArray; }
00173   const ValueType* GetDataPointer() const { return m_InternalArray; }
00174     
00176   Iterator      Begin();
00177   ConstIterator Begin() const;
00178   Iterator      End();
00179   ConstIterator End() const;
00180   ReverseIterator      rBegin();
00181   ConstReverseIterator rBegin() const;
00182   ReverseIterator      rEnd();
00183   ConstReverseIterator rEnd() const;
00184   SizeType      Size() const;
00185   void Fill(const ValueType&);
00186     
00187 private:
00189   CArray  m_InternalArray;
00190   
00191 public:
00192  
00193   static FixedArray Filled(const ValueType&);
00194 };
00195   
00196 template <typename TValueType, unsigned int VLength>
00197 std::ostream & operator<<(std::ostream &os, const FixedArray<TValueType,VLength> &arr)
00198 {
00199   os << "[";
00200   if ( VLength == 1 )
00201     {
00202     os << arr[0] ;
00203     }
00204   else
00205     {
00206     for (int i=0; i < static_cast<int>(VLength) - 1; ++i)
00207       {
00208       os << arr[i] << ", ";
00209       }
00210     os << arr[VLength-1];
00211     }
00212   os << "]";
00213   return os;
00214 }
00215 
00216 } // namespace itk
00217 
00218 #ifdef _MSC_VER
00219 # pragma warning (pop)
00220 #endif
00221 
00222 #ifndef ITK_MANUAL_INSTANTIATION
00223 #include "itkFixedArray.txx"
00224 #endif
00225 
00226 #endif

Generated at Tue Mar 29 23:51:22 2005 for ITK by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2000