KWStyle - itkVariableLengthVector.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkVariableLengthVector.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:49 $
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 DEF #ifndef _itkVariableLengthVector_txx
18 DEF #define _itkVariableLengthVector_txx
19
20 #include "itkVariableLengthVector.h"
21 #include "itkNumericTraitsVariableLengthVectorPixel.h"
22
23 namespace itk
24 {
25
26 /** Default constructor  */
27 template < typename TValueType >
28 VariableLengthVector<TValueType >
29 ::VariableLengthVector() : 
30 IND **m_LetArrayManageMemory( true ),
31 IND **m_Data( 0 ),
32 IND,IND,IND **m_NumElements( 0 ) {}
33
34
35 /** Constructor with size */
36 template < typename TValueType >
37 VariableLengthVector<TValueType >
38 ::VariableLengthVector(unsigned int length) :
39 IND **m_LetArrayManageMemory( true ),
40 IND **m_Data( 0 )
41 {
42   Reserve( length );
43 }
44
45 /** Constructor with user specified data */
46 template < typename TValueType >
47 VariableLengthVector<TValueType >
48 LEN ::VariableLengthVector( ValueType *datain, unsigned int sz, bool LetArrayManageMemory) :
49 IND **m_LetArrayManageMemory( LetArrayManageMemory ),
50 IND **m_Data( datain ),
51 IND,IND,IND **m_NumElements( sz ) {}
52
53 /** Constructor with user specified data */
54 template < typename TValueType >
55 VariableLengthVector<TValueType >
56 LEN ::VariableLengthVector( const ValueType *datain, unsigned int sz, bool LetArrayManageMemory)
57 IND **: m_LetArrayManageMemory( LetArrayManageMemory )
58 {
59   m_Data = const_cast< ValueType * >(datain);
60   m_NumElements = sz;
61 }
62
63 /** Copy constructer.. Override the default non-templated copy constructor
64  * that the compiler provides */
65 template < typename TValueType >
66 VariableLengthVector<TValueType >
67 ::VariableLengthVector(const VariableLengthVector< TValueType > & v)
68 {
69   m_NumElements = v.Size();
70   m_Data = this->AllocateElements(m_NumElements);
71   m_LetArrayManageMemory = true;
72   for( ElementIdentifier i=0; i< v.Size(); i++ )
73     {
74     this->m_Data[i] = v[i];
75     }
76 }
77
78
79 /** Destructor*/
80 template < typename TValueType >
81 VariableLengthVector<TValueType >
82 ::~VariableLengthVector()
83 {
84   // if data exists and we are responsible for its memory, get rid of it.. 
85   if(m_LetArrayManageMemory && m_Data)
86     {
87     delete [] m_Data;
88     }
89 }
90
91 /** Reserve memory of certain size for m_Data */
92 template < typename TValueType >
93 void VariableLengthVector<TValueType >
94 ::Reserve( ElementIdentifier size )
95 {
96   if( m_Data )
97     {
98     TValueType *temp = this->AllocateElements( size );
99     if( size> m_NumElements )
100       { 
101       // only copy the portion of the data used in the old buffer
102       memcpy(temp, m_Data, m_NumElements*sizeof(TValueType));
103       if (m_Data && m_LetArrayManageMemory)
104         {
105         delete[] m_Data;
106         }
107       m_Data = temp;
108       m_LetArrayManageMemory = true;
109       m_NumElements = size;
110       }
111     }
112   else
113     {
114     m_Data = this->AllocateElements(size);
115     m_NumElements = size;
116     m_LetArrayManageMemory = true;
117     }
118 }
119         
120       
121 /** Allocate memory of certain size and return it */
122 template < typename TValueType >
123 TValueType * VariableLengthVector< TValueType >
124 ::AllocateElements( ElementIdentifier size ) const
125 {
126   TValueType *data;
127   try 
128     {
129     data = new TValueType[ size ];
130     }
131   catch(...)
132     {
133     data = 0;
134     }
135   if( !data )
136     {
137     itkGenericExceptionMacro( << "Failed to allocate memory of length " << size 
138                               << " for VariableLengthVector.");
139     }
140   return data;
141 }
142
143
144 /** Set the pointer from which the data is imported.
145  * If "LetArrayManageMemory" is false, then the application retains
146  * the responsibility of freeing the memory for this data.  If
147  * "LetArrayManageMemory" is true, then this class will free the
148  * memory when this object is destroyed. Note that you need to explicitly
149  * set the number of elements.*/
150 template < typename TValueType >
151 void 
152 VariableLengthVector<TValueType >
153 ::SetData(TValueType* datain,bool LetArrayManageMemory)
154 {
155   // Free any existing data if we manage its memory
156   if(m_LetArrayManageMemory && m_Data)   
157     {
158     delete [] m_Data;
159     }  
160   
161   m_LetArrayManageMemory = LetArrayManageMemory;
162   m_Data = datain;
163 }
164
165
166 /** Similar to the previous method. In the above method, the size must be 
167  * seperately set prior to using user-supplied data. This introduces an
168  * unnecessary allocation step to be performed. This method avoids it 
169  * and should be used to import data whereever possible to avoid this.
170  * Set the pointer from which the data is imported.
171  * If "LetArrayManageMemory" is false, then the application retains
172  * the responsibility of freeing the memory for this data.  If
173  * "LetArrayManageMemory" is true, then this class will free the
174  * memory when this object is destroyed. */
175 template < typename TValueType >
176 void 
177 VariableLengthVector<TValueType >
178 ::SetData(TValueType* datain, unsigned int sz, bool LetArrayManageMemory)
179 {
180   // Free any existing data if we manage its memory
181   if(m_LetArrayManageMemory && m_Data)
182     {
183     delete [] m_Data;
184     }
185
186   m_LetArrayManageMemory = LetArrayManageMemory;
187   m_Data = datain;
188   m_NumElements = sz;
189 }
190
191
192 template < typename TValueType >
193 void VariableLengthVector<TValueType >
194 ::SetSize(unsigned int sz, bool destroyExistingData)
195 {
196   if( destroyExistingData )
197     {
198     // Free any existing data if we manage its memory and if we need to destroy
199     if(!m_LetArrayManageMemory)
200       {
201       m_Data=0;
202       m_NumElements = 0;
203       }
204     else if( m_Data )
205       {
206       if( (m_NumElements != sz))
207         {
208         if(m_NumElements>0)
209           {
210           delete [] m_Data;
211           }
212         }
213       else return;
214       }
215     }
216
217   if ( m_NumElements != sz )
218     {
219     Reserve( sz );
220     }
221 }
222
223 /** Set the all the elements of the array to the specified value */
224 template < typename TValueType > 
225 void VariableLengthVector<TValueType >
226 ::Fill (TValueType const& v) 
227 {
228   for( ElementIdentifier i=0; i< m_NumElements; i++ )
229     {
230     this->m_Data[i] = v;
231     }
232 }
233
234 /** Assignment operator  **/
235 template < typename TValueType > const VariableLengthVector< TValueType >& 
236 VariableLengthVector<TValueType >
237 ::operator=(const Self & v)
238 {
239   if( this == &v )
240     {
241     return *this;
242     }
243   this->SetSize( v.Size());
244   for( ElementIdentifier i=0; i< v.Size(); i++ )
245     {
246     this->m_Data[i] = v[i];
247     }
248   return *this;
249 }
250
251 template < typename TValueType > VariableLengthVector< TValueType >& 
252 VariableLengthVector<TValueType >
253 ::operator- ()
254 {
255   for( ElementIdentifier i=0; i< m_NumElements; i++ )
256     {
257     m_Data[i] = -m_Data[i];
258     }
259   return *this;
260 }
261
262 template < typename TValueType > bool 
263 VariableLengthVector<TValueType >
264 ::operator==( const Self & v) const
265 {
266   if( m_NumElements != v.Size() ) 
267     {
268     return false;
269     }
270   for( ElementIdentifier i=0; i< m_NumElements; i++ )
271     {
272     if( m_Data[i] != v[i] ) 
273       { 
274       return false; 
275       }
276     }
277   return true;
278 }
279
280 template < typename TValueType > bool 
281 VariableLengthVector<TValueType >
282 ::operator!=( const Self & v) const
283 {
284   if( m_NumElements != v.Size() ) 
285     {
286     return true;
287     }
288   for( ElementIdentifier i=0; i< m_NumElements; i++ )
289     {
290     if( m_Data[i] != v[i] ) 
291       { 
292       return true; 
293       }
294     }
295   return false;
296 }
297
298
299 // namespace itk
300
301 #endif
302

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