| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- /*
- ** Command & Conquer Generals(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/>.
- */
- //
- // Bin.cpp
- //
- #include "stdAfx.h"
- #include "bin.h"
- #include "assert.h"
- #include "list.h"
- Bin::Bin ( int size )
- {
- assert ( size > 0 );
- num_buckets = size;
- sh_item = NULL;
- bucket = new List[size];
- }
- Bin::~Bin ( )
- {
- Clear ();
- delete [] bucket;
- }
- void Bin::Clear ( void )
- {
- int count = num_buckets;
- sh_item = NULL;
- while ( count-- )
- {
- List *head = &bucket[count];
- BinItem *item;
- while ( ( item = (BinItem *) head->Next ()))
- {
- Remove ( item );
- }
- }
- }
- void* Bin::Get ( OLECHAR *text1, OLECHAR *text2 )
- {
- BinItem *item;
- if ( ( item = GetBinItem ( text1, text2 )) )
- {
- return item->Item();
- }
- return NULL;
- }
- void* Bin::GetNext ( void )
- {
- BinItem *item;
- if ( ( item = GetNextBinItem ( )) )
- {
- return item->Item();
- }
- return NULL;
- }
- void Bin::Add ( void *data, OLECHAR *text1, OLECHAR *text2 )
- {
- BinItem *item;
- List *list;
- int hash;
- sh_item = NULL;
- hash = calc_hash ( text1 );
- item = new BinItem ( data, hash, text1, text2 );
- list = &bucket[hash%num_buckets];
- list->AddToTail ( (ListNode *) item );
- }
- BinItem* Bin::GetBinItem ( OLECHAR *text1, OLECHAR *text2)
- {
-
- sh_size1 = sh_size2 = 0;
- sh_text1 = text1;
- sh_text2 = text2;
- sh_hash = calc_hash ( text1 );
- if ( sh_text1 )
- {
- sh_size1 = wcslen ( sh_text1 );
- }
- if ( sh_text2 )
- {
- sh_size2 = wcslen ( sh_text2 );
- }
- sh_item = (BinItem *) &bucket[sh_hash%num_buckets];
- return GetNextBinItem ();
- }
- BinItem* Bin::GetNextBinItem ( void )
- {
- if ( sh_item )
- {
- sh_item = (BinItem *) sh_item->Next ();
- }
- while ( sh_item )
- {
- if ( sh_item->Same ( sh_hash, sh_text1, sh_size1, sh_text2, sh_size2 ))
- {
- break;
- }
- sh_item = (BinItem *) sh_item->Next ();
- }
- return sh_item;
- }
- BinItem* Bin::GetBinItem ( void *item )
- {
- BinItem *bitem = NULL;
- int i;
-
- for ( i=0; i< num_buckets; i++)
- {
- if ( ( bitem = (BinItem *) bucket[i].Find ( item )))
- {
- break;
- }
- }
- return bitem;
-
- }
- void Bin::Remove ( void *item )
- {
- BinItem *bitem;
- if ( ( bitem = GetBinItem ( item ) ))
- {
- Remove ( bitem );
- }
- }
- void Bin::Remove ( OLECHAR *text1, OLECHAR *text2 )
- {
- BinItem *bitem;
- if ( ( bitem = GetBinItem ( text1, text2 ) ))
- {
- Remove ( bitem );
- }
- }
- void Bin::Remove ( BinItem *item )
- {
- sh_item = NULL;
- item->Remove ();
- delete item ;
- }
- BinItem::BinItem ( void *data, int new_hash, OLECHAR *new_text1, OLECHAR *new_text2 )
- {
- SetItem ( data );
- hash = new_hash;
- if ( (text1 = new_text1) )
- {
- text1size = wcslen ( text1 );
- }
- else
- {
- text1size = 0;
- }
- if ( (text2 = new_text2) )
- {
- text2size = wcslen ( text2 );
- }
- else
- {
- text2size = 0;
- }
- }
- int BinItem::Same ( int chash, OLECHAR *ctext1, int size1, OLECHAR *ctext2, int size2 )
- {
- if ( hash != chash || text1size != size1 || text2size != size2 )
- {
- return FALSE;
- }
- if ( wcsicmp ( text1, ctext1 ))
- {
- return FALSE;
- }
- if ( text2 && ctext2 && wcsicmp ( text2, ctext2 ))
- {
- return FALSE;
- }
- return TRUE;
- }
- int Bin::calc_hash ( OLECHAR *text )
- {
- int hash = 0;
- while ( *text )
- {
- hash += *text++;
- }
- return hash;
- }
- // Bin ID code
- BinID::BinID ( int size )
- {
- assert ( size > 0 );
- num_buckets = size;
- bucket = new List[size];
- }
- BinID::~BinID ( )
- {
- Clear ();
- delete [] bucket;
- }
- void BinID::Clear ( void )
- {
- int count = num_buckets;
- while ( count-- )
- {
- List *head = &bucket[count];
- BinIDItem *item;
- while ( ( item = (BinIDItem *) head->Next ()))
- {
- Remove ( item );
- }
- }
- }
- void* BinID::Get ( int id)
- {
- BinIDItem *item;
- if ( ( item = GetBinIDItem ( id )) )
- {
- return item->Item();
- }
- return NULL;
- }
- void BinID::Add ( void *data, int id )
- {
- BinIDItem *item;
- List *list;
- item = new BinIDItem ( data, id );
- list = &bucket[id%num_buckets];
- list->AddToTail ( (ListNode *) item );
- }
- BinIDItem* BinID::GetBinIDItem ( int id )
- {
- BinIDItem *item;
-
- item = (BinIDItem *) bucket[id%num_buckets].Next();
- while ( item )
- {
- if ( item->Same ( id ))
- {
- break;
- }
- item = (BinIDItem *) item->Next ();
- }
- return item ;
- }
- BinIDItem* BinID::GetBinIDItem ( void *item )
- {
- BinIDItem *bitem = NULL;
- int i;
-
- for ( i=0; i< num_buckets; i++)
- {
- if ( ( bitem = (BinIDItem *) bucket[i].Find ( item )))
- {
- break;
- }
- }
- return bitem;
-
- }
- void BinID::Remove ( void *item )
- {
- BinIDItem *bitem;
- if ( ( bitem = GetBinIDItem ( item ) ))
- {
- Remove ( bitem );
- }
- }
- void BinID::Remove ( int id )
- {
- BinIDItem *bitem;
- if ( ( bitem = GetBinIDItem ( id ) ))
- {
- Remove ( bitem );
- }
- }
- void BinID::Remove ( BinIDItem *item )
- {
- item->Remove ();
- delete item ;
- }
- BinIDItem::BinIDItem ( void *data, int new_id )
- {
- SetItem ( data );
- id = new_id;
- }
- int BinIDItem::Same ( int compare_id )
- {
- return id == compare_id;
- }
|