ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkFixedArray.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkFixedArray_h
00019 #define __itkFixedArray_h
00020 
00021 #include "itkMacro.h"
00022 
00023 namespace itk
00024 {
00025 //HACK:  Need to remove this function.
00026 #if 1
00027 
00033 template< typename TVector >
00034 struct GetVectorDimension {
00035   itkStaticConstMacro(VectorDimension, unsigned int, TVector::Dimension);
00036 };
00037 #endif
00038 
00062 template< typename TValueType, unsigned int VLength = 3 >
00063 class FixedArray
00064 {
00065 public:
00067   itkStaticConstMacro(Length, unsigned int, VLength);
00068 
00070   itkStaticConstMacro(Dimension, unsigned int, VLength);
00071 
00073   typedef TValueType ValueType;
00074 
00076   typedef ValueType CArray[VLength];
00077 
00079   typedef ValueType *Iterator;
00080 
00082   typedef const ValueType *ConstIterator;
00083 
00084   class ConstReverseIterator;
00085 
00090   class ReverseIterator
00091   {
00092 public:
00093     explicit ReverseIterator(Iterator i):m_Iterator(i) {}
00094     Iterator operator++()        { return --m_Iterator; }
00095     Iterator operator++(int)     { return m_Iterator--; }
00096     Iterator operator--()        { return ++m_Iterator; }
00097     Iterator operator--(int)     { return m_Iterator++; }
00098     Iterator operator->() const { return ( m_Iterator - 1 ); }
00099     ValueType & operator*() const { return *( m_Iterator - 1 ); }
00100     bool operator!=(const ReverseIterator & rit) const { return m_Iterator != rit.m_Iterator; }
00101     bool operator==(const ReverseIterator & rit) const { return m_Iterator == rit.m_Iterator; }
00102 private:
00103     Iterator m_Iterator;
00104     friend class ConstReverseIterator;
00105   };
00106 
00111   class ConstReverseIterator
00112   {
00113 public:
00114     explicit ConstReverseIterator(ConstIterator i):m_Iterator(i) {}
00115     ConstReverseIterator(const ReverseIterator & rit) { m_Iterator = rit.m_Iterator; }
00116     ConstIterator operator++()         { return --m_Iterator; }
00117     ConstIterator operator++(int)      { return m_Iterator--; }
00118     ConstIterator operator--()         { return ++m_Iterator; }
00119     ConstIterator operator--(int)      { return m_Iterator++; }
00120     ConstIterator operator->() const { return ( m_Iterator - 1 ); }
00121     const ValueType & operator*() const { return *( m_Iterator - 1 ); }
00122     bool operator!=(const ConstReverseIterator & rit) const { return m_Iterator != rit.m_Iterator; }
00123     bool operator==(const ConstReverseIterator & rit) const { return m_Iterator == rit.m_Iterator; }
00124 private:
00125     ConstIterator m_Iterator;
00126   };
00127 
00129   typedef ValueType *pointer;
00130 
00132   typedef const ValueType *const_pointer;
00133 
00135   typedef ValueType & reference;
00136 
00138   typedef const ValueType & const_reference;
00139 
00140   typedef unsigned int SizeType;
00141 public:
00143   FixedArray();
00144   FixedArray(const ValueType r[VLength]);
00145   FixedArray(const ValueType & r);
00147 
00149   template< class TFixedArrayValueType >
00150   FixedArray(const FixedArray< TFixedArrayValueType, VLength > & r)
00151   {
00152     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00153     Iterator i = this->Begin();
00154     while ( i != this->End() )
00155       {
00156       *i++ = static_cast< TValueType >( *input++ );
00157       }
00158   }
00160 
00175   template< class TFixedArrayValueType >
00176   FixedArray & operator=(const FixedArray< TFixedArrayValueType, VLength > & r)
00177   {
00178     if ( (void *)r.Begin() == (void *)m_InternalArray ) { return *this; }
00179     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
00180     Iterator i = this->Begin();
00181     while ( i != this->End() )
00182       {
00183       *i++ = static_cast< TValueType >( *input++ );
00184       }
00185     return *this;
00186   }
00188 
00189   FixedArray & operator=(const ValueType r[VLength]);
00190 
00194   bool operator==(const FixedArray & r) const;
00195 
00196   bool operator!=(const FixedArray & r) const
00197   { return !operator==(r); }
00198 
00202   reference operator[](short index)                { return m_InternalArray[index]; }
00203   const_reference operator[](short index) const { return m_InternalArray[index]; }
00204   reference operator[](unsigned short index)       { return m_InternalArray[index]; }
00205   const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
00206   reference operator[](int index)                  { return m_InternalArray[index]; }
00207   const_reference operator[](int index) const { return m_InternalArray[index]; }
00208   reference operator[](unsigned int index)         { return m_InternalArray[index]; }
00209   const_reference operator[](unsigned int index) const { return m_InternalArray[index]; }
00210   reference operator[](long index)                 { return m_InternalArray[index]; }
00211   const_reference operator[](long index) const { return m_InternalArray[index]; }
00212   reference operator[](unsigned long index)        { return m_InternalArray[index]; }
00213   const_reference operator[](unsigned long index) const { return m_InternalArray[index]; }
00214   reference operator[](long long index)                 { return m_InternalArray[index]; }
00215   const_reference operator[](long long index) const { return m_InternalArray[index]; }
00216   reference operator[](unsigned long long index)        { return m_InternalArray[index]; }
00217   const_reference operator[](unsigned long long index) const { return m_InternalArray[index]; }
00219 
00221   void SetElement(unsigned short index, const_reference value)
00222   { m_InternalArray[index] = value; }
00223   const_reference GetElement(unsigned short index) const { return m_InternalArray[index]; }
00225 
00227   ValueType * GetDataPointer()
00228   {
00229     return m_InternalArray; \
00230   }
00231 
00232   const ValueType * GetDataPointer() const
00233   {
00234     return m_InternalArray; \
00235   }
00236 
00238   Iterator      Begin();
00239 
00240   ConstIterator Begin() const;
00241 
00242   Iterator      End();
00243 
00244   ConstIterator End() const;
00245 
00246   ReverseIterator      rBegin();
00247 
00248   ConstReverseIterator rBegin() const;
00249 
00250   ReverseIterator      rEnd();
00251 
00252   ConstReverseIterator rEnd() const;
00253 
00254   SizeType      Size() const;
00255 
00256   void Fill(const ValueType &);
00257 
00258 private:
00260   CArray m_InternalArray;
00261 public:
00262 
00263   static FixedArray Filled(const ValueType &);
00264 };
00265 
00266 template< typename TValueType, unsigned int VLength >
00267 std::ostream & operator<<(std::ostream & os, const FixedArray< TValueType, VLength > & arr);
00268 } // namespace itk
00269 
00270 // Define instantiation macro for this template.
00271 #define ITK_TEMPLATE_FixedArray(_, EXPORT, TypeX, TypeY)                                  \
00272   namespace itk                                                                           \
00273   {                                                                                       \
00274   _( 2 ( class EXPORT FixedArray< ITK_TEMPLATE_2 TypeX > ) )                              \
00275   _( 1 ( EXPORT std::ostream & operator<<(std::ostream &,                                 \
00276                                           const FixedArray< ITK_TEMPLATE_2 TypeX > &) ) ) \
00277   namespace Templates                                                                     \
00278   {                                                                                       \
00279   typedef FixedArray< ITK_TEMPLATE_2 TypeX > FixedArray##TypeY;                         \
00280   }                                                                                       \
00281   }
00282 
00283 #if ITK_TEMPLATE_EXPLICIT
00284 #include "Templates/itkFixedArray+-.h"
00285 #endif
00286 
00287 #if ITK_TEMPLATE_TXX
00288 #include "itkFixedArray.hxx"
00289 #endif
00290 
00291 #include "itkNumericTraitsFixedArrayPixel.h"
00292 
00293 #endif
00294