From 8e92044718afdfa099596c5f9143955efc110f08 Mon Sep 17 00:00:00 2001 From: mpv Date: Fri, 4 May 2012 07:59:57 +0000 Subject: [PATCH] Optimization of the iterator "++": omitting of "indexOf" calling for the list of childred significantly improves performance --- src/SUIT/SUIT_DataObjectIterator.cxx | 31 +++++++++++++++------------- src/SUIT/SUIT_DataObjectIterator.h | 4 +++- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/SUIT/SUIT_DataObjectIterator.cxx b/src/SUIT/SUIT_DataObjectIterator.cxx index 971bdfe6b..a91ff2458 100644 --- a/src/SUIT/SUIT_DataObjectIterator.cxx +++ b/src/SUIT/SUIT_DataObjectIterator.cxx @@ -27,8 +27,7 @@ */ SUIT_DataObjectIterator::SUIT_DataObjectIterator( SUIT_DataObject* root, const int det, const bool fromTrueRoot ) : myRoot( root ), -myDetourType( det ), -myCurrentLevel( 0 ) +myDetourType( det ) { if ( myRoot && fromTrueRoot ) myRoot = myRoot->root(); @@ -65,7 +64,7 @@ void SUIT_DataObjectIterator::operator++() if ( myCurrent->myChildren.count() > 0 ) { myCurrent = extreme( myCurrent->myChildren, myDetourType == DepthLeft ); - myCurrentLevel++; + myChildrenIndexes.append(myDetourType == DepthLeft ? 0 : myCurrent->myChildren.size() - 1); } else do { @@ -78,18 +77,22 @@ void SUIT_DataObjectIterator::operator++() } else { - int idx = aParent->myChildren.indexOf( myCurrent ); - if ( myDetourType == DepthLeft ) - myCurrent = idx < aParent->myChildren.count() - 1 ? aParent->myChildren[idx + 1] : 0; - else - myCurrent = idx > 0 ? aParent->myChildren[idx - 1] : 0; - if ( !myCurrent ) + int idx = myChildrenIndexes.last(); + if (myDetourType == DepthLeft && idx < aParent->myChildren.count() - 1) { - myCurrent = aParent; - myCurrentLevel--; + myChildrenIndexes.last()++; + myCurrent = aParent->myChildren[idx + 1]; + exit = true; } - else + else if (myDetourType == DepthRight && idx > 0) + { + myChildrenIndexes.last()--; + myCurrent = aParent->myChildren[idx - 1]; exit = true; + } else { + myCurrent = aParent; + myChildrenIndexes.removeLast(); + } } } while ( !exit ); @@ -107,7 +110,7 @@ void SUIT_DataObjectIterator::operator++() { myExtremeChild = extreme( cur->myChildren, myDetourType == BreadthLeft ); myCurrent = myExtremeChild; - myCurrentLevel++; + myChildrenIndexes.append(myDetourType == BreadthLeft ? 0 : myCurrent->myChildren.size() - 1); } } } @@ -130,7 +133,7 @@ SUIT_DataObject* SUIT_DataObjectIterator::current() const */ int SUIT_DataObjectIterator::depth() const { - return myCurrentLevel; + return myChildrenIndexes.size(); } /*! diff --git a/src/SUIT/SUIT_DataObjectIterator.h b/src/SUIT/SUIT_DataObjectIterator.h index d3ed4783b..360a816c1 100644 --- a/src/SUIT/SUIT_DataObjectIterator.h +++ b/src/SUIT/SUIT_DataObjectIterator.h @@ -55,7 +55,9 @@ protected: private: SUIT_DataObject* myExtremeChild; - int myDetourType, myCurrentLevel; + int myDetourType; + ///! Indexes of the iterated children at each level (optimization for avoid indexOf call) + QList myChildrenIndexes; }; /*! -- 2.39.2