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-10-16 13:28:49 $
00007   Version:   $Revision: 1.42 $
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 
00087   class ReverseIterator
00088     {
00089     public:
00090       explicit ReverseIterator(Iterator i): m_Iterator(i) {}
00091       Iterator operator++()        { return --m_Iterator; }
00092       Iterator operator++(int)     { return m_Iterator--; }
00093       Iterator operator--()        { return ++m_Iterator; }
00094       Iterator operator--(int)     { return m_Iterator++; }
00095       Iterator operator->() const  { return (m_Iterator-1); }
00096       ValueType& operator*() const { return *(m_Iterator-1); }
00097       bool operator!=(const ReverseIterator &rit) const {return m_Iterator != rit.m_Iterator;}
00098       bool operator==(const ReverseIterator &rit) const {return m_Iterator == rit.m_Iterator;}
00099     private:
00100       Iterator m_Iterator;
00101     };
00102   
00105   class ConstReverseIterator
00106     {
00107     public:
00108       explicit ConstReverseIterator(ConstIterator i): m_Iterator(i) {}
00109       ConstIterator operator++()         { return --m_Iterator; }
00110       ConstIterator operator++(int)      { return m_Iterator--; }
00111       ConstIterator operator--()         { return ++m_Iterator; }
00112       ConstIterator operator--(int)      { return m_Iterator++; }
00113       ConstIterator operator->() const   { return (m_Iterator-1); }
00114       const ValueType& operator*() const { return *(m_Iterator-1); }
00115       bool operator!=(const ConstReverseIterator &rit) const {return m_Iterator != rit.m_Iterator;}
00116       bool operator==(const ConstReverseIterator &rit) const {return m_Iterator == rit.m_Iterator;}
00117     private:
00118       ConstIterator m_Iterator;
00119     };  
00120   
00122   typedef ValueType *        pointer;
00123 
00125   typedef const ValueType*  const_pointer;
00126 
00128   typedef ValueType&        reference;
00129 
00131   typedef const ValueType&  const_reference;
00132 
00133   typedef unsigned int   SizeType;
00134   
00135 public:
00137   FixedArray();
00138   FixedArray(const ValueType r[VLength]);
00140 
00142   template< class TFixedArrayValueType >
00143   FixedArray(const FixedArray< TFixedArrayValueType, VLength >& r)
00144     {
00145     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00146     Iterator i = this->Begin();
00147     while( i != this->End() ) 
00148       {
00149       *i++ = static_cast< TValueType >(*input++);
00150       }
00151     }
00153 
00154 
00169   template< class TFixedArrayValueType >
00170   FixedArray& operator= (const FixedArray< TFixedArrayValueType, VLength > & r)
00171     {
00172     if((void *)r.Begin() == (void *)m_InternalArray) return *this;
00173     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00174     Iterator i = this->Begin();
00175     while( i != this->End() ) 
00176       {
00177       *i++ = static_cast< TValueType >(*input++);
00178       }
00179     return *this;
00180     }
00182 
00183   FixedArray& operator= (const ValueType r[VLength]);
00184     
00188   bool operator==(const FixedArray& r ) const;
00189   bool operator!=(const FixedArray& r ) const
00190     { return !operator==(r); }
00192 
00196         reference operator[](short index)                { return m_InternalArray[index]; }
00197   const_reference operator[](short index) const          { return m_InternalArray[index]; }
00198         reference operator[](unsigned short index)       { return m_InternalArray[index]; }
00199   const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
00200         reference operator[](int index)                  { return m_InternalArray[index]; }
00201   const_reference operator[](int index) const            { return m_InternalArray[index]; }
00202         reference operator[](unsigned int index)         { return m_InternalArray[index]; }
00203   const_reference operator[](unsigned int index) const   { return m_InternalArray[index]; }
00204         reference operator[](long index)                 { return m_InternalArray[index]; }
00205   const_reference operator[](long index) const           { return m_InternalArray[index]; }
00206         reference operator[](unsigned long index)        { return m_InternalArray[index]; }
00207   const_reference operator[](unsigned long index) const  { return m_InternalArray[index]; }
00209 
00211   void SetElement( unsigned short index, const_reference value )
00212                                   { m_InternalArray[ index ] = value; }
00213   const_reference GetElement( unsigned short index ) const { return m_InternalArray[index]; }
00215 
00217   ValueType* GetDataPointer() { return m_InternalArray; }
00218   const ValueType* GetDataPointer() const { return m_InternalArray; }
00220 
00222   Iterator      Begin();
00223   ConstIterator Begin() const;
00224   Iterator      End();
00225   ConstIterator End() const;
00226   ReverseIterator      rBegin();
00227   ConstReverseIterator rBegin() const;
00228   ReverseIterator      rEnd();
00229   ConstReverseIterator rEnd() const;
00230   SizeType      Size() const;
00231   void Fill(const ValueType&);
00233 
00234 private:
00236   CArray  m_InternalArray;
00237 
00238 public:
00239  
00240   static FixedArray Filled(const ValueType&);
00241 };
00242   
00243 template <typename TValueType, unsigned int VLength>
00244 std::ostream & operator<<(std::ostream &os, const FixedArray<TValueType,VLength> &arr);
00245 
00246 } // namespace itk
00247 
00248 #ifdef _MSC_VER
00249 # pragma warning (pop)
00250 #endif
00251 
00252 // Define instantiation macro for this template.
00253 #define ITK_TEMPLATE_FixedArray(_, EXPORT, x, y) namespace itk { \
00254   _(2(class EXPORT FixedArray< ITK_TEMPLATE_2 x >)) \
00255   _(1(EXPORT std::ostream& operator<<(std::ostream&, \
00256                                       const FixedArray< ITK_TEMPLATE_2 x >&))) \
00257   namespace Templates { typedef FixedArray< ITK_TEMPLATE_2 x > FixedArray##y; } \
00258   }
00259 
00260 #if ITK_TEMPLATE_EXPLICIT
00261 # include "Templates/itkFixedArray+-.h"
00262 #endif
00263 
00264 #if ITK_TEMPLATE_TXX
00265 # include "itkFixedArray.txx"
00266 #endif
00267 
00268 #include "itkNumericTraitsFixedArrayPixel.h"
00269 
00270 #endif
00271 

Generated at Wed Nov 5 21:27:04 2008 for ITK by doxygen 1.5.1 written by Dimitri van Heesch, © 1997-2000