/*========================================================================= * * Copyright NumFOCUS * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * https://www.apache.org/licenses/LICENSE-2.0.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * *=========================================================================*/ #ifndef itkLeafTreeIterator_h #define itkLeafTreeIterator_h #include "itkPreOrderTreeIterator.h" namespace itk { template class ITK_TEMPLATE_EXPORT LeafTreeIterator : public TreeIteratorBase { public: /** Typedefs */ using Self = LeafTreeIterator; using Superclass = TreeIteratorBase; using TreeType = TTreeType; using ValueType = typename TreeType::ValueType; using typename Superclass::TreeNodeType; using typename Superclass::NodeType; /** Constructor */ LeafTreeIterator(const TreeType * tree); /** Constructor */ LeafTreeIterator(TreeType * tree); /** Destructor */ ~LeafTreeIterator() override; /** Return the type of iterator */ NodeType GetType() const override; /** Clone function */ TreeIteratorBase * Clone() override; protected: /** Return the next value */ const ValueType & Next() override; /** Return true if the next value exists */ bool HasNext() const override; private: /** Find the next node */ const TreeNodeType * FindNextNode() const; }; /** Constructor */ template LeafTreeIterator::LeafTreeIterator(const TTreeType * tree) : TreeIteratorBase(tree, nullptr) { this->m_Begin = const_cast(this->FindNextNode()); // // // Position // the // // iterator // to // the // first // leaf; } /** Constructor */ template LeafTreeIterator::LeafTreeIterator(TTreeType * tree) : TreeIteratorBase(tree, nullptr) { this->m_Begin = const_cast(this->FindNextNode()); // // // Position // the // // iterator // to // the // first // leaf; } /** Destructor */ template LeafTreeIterator::~LeafTreeIterator() = default; /** Return the type of iterator */ template auto LeafTreeIterator::GetType() const -> NodeType { return TreeIteratorBaseEnums::TreeIteratorBaseNode::LEAF; } /** Return true if the next value exists */ template bool LeafTreeIterator::HasNext() const { if (this->m_Position == nullptr) { return false; } if (const_cast(FindNextNode()) != nullptr) { return true; } return false; } /** Return the next node */ template const typename LeafTreeIterator::ValueType & LeafTreeIterator::Next() { this->m_Position = const_cast(FindNextNode()); if (this->m_Position == nullptr) { return this->m_Root->Get(); // value irrelevant, but we have to return something } return this->m_Position->Get(); } /** Find the next node given the position */ template const typename LeafTreeIterator::TreeNodeType * LeafTreeIterator::FindNextNode() const { PreOrderTreeIterator it(this->m_Tree, this->m_Position); it.m_Root = this->m_Root; ++it; // go next if (it.IsAtEnd()) { return nullptr; } if (!it.HasChild()) { return it.GetNode(); } while (!it.IsAtEnd()) { if (!it.HasChild()) { return it.GetNode(); } ++it; } return nullptr; } /** Clone function */ template TreeIteratorBase * LeafTreeIterator::Clone() { auto * clone = new LeafTreeIterator(this->m_Tree); *clone = *this; return clone; } } // end namespace itk #endif