KWStyle - itkFixedArray.h
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkFixedArray.h.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:35 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 #ifndef __itkFixedArray_h
18 #define __itkFixedArray_h
19
20 #include "itkMacro.h"
21
22 #ifdef _MSC_VER
23 # pragma warning (push)
24 LEN # pragma warning (disable: 4284) // operator-> returning pointer to non-aggregate
25 #endif
26
27 namespace itk
28 {
29
30 /**
31  * Due to a bug in MSVC, an enum value cannot be accessed out of a template
32  * parameter until the template class opens.  In order for templated classes
33  * to access the dimension of a vector template parameter in defining their
34  * own dimension, this class is needed as a work-around.
35  */
36 template <typename TVector>
37 struct GetVectorDimension
38 {
39   itkStaticConstMacro(VectorDimension, unsigned int, TVector::Dimension);
40 }; 
41
42
43   
44 /** \class FixedArray
45  *  \brief Simulate a standard C array with copy semnatics.
46  *
47  * Simulates a standard C array, except that copy semantics are used instead
48  * of reference semantics.  Also, arrays of different sizes cannot be
49  * assigned to one another, and size information is known for function
50  * returns.
51  *
52  * Template parameters for class FixedArray:
53  * - TValueType = Element type stored at each location in the array.
54  * - VLength    = Length of the array.
55  *
56  * The length of the array is fixed at compile time. If you wish to
57  * specify the length of the array at run-time, use the class itk::Array.
58  * If you wish to change to change the length of the array at run-time,
59  * you're best off using std::vector<>.
60  *
61  * \ingroup DataRepresentation 
62  */
63 template <typename TValueType, unsigned int VLength=3>
64 class FixedArray
65 {
66 public:
67   /** Length constant */
68   itkStaticConstMacro(Length, unsigned int, VLength);
69   
70   /** Dimension constant */
71   itkStaticConstMacro(Dimension, unsigned int, VLength);
72   
73   /** The element type stored at each location in the FixedArray. */
74   typedef TValueType  ValueType;
75   
76   /** A type representing the C-array version of this FixedArray. */
77   typedef ValueType         CArray[VLength];
78   
79   /** An iterator through the array. */
80   typedef ValueType*        Iterator;
81
82   /** A const iterator through the array. */
83   typedef const ValueType*  ConstIterator;
84
85   /** \brief A reverse iterator through the array. */
86   class ReverseIterator
87 MCM,IND **{
88   public:
89     explicit ReverseIterator(Iterator i): m_Iterator(i) {}
90     Iterator operator++()        { return --m_Iterator; }
91     Iterator operator++(int)     { return m_Iterator--; }
92     Iterator operator--()        { return ++m_Iterator; }
93     Iterator operator--(int)     { return m_Iterator++; }
94     Iterator operator->() const  { return (m_Iterator-1); }
95     ValueType& operator*() const { return *(m_Iterator-1); }
96 LEN     bool operator!=(const ReverseIterator &rit) const {return m_Iterator != rit.m_Iterator;};
97 LEN     bool operator==(const ReverseIterator &rit) const {return m_Iterator == rit.m_Iterator;};
98   private:
99     Iterator m_Iterator;
100 IND **};
101   
102   /** \brief A const reverse iterator through the array. */
103   class ConstReverseIterator
104 MCM,IND **{
105   public:
106     explicit ConstReverseIterator(ConstIterator i): m_Iterator(i) {}
107     ConstIterator operator++()         { return --m_Iterator; }
108     ConstIterator operator++(int)      { return m_Iterator--; }
109     ConstIterator operator--()         { return ++m_Iterator; }
110     ConstIterator operator--(int)      { return m_Iterator++; }
111     ConstIterator operator->() const   { return (m_Iterator-1); }
112     const ValueType& operator*() const { return *(m_Iterator-1); }
113 LEN     bool operator!=(const ConstReverseIterator &rit) const {return m_Iterator != rit.m_Iterator;};
114 LEN     bool operator==(const ConstReverseIterator &rit) const {return m_Iterator == rit.m_Iterator;};
115   private:
116     ConstIterator m_Iterator;
117 IND **};  
118   
119   /** A pointer to the ValueType. */
120 TDR   typedef ValueType*        pointer;
121
122   /** A const pointer to the ValueType. */
123 TDR   typedef const ValueType*  const_pointer;
124
125   /** A reference to the ValueType. */
126 TDR   typedef ValueType&        reference;
127
128   /** A const reference to the ValueType. */
129 TDR   typedef const ValueType&  const_reference;
130   
131   typedef unsigned int   SizeType;
132   
133 public:
134   /** Constructors */
135   FixedArray();
136   FixedArray(const ValueType r[VLength]);
137
138   /** Constructor to initialize a fixed array from another of any data type */
139   template< class TFixedArrayValueType >
140   FixedArray(const FixedArray< TFixedArrayValueType, VLength >& r)
141     {
142 LEN     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
143 SEM,SEM     for(Iterator i = this->Begin() ; i != this->End() ;) 
144 IND *******i++ = static_cast< TValueType >(*input++);
145     }
146   
147
148   /** This destructor is not virtual for performance reasons. However, this
149    * means that subclasses cannot allocate memory. */
150   ~FixedArray();
151   
152   /** Operator= defined for a variety of types. */
153   template< class TFixedArrayValueType >
154   FixedArray& operator= (const FixedArray< TFixedArrayValueType, VLength > & r)
155     {
156     if((void *)r.Begin() == (void *)m_InternalArray) return *this;
157 LEN     typename FixedArray< TFixedArrayValueType, VLength >::ConstIterator input = r.Begin();
158 SEM,SEM     for(Iterator i = this->Begin() ; i != this->End() ;) 
159 IND *******i++ = static_cast< TValueType >(*input++);
160     return *this;
161     }
162
163   FixedArray& operator= (const ValueType r[VLength]);
164     
165   /** Operators == and != are used to compare whether two arrays are equal.
166    * Note that arrays are equal when the number of components (size) is the
167    * same, and each component value is equal. */
168   bool operator==(const FixedArray& r ) const;
169   bool operator!=(const FixedArray& r ) const
170     { return !operator==(r); }
171   
172   /** Allow the FixedArray to be indexed normally.  No bounds checking is done.
173    * The separate versions are a work-around for an integer conversion bug in
174    * Visual C++. */
175 LEN,IND ********reference operator[](short index)                { return m_InternalArray[index]; }
176 LEN   const_reference operator[](short index) const          { return m_InternalArray[index]; }
177 LEN,IND ********reference operator[](unsigned short index)       { return m_InternalArray[index]; }
178 LEN   const_reference operator[](unsigned short index) const { return m_InternalArray[index]; }
179 LEN,IND ********reference operator[](int index)                  { return m_InternalArray[index]; }
180 LEN   const_reference operator[](int index) const            { return m_InternalArray[index]; }
181 LEN,IND ********reference operator[](unsigned int index)         { return m_InternalArray[index]; }
182 LEN   const_reference operator[](unsigned int index) const   { return m_InternalArray[index]; }
183 LEN,IND ********reference operator[](long index)                 { return m_InternalArray[index]; }
184 LEN   const_reference operator[](long index) const           { return m_InternalArray[index]; }
185 LEN,IND ********reference operator[](unsigned long index)        { return m_InternalArray[index]; }
186 LEN   const_reference operator[](unsigned long index) const  { return m_InternalArray[index]; }
187   
188   /** Set/Get element methods are more convenient in wrapping languages */
189   void SetElement( unsigned short index, const_reference value )
190 IND **********************************{ m_InternalArray[ index ] = value; }
191 LEN   const_reference GetElement( unsigned short index ) const { return m_InternalArray[index]; }
192   
193   /** Return a pointer to the data. */
194   ValueType* GetDataPointer() { return m_InternalArray; }
195   const ValueType* GetDataPointer() const { return m_InternalArray; }
196     
197   /** Get various iterators to the array. */
198   Iterator      Begin();
199   ConstIterator Begin() const;
200   Iterator      End();
201   ConstIterator End() const;
202   ReverseIterator      rBegin();
203   ConstReverseIterator rBegin() const;
204   ReverseIterator      rEnd();
205   ConstReverseIterator rEnd() const;
206   SizeType      Size() const;
207   void Fill(const ValueType&);
208     
209 private:
210   /** Internal C array representation. */
211   CArray  m_InternalArray;
212   
213 public:
214  
215   static FixedArray Filled(const ValueType&);
216 };
217   
218 template <typename TValueType, unsigned int VLength>
219 LEN std::ostream & operator<<(std::ostream &os, const FixedArray<TValueType,VLength> &arr)
220 {
221   os << "[";
222   if ( VLength == 1 )
223     {
224 SEM     os << arr[0] ;
225     }
226   else
227     {
228     for (int i=0; i < static_cast<int>(VLength) - 1; ++i)
229       {
230       os << arr[i] << ", ";
231       }
232     os << arr[VLength-1];
233     }
234   os << "]";
235   return os;
236 }
237
238 // namespace itk
239
240 #ifdef _MSC_VER
241 # pragma warning (pop)
242 #endif
243
244 #ifndef ITK_MANUAL_INSTANTIATION
245 #include "itkFixedArray.txx"
246 #endif
247
248 #endif
249

Generated by KWStyle 1.0b on Tuesday January,17 at 02:14:08PM
© Kitware Inc.