diff --git a/common/include/hash.h b/common/include/hash.h index 1714c3b42..bde91b888 100644 --- a/common/include/hash.h +++ b/common/include/hash.h @@ -23,21 +23,19 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. #define _HASH_H #ifdef __cplusplus +#include struct hashtable { - int bitsize; - int and_mask; - int size; - int nitems; - const char **key; - int *value; + struct compare_t + { + bool operator()(const char *l, const char *r) const; + }; + std::map m; }; -int hashtable_init( hashtable *ht, int size ); -void hashtable_free( hashtable *ht ); -int hashtable_search( hashtable *ht, char *key ); -void hashtable_insert( hashtable *ht, char *key, int value ); +int hashtable_search( hashtable *ht, const char *key ); +void hashtable_insert( hashtable *ht, const char *key, int value ); #endif diff --git a/common/main/piggy.h b/common/main/piggy.h index dabdeb161..d815b1446 100644 --- a/common/main/piggy.h +++ b/common/main/piggy.h @@ -86,8 +86,8 @@ int properties_init(); void piggy_close(); bitmap_index piggy_register_bitmap( grs_bitmap * bmp, const char * name, int in_file ); int piggy_register_sound( digi_sound * snd, const char * name, int in_file ); -bitmap_index piggy_find_bitmap( char * name ); -int piggy_find_sound( char * name ); +bitmap_index piggy_find_bitmap(const char *name); +int piggy_find_sound(const char *name); void piggy_read_bitmap_data(grs_bitmap * bmp); void piggy_read_sound_data(digi_sound *snd); diff --git a/similar/editor/group.cpp b/similar/editor/group.cpp index d4ecc308a..a8e7fa921 100644 --- a/similar/editor/group.cpp +++ b/similar/editor/group.cpp @@ -1315,9 +1315,6 @@ static int med_load_group( const char *filename, group::vertex_array_type_t &ver Assert (NumTextures < MAX_TEXTURES); { hashtable ht; - - hashtable_init( &ht, NumTextures ); - // Remove all the file extensions in the textures list for (i=0;i(old_tmap_list[j])); if (tmap_xlate_table[j] < 0 ) tmap_xlate_table[j] = 0; if (tmap_xlate_table[j] != j ) translate = 1; } - - hashtable_free( &ht ); } diff --git a/similar/main/gamemine.cpp b/similar/main/gamemine.cpp index 61a7eee99..9dd364367 100644 --- a/similar/main/gamemine.cpp +++ b/similar/main/gamemine.cpp @@ -551,9 +551,6 @@ int load_mine_data(PHYSFS_file *LoadFile) { hashtable ht; - - hashtable_init( &ht, NumTextures ); - // Remove all the file extensions in the textures list for (i=0;i +#include #include #include #include - -#include "u_mem.h" -#include "strutil.h" -#include "dxxerror.h" #include "hash.h" - -int hashtable_init( hashtable *ht, int size ) { - int i; - ht->size=0; - -#if defined(DXX_BUILD_DESCENT_I) -#define ITERATION_COUNT 12 -#elif defined(DXX_BUILD_DESCENT_II) -#define ITERATION_COUNT 13 -#endif - - for (i=1; i= size ) { - ht->bitsize = i; - ht->size = 1<(*l)), lr = tolower(static_cast(*r)); + if (ll < lr) + return true; + if (lr < ll || !ll) + return false; } - size = ht->size; - ht->and_mask = ht->size - 1; - if (ht->size==0) - Error( "Hashtable has size of 0" ); - - MALLOC(ht->key, const char *, size ); - if (ht->key==NULL) - Error( "Not enough memory to create a hash table of size %d", size ); - - for (i=0; ikey[i] = NULL; - - // Use calloc cause we want zero'd array. - CALLOC(ht->value, int, size); - if (ht->value==NULL) { - d_free(ht->key); - Error( "Not enough memory to create a hash table of size %d\n", size ); - } - - ht->nitems = 0; - - return 0; } -void hashtable_free( hashtable *ht ) { - if (ht->key != NULL ) - d_free( ht->key ); - if (ht->value != NULL ) - d_free( ht->value ); - ht->size = 0; -} - -static int hashtable_getkey( const char *key ) { - int k = 0, i=0; - - while( *key ) { - k^=((int)(*key++))<size ) { - j = (k+(i++)) & ht->and_mask; - if ( ht->key[j] == NULL ) - return -1; - if (!d_stricmp(ht->key[j], key )) - return ht->value[j]; - } +int hashtable_search(hashtable *ht, const char *key) +{ + auto i = ht->m.find(key); + if (i != ht->m.end()) + return i->second; return -1; } -void hashtable_insert( hashtable *ht, char *key, int value ) { - int i,j,k; - - d_strlwr( key ); - k = hashtable_getkey( key ); - i = 0; - - while(i < ht->size) { - j = (k+(i++)) & ht->and_mask; - if ( ht->key[j] == NULL ) { - ht->nitems++; - ht->key[j] = key; - ht->value[j] = value; - return; - } else if (!d_stricmp( key, ht->key[j] )) { - return; - } - } - Error( "Out of hash slots\n" ); +void hashtable_insert(hashtable *ht, const char *key, int value) +{ + ht->m.insert(std::make_pair(key, value)); }