ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkVariableLengthVector.h
Go to the documentation of this file.
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 __itkVariableLengthVector_h
19 #define __itkVariableLengthVector_h
20 
21 #include "itkNumericTraits.h"
22 
23 namespace itk
24 {
75 template< typename TValueType >
77 {
78 public:
79 
81  typedef TValueType ValueType;
82  typedef TValueType ComponentType;
85 
87  typedef unsigned int ElementIdentifier;
88 
92 
94  explicit VariableLengthVector(unsigned int dimension);
95 
102  VariableLengthVector(ValueType *data, unsigned int sz,
103  bool LetArrayManageMemory = false);
104 
111  VariableLengthVector(const ValueType *data, unsigned int sz,
112  bool LetArrayManageMemory = false);
113 
123  template< class T >
125  {
126  m_NumElements = v.Size();
128  m_LetArrayManageMemory = true;
129  for ( ElementIdentifier i = 0; i < v.Size(); i++ )
130  {
131  this->m_Data[i] = static_cast< ValueType >( v[i] );
132  }
133  }
135 
139 
141  void Fill(TValueType const & v);
142 
144  template< class T >
145  const VariableLengthVector< TValueType > & operator=
147  {
148  if ( m_Data == static_cast< void * >( const_cast< T * >
149  ( ( const_cast< VariableLengthVector< T > & >( v ) ).GetDataPointer() ) ) )
150  {
151  return *this;
152  }
153  this->SetSize( v.Size() );
154  for ( ElementIdentifier i = 0; i < v.Size(); i++ )
155  {
156  this->m_Data[i] = static_cast< ValueType >( v[i] );
157  }
158  return *this;
159  }
161 
163  const Self & operator=(const Self & v);
164 
165  const Self & operator=(TValueType const & v);
166 
168  inline unsigned int Size(void) const { return m_NumElements; }
169  inline unsigned int GetNumberOfElements(void) const { return m_NumElements; }
171 
173  TValueType & operator[](unsigned int i) { return this->m_Data[i]; }
174 
176  TValueType const & operator[](unsigned int i) const { return this->m_Data[i]; }
177 
179  inline const TValueType & GetElement(unsigned int i) const { return m_Data[i]; }
180 
182  void SetElement(unsigned int i, const TValueType & value) { m_Data[i] = value; }
183 
194  void SetSize(unsigned int sz, bool destroyExistingData = true);
195 
198  void DestroyExistingData();
199 
200  inline unsigned int GetSize(void) const { return m_NumElements; }
201 
207  void SetData(TValueType *data, bool LetArrayManageMemory = false);
208 
218  void SetData(TValueType *data, unsigned int sz, bool LetArrayManageMemory = false);
219 
223 
231 
233  TValueType * AllocateElements(ElementIdentifier size) const;
234 
235  const TValueType * GetDataPointer() const { return m_Data; }
236 
243  template< class T >
244  inline Self operator+(const VariableLengthVector< T > & v) const
245  {
246  // if( m_NumElements != v.GetSize() )
247  // {
248  // itkGenericExceptionMacro( << "Cannot add VariableLengthVector of length
249  // "
250  // << m_NumElements " and " << v.GetSize() );
251  // }
252  const ElementIdentifier length = v.Size();
253  Self result(length);
255 
256  for ( ElementIdentifier i = 0; i < length; i++ )
257  {
258  result[i] = ( *this )[i] + static_cast< ValueType >( v[i] );
259  }
260  return result;
261  }
262 
270  template< class T >
271  inline Self operator-(const VariableLengthVector< T > & v) const
272  {
273  // if( m_NumElements != v.GetSize() )
274  // {
275  // itkGenericExceptionMacro( << "Cannot add VariableLengthVector of length
276  // "
277  // << m_NumElements " and " << v.GetSize() );
278  // }
279  const ElementIdentifier length = v.Size();
280  Self result(length);
282 
283  for ( ElementIdentifier i = 0; i < length; i++ )
284  {
285  result[i] = ( *this )[i] - static_cast< ValueType >( v[i] );
286  }
287  return result;
288  }
289 
294  template< class T >
295  inline Self operator*(T s) const
296  {
297  Self result(m_NumElements);
298 
299  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
300  {
301  result[i] = m_Data[i] * static_cast< ValueType >( s );
302  }
303  return result;
304  }
305 
310  template< class T >
311  inline Self operator/(T s) const
312  {
313  Self result(m_NumElements);
314 
315  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
316  {
317  result[i] = static_cast< ValueType >(
318  static_cast< RealValueType >( m_Data[i] )
319  / static_cast< RealValueType >( s ) );
320  }
321  return result;
322  }
323 
325  inline Self operator+(TValueType s) const
326  {
327  Self result(m_NumElements);
328 
329  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
330  {
331  result[i] = m_Data[i] + s;
332  }
333  return result;
334  }
335 
337  inline Self operator-(TValueType s) const
338  {
339  Self result(m_NumElements);
340 
341  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
342  {
343  result[i] = m_Data[i] - s;
344  }
345  return result;
346  }
347 
350  inline Self & operator--()
351  {
352  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
353  {
354  this->m_Data[i] -= static_cast< ValueType >( 1.0 );
355  }
356  return *this;
357  }
359 
361  inline Self & operator++() // prefix operator ++v;
362  {
363  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
364  {
365  this->m_Data[i] += static_cast< ValueType >( 1.0 );
366  }
367  return *this;
368  }
370 
373  inline Self operator--(int) // postfix operator v--;
374  {
375  Self tmp(*this);
377 
378  --tmp;
379  return tmp;
380  }
381 
383  inline Self operator++(int) // postfix operator v++;
384  {
385  Self tmp(*this);
387 
388  ++tmp;
389  return tmp;
390  }
391 
399  template< class T >
400  inline Self & operator-=
402  {
403  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
404  {
405  m_Data[i] -= static_cast< ValueType >( v[i] );
406  }
407  return *this;
408  }
410 
412  inline Self & operator-=(TValueType s)
413  {
414  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
415  {
416  m_Data[i] -= s;
417  }
418  return *this;
419  }
421 
429  template< class T >
430  inline Self & operator+=
432  {
433  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
434  {
435  m_Data[i] += static_cast< ValueType >( v[i] );
436  }
437  return *this;
438  }
440 
442  inline Self & operator+=(TValueType s)
443  {
444  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
445  {
446  m_Data[i] += s;
447  }
448  return *this;
449  }
451 
455  template< class T >
456  inline Self & operator*=(T s)
457  {
458  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
459  {
460  m_Data[i] *= ( static_cast< ValueType >( s ) );
461  }
462  return *this;
463  }
465 
470  template< class T >
471  inline Self & operator/=(T s)
472  {
473  for ( ElementIdentifier i = 0; i < m_NumElements; i++ )
474  {
475  m_Data[i] = static_cast< ValueType >(
476  static_cast< RealValueType >( m_Data[i] )
477  / static_cast< RealValueType >( s ) );
478  }
479  return *this;
480  }
482 
484  Self & operator-(); // negation operator
485 
486  bool operator==(const Self & v) const;
487 
488  bool operator!=(const Self & v) const;
489 
491  RealValueType GetNorm() const;
492 
495 
496 private:
497 
498  bool m_LetArrayManageMemory; // if true, the array is responsible
499  // for memory of data
500  TValueType * m_Data; // Array to hold data
502 };
503 
507 template< class TValueType, class T >
508 inline
510 operator*(const T & scalar, const VariableLengthVector< TValueType > & v)
511 {
512  return v * scalar;
513 }
514 
515 template< typename TValueType >
516 std::ostream & operator<<(std::ostream & os, const VariableLengthVector< TValueType > & arr)
517 {
518  const unsigned int length = arr.Size();
519  const signed int last = (unsigned int)length - 1;
520 
521  os << "[";
522  for ( signed int i = 0; i < last; ++i )
523  {
524  os << arr[i] << ", ";
525  }
526  if ( length >= 1 )
527  {
528  os << arr[last];
529  }
530  os << "]";
531  return os;
532 }
533 } // namespace itk
534 
536 
537 #ifndef ITK_MANUAL_INSTANTIATION
538 #include "itkVariableLengthVector.hxx"
539 #endif
540 
541 #endif
542