Replace hashtable with std::map

This commit is contained in:
Kp 2014-07-26 23:51:39 +00:00
parent 9c6cc3b3c9
commit b29f2ef7bd
6 changed files with 33 additions and 134 deletions

View file

@ -23,21 +23,19 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#define _HASH_H #define _HASH_H
#ifdef __cplusplus #ifdef __cplusplus
#include <map>
struct hashtable struct hashtable
{ {
int bitsize; struct compare_t
int and_mask; {
int size; bool operator()(const char *l, const char *r) const;
int nitems; };
const char **key; std::map<const char *, int, compare_t> m;
int *value;
}; };
int hashtable_init( hashtable *ht, int size ); int hashtable_search( hashtable *ht, const char *key );
void hashtable_free( hashtable *ht ); void hashtable_insert( hashtable *ht, const char *key, int value );
int hashtable_search( hashtable *ht, char *key );
void hashtable_insert( hashtable *ht, char *key, int value );
#endif #endif

View file

@ -86,8 +86,8 @@ int properties_init();
void piggy_close(); void piggy_close();
bitmap_index piggy_register_bitmap( grs_bitmap * bmp, const char * name, int in_file ); 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 ); int piggy_register_sound( digi_sound * snd, const char * name, int in_file );
bitmap_index piggy_find_bitmap( char * name ); bitmap_index piggy_find_bitmap(const char *name);
int piggy_find_sound( char * name ); int piggy_find_sound(const char *name);
void piggy_read_bitmap_data(grs_bitmap * bmp); void piggy_read_bitmap_data(grs_bitmap * bmp);
void piggy_read_sound_data(digi_sound *snd); void piggy_read_sound_data(digi_sound *snd);

View file

@ -1315,9 +1315,6 @@ static int med_load_group( const char *filename, group::vertex_array_type_t &ver
Assert (NumTextures < MAX_TEXTURES); Assert (NumTextures < MAX_TEXTURES);
{ {
hashtable ht; hashtable ht;
hashtable_init( &ht, NumTextures );
// Remove all the file extensions in the textures list // Remove all the file extensions in the textures list
for (i=0;i<NumTextures;i++) { for (i=0;i<NumTextures;i++) {
@ -1333,13 +1330,11 @@ static int med_load_group( const char *filename, group::vertex_array_type_t &ver
temptr = strchr(&old_tmap_list[j][0u], '.'); temptr = strchr(&old_tmap_list[j][0u], '.');
if (temptr) *temptr = '\0'; if (temptr) *temptr = '\0';
tmap_xlate_table[j] = hashtable_search( &ht,&old_tmap_list[j][0u]); tmap_xlate_table[j] = hashtable_search( &ht, static_cast<const char *>(old_tmap_list[j]));
if (tmap_xlate_table[j] < 0 ) if (tmap_xlate_table[j] < 0 )
tmap_xlate_table[j] = 0; tmap_xlate_table[j] = 0;
if (tmap_xlate_table[j] != j ) translate = 1; if (tmap_xlate_table[j] != j ) translate = 1;
} }
hashtable_free( &ht );
} }

View file

@ -551,9 +551,6 @@ int load_mine_data(PHYSFS_file *LoadFile)
{ {
hashtable ht; hashtable ht;
hashtable_init( &ht, NumTextures );
// Remove all the file extensions in the textures list // Remove all the file extensions in the textures list
for (i=0;i<NumTextures;i++) { for (i=0;i<NumTextures;i++) {
@ -575,8 +572,6 @@ int load_mine_data(PHYSFS_file *LoadFile)
} }
if (tmap_xlate_table[j] != j ) translate = 1; if (tmap_xlate_table[j] != j ) translate = 1;
} }
hashtable_free( &ht );
} }
//====================== READ VERTEX INFO ========================== //====================== READ VERTEX INFO ==========================

View file

@ -352,7 +352,7 @@ int piggy_register_sound( digi_sound * snd, const char * name, int in_file )
return i; return i;
} }
bitmap_index piggy_find_bitmap( char * name ) bitmap_index piggy_find_bitmap(const char * name)
{ {
bitmap_index bmp; bitmap_index bmp;
int i; int i;
@ -361,7 +361,7 @@ bitmap_index piggy_find_bitmap( char * name )
#if defined(DXX_BUILD_DESCENT_II) #if defined(DXX_BUILD_DESCENT_II)
size_t namelen; size_t namelen;
char *t; const char *t;
if ((t=strchr(name,'#'))!=NULL) if ((t=strchr(name,'#'))!=NULL)
namelen = t - name; namelen = t - name;
else else
@ -391,7 +391,7 @@ bitmap_index piggy_find_bitmap( char * name )
return bmp; return bmp;
} }
int piggy_find_sound( char * name ) int piggy_find_sound(const char *name)
{ {
int i; int i;
@ -428,11 +428,6 @@ int properties_init()
int Pigdata_start; int Pigdata_start;
int pigsize; int pigsize;
int retval; int retval;
hashtable_init( &AllBitmapsNames, MAX_BITMAP_FILES );
hashtable_init( &AllDigiSndNames, MAX_SOUND_FILES );
for (i=0; i<MAX_SOUND_FILES; i++ ) { for (i=0; i<MAX_SOUND_FILES; i++ ) {
#ifdef ALLEGRO #ifdef ALLEGRO
GameSounds[i].len = 0; GameSounds[i].len = 0;
@ -1150,10 +1145,6 @@ int properties_init(void)
{ {
int ham_ok=0,snd_ok=0; int ham_ok=0,snd_ok=0;
int i; int i;
hashtable_init( &AllBitmapsNames, MAX_BITMAP_FILES );
hashtable_init( &AllDigiSndNames, MAX_SOUND_FILES );
for (i=0; i<MAX_SOUND_FILES; i++ ) { for (i=0; i<MAX_SOUND_FILES; i++ ) {
GameSounds[i].length = 0; GameSounds[i].length = 0;
GameSounds[i].data = NULL; GameSounds[i].data = NULL;
@ -1657,10 +1648,6 @@ void piggy_close()
for (i = 0; i < Num_sound_files; i++) for (i = 0; i < Num_sound_files; i++)
if (SoundOffset[i] == 0) if (SoundOffset[i] == 0)
d_free(GameSounds[i].data); d_free(GameSounds[i].data);
hashtable_free( &AllBitmapsNames );
hashtable_free( &AllDigiSndNames );
#if defined(DXX_BUILD_DESCENT_II) #if defined(DXX_BUILD_DESCENT_II)
free_bitmap_replacements(); free_bitmap_replacements();
free_d1_tmap_nums(); free_d1_tmap_nums();

View file

@ -18,110 +18,34 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
*/ */
#include <cctype>
#include <cstdint>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "u_mem.h"
#include "strutil.h"
#include "dxxerror.h"
#include "hash.h" #include "hash.h"
int hashtable_init( hashtable *ht, int size ) {
int i;
ht->size=0; bool hashtable::compare_t::operator()(const char *l, const char *r) const
{
#if defined(DXX_BUILD_DESCENT_I) for (;; ++l, ++r)
#define ITERATION_COUNT 12 {
#elif defined(DXX_BUILD_DESCENT_II) uint_fast32_t ll = tolower(static_cast<unsigned>(*l)), lr = tolower(static_cast<unsigned>(*r));
#define ITERATION_COUNT 13 if (ll < lr)
#endif return true;
if (lr < ll || !ll)
for (i=1; i<ITERATION_COUNT; i++ ) { return false;
if ( (1<<i) >= size ) {
ht->bitsize = i;
ht->size = 1<<i;
break;
}
} }
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; i<size; i++ )
ht->key[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 ) { int hashtable_search(hashtable *ht, const char *key)
if (ht->key != NULL ) {
d_free( ht->key ); auto i = ht->m.find(key);
if (ht->value != NULL ) if (i != ht->m.end())
d_free( ht->value ); return i->second;
ht->size = 0;
}
static int hashtable_getkey( const char *key ) {
int k = 0, i=0;
while( *key ) {
k^=((int)(*key++))<<i;
i++;
}
return k;
}
int hashtable_search( hashtable *ht, char *key ) {
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 )
return -1;
if (!d_stricmp(ht->key[j], key ))
return ht->value[j];
}
return -1; return -1;
} }
void hashtable_insert( hashtable *ht, char *key, int value ) { void hashtable_insert(hashtable *ht, const char *key, int value)
int i,j,k; {
ht->m.insert(std::make_pair(key, value));
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" );
} }