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: 2006/04/20 14:54:09 $
00007   Version:   $Revision: 1.38 $
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]);
00138 
00140   template< class TFixedArrayValueType >
00141   FixedArray(const FixedArray< TFixedArrayValueType, VLength >& r)
00142     {
00143     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00144     for(Iterator i = this->Begin() ; i != this->End() ;) 
00145       *i++ = static_cast< TValueType >(*input++);
00146     }
00148 
00149 
00152   ~FixedArray();
00153 
00155   template< class TFixedArrayValueType >
00156   FixedArray& operator= (const FixedArray< TFixedArrayValueType, VLength > & r)
00157     {
00158     if((void *)r.Begin() == (void *)m_InternalArray) return *this;
00159     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00160     for(Iterator i = this->Begin() ; i != this->End() ;) 
00161       *i++ = static_cast< TValueType >(*input++);
00162     return *this;
00163     }
00165 
00166   FixedArray& operator= (const ValueType r[VLength]);
00167     
00171   bool operator==(const FixedArray& r ) const;
00172   bool operator!=(const FixedArray& r ) const
00173     { return !operator==(r); }
00175 
00179         reference operator[](short index)                { return m_InternalArray[index]; }
00180   const_reference operator[](short index) const          { return m_InternalArray[index]; }
00181         reference operator[](unsigned short index)       { return m_InternalArray[index]; }
00182   const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
00183         reference operator[](int index)                  { return m_InternalArray[index]; }
00184   const_reference operator[](int index) const            { return m_InternalArray[index]; }
00185         reference operator[](unsigned int index)         { return m_InternalArray[index]; }
00186   const_reference operator[](unsigned int index) const   { return m_InternalArray[index]; }
00187         reference operator[](long index)                 { return m_InternalArray[index]; }
00188   const_reference operator[](long index) const           { return m_InternalArray[index]; }
00189         reference operator[](unsigned long index)        { return m_InternalArray[index]; }
00190   const_reference operator[](unsigned long index) const  { return m_InternalArray[index]; }
00192 
00194   void SetElement( unsigned short index, const_reference value )
00195                                   { m_InternalArray[ index ] = value; }
00196   const_reference GetElement( unsigned short index ) const { return m_InternalArray[index]; }
00198 
00200   ValueType* GetDataPointer() { return m_InternalArray; }
00201   const ValueType* GetDataPointer() const { return m_InternalArray; }
00203 
00205   Iterator      Begin();
00206   ConstIterator Begin() const;
00207   Iterator      End();
00208   ConstIterator End() const;
00209   ReverseIterator      rBegin();
00210   ConstReverseIterator rBegin() const;
00211   ReverseIterator      rEnd();
00212   ConstReverseIterator rEnd() const;
00213   SizeType      Size() const;
00214   void Fill(const ValueType&);
00216 
00217 private:
00219   CArray  m_InternalArray;
00220 
00221 public:
00222  
00223   static FixedArray Filled(const ValueType&);
00224 };
00225   
00226 template <typename TValueType, unsigned int VLength>
00227 std::ostream & operator<<(std::ostream &os, const FixedArray<TValueType,VLength> &arr);
00228 
00229 } // namespace itk
00230 
00231 #ifdef _MSC_VER
00232 # pragma warning (pop)
00233 #endif
00234 
00235 // Define instantiation macro for this template.
00236 #define ITK_TEMPLATE_FixedArray(_, EXPORT, x, y) namespace itk { \
00237   _(2(class EXPORT FixedArray< ITK_TEMPLATE_2 x >)) \
00238   _(1(EXPORT std::ostream& operator<<(std::ostream&, \
00239                                       const FixedArray< ITK_TEMPLATE_2 x >&))) \
00240   namespace Templates { typedef FixedArray< ITK_TEMPLATE_2 x > FixedArray##y; } \
00241   }
00242 
00243 #if ITK_TEMPLATE_EXPLICIT
00244 # include "Templates/itkFixedArray+-.h"
00245 #endif
00246 
00247 #if ITK_TEMPLATE_TXX
00248 # include "itkFixedArray.txx"
00249 #endif
00250 
00251 #endif
00252 

Generated at Mon Mar 12 00:19:52 2007 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000