ITK  4.8.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 TValue >
77 {
78 public:
79 
81  typedef TValue ValueType;
82  typedef TValue 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< typename 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(TValue const & v);
142 
144  template< typename T >
145  const VariableLengthVector< TValue > & 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=(TValue 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  TValue & operator[](unsigned int i) { return this->m_Data[i]; }
174 
176  TValue const & operator[](unsigned int i) const { return this->m_Data[i]; }
177 
179  inline const TValue & GetElement(unsigned int i) const { return m_Data[i]; }
180 
182  void SetElement(unsigned int i, const TValue & 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(TValue *data, bool LetArrayManageMemory = false);
208 
218  void SetData(TValue *data, unsigned int sz, bool LetArrayManageMemory = false);
219 
223 
231 
233  TValue * AllocateElements(ElementIdentifier size) const;
234 
235  const TValue * GetDataPointer() const { return m_Data; }
236 
243  template< typename 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< typename 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< typename 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< typename 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+(TValue 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-(TValue 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< typename 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-=(TValue 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< typename 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+=(TValue 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< typename 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< typename 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  TValue * m_Data; // Array to hold data
502 };
503 
507 template< typename TValue, typename T >
508 inline
510 operator*(const T & scalar, const VariableLengthVector< TValue > & v)
511 {
512  return v.operator*(scalar);
513 }
514 
515 
516 template< typename TValue >
517 std::ostream & operator<<(std::ostream & os, const VariableLengthVector< TValue > & arr)
518 {
519  const unsigned int length = arr.Size();
520  const signed int last = (unsigned int)length - 1;
521 
522  os << "[";
523  for ( signed int i = 0; i < last; ++i )
524  {
525  os << arr[i] << ", ";
526  }
527  if ( length >= 1 )
528  {
529  os << arr[last];
530  }
531  os << "]";
532  return os;
533 }
534 } // namespace itk
535 
537 
538 #ifndef ITK_MANUAL_INSTANTIATION
539 #include "itkVariableLengthVector.hxx"
540 #endif
541 
542 #endif
void Reserve(ElementIdentifier)
TValue & operator[](unsigned int i)
unsigned int Size(void) const
VariableLengthVector(const VariableLengthVector< T > &v)
unsigned int GetNumberOfElements(void) const
bool operator==(const Self &v) const
CovariantVector< T, NVectorDimension > operator*(const T &scalar, const CovariantVector< T, NVectorDimension > &v)
RealValueType GetNorm() const
void SetElement(unsigned int i, const TValue &value)
RealValueType GetSquaredNorm() const
Self operator+(const VariableLengthVector< T > &v) const
const TValue & GetElement(unsigned int i) const
Self operator-(const VariableLengthVector< T > &v) const
bool operator!=(const Self &v) const
const TValue * GetDataPointer() const
unsigned int GetSize(void) const
Represents an array whose length can be defined at run-time.
TValue * AllocateElements(ElementIdentifier size) const
void SetSize(unsigned int sz, bool destroyExistingData=true)
NumericTraits< ValueType >::RealType RealValueType
void SetData(TValue *data, bool LetArrayManageMemory=false)
void Fill(TValue const &v)
const VariableLengthVector< TValue > & operator=(const VariableLengthVector< T > &v)
TValue const & operator[](unsigned int i) const