| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkArray.h.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 |
|
#ifndef __itkArray_h |
| 18 |
|
#define __itkArray_h |
| 19 |
|
|
| 20 |
|
#include "itkMacro.h" |
| 21 |
|
#include "vnl/vnl_vector.h" |
| 22 |
|
|
| 23 |
|
namespace itk |
| 24 |
|
{ |
| 25 |
|
|
| 26 |
|
|
| 27 |
|
/** \class Array |
| 28 |
|
* \brief Array class with size defined at construction time. |
| 29 |
|
* |
| 30 |
|
* This class derives from the vnl_vector<> class. |
| 31 |
|
* Its size is assigned at construction time (run time) and can |
| 32 |
|
* not be changed afterwards except by using assignment to another |
| 33 |
|
* Array. |
| 34 |
|
* |
| 35 |
|
* The class is templated over the type of the elements. |
| 36 |
|
* |
| 37 |
|
* Template parameters for class Array: |
| 38 |
|
* |
| 39 |
|
* - TValueType = Element type stored at each location in the array. |
| 40 |
|
* |
| 41 |
|
* \ingroup DataRepresentation |
| 42 |
|
*/ |
| 43 |
|
template <typename TValueType > |
| 44 |
|
class Array : public vnl_vector< TValueType > |
| 45 |
|
{ |
| 46 |
|
public: |
| 47 |
|
|
| 48 |
|
/** The element type stored at each location in the Array. */ |
| 49 |
|
typedef TValueType ValueType; |
| 50 |
TDA |
typedef Array Self; |
| 51 |
TDA |
typedef vnl_vector<TValueType> VnlVectorType; |
| 52 |
|
|
| 53 |
|
public: |
| 54 |
|
|
| 55 |
|
/** Default constructor. It is created with an empty array |
| 56 |
|
* it has to be allocated later by assignment */ |
| 57 |
|
Array(); |
| 58 |
|
|
| 59 |
|
/** Constructor with size. Size can only be changed by assignment */ |
| 60 |
|
Array(unsigned int dimension); |
| 61 |
|
|
| 62 |
|
/** Constructor that initializes array with contents from a user supplied |
| 63 |
|
* buffer. The pointer to the buffer and the length is specified. By default, |
| 64 |
|
* the array does not manage the memory of the buffer. It merely points to |
| 65 |
|
* that location and it is the user's responsibility to delete it. |
| 66 |
|
* If "LetArrayManageMemory" is true, then this class will free the |
| 67 |
|
* memory when this object is destroyed. */ |
| 68 |
|
Array( ValueType* data, unsigned int sz, bool LetArrayManageMemory = false); |
| 69 |
|
|
| 70 |
|
/** Constructor that initializes array with contents from a user supplied |
| 71 |
|
* buffer. The pointer to the buffer and the length is specified. By default, |
| 72 |
|
* the array does not manage the memory of the buffer. It merely points to |
| 73 |
|
* that location and it is the user's responsibility to delete it. |
| 74 |
|
* If "LetArrayManageMemory" is true, then this class will free the |
| 75 |
|
* memory when this object is destroyed. */ |
| 76 |
LEN |
Array( const ValueType* data, unsigned int sz, bool LetArrayManageMemory = false); |
| 77 |
|
|
| 78 |
|
|
| 79 |
|
/** Set the all the elements of the array to the specified value */ |
| 80 |
|
void Fill (TValueType const& v) { fill(v); } |
| 81 |
|
|
| 82 |
|
/** Copy opertor */ |
| 83 |
|
const Self & operator=( const Self &rhs ); |
| 84 |
|
const Self & operator=( const VnlVectorType & rhs ); |
| 85 |
|
|
| 86 |
|
/** Return the number of elements in the Array */ |
| 87 |
|
unsigned int Size (void ) const |
| 88 |
IND |
******{ return static_cast<unsigned int>( this->size() ); } |
| 89 |
|
unsigned int GetNumberOfElements(void) const |
| 90 |
IND |
******{ return static_cast<unsigned int>( this->size() ); } |
| 91 |
|
|
| 92 |
|
/** Get one element */ |
| 93 |
|
const TValueType & GetElement( unsigned int i ) const |
| 94 |
|
{ return this->operator[]( i ); } |
| 95 |
|
|
| 96 |
|
/** Set one element */ |
| 97 |
|
void SetElement( unsigned int i, const TValueType & value ) |
| 98 |
|
{ this->operator[]( i ) = value; } |
| 99 |
|
|
| 100 |
|
/** Destructively set the size to that given. Will lose data. */ |
| 101 |
|
void SetSize(unsigned int sz); |
| 102 |
|
unsigned int GetSize(void) const |
| 103 |
IND |
******{ return static_cast<unsigned int>( this->size() ); } |
| 104 |
|
|
| 105 |
|
/** Set the pointer from which the data is imported. |
| 106 |
|
* If "LetArrayManageMemory" is false, then the application retains |
| 107 |
|
* the responsibility of freeing the memory for this data. If |
| 108 |
|
* "LetArrayManageMemory" is true, then this class will free the |
| 109 |
|
* memory when this object is destroyed. */ |
| 110 |
|
void SetData(TValueType* data,bool LetArrayManageMemory = false); |
| 111 |
|
|
| 112 |
|
/** Similar to the previous method. In the above method, the size must be |
| 113 |
|
* seperately set prior to using user-supplied data. This introduces an |
| 114 |
|
* unnecessary allocation step to be performed. This method avoids it |
| 115 |
|
* and should be used to import data whereever possible to avoid this. |
| 116 |
|
* Set the pointer from which the data is imported. |
| 117 |
|
* If "LetArrayManageMemory" is false, then the application retains |
| 118 |
|
* the responsibility of freeing the memory for this data. If |
| 119 |
|
* "LetArrayManageMemory" is true, then this class will free the |
| 120 |
|
* memory when this object is destroyed. */ |
| 121 |
LEN |
void SetData(TValueType* data, unsigned int sz, bool LetArrayManageMemory = false); |
| 122 |
|
|
| 123 |
|
|
| 124 |
|
/** This destructor is not virtual for performance reasons. However, this |
| 125 |
|
* means that subclasses cannot allocate memory. */ |
| 126 |
|
~Array(); |
| 127 |
|
|
| 128 |
|
|
| 129 |
EML |
|
| 130 |
|
private: |
| 131 |
|
|
| 132 |
|
bool m_LetArrayManageMemory; |
| 133 |
|
|
| 134 |
|
}; |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
template <typename TValueType > |
| 139 |
|
std::ostream & operator<<(std::ostream &os, const Array<TValueType> &arr) |
| 140 |
|
{ |
| 141 |
|
const unsigned int length = arr.size(); |
| 142 |
|
const signed int last = (unsigned int) length - 1; |
| 143 |
|
|
| 144 |
|
os << "["; |
| 145 |
|
for (signed int i=0; i < last; ++i) |
| 146 |
|
{ |
| 147 |
|
os << arr[i] << ", "; |
| 148 |
|
} |
| 149 |
|
if (length >= 1) |
| 150 |
|
{ |
| 151 |
|
os << arr[last]; |
| 152 |
|
} |
| 153 |
|
os << "]"; |
| 154 |
|
return os; |
| 155 |
|
} |
| 156 |
|
|
| 157 |
|
} // namespace itk |
| 158 |
|
|
| 159 |
|
|
| 160 |
EML |
|
| 161 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 162 |
|
#include "itkArray.txx" |
| 163 |
|
#endif |
| 164 |
|
|
| 165 |
|
|
| 166 |
|
#endif |
| 167 |
|
|