| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkChildTreeIterator.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:33 $ |
| 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 __itkChildTreeIterator_h |
| 18 |
|
#define __itkChildTreeIterator_h |
| 19 |
|
|
| 20 |
|
#include <itkTreeIteratorBase.h> |
| 21 |
|
|
| 22 |
|
|
| 23 |
NMS |
namespace itk{ |
| 24 |
|
|
| 25 |
|
template <class TTreeType> |
| 26 |
|
class ChildTreeIterator : public TreeIteratorBase<TTreeType> |
| 27 |
|
{ |
| 28 |
|
public: |
| 29 |
|
|
| 30 |
|
/** Typedefs */ |
| 31 |
|
typedef TreeIteratorBase<TTreeType> Superclass; |
| 32 |
TDA |
typedef TTreeType TreeType; |
| 33 |
TDA |
typedef typename Superclass::Self Self; |
| 34 |
TDA |
typedef typename TTreeType::ValueType ValueType; |
| 35 |
TDA |
typedef typename Superclass::TreeNodeType TreeNodeType; |
| 36 |
|
|
| 37 |
|
/** Constructor */ |
| 38 |
|
ChildTreeIterator( TreeType* tree,const TreeNodeType* start=NULL ); |
| 39 |
|
|
| 40 |
|
/** Constructor */ |
| 41 |
|
ChildTreeIterator( const TreeIteratorBase<TTreeType>& iterator ); |
| 42 |
|
|
| 43 |
|
/** Get the type of the iterator */ |
| 44 |
|
int GetType( ) const; |
| 45 |
|
|
| 46 |
|
/** Go to a specific child node */ |
| 47 |
|
virtual bool GoToChild( int number = 0 ); |
| 48 |
|
|
| 49 |
|
/** Go to a parent node */ |
| 50 |
|
virtual bool GoToParent(); |
| 51 |
|
|
| 52 |
|
/** Clone function */ |
| 53 |
|
TreeIteratorBase<TTreeType>* Clone(); |
| 54 |
|
|
| 55 |
|
/** operator = */ |
| 56 |
|
Self& operator=(Superclass& iterator) |
| 57 |
|
{ |
| 58 |
|
Superclass::operator=(iterator); |
| 59 |
LEN |
ChildTreeIterator<TTreeType>& it = static_cast<ChildTreeIterator<TTreeType>&>(iterator); |
| 60 |
|
m_ListPosition = it.m_ListPosition; |
| 61 |
|
m_ParentNode = it.m_ParentNode; |
| 62 |
|
return *this; |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
protected: |
| 66 |
|
|
| 67 |
|
/** Get the next value */ |
| 68 |
|
const ValueType& Next(); |
| 69 |
|
|
| 70 |
|
/** Return true if the next value exists */ |
| 71 |
|
bool HasNext() const; |
| 72 |
|
|
| 73 |
|
private: |
| 74 |
|
|
| 75 |
|
mutable int m_ListPosition; |
| 76 |
|
TreeNode<ValueType>* m_ParentNode; |
| 77 |
|
}; |
| 78 |
|
|
| 79 |
|
/** Constructor */ |
| 80 |
|
template <class TTreeType> |
| 81 |
LEN |
ChildTreeIterator<TTreeType>::ChildTreeIterator(TTreeType* tree, const TreeNodeType* start) |
| 82 |
IND |
**:TreeIteratorBase<TTreeType>(tree, start) |
| 83 |
|
{ |
| 84 |
|
m_ListPosition = 0; |
| 85 |
|
m_ParentNode = this->m_Position; |
| 86 |
|
this->m_Position = m_ParentNode->GetChild( m_ListPosition ); |
| 87 |
|
this->m_Begin = this->m_Position; |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
template <class TTreeType> |
| 91 |
LEN |
ChildTreeIterator<TTreeType>::ChildTreeIterator(const TreeIteratorBase<TTreeType>& iterator) |
| 92 |
IND |
**:TreeIteratorBase<TTreeType>(iterator.GetTree(), iterator.GetNode()) |
| 93 |
|
{ |
| 94 |
|
m_ListPosition = 0; |
| 95 |
|
m_ParentNode = this->m_Position; |
| 96 |
|
this->m_Position = m_ParentNode->GetChild( m_ListPosition ); |
| 97 |
|
} |
| 98 |
|
|
| 99 |
|
/** Go to a specific child */ |
| 100 |
|
template <class TTreeType> |
| 101 |
|
bool |
| 102 |
|
ChildTreeIterator<TTreeType>::GoToChild(int number) |
| 103 |
|
{ |
| 104 |
|
if ( m_ParentNode->GetChild( number ) == NULL ) |
| 105 |
|
{ |
| 106 |
|
return false; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
m_ListPosition = 0; |
| 110 |
|
m_ParentNode = m_ParentNode->GetChild( number ); |
| 111 |
|
this->m_Position = m_ParentNode->GetChild( m_ListPosition ); |
| 112 |
|
this->m_Begin = this->m_Position; |
| 113 |
|
return true; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
/** Go to the parent node */ |
| 117 |
|
template <class TTreeType> |
| 118 |
|
bool |
| 119 |
|
ChildTreeIterator<TTreeType>::GoToParent() |
| 120 |
|
{ |
| 121 |
|
TreeNode<ValueType>* parent = m_ParentNode->GetParent(); |
| 122 |
|
|
| 123 |
|
if ( parent == NULL ) |
| 124 |
|
{ |
| 125 |
|
return false; |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
m_ListPosition = 0; |
| 129 |
|
m_ParentNode = parent; |
| 130 |
|
this->m_Position = m_ParentNode->GetChild( m_ListPosition ); |
| 131 |
|
this->m_Begin = this->m_Position; |
| 132 |
|
return true; |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
/** Return the type of the iterator */ |
| 136 |
|
template <class TTreeType> |
| 137 |
|
int |
| 138 |
|
ChildTreeIterator<TTreeType>::GetType() const |
| 139 |
|
{ |
| 140 |
|
return TreeIteratorBase<TTreeType>::CHILD; |
| 141 |
|
} |
| 142 |
|
|
| 143 |
|
/** Return true if the next node exists */ |
| 144 |
|
template <class TTreeType> |
| 145 |
|
bool |
| 146 |
|
ChildTreeIterator<TTreeType>::HasNext() const |
| 147 |
|
{ |
| 148 |
|
if( m_ListPosition < m_ParentNode->CountChildren() - 1 ) |
| 149 |
|
{ |
| 150 |
|
return true; |
| 151 |
|
} |
| 152 |
|
else |
| 153 |
|
{ |
| 154 |
|
return false; |
| 155 |
|
} |
| 156 |
|
} |
| 157 |
|
|
| 158 |
|
/** Return the next node */ |
| 159 |
|
template <class TTreeType> |
| 160 |
|
const typename ChildTreeIterator<TTreeType>::ValueType& |
| 161 |
|
ChildTreeIterator<TTreeType>::Next() |
| 162 |
|
{ |
| 163 |
|
m_ListPosition++; |
| 164 |
|
this->m_Position = m_ParentNode->GetChild( m_ListPosition ); |
| 165 |
|
return this->m_Position->Get(); |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
/** Clone function */ |
| 169 |
|
template <class TTreeType> |
| 170 |
|
TreeIteratorBase<TTreeType>* ChildTreeIterator<TTreeType>::Clone() |
| 171 |
|
{ |
| 172 |
LEN |
ChildTreeIterator<TTreeType>* clone = new ChildTreeIterator<TTreeType>( const_cast<TTreeType*>(this->m_Tree), this->m_Position ); |
| 173 |
|
*clone = *this; |
| 174 |
|
return clone; |
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
} // end namespace itk |
| 178 |
|
|
| 179 |
|
#endif |
| 180 |
|
|