1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library 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 GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SUIT_DataObjectIterator.h"
28 SUIT_DataObjectIterator::SUIT_DataObjectIterator( SUIT_DataObject* root, const int det, const bool fromTrueRoot )
32 if ( myRoot && fromTrueRoot )
33 myRoot = myRoot->root();
35 myCurrent = myExtremeChild = myRoot;
39 Gets parent for object \a obj.
41 SUIT_DataObject* SUIT_DataObjectIterator::parent( SUIT_DataObject* obj ) const
43 SUIT_DataObject* result = 0;
44 if ( obj && obj != myRoot )
45 result = obj->parent();
52 void SUIT_DataObjectIterator::operator++()
54 SUIT_DataObject* aNext = 0;
55 SUIT_DataObject* aParent = 0;
61 if ( myDetourType == DepthLeft || myDetourType == DepthRight )
63 //Depth detour algorithm
64 if ( myCurrent->myChildren.count() > 0 )
66 myCurrent = extreme( myCurrent->myChildren, myDetourType == DepthLeft );
67 myChildrenIndexes.append(myDetourType == DepthLeft ? 0 : myCurrent->myChildren.size() - 1);
72 aParent = parent( myCurrent );
75 myCurrent = 0; //the tree is passed completely
80 int idx = myChildrenIndexes.last();
81 if (myDetourType == DepthLeft && idx < aParent->myChildren.count() - 1)
83 myChildrenIndexes.last()++;
84 myCurrent = aParent->myChildren[idx + 1];
87 else if (myDetourType == DepthRight && idx > 0)
89 myChildrenIndexes.last()--;
90 myCurrent = aParent->myChildren[idx - 1];
94 myChildrenIndexes.removeLast();
100 else if ( myDetourType == BreadthLeft || myDetourType == BreadthRight )
102 //Breadth detour algorithm
103 aNext = globalSibling( myCurrent, myDetourType == BreadthLeft );
107 for ( SUIT_DataObject* cur = myExtremeChild; cur && !myCurrent; cur = globalSibling( cur, myDetourType == BreadthLeft ) )
109 if ( cur->myChildren.count() > 0 )
111 myExtremeChild = extreme( cur->myChildren, myDetourType == BreadthLeft );
112 myCurrent = myExtremeChild;
113 myChildrenIndexes.append(myDetourType == BreadthLeft ? 0 : myCurrent->myChildren.size() - 1);
124 Gets current data object.
126 SUIT_DataObject* SUIT_DataObjectIterator::current() const
132 Gets depth of current lavel.
134 int SUIT_DataObjectIterator::depth() const
136 return myChildrenIndexes.size();
142 int SUIT_DataObjectIterator::detour() const
148 Gets global sibling for object \a obj
150 SUIT_DataObject* SUIT_DataObjectIterator::globalSibling( SUIT_DataObject* obj, bool next ) const
152 SUIT_DataObject* par;
154 if ( obj && ( par = parent( obj ) ) )
156 int idx = par->myChildren.indexOf( obj );
157 if ( idx < par->myChildren.count() - 1 )
158 return par->myChildren[idx + 1];
161 for ( ; par; par = globalSibling( par, next ) )
163 if ( par->myChildren.count() > 0 )
164 return extreme( par->myChildren, next );
174 * Gets first or last data object from list.
175 * Get firls, if \a FromLeft == true, else last.
177 SUIT_DataObject* SUIT_DataObjectIterator::extreme( DataObjectList& aList, bool FromLeft ) const
180 return aList.first();
188 SUIT_DataObjectLevelIterator::SUIT_DataObjectLevelIterator( SUIT_DataObject* root,
189 int start, int end, bool LeftToRight )
190 : SUIT_DataObjectIterator( root, LeftToRight ? BreadthLeft : BreadthRight )
192 myStartLevel = start;
196 myEndLevel = myStartLevel;
198 while ( current() && depth() < myStartLevel )
199 SUIT_DataObjectIterator::operator++();
205 void SUIT_DataObjectLevelIterator::operator++()
209 SUIT_DataObjectIterator::operator++();
210 if ( depth() > myEndLevel )