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: 2008-06-07 15:10:48 $
00007   Version:   $Revision: 1.41 $
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 
00164   template< class TFixedArrayValueType >
00165   FixedArray& operator= (const FixedArray< TFixedArrayValueType, VLength > & r)
00166     {
00167     if((void *)r.Begin() == (void *)m_InternalArray) return *this;
00168     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00169     for(Iterator i = this->Begin() ; i != this->End() ;) 
00170       *i++ = static_cast< TValueType >(*input++);
00171     return *this;
00172     }
00174 
00175   FixedArray& operator= (const ValueType r[VLength]);
00176     
00180   bool operator==(const FixedArray& r ) const;
00181   bool operator!=(const FixedArray& r ) const
00182     { return !operator==(r); }
00184 
00188         reference operator[](short index)                { return m_InternalArray[index]; }
00189   const_reference operator[](short index) const          { return m_InternalArray[index]; }
00190         reference operator[](unsigned short index)       { return m_InternalArray[index]; }
00191   const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
00192         reference operator[](int index)                  { return m_InternalArray[index]; }
00193   const_reference operator[](int index) const            { return m_InternalArray[index]; }
00194         reference operator[](unsigned int index)         { return m_InternalArray[index]; }
00195   const_reference operator[](unsigned int index) const   { return m_InternalArray[index]; }
00196         reference operator[](long index)                 { return m_InternalArray[index]; }
00197   const_reference operator[](long index) const           { return m_InternalArray[index]; }
00198         reference operator[](unsigned long index)        { return m_InternalArray[index]; }
00199   const_reference operator[](unsigned long index) const  { return m_InternalArray[index]; }
00201 
00203   void SetElement( unsigned short index, const_reference value )
00204                                   { m_InternalArray[ index ] = value; }
00205   const_reference GetElement( unsigned short index ) const { return m_InternalArray[index]; }
00207 
00209   ValueType* GetDataPointer() { return m_InternalArray; }
00210   const ValueType* GetDataPointer() const { return m_InternalArray; }
00212 
00214   Iterator      Begin();
00215   ConstIterator Begin() const;
00216   Iterator      End();
00217   ConstIterator End() const;
00218   ReverseIterator      rBegin();
00219   ConstReverseIterator rBegin() const;
00220   ReverseIterator      rEnd();
00221   ConstReverseIterator rEnd() const;
00222   SizeType      Size() const;
00223   void Fill(const ValueType&);
00225 
00226 private:
00228   CArray  m_InternalArray;
00229 
00230 public:
00231  
00232   static FixedArray Filled(const ValueType&);
00233 };
00234   
00235 template <typename TValueType, unsigned int VLength>
00236 std::ostream & operator<<(std::ostream &os, const FixedArray<TValueType,VLength> &arr);
00237 
00238 } // namespace itk
00239 
00240 #ifdef _MSC_VER
00241 # pragma warning (pop)
00242 #endif
00243 
00244 // Define instantiation macro for this template.
00245 #define ITK_TEMPLATE_FixedArray(_, EXPORT, x, y) namespace itk { \
00246   _(2(class EXPORT FixedArray< ITK_TEMPLATE_2 x >)) \
00247   _(1(EXPORT std::ostream& operator<<(std::ostream&, \
00248                                       const FixedArray< ITK_TEMPLATE_2 x >&))) \
00249   namespace Templates { typedef FixedArray< ITK_TEMPLATE_2 x > FixedArray##y; } \
00250   }
00251 
00252 #if ITK_TEMPLATE_EXPLICIT
00253 # include "Templates/itkFixedArray+-.h"
00254 #endif
00255 
00256 #if ITK_TEMPLATE_TXX
00257 # include "itkFixedArray.txx"
00258 #endif
00259 
00260 #include "itkNumericTraitsFixedArrayPixel.h"
00261 
00262 #endif
00263 

Generated at Tue Jul 29 20:02:54 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000