From 8cae692103e2ed9aee5bc061fac42579398c6aef Mon Sep 17 00:00:00 2001 From: Jeff Hill Date: Wed, 8 Nov 2000 01:02:04 +0000 Subject: [PATCH] fixed iterator performance --- src/libCom/cxxTemplates/tsSLList.h | 86 +++++------------------------- 1 file changed, 14 insertions(+), 72 deletions(-) diff --git a/src/libCom/cxxTemplates/tsSLList.h b/src/libCom/cxxTemplates/tsSLList.h index f901eeaf6..c2ba7531c 100644 --- a/src/libCom/cxxTemplates/tsSLList.h +++ b/src/libCom/cxxTemplates/tsSLList.h @@ -58,15 +58,10 @@ friend class tsSLList; friend class tsSLIter; friend class tsSLIterRm; public: - tsSLNode (); - void operator = (const tsSLNode &) const; - private: - void removeNextItem (); // removes the item after this node - T *pNext; }; @@ -79,15 +74,10 @@ template class tsSLList : public tsSLNode { public: tsSLList (); // creates an empty list - void insert (T &item, tsSLNode &itemBefore); // insert after item before - void add (T &item); // add to the beginning of the list - T * get (); // remove from the beginning of the list - T * pop (); // same as get - void push (T &item); // same as add private: tsSLList (const tsSLList &); // intentionally _not_ implemented @@ -100,14 +90,10 @@ template class tsSLIter { public: tsSLIter (const tsSLList &listIn); - T * next (); // move iterator forward - T * operator () (); // same as next () - private: T *pCurrent; - const tsSLList *pList; // ptr allows cpy op }; // @@ -124,17 +110,12 @@ public: class noCurrentItemInIterator {}; tsSLIterRm (tsSLList &listIn); - T * next (); // move iterator forward - T * operator () (); // same as next () - void remove (); // remove current node - private: T *pPrevious; T *pCurrent; - tsSLList *pList; // ptr allows cpy op }; ////////////////////////////////////////// @@ -249,34 +230,20 @@ inline void tsSLList::push(T &item) // // tsSLIter::tsSLIter // -template -inline tsSLIter::tsSLIter (const tsSLList &listIn) : - pCurrent (0), pList (&listIn) {} +template < class T > +inline tsSLIter < T > :: tsSLIter ( const tsSLList < T > &listIn ) : + pCurrent ( &listIn ) {} // // tsSLIter::next () // // move iterator forward // -// NULL test here is inefficient, but it appears that some architectures -// (intel) dont like to cast a NULL pointer from a tsSLNode to a T even if -// tsSLNode is always a base class of a T. -// -template -inline T * tsSLIter::next () +template < class T > +inline T * tsSLIter < T > :: next () { - if (this->pCurrent!=0) { - tsSLNode *pCurNode = this->pCurrent; - this->pCurrent = pCurNode->pNext; - } - else { - const tsSLNode &first = *this->pList; - // - // assume that we are starting (or restarting) at the - // beginning of the list - // - this->pCurrent = first.pNext; - } + tsSLNode < T > *pCurNode = this->pCurrent; + this->pCurrent = pCurNode->pNext; return this->pCurrent; } @@ -313,8 +280,8 @@ inline T * tsSLIter::operator () () // tsSLIterRm::tsSLIterRm () // template -inline tsSLIterRm::tsSLIterRm (tsSLList &listIn) : - pPrevious (0), pCurrent (0), pList (&listIn) {} +inline tsSLIterRm::tsSLIterRm ( tsSLList &listIn ) : + pPrevious ( 0 ), pCurrent ( &listIn ) {} // @@ -329,20 +296,9 @@ inline tsSLIterRm::tsSLIterRm (tsSLList &listIn) : template inline T * tsSLIterRm::next () { - if (this->pCurrent!=0) { - tsSLNode *pCurNode = this->pCurrent; - this->pPrevious = this->pCurrent; - this->pCurrent = pCurNode->pNext; - } - else { - const tsSLNode &first = *this->pList; - // - // assume that we are starting (or restarting) at the - // beginning of the list - // - this->pCurrent = first.pNext; - this->pPrevious = 0; - } + tsSLNode *pCurNode = this->pCurrent; + this->pPrevious = this->pCurrent; + this->pCurrent = pCurNode->pNext; return this->pCurrent; } @@ -372,27 +328,13 @@ inline T * tsSLIterRm::operator () () template void tsSLIterRm::remove () { - if (this->pCurrent==0) { + if ( this->pCurrent == 0 || this->pPrevious == 0 ) { throwWithLocation ( noCurrentItemInIterator () ); } - tsSLNode *pPrevNode; + tsSLNode *pPrevNode = this->pPrevious; tsSLNode *pCurNode = this->pCurrent; - if (this->pPrevious==0) { - pPrevNode = this->pList; - // - // fail if it is an attempt to - // delete twice without moving the iterator - // - if (pPrevNode->pNext != this->pCurrent) { - throwWithLocation ( noCurrentItemInIterator ()); - } - } - else { - pPrevNode = this->pPrevious; - } - pPrevNode->pNext = pCurNode->pNext; this->pCurrent = this->pPrevious; this->pPrevious = 0;