| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
#ifndef KONCLUDE_REASONER_TAXONOMY_CHIERARCHYDEPTHTRAVERSALITERATOR_CPP |
| 21 |
#define KONCLUDE_REASONER_TAXONOMY_CHIERARCHYDEPTHTRAVERSALITERATOR_CPP |
| 22 |
|
| 23 |
|
| 24 |
#include "CHierarchyDepthTraversalIterator.h" |
| 25 |
|
| 26 |
|
| 27 |
namespace Konclude { |
| 28 |
|
| 29 |
namespace Reasoner { |
| 30 |
|
| 31 |
namespace Taxonomy { |
| 32 |
|
| 33 |
|
| 34 |
template<class N> |
| 35 |
CHierarchyDepthTraversalIterator<N>::CHierarchyDepthTraversalIterator() : mCurrentIterator(QSet<N*>()) { |
| 36 |
mTopNode = nullptr; |
| 37 |
mBottomNode = nullptr; |
| 38 |
mUnigueVisit = false; |
| 39 |
mCurrentNode = nullptr; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
template<class N> |
| 44 |
CHierarchyDepthTraversalIterator<N>::CHierarchyDepthTraversalIterator(N* topNode, N* bottomNode, bool forceUniqueVisit) : mCurrentIterator(*topNode->getChildNodeSet()) { |
| 45 |
mTopNode = topNode; |
| 46 |
mBottomNode = bottomNode; |
| 47 |
mUnigueVisit = forceUniqueVisit; |
| 48 |
mCurrentNode = topNode; |
| 49 |
mUniqueNodeSet.insert(mBottomNode); |
| 50 |
} |
| 51 |
|
| 52 |
template<class N> |
| 53 |
bool CHierarchyDepthTraversalIterator<N>::hasNextHierarchyNode() { |
| 54 |
if (mCurrentIterator.hasNext()) { |
| 55 |
QSetIterator<N*> tmpIterator(mCurrentIterator); |
| 56 |
while (tmpIterator.hasNext()) { |
| 57 |
N* nextNode = tmpIterator.next(); |
| 58 |
if (!mUniqueNodeSet.contains(nextNode)) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
for (typename QList< QSetIterator<N*> >::const_iterator it = mParentNodeIteratorList.constBegin(), itEnd = mParentNodeIteratorList.constEnd(); it != itEnd; ++it) { |
| 64 |
QSetIterator<N*> prevIt(*it); |
| 65 |
QSetIterator<N*> tmpIterator(prevIt); |
| 66 |
while (tmpIterator.hasNext()) { |
| 67 |
N* nextNode = tmpIterator.next(); |
| 68 |
if (!mUniqueNodeSet.contains(nextNode)) { |
| 69 |
return true; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
template<class N> |
| 77 |
N* CHierarchyDepthTraversalIterator<N>::nextHierarchyNode(bool moveNext) { |
| 78 |
N* node = nullptr; |
| 79 |
if (mCurrentIterator.hasNext()) { |
| 80 |
node = mCurrentIterator.peekNext(); |
| 81 |
while (node && mUniqueNodeSet.contains(node)) { |
| 82 |
mCurrentIterator.next(); |
| 83 |
if (mCurrentIterator.hasNext()) { |
| 84 |
node = mCurrentIterator.peekNext(); |
| 85 |
} else { |
| 86 |
node = nullptr; |
| 87 |
} |
| 88 |
} |
| 89 |
if (moveNext && node) { |
| 90 |
mCurrentIterator.next(); |
| 91 |
mParentNodeList.append(mCurrentNode); |
| 92 |
mParentNodeIteratorList.append(mCurrentIterator); |
| 93 |
mCurrentNode = node; |
| 94 |
mCurrentIterator = QSetIterator<N*>(*node->getChildNodeSet()); |
| 95 |
} |
| 96 |
} |
| 97 |
if (!node) { |
| 98 |
QListIterator<N*> lastNodeIt(mParentNodeList); |
| 99 |
lastNodeIt.toBack(); |
| 100 |
QListIterator< QSetIterator<N*> > lastIteratorIt(mParentNodeIteratorList); |
| 101 |
lastIteratorIt.toBack(); |
| 102 |
|
| 103 |
while (!node && lastNodeIt.hasPrevious()) { |
| 104 |
N* lastNode = lastNodeIt.previous(); |
| 105 |
QSetIterator<N*> lastIterator = lastIteratorIt.previous(); |
| 106 |
if (lastIterator.hasNext()) { |
| 107 |
node = lastIterator.peekNext(); |
| 108 |
while (node && mUniqueNodeSet.contains(node)) { |
| 109 |
lastIterator.next(); |
| 110 |
if (lastIterator.hasNext()) { |
| 111 |
node = lastIterator.peekNext(); |
| 112 |
} else { |
| 113 |
node = nullptr; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
if (moveNext && node) { |
| 118 |
mClosedNodeBranchesList.append(mCurrentNode); |
| 119 |
QListIterator<N*> lastNodeRemIt(mParentNodeList); |
| 120 |
lastNodeRemIt.toBack(); |
| 121 |
while (lastNodeRemIt.previous() != lastNode) { |
| 122 |
mClosedNodeBranchesList.append(mParentNodeList.takeLast()); |
| 123 |
mParentNodeIteratorList.removeLast(); |
| 124 |
} |
| 125 |
mParentNodeList.removeLast(); |
| 126 |
mParentNodeIteratorList.removeLast(); |
| 127 |
lastIterator.next(); |
| 128 |
mParentNodeList.append(lastNode); |
| 129 |
mParentNodeIteratorList.append(lastIterator); |
| 130 |
mCurrentNode = node; |
| 131 |
mCurrentIterator = QSetIterator<N*>(*node->getChildNodeSet()); |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
if (node && mUnigueVisit) { |
| 136 |
mUniqueNodeSet.insert(node); |
| 137 |
} |
| 138 |
|
| 139 |
return node; |
| 140 |
} |
| 141 |
|
| 142 |
template<class N> |
| 143 |
CHierarchyNodeIterator<N> CHierarchyDepthTraversalIterator<N>::getTraversedParentNodeIterator() { |
| 144 |
return CHierarchyNodeIterator<N>(&mParentNodeList,true); |
| 145 |
} |
| 146 |
|
| 147 |
template<class N> |
| 148 |
CHierarchyNodeIterator<N> CHierarchyDepthTraversalIterator<N>::getClosedTraversedNodeIterator() { |
| 149 |
return CHierarchyNodeIterator<N>(&mClosedNodeBranchesList,true); |
| 150 |
} |
| 151 |
|
| 152 |
}; // end namespace Taxonomy |
| 153 |
|
| 154 |
}; // end namespace Reasoner |
| 155 |
|
| 156 |
}; // end namespace Konclude |
| 157 |
|
| 158 |
#endif // KONCLUDE_REASONER_TAXONOMY_CHIERARCHYDEPTHTRAVERSALITERATOR_CPP |