| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- /*
- ** Command & Conquer Generals Zero Hour(tm)
- ** Copyright 2025 Electronic Arts Inc.
- **
- ** This program is free software: you can redistribute it and/or modify
- ** it under the terms of the GNU General Public License as published by
- ** the Free Software Foundation, either version 3 of the License, or
- ** (at your option) any later version.
- **
- ** This program is distributed in the hope that it will be useful,
- ** but WITHOUT ANY WARRANTY; without even the implied warranty of
- ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- ** GNU General Public License for more details.
- **
- ** You should have received a copy of the GNU General Public License
- ** along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- ////////////////////////////////////////////////////////////////////////////////
- // //
- // (c) 2001-2003 Electronic Arts Inc. //
- // //
- ////////////////////////////////////////////////////////////////////////////////
- //----------------------------------------------------------------------------
- //
- // Westwood Studios Pacific.
- //
- // Confidential Information
- // Copyright(C) 2001 - All Rights Reserved
- //
- //----------------------------------------------------------------------------
- //
- // Project: WSYS Library
- //
- // Module: List
- //
- // File name: WSYS_List.cpp
- //
- // Created: 10/31/01 TR
- //
- //----------------------------------------------------------------------------
- //----------------------------------------------------------------------------
- // Includes
- //----------------------------------------------------------------------------
- #include "PreRTS.h"
- #include "Common/List.h"
- // 'assignment within condition expression'.
- #pragma warning(disable : 4706)
- //----------------------------------------------------------------------------
- // Externals
- //----------------------------------------------------------------------------
- //----------------------------------------------------------------------------
- // Defines
- //----------------------------------------------------------------------------
- //----------------------------------------------------------------------------
- // Private Types
- //----------------------------------------------------------------------------
- //----------------------------------------------------------------------------
- // Private Data
- //----------------------------------------------------------------------------
- //----------------------------------------------------------------------------
- // Public Data
- //----------------------------------------------------------------------------
- //----------------------------------------------------------------------------
- // Private Prototypes
- //----------------------------------------------------------------------------
- //----------------------------------------------------------------------------
- // Private Functions
- //----------------------------------------------------------------------------
- //----------------------------------------------------------------------------
- // Public Functions
- //----------------------------------------------------------------------------
- //============================================================================
- // LList::LList
- //============================================================================
- LList::LList( )
- : m_sortMode(DESCENDING)
- {
- m_head.setItem( &m_head.m_item);
- };
- //=================================================================
- // LList::add
- //=================================================================
- void LList::add( LListNode* new_node )
- {
- LListNode* node;
- Int pri;
- if ( m_addToEndOfGroup )
- {
- pri = new_node->priority();
- node = &m_head;
- while( (node = node->prev() ))
- {
- if( (m_sortMode == ASCENDING && node->priority() >= pri)
- || (m_sortMode == DESCENDING && node->priority() <= pri) )
- {
- node->append( new_node );
- return;
- }
- }
- m_head.append( new_node );
- }
- else
- {
- pri = new_node->priority();
- node = &m_head;
- while( (node = node->next() ))
- {
- if( (m_sortMode == ASCENDING && node->priority() <= pri)
- || (m_sortMode == DESCENDING && node->priority() >= pri) )
- {
- node->insert( new_node );
- return;
- }
- }
- m_head.insert( new_node );
- }
- }
- //============================================================================
- // LList::addGDFNode
- //============================================================================
- void LList::addItem( Int pri, void* item )
- {
- LListNode *node = NEW LListNode(); // poolify
- if ( node )
- {
- node->setPriority( pri );
- node->setItem( item );
- node->autoDelete();
- add( node );
- }
- }
- //============================================================================
- // LList::addGDFNodeToHead
- //============================================================================
- void LList::addItemToHead( void *item )
- {
- LListNode *node = NEW LListNode();
- if ( node )
- {
- node->setItem( item );
- node->autoDelete();
- addToHead( node );
- }
- }
- //============================================================================
- // LList::addGDFNodeToTail
- //============================================================================
- void LList::addItemToTail( void *item )
- {
- LListNode *node = NEW LListNode();
- if ( node )
- {
- node->setItem( item );
- node->autoDelete();
- addToTail( node );
- }
- }
- //============================================================================
- // LList::Clear
- //============================================================================
- void LList::clear( void )
- {
- LListNode *node;
- while ( (node = firstNode()) != NULL )
- {
- node->remove();
- node->destroy();
- }
- }
- //=================================================================
- // LList::nodeCount
- //=================================================================
- Int LList::nodeCount( void )
- {
- LListNode* node;
- Int count = 0;
- node = firstNode();
- while(node)
- {
- count++;
- node = node->next();
- }
- return count;
- }
- //=================================================================
- // LList::getNode
- //=================================================================
- LListNode* LList::getNode( Int index )
- {
- LListNode* node;
- node = firstNode();
- while( node && index >= 0 )
- {
- if( index-- == 0 )
- {
- return node;
- }
- node = node->next();
- }
- return NULL;
- }
- //============================================================================
- // LList::merge
- //============================================================================
- void LList::merge( LList *list )
- {
- if ( list == NULL || list->isEmpty() )
- {
- return;
- }
- m_head.m_prev->m_next = list->m_head.m_next;
- list->m_head.m_next->m_prev = m_head.m_prev;
- list->m_head.m_prev->m_next = &m_head;
- m_head.m_prev = list->m_head.m_prev;
- list->m_head.m_next = &list->m_head;
- list->m_head.m_prev = &list->m_head;
- }
- //============================================================================
- // LList::hasReference
- //============================================================================
- Bool LList::hasItem( void *item )
- {
- return findItem( item ) != NULL;
- }
- //============================================================================
- // LList::findReference
- //============================================================================
- LListNode* LList::findItem( void *item )
- {
- LListNode* node;
- node = firstNode();
- while( node )
- {
- if( node->item() == item )
- {
- return node;
- }
- node = node->next();
- }
- return NULL;
- }
- //============================================================================
- // LListNode::LListNode
- //============================================================================
- LListNode::LListNode()
- : m_pri(0),
- m_item(NULL),
- m_autoDelete(FALSE)
- {
- m_next = m_prev = this;
- };
- //=================================================================
- // LListNode::insert
- //=================================================================
- void LListNode::insert( LListNode* new_node )
- {
- new_node->m_prev = m_prev;
- new_node->m_next = this;
- m_prev = new_node;
- new_node->m_prev->m_next = new_node;
- }
- //=================================================================
- // LListNode::append
- //=================================================================
- void LListNode::append( LListNode* new_node )
- {
- new_node->m_prev = this;
- new_node->m_next = m_next;
- this->m_next = new_node;
- new_node->m_next->m_prev = new_node;
- }
- //=================================================================
- // LListNode::remove
- //=================================================================
- void LListNode::remove( void )
- {
- m_prev->m_next = m_next;
- m_next->m_prev = m_prev;
- m_prev = m_next = this; // so we know that the node is not in a list
- }
- //=================================================================
- // LListNode::next
- //=================================================================
- LListNode* LListNode::next( void )
- {
- if( m_next->isHead( ))
- {
- return NULL;
- }
- return m_next;
- }
- //=================================================================
- // LListNode::prev
- //=================================================================
- LListNode* LListNode::prev( void )
- {
- if( m_prev->isHead())
- {
- return NULL;
- }
- return m_prev;
- }
- //=================================================================
- // LListNode::loopNext
- //=================================================================
- LListNode* LListNode::loopNext( void )
- {
- LListNode* next;
- next = m_next;
- if( next->isHead( ))
- {
- // skip head node
- next = next->m_next;
- if( next->isHead( ))
- {
- return NULL; // it is an empty list
- }
- }
- return next;
- }
- //=================================================================
- // LListNode::loopPrev
- //=================================================================
- LListNode* LListNode::loopPrev( void )
- {
- LListNode* prev;
- prev = m_prev;
- if( prev->isHead())
- {
- // skip head node
- prev = prev->m_prev;
- if( prev->isHead())
- {
- return NULL; // it is an empty list
- }
- }
- return prev;
- }
- //============================================================================
- // LListNode::destroy
- //============================================================================
- void LListNode::destroy( void )
- {
- if ( m_autoDelete )
- {
- delete this;
- }
- }
- //============================================================================
- // LList::addToEndOfGroup
- //============================================================================
- void LList::addToEndOfGroup( Bool yes )
- {
- m_addToEndOfGroup = yes;
- }
|