diff options
Diffstat (limited to 'src/tools/qglist.cpp')
-rw-r--r-- | src/tools/qglist.cpp | 355 |
1 files changed, 327 insertions, 28 deletions
diff --git a/src/tools/qglist.cpp b/src/tools/qglist.cpp index f1c89023b..6c0455c9c 100644 --- a/src/tools/qglist.cpp +++ b/src/tools/qglist.cpp @@ -43,6 +43,10 @@ #include "ntqdatastream.h" #include "ntqvaluelist.h" +#if defined(QT_THREAD_SUPPORT) + #include "ntqmutex.h" +#endif // defined(QT_THREAD_SUPPORT) + /*! \class TQLNode ntqglist.h \reentrant @@ -221,6 +225,9 @@ TQDataStream &TQGList::write( TQDataStream &s, TQPtrCollection::Item ) const TQGList::TQGList() { +#if defined(QT_THREAD_SUPPORT) + //mutex = new TQMutex(true); +#endif firstNode = lastNode = curNode = 0; // initialize list numNodes = 0; curIndex = -1; @@ -234,6 +241,9 @@ TQGList::TQGList() TQGList::TQGList( const TQGList & list ) : TQPtrCollection( list ) { +#if defined(QT_THREAD_SUPPORT) + //mutex = new TQMutex(true); +#endif firstNode = lastNode = curNode = 0; // initialize list numNodes = 0; curIndex = -1; @@ -258,6 +268,9 @@ TQGList::~TQGList() // twice on the same address! This is insane but let's try not to crash // here. iterators = 0; +#if defined(QT_THREAD_SUPPORT) + //delete mutex; +#endif } @@ -290,11 +303,13 @@ TQGList& TQGList::operator=( const TQGList &list ) bool TQGList::operator==( const TQGList &list ) const { - if ( count() != list.count() ) + if ( count() != list.count() ) { return FALSE; + } - if ( count() == 0 ) + if ( count() == 0 ) { return TRUE; + } TQLNode *n1 = firstNode; TQLNode *n2 = list.firstNode; @@ -322,8 +337,15 @@ bool TQGList::operator==( const TQGList &list ) const TQLNode *TQGList::locate( uint index ) { - if ( index == (uint)curIndex ) // current node ? +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif + if ( index == (uint)curIndex ) { // current node ? +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return curNode; + } if ( !curNode && firstNode ) { // set current node curNode = firstNode; curIndex = 0; @@ -332,13 +354,18 @@ TQLNode *TQGList::locate( uint index ) int distance = index - curIndex; // node distance to cur node bool forward; // direction to traverse - if ( index >= numNodes ) + if ( index >= numNodes ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return 0; + } - if ( distance < 0 ) + if ( distance < 0 ) { distance = -distance; + } if ( (uint)distance < index && (uint)distance < numNodes - index ) { - node = curNode; // start from current node + node = curNode; // start from current node forward = index > (uint)curIndex; } else if ( index < numNodes - index ) { // start from first node node = firstNode; @@ -352,13 +379,18 @@ TQLNode *TQGList::locate( uint index ) forward = FALSE; } if ( forward ) { // now run through nodes - while ( distance-- ) + while ( distance-- ) { node = node->next; + } } else { - while ( distance-- ) + while ( distance-- ) { node = node->prev; + } } curIndex = index; // must update index +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return curNode = node; } @@ -369,6 +401,9 @@ TQLNode *TQGList::locate( uint index ) void TQGList::inSort( TQPtrCollection::Item d ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif int index = 0; register TQLNode *n = firstNode; while ( n && compareItems(n->data,d) < 0 ){ // find position in list @@ -376,6 +411,9 @@ void TQGList::inSort( TQPtrCollection::Item d ) index++; } insertAt( index, d ); +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif } @@ -385,6 +423,9 @@ void TQGList::inSort( TQPtrCollection::Item d ) void TQGList::prepend( TQPtrCollection::Item d ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif register TQLNode *n = new TQLNode( newItem(d) ); TQ_CHECK_PTR( n ); n->prev = 0; @@ -395,6 +436,9 @@ void TQGList::prepend( TQPtrCollection::Item d ) firstNode = curNode = n; // curNode affected numNodes++; curIndex = 0; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif } @@ -404,16 +448,24 @@ void TQGList::prepend( TQPtrCollection::Item d ) void TQGList::append( TQPtrCollection::Item d ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif register TQLNode *n = new TQLNode( newItem(d) ); TQ_CHECK_PTR( n ); n->next = 0; - if ( (n->prev = lastNode) ) // list is not empty + if ( (n->prev = lastNode) ) { // list is not empty lastNode->next = n; - else // initialize list + } + else { // initialize list firstNode = n; + } lastNode = curNode = n; // curNode affected curIndex = numNodes; numNodes++; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif } @@ -423,25 +475,43 @@ void TQGList::append( TQPtrCollection::Item d ) bool TQGList::insertAt( uint index, TQPtrCollection::Item d ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif if ( index == 0 ) { prepend( d ); +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return TRUE; - } else if ( index == numNodes ) { + } + else if ( index == numNodes ) { append( d ); +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return TRUE; } TQLNode *nextNode = locate( index ); - if ( !nextNode ) + if ( !nextNode ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return FALSE; + } TQLNode *prevNode = nextNode->prev; register TQLNode *n = new TQLNode( newItem(d) ); TQ_CHECK_PTR( n ); nextNode->prev = n; + Q_ASSERT( (!((curIndex > 0) && (!prevNode))) ); prevNode->next = n; n->prev = prevNode; // link new node into list n->next = nextNode; curNode = n; // curIndex set by locate() numNodes++; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return TRUE; } @@ -452,18 +522,30 @@ bool TQGList::insertAt( uint index, TQPtrCollection::Item d ) void TQGList::relinkNode( TQLNode *n ) { - if ( n == firstNode ) // already first +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif + if ( n == firstNode ) { // already first +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return; + } curNode = n; unlink(); n->prev = 0; - if ( (n->next = firstNode) ) // list is not empty + if ( (n->next = firstNode) ) { // list is not empty firstNode->prev = n; - else // initialize list + } + else { // initialize list lastNode = n; + } firstNode = curNode = n; // curNode affected numNodes++; curIndex = 0; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif } @@ -473,8 +555,15 @@ void TQGList::relinkNode( TQLNode *n ) TQLNode *TQGList::unlink() { - if ( curNode == 0 ) // null current node +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif + if ( curNode == 0 ) { // null current node +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return 0; + } register TQLNode *n = curNode; // unlink this node if ( n == firstNode ) { // removing first node ? if ( (firstNode = n->next) ) { @@ -500,9 +589,13 @@ TQLNode *TQGList::unlink() curIndex--; } - if ( iterators ) + if ( iterators ) { iterators->notifyRemove( n, curNode ); + } numNodes--; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return n; } @@ -513,6 +606,9 @@ TQLNode *TQGList::unlink() bool TQGList::removeNode( TQLNode *n ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif #if defined(QT_CHECK_NULL) if ( n == 0 || (n->prev && n->prev->next != n) || (n->next && n->next->prev != n) ) { @@ -526,6 +622,9 @@ bool TQGList::removeNode( TQLNode *n ) delete n; curNode = firstNode; curIndex = curNode ? 0 : -1; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return TRUE; } @@ -537,13 +636,27 @@ bool TQGList::removeNode( TQLNode *n ) bool TQGList::remove( TQPtrCollection::Item d ) { - if ( d && find(d) == -1 ) +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif + if ( d && find(d) == -1 ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return FALSE; + } TQLNode *n = unlink(); - if ( !n ) + if ( !n ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return FALSE; + } deleteItem( n->data ); delete n; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return TRUE; } @@ -553,13 +666,27 @@ bool TQGList::remove( TQPtrCollection::Item d ) bool TQGList::removeRef( TQPtrCollection::Item d ) { - if ( findRef(d) == -1 ) +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif + if ( findRef(d) == -1 ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return FALSE; + } TQLNode *n = unlink(); - if ( !n ) + if ( !n ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return FALSE; + } deleteItem( n->data ); delete n; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return TRUE; } @@ -581,13 +708,27 @@ bool TQGList::removeRef( TQPtrCollection::Item d ) bool TQGList::removeAt( uint index ) { - if ( !locate(index) ) +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif + if ( !locate(index) ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return FALSE; + } TQLNode *n = unlink(); - if ( !n ) + if ( !n ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return FALSE; + } deleteItem( n->data ); delete n; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return TRUE; } @@ -597,13 +738,23 @@ bool TQGList::removeAt( uint index ) */ bool TQGList::replaceAt( uint index, TQPtrCollection::Item d ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif TQLNode *n = locate( index ); - if ( !n ) + if ( !n ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return FALSE; + } if ( n->data != d ) { deleteItem( n->data ); n->data = newItem( d ); } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return TRUE; } @@ -615,10 +766,16 @@ bool TQGList::replaceAt( uint index, TQPtrCollection::Item d ) TQPtrCollection::Item TQGList::takeNode( TQLNode *n ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif #if defined(QT_CHECK_NULL) if ( n == 0 || (n->prev && n->prev->next != n) || (n->next && n->next->prev != n) ) { tqWarning( "TQGList::takeNode: Corrupted node" ); +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return 0; } #endif @@ -628,6 +785,9 @@ TQPtrCollection::Item TQGList::takeNode( TQLNode *n ) delete n; // delete the node, not data curNode = firstNode; curIndex = curNode ? 0 : -1; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return d; } @@ -637,9 +797,15 @@ TQPtrCollection::Item TQGList::takeNode( TQLNode *n ) TQPtrCollection::Item TQGList::take() { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif TQLNode *n = unlink(); // unlink node Item d = n ? n->data : 0; delete n; // delete node, keep contents +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return d; } @@ -649,11 +815,21 @@ TQPtrCollection::Item TQGList::take() TQPtrCollection::Item TQGList::takeAt( uint index ) { - if ( !locate(index) ) +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif + if ( !locate(index) ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return 0; + } TQLNode *n = unlink(); // unlink node Item d = n ? n->data : 0; delete n; // delete node, keep contents +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return d; } @@ -663,10 +839,16 @@ TQPtrCollection::Item TQGList::takeAt( uint index ) TQPtrCollection::Item TQGList::takeFirst() { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif first(); TQLNode *n = unlink(); // unlink node Item d = n ? n->data : 0; delete n; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return d; } @@ -676,10 +858,16 @@ TQPtrCollection::Item TQGList::takeFirst() TQPtrCollection::Item TQGList::takeLast() { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif last(); TQLNode *n = unlink(); // unlink node Item d = n ? n->data : 0; delete n; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return d; } @@ -690,14 +878,18 @@ TQPtrCollection::Item TQGList::takeLast() void TQGList::clear() { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif register TQLNode *n = firstNode; firstNode = lastNode = curNode = 0; // initialize list numNodes = 0; curIndex = -1; - if ( iterators ) + if ( iterators ) { iterators->notifyClear( FALSE ); + } TQLNode *prevNode; while ( n ) { // for all nodes ... @@ -706,6 +898,9 @@ void TQGList::clear() n = n->next; delete prevNode; // deallocate node } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif } @@ -716,6 +911,9 @@ void TQGList::clear() int TQGList::findRef( TQPtrCollection::Item d, bool fromStart ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif register TQLNode *n; int index; if ( fromStart ) { // start from first node @@ -731,6 +929,9 @@ int TQGList::findRef( TQPtrCollection::Item d, bool fromStart ) } curNode = n; curIndex = n ? index : -1; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return curIndex; // return position of item } @@ -742,6 +943,9 @@ int TQGList::findRef( TQPtrCollection::Item d, bool fromStart ) int TQGList::find( TQPtrCollection::Item d, bool fromStart ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif register TQLNode *n; int index; if ( fromStart ) { // start from first node @@ -757,6 +961,9 @@ int TQGList::find( TQPtrCollection::Item d, bool fromStart ) } curNode = n; curIndex = n ? index : -1; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return curIndex; // return position of item } @@ -767,6 +974,9 @@ int TQGList::find( TQPtrCollection::Item d, bool fromStart ) uint TQGList::containsRef( TQPtrCollection::Item d ) const { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif register TQLNode *n = firstNode; uint count = 0; while ( n ) { // for all nodes... @@ -774,6 +984,9 @@ uint TQGList::containsRef( TQPtrCollection::Item d ) const count++; n = n->next; } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return count; } @@ -784,6 +997,9 @@ uint TQGList::containsRef( TQPtrCollection::Item d ) const uint TQGList::contains( TQPtrCollection::Item d ) const { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif register TQLNode *n = firstNode; uint count = 0; TQGList *that = (TQGList*)this; // mutable for compareItems() @@ -792,6 +1008,9 @@ uint TQGList::contains( TQPtrCollection::Item d ) const count++; n = n->next; } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return count; } @@ -839,10 +1058,19 @@ uint TQGList::contains( TQPtrCollection::Item d ) const TQPtrCollection::Item TQGList::first() { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif if ( firstNode ) { curIndex = 0; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return (curNode=firstNode)->data; } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return 0; } @@ -852,10 +1080,19 @@ TQPtrCollection::Item TQGList::first() TQPtrCollection::Item TQGList::last() { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif if ( lastNode ) { curIndex = numNodes-1; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return (curNode=lastNode)->data; } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return 0; } @@ -865,15 +1102,24 @@ TQPtrCollection::Item TQGList::last() TQPtrCollection::Item TQGList::next() { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif if ( curNode ) { if ( curNode->next ) { curIndex++; curNode = curNode->next; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return curNode->data; } curIndex = -1; curNode = 0; } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return 0; } @@ -883,15 +1129,24 @@ TQPtrCollection::Item TQGList::next() TQPtrCollection::Item TQGList::prev() { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif if ( curNode ) { if ( curNode->prev ) { curIndex--; curNode = curNode->prev; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return curNode->data; } curIndex = -1; curNode = 0; } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return 0; } @@ -902,9 +1157,16 @@ TQPtrCollection::Item TQGList::prev() void TQGList::toVector( TQGVector *vector ) const { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif vector->clear(); - if ( !vector->resize( count() ) ) + if ( !vector->resize( count() ) ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return; + } register TQLNode *n = firstNode; uint i = 0; while ( n ) { @@ -912,10 +1174,16 @@ void TQGList::toVector( TQGVector *vector ) const n = n->next; i++; } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif } void TQGList::heapSortPushDown( TQPtrCollection::Item* heap, int first, int last ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif int r = first; while( r <= last/2 ) { // Node r has only one child ? @@ -950,6 +1218,9 @@ void TQGList::heapSortPushDown( TQPtrCollection::Item* heap, int first, int last } } } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif } @@ -962,9 +1233,16 @@ void TQGList::heapSortPushDown( TQPtrCollection::Item* heap, int first, int last void TQGList::sort() { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif uint n = count(); - if ( n < 2 ) + if ( n < 2 ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return; + } // Create the heap TQPtrCollection::Item* realheap = new TQPtrCollection::Item[ n ]; @@ -995,6 +1273,9 @@ void TQGList::sort() } delete [] realheap; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif } @@ -1019,6 +1300,9 @@ TQDataStream &operator<<( TQDataStream &s, const TQGList &list ) TQDataStream &TQGList::read( TQDataStream &s ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif uint num; s >> num; // read number of items clear(); // clear list @@ -1042,6 +1326,9 @@ TQDataStream &TQGList::read( TQDataStream &s ) } curNode = firstNode; curIndex = curNode ? 0 : -1; +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return s; } @@ -1051,12 +1338,18 @@ TQDataStream &TQGList::read( TQDataStream &s ) TQDataStream &TQGList::write( TQDataStream &s ) const { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif s << count(); // write number of items TQLNode *n = firstNode; while ( n ) { // write all items write( s, n->data ); n = n->next; } +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return s; } @@ -1068,9 +1361,15 @@ TQDataStream &TQGList::write( TQDataStream &s ) const */ TQLNode* TQGList::erase( TQLNode* it ) { +#if defined(QT_THREAD_SUPPORT) + //mutex->lock(); +#endif TQLNode* n = it; it = it->next; removeNode( n ); +#if defined(QT_THREAD_SUPPORT) + //mutex->unlock(); +#endif return it; } |