| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkVectorContainer.h.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 |
|
#ifndef __itkVectorContainer_h |
| 18 |
|
#define __itkVectorContainer_h |
| 19 |
|
|
| 20 |
|
#include "itkObject.h" |
| 21 |
|
#include "itkObjectFactory.h" |
| 22 |
|
|
| 23 |
|
#include <utility> |
| 24 |
|
#include <vector> |
| 25 |
|
|
| 26 |
|
namespace itk |
| 27 |
|
{ |
| 28 |
|
|
| 29 |
|
/** \class VectorContainer |
| 30 |
|
* Define a front-end to the STL "vector" container that conforms to the |
| 31 |
|
* IndexedContainerInterface. This is a full-fleged Object, so |
| 32 |
|
* there is modification time, debug, and reference count information. |
| 33 |
|
* |
| 34 |
|
* Template parameters for VectorContainer: |
| 35 |
|
* |
| 36 |
|
* TElementIdentifier = |
| 37 |
|
* An INTEGRAL type for use in indexing the vector. |
| 38 |
|
* |
| 39 |
|
* TElement = |
| 40 |
|
* The element type stored in the container. |
| 41 |
|
* |
| 42 |
|
* \ingroup DataRepresentation |
| 43 |
|
*/ |
| 44 |
|
template < |
| 45 |
|
typename TElementIdentifier, |
| 46 |
|
typename TElement |
| 47 |
IND |
**> |
| 48 |
|
class ITK_EXPORT VectorContainer: |
| 49 |
IND |
**public Object, |
| 50 |
IND |
**public std::vector<TElement> |
| 51 |
|
{ |
| 52 |
|
public: |
| 53 |
|
/** Standard class typedefs. */ |
| 54 |
|
typedef VectorContainer Self; |
| 55 |
TDA |
typedef Object Superclass; |
| 56 |
|
typedef SmartPointer<Self> Pointer; |
| 57 |
TDA |
typedef SmartPointer<const Self> ConstPointer; |
| 58 |
|
|
| 59 |
|
/** Save the template parameters. */ |
| 60 |
|
typedef TElementIdentifier ElementIdentifier; |
| 61 |
|
typedef TElement Element; |
| 62 |
|
|
| 63 |
|
private: |
| 64 |
|
/** Quick access to the STL vector type that was inherited. */ |
| 65 |
|
typedef std::vector<Element> VectorType; |
| 66 |
TDA,TDR |
typedef typename VectorType::size_type size_type; |
| 67 |
TDA |
typedef typename VectorType::iterator VectorIterator; |
| 68 |
TDA |
typedef typename VectorType::const_iterator VectorConstIterator; |
| 69 |
|
|
| 70 |
|
protected: |
| 71 |
|
/** Provide pass-through constructors corresponding to all the STL |
| 72 |
|
* vector constructors. These are for internal use only since this is also |
| 73 |
|
* an Object which must be constructed through the "New()" routine. */ |
| 74 |
|
VectorContainer(): |
| 75 |
IND |
****Object(), VectorType() {} |
| 76 |
|
VectorContainer(size_type n): |
| 77 |
IND |
****Object(), VectorType(n) {} |
| 78 |
|
VectorContainer(size_type n, const Element& x): |
| 79 |
IND |
****Object(), VectorType(n, x) {} |
| 80 |
|
VectorContainer(const Self& r): |
| 81 |
IND |
****Object(), VectorType(r) {} |
| 82 |
|
template <typename InputIterator> |
| 83 |
|
VectorContainer(InputIterator first, InputIterator last): |
| 84 |
IND |
****Object(), VectorType(first, last) {} |
| 85 |
|
|
| 86 |
|
public: |
| 87 |
|
|
| 88 |
|
/** This type is provided to Adapt this container as an STL container */ |
| 89 |
|
typedef VectorType STLContainerType; |
| 90 |
|
|
| 91 |
|
/** Method for creation through the object factory. */ |
| 92 |
|
itkNewMacro(Self); |
| 93 |
|
|
| 94 |
|
/** Standard part of every itk Object. */ |
| 95 |
|
itkTypeMacro(VectorContainer, Object); |
| 96 |
|
|
| 97 |
|
/** Convenient typedefs for the iterator and const iterator. */ |
| 98 |
|
class Iterator; |
| 99 |
|
class ConstIterator; |
| 100 |
|
|
| 101 |
|
/** Cast the container to a STL container type */ |
| 102 |
|
STLContainerType & CastToSTLContainer() { |
| 103 |
IND |
*****return dynamic_cast<STLContainerType &>(*this); } |
| 104 |
|
|
| 105 |
|
/** Cast the container to a const STL container type */ |
| 106 |
|
const STLContainerType & CastToSTLConstContainer() const { |
| 107 |
IND |
*****return dynamic_cast<const STLContainerType &>(*this); } |
| 108 |
|
|
| 109 |
|
/** Friends to this class. */ |
| 110 |
|
friend class Iterator; |
| 111 |
|
friend class ConstIterator; |
| 112 |
|
|
| 113 |
|
/** Simulate STL-map style iteration where dereferencing the iterator |
| 114 |
|
* gives access to both the index and the value. */ |
| 115 |
|
class Iterator |
| 116 |
MCM,IND |
**{ |
| 117 |
|
public: |
| 118 |
|
Iterator() {} |
| 119 |
|
Iterator(size_type d, const VectorIterator& i): m_Pos(d), m_Iter(i) {} |
| 120 |
|
|
| 121 |
|
Iterator& operator* () { return *this; } |
| 122 |
|
Iterator* operator-> () { return this; } |
| 123 |
|
Iterator& operator++ () { ++m_Pos; ++m_Iter; return *this; } |
| 124 |
LEN |
Iterator operator++ (int) { Iterator temp(*this); ++m_Pos; ++m_Iter; return temp; } |
| 125 |
|
Iterator& operator-- () { --m_Pos; --m_Iter; return *this; } |
| 126 |
LEN |
Iterator operator-- (int) { Iterator temp(*this); --m_Pos; --m_Iter; return temp; } |
| 127 |
|
|
| 128 |
|
bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; } |
| 129 |
|
bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; } |
| 130 |
LEN |
bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; } |
| 131 |
LEN |
bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; } |
| 132 |
|
|
| 133 |
LEN |
/** Get the index into the VectorContainer associated with this iterator. */ |
| 134 |
LEN |
ElementIdentifier Index(void) const { return static_cast<ElementIdentifier>( m_Pos ); } |
| 135 |
|
|
| 136 |
|
/** Get the value at this iterator's location in the VectorContainer. */ |
| 137 |
|
Element& Value(void) const { return *m_Iter; } |
| 138 |
|
|
| 139 |
|
private: |
| 140 |
|
size_type m_Pos; |
| 141 |
|
VectorIterator m_Iter; |
| 142 |
|
friend class ConstIterator; |
| 143 |
IND |
**}; |
| 144 |
|
|
| 145 |
|
/** Simulate STL-map style const iteration where dereferencing the iterator |
| 146 |
|
* gives read access to both the index and the value. */ |
| 147 |
|
class ConstIterator |
| 148 |
MCM,IND |
**{ |
| 149 |
|
public: |
| 150 |
|
ConstIterator() {} |
| 151 |
LEN |
ConstIterator(size_type d, const VectorConstIterator& i): m_Pos(d), m_Iter(i) {} |
| 152 |
|
ConstIterator(const Iterator& r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; } |
| 153 |
|
|
| 154 |
|
ConstIterator& operator* () { return *this; } |
| 155 |
|
ConstIterator* operator-> () { return this; } |
| 156 |
|
ConstIterator& operator++ () { ++m_Pos; ++m_Iter; return *this; } |
| 157 |
LEN |
ConstIterator operator++ (int) { ConstIterator temp(*this); ++m_Pos; ++m_Iter; return temp; } |
| 158 |
|
ConstIterator& operator-- () { --m_Pos; --m_Iter; return *this; } |
| 159 |
LEN |
ConstIterator operator-- (int) { ConstIterator temp(*this); --m_Pos; --m_Iter; return temp; } |
| 160 |
|
|
| 161 |
LEN |
ConstIterator& operator = (const Iterator& r) { m_Pos = r.m_Pos; m_Iter = r.m_Iter; return *this; } |
| 162 |
|
|
| 163 |
|
bool operator == (const Iterator& r) const { return m_Iter == r.m_Iter; } |
| 164 |
|
bool operator != (const Iterator& r) const { return m_Iter != r.m_Iter; } |
| 165 |
LEN |
bool operator == (const ConstIterator& r) const { return m_Iter == r.m_Iter; } |
| 166 |
LEN |
bool operator != (const ConstIterator& r) const { return m_Iter != r.m_Iter; } |
| 167 |
|
|
| 168 |
LEN |
/** Get the index into the VectorContainer associated with this iterator. */ |
| 169 |
LEN |
ElementIdentifier Index(void) const { return static_cast<ElementIdentifier>( m_Pos ); } |
| 170 |
|
|
| 171 |
|
/** Get the value at this iterator's location in the VectorContainer. */ |
| 172 |
|
const Element& Value(void) const { return *m_Iter; } |
| 173 |
|
|
| 174 |
|
private: |
| 175 |
|
size_type m_Pos; |
| 176 |
|
VectorConstIterator m_Iter; |
| 177 |
|
friend class Iterator; |
| 178 |
IND |
**}; |
| 179 |
|
|
| 180 |
|
/** Declare the public interface routines. */ |
| 181 |
|
Element& ElementAt(ElementIdentifier); |
| 182 |
|
const Element& ElementAt(ElementIdentifier) const; |
| 183 |
|
Element& CreateElementAt(ElementIdentifier); |
| 184 |
|
Element GetElement(ElementIdentifier) const; |
| 185 |
|
void SetElement(ElementIdentifier, Element); |
| 186 |
|
void InsertElement(ElementIdentifier, Element); |
| 187 |
|
bool IndexExists(ElementIdentifier) const; |
| 188 |
|
bool GetElementIfIndexExists(ElementIdentifier, Element*) const; |
| 189 |
|
void CreateIndex(ElementIdentifier); |
| 190 |
|
void DeleteIndex(ElementIdentifier); |
| 191 |
|
ConstIterator Begin(void) const; |
| 192 |
|
ConstIterator End(void) const; |
| 193 |
|
Iterator Begin(void); |
| 194 |
|
Iterator End(void); |
| 195 |
|
unsigned long Size(void) const; |
| 196 |
|
void Reserve(ElementIdentifier); |
| 197 |
|
void Squeeze(void); |
| 198 |
|
void Initialize(void); |
| 199 |
|
|
| 200 |
|
}; |
| 201 |
|
|
| 202 |
|
} // end namespace itk |
| 203 |
|
|
| 204 |
|
#ifndef ITK_MANUAL_INSTANTIATION |
| 205 |
|
#include "itkVectorContainer.txx" |
| 206 |
|
#endif |
| 207 |
|
|
| 208 |
|
#endif |
| 209 |
|
|