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
#ifdef __cplusplus
#include <map>
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<const char *, int, compare_t> 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

View file

@ -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);

View file

@ -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<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], '.');
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 )
tmap_xlate_table[j] = 0;
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_init( &ht, NumTextures );
// Remove all the file extensions in the textures list
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;
}
hashtable_free( &ht );
}
//====================== READ VERTEX INFO ==========================

View file

@ -352,7 +352,7 @@ int piggy_register_sound( digi_sound * snd, const char * name, int in_file )
return i;
}
bitmap_index piggy_find_bitmap( char * name )
bitmap_index piggy_find_bitmap(const char * name)
{
bitmap_index bmp;
int i;
@ -361,7 +361,7 @@ bitmap_index piggy_find_bitmap( char * name )
#if defined(DXX_BUILD_DESCENT_II)
size_t namelen;
char *t;
const char *t;
if ((t=strchr(name,'#'))!=NULL)
namelen = t - name;
else
@ -391,7 +391,7 @@ bitmap_index piggy_find_bitmap( char * name )
return bmp;
}
int piggy_find_sound( char * name )
int piggy_find_sound(const char *name)
{
int i;
@ -428,11 +428,6 @@ int properties_init()
int Pigdata_start;
int pigsize;
int retval;
hashtable_init( &AllBitmapsNames, MAX_BITMAP_FILES );
hashtable_init( &AllDigiSndNames, MAX_SOUND_FILES );
for (i=0; i<MAX_SOUND_FILES; i++ ) {
#ifdef ALLEGRO
GameSounds[i].len = 0;
@ -1150,10 +1145,6 @@ int properties_init(void)
{
int ham_ok=0,snd_ok=0;
int i;
hashtable_init( &AllBitmapsNames, MAX_BITMAP_FILES );
hashtable_init( &AllDigiSndNames, MAX_SOUND_FILES );
for (i=0; i<MAX_SOUND_FILES; i++ ) {
GameSounds[i].length = 0;
GameSounds[i].data = NULL;
@ -1657,10 +1648,6 @@ void piggy_close()
for (i = 0; i < Num_sound_files; i++)
if (SoundOffset[i] == 0)
d_free(GameSounds[i].data);
hashtable_free( &AllBitmapsNames );
hashtable_free( &AllDigiSndNames );
#if defined(DXX_BUILD_DESCENT_II)
free_bitmap_replacements();
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 <stdio.h>
#include <string.h>
#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<ITERATION_COUNT; i++ ) {
if ( (1<<i) >= size ) {
ht->bitsize = i;
ht->size = 1<<i;
break;
}
bool hashtable::compare_t::operator()(const char *l, const char *r) const
{
for (;; ++l, ++r)
{
uint_fast32_t ll = tolower(static_cast<unsigned>(*l)), lr = tolower(static_cast<unsigned>(*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; 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 ) {
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++))<<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];
}
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));
}