| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- /*
- ** 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/>.
- */
- //
- // list.cpp
- //
- #include "stdAfx.h"
- #include <assert.h>
- #include "list.h"
- ListNode::ListNode ( void )
- {
- prev = next = this;
- pri = NORMAL_PRIORITY;
- item = NULL;
- }
- void ListNode::Append ( ListNode *new_node )
- {
- assert ( ! new_node->InList()); /* node is already in a list or was not initialized*/
- new_node->prev = this;
- new_node->next = next;
- next = new_node;
- new_node->next->prev = new_node;
- }
- void ListNode::Prepend ( ListNode *new_node )
- {
- assert ( !new_node->InList() ); /* node is already in a list or was not initialized*/
- new_node->prev = prev;
- new_node->next = this;
- prev = new_node;
- new_node->prev->next = new_node;
- }
- void ListNode::Link ( ListNode *node)
- {
- next = node;
- node->prev = next;
- }
- void ListNode::Remove ( void )
- {
- prev->next = next;
- next->prev = prev;
- prev = next = this; /* so we know that the node is not in a list */
- }
- ListNode* ListNode::Next ( void )
- {
- if ( next->IsHead ( ) )
- {
- return NULL;
- }
- return next;
- }
- ListNode* ListNode::Prev ( void )
- {
- if ( prev->IsHead () )
- {
- return NULL;
- }
- return prev;
- }
- ListNode* ListNode::NextLoop ( void )
- {
- ListNode *next_node = next;
- if ( next_node->IsHead ( ))
- {
- /* skip head node */
- next_node = next_node->next;
- if ( next_node->IsHead ( ))
- {
- return NULL; /* it is an empty list */
- }
- }
- return next_node;
- }
- ListNode* ListNode::PrevLoop ( void )
- {
- ListNode *prev_node = prev;
- if ( prev_node->IsHead ( ))
- {
- /* skip head node */
- prev_node = prev_node->prev;
- if ( prev_node->IsHead ( ))
- {
- return NULL; /* it is an empty list */
- }
- }
- return prev_node;
- }
- void* ListNode::Item ( void )
- {
- assert ( !IsHead () );
- return item;
- }
- void ListNode::SetItem ( void *new_item )
- {
- assert ( !IsHead () );
- item = new_item ;
- }
- int ListNode::InList ( void )
- {
- return prev != this;
- }
- int ListNode::IsHead ( void )
- {
- return item == &this->item;
- }
- int ListNode::Priority ( void )
- {
- return pri;
- }
- void ListNode::SetPriority ( int new_pri )
- {
- assert ( new_pri <= HIGHEST_PRIORITY );
- assert ( new_pri >= LOWEST_PRIORITY );
- pri = new_pri;
- }
- List::List ( void )
- {
- SetItem ( &this->item );
- }
- void List::AddToTail ( ListNode *node )
- {
- assert ( IsHead ());
- Prepend ( node );
- }
- void List::AddToHead ( ListNode *node )
- {
- assert ( IsHead ());
- Append ( node );
- }
- void List::Add ( ListNode *new_node )
- {
- ListNode* node;
- int pri;
- assert ( IsHead ());
- assert ( !new_node->InList ());
- pri = new_node->Priority();
- node = FirstNode ( );
- while( node )
- {
- if (node->Priority() <= pri )
- {
- node->Prepend ( new_node );
- return;
- }
- node = node->Next ();
- }
- Prepend ( new_node );
- }
- void List::Merge ( List *list )
- {
- ListNode *first,
- *last,
- *node;
- assert ( IsHead ());
- first = list->Next();
- last = list->Prev();
- if ( !first || !last )
- {
- return;
- }
-
- node = Prev();
- node->Link ( first );
- last->Link ( this );
-
- list->Empty ();
- }
- int List::NumItems ( void )
- {
- int count = 0;
- ListNode *node;
- assert ( IsHead ());
- node = FirstNode();
- while ( node )
- {
- count++;
- node = node->Next ();
- }
- return count;
- }
- void* List::Item ( int list_index )
- {
- ListNode *node;
- assert ( IsHead ());
- node = FirstNode();
- while (list_index && node )
- {
- list_index--;
- node = node->Next ();
- }
- if ( node )
- {
- return node->Item();
- }
- return NULL;
- }
- ListNode* List::FirstNode ( void )
- {
- assert ( IsHead ());
- return Next ();
- }
- ListNode* List::LastNode ( void )
- {
- assert ( IsHead ());
- return Prev ();
- }
- int List::IsEmpty ( void )
- {
- assert ( IsHead ());
- return !InList();
- }
- void List::Empty ( void )
- {
- assert ( IsHead ());
- Remove ();
- }
- ListNode* List::Find ( void *item )
- {
- ListNode *node;
- node = FirstNode ();
- while ( node )
- {
- if ( node->Item () == item )
- {
- return node;
- }
- node = node->Next ();
- }
- return NULL;
- }
|