KWStyle - itkArray.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkArray.txx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:32 $
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 _itkArray_txx
18 DEF #define _itkArray_txx
19
20 #include "itkArray.h"
21
22 namespace itk
23 {
24
25 /** Default constructor  */
26 template < typename TValueType >
27 Array<TValueType >
28 ::Array():vnl_vector<TValueType>()
29 {
30   m_LetArrayManageMemory = true;
31 }
32
33
34 /** Constructor with size */
35 template < typename TValueType >
36 Array<TValueType >
37 ::Array(unsigned int dimension):vnl_vector<TValueType>(dimension)
38 {
39   m_LetArrayManageMemory = true;
40 }
41
42 /** Constructor with user specified data */
43 template < typename TValueType >
44 Array<TValueType >
45 ::Array( ValueType *datain, unsigned int sz, bool LetArrayManageMemory)
46 {
47   vnl_vector<TValueType>::data = datain;
48   vnl_vector<TValueType>::num_elmts = sz;
49   m_LetArrayManageMemory = LetArrayManageMemory;
50 }
51
52 /** Constructor with user specified data */
53 template < typename TValueType >
54 Array<TValueType >
55 ::Array( const ValueType *datain, unsigned int sz, bool LetArrayManageMemory)
56 {
57   vnl_vector<TValueType>::data = const_cast< TValueType * >( datain ); 
58 IND *******************************// Argh!! Discard constness WRONG.!!
59   vnl_vector<TValueType>::num_elmts = sz;
60   m_LetArrayManageMemory = LetArrayManageMemory;
61 }
62
63
64 /** Destructor*/
65 template < typename TValueType >
66 Array<TValueType >
67 ::~Array()
68 {
69   if(!m_LetArrayManageMemory)
70     {
71     vnl_vector<TValueType>::data = 0;
72     }
73 }
74
75
76 /** Set the pointer from which the data is imported.
77  * If "LetArrayManageMemory" is false, then the application retains
78  * the responsibility of freeing the memory for this data.  If
79  * "LetArrayManageMemory" is true, then this class will free the
80  * memory when this object is destroyed. */
81 template < typename TValueType >
82 void 
83 Array<TValueType >
84 ::SetData(TValueType* datain,bool LetArrayManageMemory)
85 {
86   if(m_LetArrayManageMemory)
87     {
88     vnl_vector<TValueType>::destroy();
89     }
90   vnl_vector<TValueType>::data = datain;
91   m_LetArrayManageMemory = LetArrayManageMemory;
92 }
93
94
95 /** Similar to the previous method. In the above method, the size must be 
96  * seperately set prior to using user-supplied data. This introduces an
97  * unnecessary allocation step to be performed. This method avoids it 
98  * and should be used to import data whereever possible to avoid this.
99  * Set the pointer from which the data is imported.
100  * If "LetArrayManageMemory" is false, then the application retains
101  * the responsibility of freeing the memory for this data.  If
102  * "LetArrayManageMemory" is true, then this class will free the
103  * memory when this object is destroyed. */
104 template < typename TValueType >
105 void 
106 Array<TValueType >
107 ::SetData(TValueType* datain, unsigned int sz, bool LetArrayManageMemory)
108 {
109   if(m_LetArrayManageMemory)
110     {
111     vnl_vector<TValueType>::destroy();
112     }
113   vnl_vector<TValueType>::data = datain;
114   vnl_vector<TValueType>::num_elmts = sz;
115   m_LetArrayManageMemory = LetArrayManageMemory;
116 }
117
118
119 template < typename TValueType >
120 void Array<TValueType >
121 ::SetSize(unsigned int sz)
122 {
123
124   if ( this->size() != sz )
125     {
126
127     // If the array doesn't own the data we do not want to erase it
128     // on a resize
129     if(!m_LetArrayManageMemory)
130       {
131       vnl_vector<TValueType>::data = 0;
132       }
133
134     // Call the superclass's set_size
135     this->set_size(sz);
136
137     // Size we have allocated new data we need to take
138     // responsibility for deleting it
139     m_LetArrayManageMemory = true;
140
141     }
142     
143 }
144
145
146 EML
147 template < typename TValueType >
148 const typename Array<TValueType>
149 ::Self&
150 Array<TValueType>
151 ::operator=( const Self& rhs )
152 {
153
154   if( this == &rhs ) { return *this; }
155
156   // Set the size the same as rhs.
157   // The SetSize method takes care of who is responsible
158   // for memory management
159   //
160   this->SetSize( rhs.GetSize() );
161
162   // Call the superclass implementation
163   this->VnlVectorType::operator=(rhs);
164
165   return *this;
166 }
167
168
169 template < typename TValueType >
170 const typename Array<TValueType>
171 ::Self&
172 Array<TValueType>
173 ::operator=( const VnlVectorType& rhs )
174 {
175
176   if( this == &rhs ) { return *this; }
177
178   // Set the size the same as rhs.
179   // The SetSize method takes care of who is responsible
180   // for memory management
181   //
182   this->SetSize( rhs.size() );
183
184   // Call the superclass implementation
185   this->VnlVectorType::operator=(rhs);
186
187   return *this;
188 }
189
190
191 // namespace itk
192
193 #endif
194

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