From 9cbceddd8bb7b1c6e00fe0342f4450481d413b4e Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Thu, 11 Jul 2019 03:56:06 +0300 Subject: [PATCH] MenuStrings: use CUtlHashMap from miniutl --- MenuStrings.cpp | 92 ++++++++++--------------------------------------- extdll_menu.h | 2 +- miniutl | 2 +- wscript | 2 ++ 4 files changed, 22 insertions(+), 76 deletions(-) diff --git a/MenuStrings.cpp b/MenuStrings.cpp index 74d4885e..bc118103 100644 --- a/MenuStrings.cpp +++ b/MenuStrings.cpp @@ -18,6 +18,8 @@ GNU General Public License for more details. #include "BaseMenu.h" #include "Utils.h" #include "MenuStrings.h" +#include "utlhashmap.h" +#include "generichash.h" #include "unicode_strtools.h" #define EMPTY_STRINGS_1 "" @@ -28,13 +30,7 @@ GNU General Public License for more details. #define EMPTY_STRINGS_50 EMPTY_STRINGS_20, EMPTY_STRINGS_20, EMPTY_STRINGS_10 #define EMPTY_STRINGS_100 EMPTY_STRINGS_50, EMPTY_STRINGS_50 -#define HASH_SIZE 256 // 256 * 4 * 4 == 4096 bytes -static struct dictionary_t -{ - const char *name; - const char *value; - dictionary_t *next; -} *hashed_cmds[HASH_SIZE]; +CUtlHashMap hashed_cmds; const char *MenuStrings[IDS_LAST] = { @@ -88,58 +84,14 @@ EMPTY_STRINGS_50, // 540..589 EMPTY_STRINGS_10, // 590..599 }; -/* -================= -Com_HashKey - -returns hash key for string -================= -*/ -static uint Com_HashKey( const char *string, uint hashSize ) -{ - uint i, hashKey = 0; - - for( i = 0; string[i]; i++ ) - { - hashKey = (hashKey + i) * 37 + tolower( string[i] ); - } - - return (hashKey % hashSize); -} - -static inline dictionary_t *Dictionary_FindInBucket( dictionary_t *bucket, const char *name ) -{ - dictionary_t *i = bucket; - for( ; i && stricmp( name, i->name ); // filter out - i = i->next ); - - return i; -} - -static void Dictionary_Insert( const char *name, const char *second ) +static void Dictionary_Insert( const char *key, const char *value ) { - uint hash = Com_HashKey( name, HASH_SIZE ); - dictionary_t *elem; + const char *first, *second; - elem = Dictionary_FindInBucket( hashed_cmds[hash], name ); - if( elem ) - { - delete [] (char*) elem->value; - elem->value = StringCopy( second ); - } - else - { - elem = new dictionary_t; - elem->name = StringCopy(name); - elem->value = StringCopy(second); - elem->next = hashed_cmds[hash]; - hashed_cmds[hash] = elem; - } -} + first = StringCopy( key ); + second = StringCopy( value ); -static inline dictionary_t *Dictionary_GetBucket( const char *name ) -{ - return hashed_cmds[ Com_HashKey( name, HASH_SIZE ) ]; + hashed_cmds.InsertOrReplace( first, second ); } static void UI_InitAliasStrings( void ) @@ -396,7 +348,7 @@ static void Localize_Init( void ) { EngFuncs::ClientCmd( TRUE, "exec mainui.cfg\n" ); - memset( hashed_cmds, 0, sizeof( hashed_cmds ) ); + hashed_cmds.Purge(); // strings.lst first for( int i = 0; i < IDS_LAST; i++ ) @@ -431,23 +383,16 @@ static void Localize_Init( void ) static void Localize_Free( void ) { - for( int i = 0; i < HASH_SIZE; i++ ) + FOR_EACH_HASHMAP( hashed_cmds, i ) { - dictionary_t *base = hashed_cmds[i]; - while( base ) - { - dictionary_t *next = base->next; + const char *first = hashed_cmds.Key( i ); + const char *second = hashed_cmds.Element( i ); - delete [] (char*)base->value; - delete [] (char*)base->name; - - delete base; - - base = next; - } + delete[] first; + delete[] second; } - return; + hashed_cmds.Purge(); } void UI_LoadCustomStrings( void ) @@ -492,11 +437,10 @@ const char *L( const char *szStr ) // L means Localize! if( *szStr == '#' ) szStr++; - dictionary_t *base = Dictionary_GetBucket( szStr ); - dictionary_t *found = Dictionary_FindInBucket( base, szStr ); + int i = hashed_cmds.Find( szStr ); - if( found ) - return found->value; + if( i != hashed_cmds.InvalidIndex() ) + return hashed_cmds[i]; } return szStr; diff --git a/extdll_menu.h b/extdll_menu.h index c42dffd9..4fcae9a8 100644 --- a/extdll_menu.h +++ b/extdll_menu.h @@ -40,7 +40,7 @@ GNU General Public License for more details. #pragma warning(disable : 4520) // multiple default constructors specified #pragma warning(disable : 4996) // This function or variable may be unsafe // disable c++11 on old msvc -#if _MSC_VER < 1800 +#if _MSC_VER < 1800 && !defined MY_COMPILER_SUCKS #define MY_COMPILER_SUCKS #endif #endif diff --git a/miniutl b/miniutl index 3cd803e1..5914dd1a 160000 --- a/miniutl +++ b/miniutl @@ -1 +1 @@ -Subproject commit 3cd803e14f3ccd64088a126920bab27269dcbc06 +Subproject commit 5914dd1abaa2e28fd1c21a5948577b0522313ff1 diff --git a/wscript b/wscript index 30f5549f..8e853e46 100644 --- a/wscript +++ b/wscript @@ -64,7 +64,9 @@ def build(bld): source = bld.path.ant_glob([ '*.cpp', + 'miniutl/generichash.cpp', 'miniutl/utlmemory.cpp', + 'miniutl/bitstring.cpp', 'font/*.cpp', 'menus/*.cpp', 'menus/dynamic/*.cpp',