1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18 #ifndef itkArray_hxx
19 #define itkArray_hxx
20
21 #include "itkArray.h"
22 #include "itkNumericTraitsArrayPixel.h"
23
24 namespace itk
25 {
26 /** Default constructor */
27 template< typename TValue >
28 Array< TValue >
Array()29 ::Array():vnl_vector< TValue >()
30 {
31 m_LetArrayManageMemory = true;
32 }
33
34 /** Copy constructor */
35 template < typename TValue >
36 Array<TValue>
Array(const Self & rhs)37 ::Array(const Self & rhs)
38 : vnl_vector<TValue>(rhs),
39 // The vnl vector copy constructor creates new memory
40 // no matter the setting of let array manage memory of rhs
41 m_LetArrayManageMemory(true)
42 {
43 }
44
45 /** Constructor with size */
46 template< typename TValue >
47 Array< TValue >
Array(SizeValueType dimension)48 ::Array(SizeValueType dimension)
49 : vnl_vector< TValue >(dimension),
50 // The vnl vector copy constructor creates new memory
51 // no matter the setting of let array manage memory of rhs
52 m_LetArrayManageMemory(true)
53 {
54 }
55
56 /** Constructor with user specified data */
57 template< typename TValue >
58 Array< TValue >
Array(ValueType * datain,SizeValueType sz,bool LetArrayManageMemory)59 ::Array(ValueType *datain, SizeValueType sz, bool LetArrayManageMemory):
60 m_LetArrayManageMemory(LetArrayManageMemory)
61 {
62 vnl_vector< TValue >::data = datain;
63 vnl_vector< TValue >::num_elmts = sz;
64 }
65
66 #if defined ( ITK_LEGACY_REMOVE )
67 /** Constructor with user specified const data */
68 template< typename TValue >
69 Array< TValue >
Array(const ValueType * datain,SizeValueType sz)70 ::Array(const ValueType *datain, SizeValueType sz):
71 vnl_vector< TValue >( datain, sz),
72 // The vnl vector copy constructor creates new memory
73 // no matter the setting of let array manage memory of rhs
74 m_LetArrayManageMemory(true)
75 {
76 }
77
78 #else // defined ( ITK_LEGACY_REMOVE )
79 /** Constructor with user specified const data */
80 template< typename TValue >
81 Array< TValue >
Array(const ValueType * datain,SizeValueType sz,bool)82 ::Array(const ValueType *datain, SizeValueType sz, bool /* LetArrayManageMemory */):
83 /* NOTE: The 3rd argument "LetArrayManageMemory, was never valid to use, but is
84 * preserved to maintain backwards compatibility*/
85 vnl_vector< TValue >( datain, sz),
86 // The vnl vector copy constructor creates new memory
87 // no matter the setting of let array manage memory of rhs
88 m_LetArrayManageMemory(true)
89 {
90 }
91 #endif
92
93
94 /** Destructor */
95 template< typename TValue >
96 Array< TValue >
~Array()97 ::~Array()
98 {
99 if ( !m_LetArrayManageMemory )
100 {
101 vnl_vector< TValue >::data = nullptr;
102 }
103 }
104
105 template< typename TValue >
106 void
107 Array< TValue >
SetDataSameSize(TValue * datain,bool LetArrayManageMemory)108 ::SetDataSameSize(TValue *datain, bool LetArrayManageMemory)
109 {
110 if ( m_LetArrayManageMemory )
111 {
112 vnl_vector< TValue >::destroy();
113 }
114 vnl_vector< TValue >::data = datain;
115 // NOTE: Required to have same size vnl_vector< TValue >::num_elmts = sz;
116 m_LetArrayManageMemory = LetArrayManageMemory;
117 }
118
119 template< typename TValue >
120 void
121 Array< TValue >
SetData(TValue * datain,SizeValueType sz,bool LetArrayManageMemory)122 ::SetData(TValue *datain, SizeValueType sz, bool LetArrayManageMemory)
123 {
124 if ( m_LetArrayManageMemory )
125 {
126 vnl_vector< TValue >::destroy();
127 }
128 vnl_vector< TValue >::data = datain;
129 vnl_vector< TValue >::num_elmts = sz;
130 m_LetArrayManageMemory = LetArrayManageMemory;
131 }
132
133 template< typename TValue >
134 void Array< TValue >
SetSize(SizeValueType sz)135 ::SetSize(SizeValueType sz)
136 {
137 if ( this->size() != sz )
138 {
139 // If the array doesn't own the data we do not want to erase it
140 // on a resize
141 if ( !m_LetArrayManageMemory )
142 {
143 vnl_vector< TValue >::data = nullptr;
144 }
145
146 // Call the superclass's set_size
147 this->set_size(sz);
148
149 // Size we have allocated new data we need to take
150 // responsibility for deleting it
151 m_LetArrayManageMemory = true;
152 }
153 }
154
155 template< typename TValue >
156 const typename Array< TValue >
157 ::Self &
158 Array< TValue >
operator =(const Self & rhs)159 ::operator=(const Self & rhs)
160 {
161 if ( this != &rhs )
162 {
163
164 // Set the size the same as rhs.
165 // The SetSize method takes care of who is responsible
166 // for memory management
167 //
168 this->SetSize( rhs.GetSize() );
169
170 // Call the superclass implementation
171 this->VnlVectorType::operator=(rhs);
172 }
173 return *this;
174 }
175
176 template< typename TValue >
177 const typename Array< TValue >
178 ::Self &
179 Array< TValue >
operator =(const VnlVectorType & rhs)180 ::operator=(const VnlVectorType & rhs)
181 {
182 if ( this != &rhs )
183 {
184
185 // Set the size the same as rhs.
186 // The SetSize method takes care of who is responsible
187 // for memory management
188 //
189 this->SetSize( rhs.size() );
190
191 // Call the superclass implementation
192 this->VnlVectorType::operator=(rhs);
193 }
194 return *this;
195 }
196 } // namespace itk
197
198 #endif
199