KWStyle - itkVectorContainer.txx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkVectorContainer.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 _itkVectorContainer_txx
18 DEF #define _itkVectorContainer_txx
19 #include "itkVectorContainer.h"
20
21 #include "itkNumericTraits.h"
22
23 namespace itk
24 {
25
26 /**
27  * Get a reference to the element at the given index.
28  * It is assumed that the index exists, and it will not automatically
29  * be created.
30  *
31  * It is assumed that the value of the element is modified through the
32  * reference.
33  */
34 template <typename TElementIdentifier, typename TElement>
35 typename VectorContainer< TElementIdentifier , TElement >::Element&
36 VectorContainer< TElementIdentifier , TElement >
37 ::ElementAt(ElementIdentifier id)
38 {
39   this->Modified();
40   return this->VectorType::operator[](id);
41 }
42
43 /**
44  * Get a reference to the element at the given index.
45  * It is assumed that the index exists, and it will not automatically
46  * be created.
47  *
48  */
49 template <typename TElementIdentifier, typename TElement>
50 const typename VectorContainer< TElementIdentifier , TElement >::Element&
51 VectorContainer< TElementIdentifier , TElement >
52 ::ElementAt(ElementIdentifier id) const
53 {
54   return this->VectorType::operator[](id);
55 }
56
57
58 EML
59 /**
60  * Get a reference to the element at the given index.
61  * If the element location does not exist, it will be created with a
62  * default element value.
63  *
64  * It is assumed that the value of the element is modified through the
65  * reference.
66  */
67 template <typename TElementIdentifier, typename TElement>
68 typename VectorContainer< TElementIdentifier , TElement >::Element&
69 VectorContainer< TElementIdentifier , TElement >
70 ::CreateElementAt(ElementIdentifier id)
71 {
72   if(id >= this->VectorType::size())
73     {
74     this->CreateIndex(id);
75     }
76   this->Modified();
77   return this->VectorType::operator[](id);
78 }
79
80
81 /**
82  * Read the element from the given index.
83  * It is assumed that the index exists.
84  */
85 template <typename TElementIdentifier, typename TElement>
86 typename VectorContainer< TElementIdentifier , TElement >::Element
87 VectorContainer< TElementIdentifier , TElement >
88 ::GetElement(ElementIdentifier id) const
89 {
90   return this->VectorType::operator[](id);
91 }
92
93
94 /**
95  * Set the element value at the given index.
96  * It is assumed that the index exists.
97  */
98 template <typename TElementIdentifier, typename TElement>
99 void
100 VectorContainer< TElementIdentifier , TElement >
101 ::SetElement(ElementIdentifier id, Element element)
102 {
103   this->VectorType::operator[](id) = element;
104   this->Modified();
105 }
106
107
108 /**
109  * Set the element value at the given index.
110  * If the element location does not exist, it will be created with a
111  * default element value.
112  */
113 template <typename TElementIdentifier, typename TElement>
114 void
115 VectorContainer< TElementIdentifier , TElement >
116 ::InsertElement(ElementIdentifier id, Element element)
117 {
118   if(id >= static_cast<ElementIdentifier>(this->VectorType::size()))
119     {
120     this->CreateIndex(id);
121     }
122   this->VectorType::operator[](id) = element;
123   this->Modified();
124 }
125
126
127 /**
128  * Check if the index range of the STL vector is large enough to allow the
129  * given index without expansion.
130  */
131 template <typename TElementIdentifier, typename TElement>
132 bool
133 VectorContainer< TElementIdentifier , TElement >
134 ::IndexExists(ElementIdentifier id) const
135 {
136   return (NumericTraits<ElementIdentifier>::IsNonnegative(id)
137 IND **********&& (id < this->VectorType::size()));
138 }
139
140
141 EML
142 /**
143  * Check if the given index is in range of the STL vector.  If it is not,
144  * return false.  Otherwise, set the element through the pointer (if it isn't
145  * NULL), and return true.
146  */
147 template <typename TElementIdentifier, typename TElement>
148 bool
149 VectorContainer< TElementIdentifier , TElement >
150 ::GetElementIfIndexExists(ElementIdentifier id, Element* element) const
151 {
152   if (NumericTraits<ElementIdentifier>::IsNonnegative(id)
153 IND ******&& (id < this->VectorType::size()))
154     {
155     if(element)
156       {
157       *element = this->VectorType::operator[](id);
158       }
159     return true;
160     }
161   return false;
162 }
163
164
165 /**
166  * Make sure that the index range of the STL vector is large enough to allow
167  * the given index, expanding it if necessary.  The index will contain
168  * the default element regardless of whether expansion occured.
169  */
170 template <typename TElementIdentifier, typename TElement>
171 void
172 VectorContainer< TElementIdentifier , TElement >
173 ::CreateIndex(ElementIdentifier id)
174 {
175   if(id >= static_cast<ElementIdentifier>(this->VectorType::size()))
176     {
177     /**
178      * The vector must be expanded to fit the
179      * new id.
180      */
181     this->VectorType::resize(id+1);
182     this->Modified();
183     }
184   else if(id > 0)
185     {
186     /**
187      * No expansion was necessary.  Just overwrite the index's entry with
188      * the default element.
189      */
190     this->VectorType::operator[](id) = Element();
191     this->Modified();
192     }
193 }
194
195
196 /**
197  * It doesn't make sense to delete a vector index.
198  * Instead, just overwrite the index with the default element.
199  */
200 template <typename TElementIdentifier, typename TElement>
201 void
202 VectorContainer< TElementIdentifier , TElement >
203 ::DeleteIndex(ElementIdentifier id)
204 {
205   this->VectorType::operator[](id) = Element();
206   this->Modified();
207 }
208
209
210 /**
211  * Get a begin const iterator for the vector.
212  */
213 template <typename TElementIdentifier, typename TElement>
214 typename VectorContainer< TElementIdentifier , TElement >::ConstIterator
215 VectorContainer< TElementIdentifier , TElement >
216 ::Begin(void) const
217 {
218   return ConstIterator(0, this->VectorType::begin());
219 }
220
221
222 /**
223  * Get an end const iterator for the vector.
224  */
225 template <typename TElementIdentifier, typename TElement>
226 typename VectorContainer< TElementIdentifier , TElement >::ConstIterator
227 VectorContainer< TElementIdentifier , TElement >
228 ::End(void) const
229 {
230   return ConstIterator(this->VectorType::size()-1, this->VectorType::end());
231 }
232
233
234 /**
235  * Get a begin iterator for the vector.
236  */
237 template <typename TElementIdentifier, typename TElement>
238 typename VectorContainer< TElementIdentifier , TElement >::Iterator
239 VectorContainer< TElementIdentifier , TElement >
240 ::Begin(void) 
241 {
242   return Iterator(0, this->VectorType::begin());
243 }
244
245
246 /**
247  * Get an end iterator for the vector.
248  */
249 template <typename TElementIdentifier, typename TElement>
250 typename VectorContainer< TElementIdentifier , TElement >::Iterator
251 VectorContainer< TElementIdentifier , TElement >
252 ::End(void) 
253 {
254   return Iterator(this->VectorType::size()-1, this->VectorType::end());
255 }
256
257
258 /**
259  * Get the number of elements currently stored in the vector.
260  */
261 template <typename TElementIdentifier, typename TElement>
262 unsigned long
263 VectorContainer< TElementIdentifier , TElement >
264 ::Size(void) const
265 {
266   return static_cast<unsigned long>( this->VectorType::size() );
267 }
268
269
270 /**
271  * Clear the elements. The final size will be zero.
272  */
273 template <typename TElementIdentifier, typename TElement>
274 void 
275 VectorContainer< TElementIdentifier , TElement >
276 ::Initialize(void) 
277 {
278   this->VectorType::clear();
279 }
280
281
282 EML
283 /**
284  * Tell the container to allocate enough memory to allow at least
285  * as many elements as the size given to be stored.  This is NOT
286  * guaranteed to actually allocate any memory, but is useful if the
287  * implementation of the container allocates contiguous storage.
288  */
289 template <typename TElementIdentifier, typename TElement>
290 void
291 VectorContainer< TElementIdentifier , TElement >
292 ::Reserve(ElementIdentifier size)
293 {
294   this->CreateIndex(size-1);
295 }
296
297
298 /**
299  * Tell the container to try to minimize its memory usage for storage of
300  * the current number of elements.  This is NOT guaranteed to decrease
301  * memory usage.
302  */
303 template <typename TElementIdentifier, typename TElement>
304 void
305 VectorContainer< TElementIdentifier , TElement >
306 ::Squeeze(void)
307 {
308 }
309
310 // end namespace itk
311
312 #endif
313

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