/* * Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. * * This file is part of the reasoning system Konclude. * For details and support, see . * * Konclude is free software: you can redistribute it and/or modify * it under the terms of version 3 of the GNU Lesser General Public * License (LGPLv3) as published by the Free Software Foundation. * * Konclude is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU (Lesser) General Public License for more details. * * You should have received a copy of the GNU (Lesser) General Public * License along with Konclude. If not, see . * */ #ifndef KONCLUDE_REASONER_TAXONOMY_CHIERARCHYDEPTHTRAVERSALITERATOR_CPP #define KONCLUDE_REASONER_TAXONOMY_CHIERARCHYDEPTHTRAVERSALITERATOR_CPP #include "CHierarchyDepthTraversalIterator.h" namespace Konclude { namespace Reasoner { namespace Taxonomy { template CHierarchyDepthTraversalIterator::CHierarchyDepthTraversalIterator() : mCurrentIterator(QSet()) { mTopNode = nullptr; mBottomNode = nullptr; mUnigueVisit = false; mCurrentNode = nullptr; } template CHierarchyDepthTraversalIterator::CHierarchyDepthTraversalIterator(N* topNode, N* bottomNode, bool forceUniqueVisit) : mCurrentIterator(*topNode->getChildNodeSet()) { mTopNode = topNode; mBottomNode = bottomNode; mUnigueVisit = forceUniqueVisit; mCurrentNode = topNode; mUniqueNodeSet.insert(mBottomNode); } template bool CHierarchyDepthTraversalIterator::hasNextHierarchyNode() { if (mCurrentIterator.hasNext()) { QSetIterator tmpIterator(mCurrentIterator); while (tmpIterator.hasNext()) { N* nextNode = tmpIterator.next(); if (!mUniqueNodeSet.contains(nextNode)) { return true; } } } for (typename QList< QSetIterator >::const_iterator it = mParentNodeIteratorList.constBegin(), itEnd = mParentNodeIteratorList.constEnd(); it != itEnd; ++it) { QSetIterator prevIt(*it); QSetIterator tmpIterator(prevIt); while (tmpIterator.hasNext()) { N* nextNode = tmpIterator.next(); if (!mUniqueNodeSet.contains(nextNode)) { return true; } } } return false; } template N* CHierarchyDepthTraversalIterator::nextHierarchyNode(bool moveNext) { N* node = nullptr; if (mCurrentIterator.hasNext()) { node = mCurrentIterator.peekNext(); while (node && mUniqueNodeSet.contains(node)) { mCurrentIterator.next(); if (mCurrentIterator.hasNext()) { node = mCurrentIterator.peekNext(); } else { node = nullptr; } } if (moveNext && node) { mCurrentIterator.next(); mParentNodeList.append(mCurrentNode); mParentNodeIteratorList.append(mCurrentIterator); mCurrentNode = node; mCurrentIterator = QSetIterator(*node->getChildNodeSet()); } } if (!node) { QListIterator lastNodeIt(mParentNodeList); lastNodeIt.toBack(); QListIterator< QSetIterator > lastIteratorIt(mParentNodeIteratorList); lastIteratorIt.toBack(); while (!node && lastNodeIt.hasPrevious()) { N* lastNode = lastNodeIt.previous(); QSetIterator lastIterator = lastIteratorIt.previous(); if (lastIterator.hasNext()) { node = lastIterator.peekNext(); while (node && mUniqueNodeSet.contains(node)) { lastIterator.next(); if (lastIterator.hasNext()) { node = lastIterator.peekNext(); } else { node = nullptr; } } } if (moveNext && node) { mClosedNodeBranchesList.append(mCurrentNode); QListIterator lastNodeRemIt(mParentNodeList); lastNodeRemIt.toBack(); while (lastNodeRemIt.previous() != lastNode) { mClosedNodeBranchesList.append(mParentNodeList.takeLast()); mParentNodeIteratorList.removeLast(); } mParentNodeList.removeLast(); mParentNodeIteratorList.removeLast(); lastIterator.next(); mParentNodeList.append(lastNode); mParentNodeIteratorList.append(lastIterator); mCurrentNode = node; mCurrentIterator = QSetIterator(*node->getChildNodeSet()); } } } if (node && mUnigueVisit) { mUniqueNodeSet.insert(node); } return node; } template CHierarchyNodeIterator CHierarchyDepthTraversalIterator::getTraversedParentNodeIterator() { return CHierarchyNodeIterator(&mParentNodeList,true); } template CHierarchyNodeIterator CHierarchyDepthTraversalIterator::getClosedTraversedNodeIterator() { return CHierarchyNodeIterator(&mClosedNodeBranchesList,true); } }; // end namespace Taxonomy }; // end namespace Reasoner }; // end namespace Konclude #endif // KONCLUDE_REASONER_TAXONOMY_CHIERARCHYDEPTHTRAVERSALITERATOR_CPP