Move hash table code into engine and use hash table for OSD symbol lookup

git-svn-id: https://svn.eduke32.com/eduke32@1025 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
terminx 2008-08-25 02:12:02 +00:00
parent 25adc68899
commit 352ace8bbc
6 changed files with 221 additions and 196 deletions

View file

@ -588,6 +588,27 @@ int loaddefinitionsfile(char *fn);
extern int mapversion; // if loadboard() fails with -2 return, try loadoldboard(). if it fails with -2, board is dodgy extern int mapversion; // if loadboard() fails with -2 return, try loadoldboard(). if it fails with -2, board is dodgy
int loadoldboard(char *filename, char fromwhere, int *daposx, int *daposy, int *daposz, short *daang, short *dacursectnum); int loadoldboard(char *filename, char fromwhere, int *daposx, int *daposy, int *daposz, short *daang, short *dacursectnum);
// Hash functions
struct HASH_item // size is 12/24 bits.
{
const char *string;
int key;
struct HASH_item *next;
};
struct HASH_table
{
int size;
struct HASH_item **items;
};
void HASH_init(struct HASH_table *t);
void HASH_free(struct HASH_table *t);
int HASH_findcase(struct HASH_table *t, const char *s);
int HASH_find(struct HASH_table *t, const char *s);
void HASH_replace(struct HASH_table *t, const char *s, int key);
void HASH_add(struct HASH_table *t, const char *s, int key);
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma pack() #pragma pack()
#endif #endif

View file

@ -12163,6 +12163,157 @@ void setpolymost2dview(void)
#endif #endif
} }
void HASH_init(struct HASH_table *t)
{
HASH_free(t);
t->items=Bcalloc(1,t->size * sizeof(struct HASH_item));
Bmemset(t->items,0,t->size * sizeof(struct HASH_item));
}
void HASH_free(struct HASH_table *t)
{
struct HASH_item *cur, *tmp;
int i;
int num;
if (t->items==NULL)return;
// initprintf("*free\n");
i=t->size-1;
do
{
cur=t->items[i];
num=0;
while (cur)
{
tmp=cur;
cur=cur->next;
// initprintf("Free %4d '%s'\n",tmp->key,(tmp->string)?tmp->string:".");
Bfree(tmp);
num++;
}
// initprintf("Bucket #%4d: %3d\n",i,num);
}
while (--i>=0);
Bfree(t->items);
t->items=0;
}
inline int HASH_getcode(const char *s)
{
int i=0, fact=1;
while (*s)
{
i+=*s;
i+=1<<fact;
s++;
}
return i;
}
void HASH_add(struct HASH_table *t, const char *s, int key)
{
struct HASH_item *cur, *prev=NULL;
int code;
if (!s)return;
if (t->items==NULL) {initprintf("HASH_add: not initalized\n");return;}
code=HASH_getcode(s)%t->size;
cur=t->items[code];
if (!cur)
{
cur=Bcalloc(1,sizeof(struct HASH_item));
cur->string=s;
cur->key=key;
cur->next=NULL;
t->items[code]=cur;
return;
}
do
{
if (Bstrcmp(s,cur->string)==0)return;
prev=cur;
cur=cur->next;
}
while (cur);
cur=Bcalloc(1,sizeof(struct HASH_item));
cur->string=s;
cur->key=key;
cur->next=NULL;
prev->next=cur;
}
void HASH_replace(struct HASH_table *t, const char *s, int key)
{
struct HASH_item *cur, *prev=NULL;
int code;
if (t->items==NULL) {initprintf("HASH_add: not initalized\n");return;}
code=HASH_getcode(s)%t->size;
cur=t->items[code];
if (!cur)
{
cur=Bcalloc(1,sizeof(struct HASH_item));
cur->string=s;
cur->key=key;
cur->next=NULL;
t->items[code]=cur;
return;
}
do
{
if (Bstrcmp(s,cur->string)==0)
{
cur->key=key;
return;
}
prev=cur;
cur=cur->next;
}
while (cur);
cur=Bcalloc(1,sizeof(struct HASH_item));
cur->string=s;
cur->key=key;
cur->next=NULL;
prev->next=cur;
}
int HASH_find(struct HASH_table *t, const char *s)
{
struct HASH_item *cur;
// initprintf("{");
if (t->items==NULL) {initprintf("HASH_findyy: not initalized\n");return -1;}
cur=t->items[HASH_getcode(s)%t->size];
while (cur)
{
if (Bstrcmp(s,cur->string)==0)return cur->key;
cur=cur->next;
}
// initprintf("}");
return -1;
}
int HASH_findcase(struct HASH_table *t, const char *s)
{
struct HASH_item *cur;
// initprintf("{");
if (t->items==NULL) {initprintf("HASH_findcase: not initalized\n");return -1;}
cur=t->items[HASH_getcode(s)%t->size];
while (cur)
{
if (Bstrcasecmp(s,cur->string)==0)return cur->key;
cur=cur->next;
}
// initprintf("}");
return -1;
}
/* /*
* vim:ts=8: * vim:ts=8:

View file

@ -101,6 +101,12 @@ static int osdtextpal=0;
/* static int osdcursorshade=0; /* static int osdcursorshade=0;
static int osdcursorpal=0; */ static int osdcursorpal=0; */
#define MAXSYMBOLS 256
static symbol_t *osdsymbptrs[MAXSYMBOLS];
static int osdnumsymbols = 0;
static struct HASH_table osdsymbolsH = { MAXSYMBOLS, NULL };
// application callbacks // application callbacks
static void (*drawosdchar)(int, int, char, int, int) = _internal_drawosdchar; static void (*drawosdchar)(int, int, char, int, int) = _internal_drawosdchar;
static void (*drawosdstr)(int, int, char*, int, int, int) = _internal_drawosdstr; static void (*drawosdstr)(int, int, char*, int, int, int) = _internal_drawosdstr;
@ -617,6 +623,8 @@ void OSD_Cleanup(void)
{ {
symbol_t *s; symbol_t *s;
HASH_free(&osdsymbolsH);
for (; symbols; symbols=s) for (; symbols; symbols=s)
{ {
s=symbols->next; s=symbols->next;
@ -637,6 +645,12 @@ void OSD_Init(void)
{ {
Bmemset(osdtext, 32, TEXTSIZE); Bmemset(osdtext, 32, TEXTSIZE);
Bmemset(osdfmt, osdtextpal+(osdtextshade<<5), TEXTSIZE); Bmemset(osdfmt, osdtextpal+(osdtextshade<<5), TEXTSIZE);
Bmemset(osdsymbptrs, 0, sizeof(osdsymbptrs));
osdnumsymbols = 0;
HASH_init(&osdsymbolsH);
osdlines=1; osdlines=1;
osdinited=1; osdinited=1;
@ -1764,10 +1778,12 @@ void OSD_SetVersionString(const char *version, int shade, int pal)
// addnewsymbol() -- Allocates space for a new symbol and attaches it // addnewsymbol() -- Allocates space for a new symbol and attaches it
// appropriately to the lists, sorted. // appropriately to the lists, sorted.
// //
static symbol_t *addnewsymbol(const char *name) static symbol_t *addnewsymbol(const char *name)
{ {
symbol_t *newsymb, *s, *t; symbol_t *newsymb, *s, *t;
if (osdnumsymbols >= MAXSYMBOLS) return NULL;
newsymb = (symbol_t *)Bmalloc(sizeof(symbol_t)); newsymb = (symbol_t *)Bmalloc(sizeof(symbol_t));
if (!newsymb) { return NULL; } if (!newsymb) { return NULL; }
Bmemset(newsymb, 0, sizeof(symbol_t)); Bmemset(newsymb, 0, sizeof(symbol_t));
@ -1798,7 +1814,8 @@ static symbol_t *addnewsymbol(const char *name)
newsymb->next = t; newsymb->next = t;
} }
} }
HASH_add(&osdsymbolsH,name,osdnumsymbols);
osdsymbptrs[osdnumsymbols++] = newsymb;
return newsymb; return newsymb;
} }
@ -1823,14 +1840,12 @@ static symbol_t *findsymbol(const char *name, symbol_t *startingat)
// //
static symbol_t *findexactsymbol(const char *name) static symbol_t *findexactsymbol(const char *name)
{ {
symbol_t *startingat; int symb;
if (!symbols) return NULL; if (!symbols) return NULL;
startingat = symbols; symb = HASH_find(&osdsymbolsH,name);
if (symb > -1)
for (; startingat; startingat=startingat->next) return osdsymbptrs[symb];
if (startingat->func != (void *)OSD_UNALIASED && !Bstrcasecmp(name, startingat->name)) return startingat;
return NULL; return NULL;
} }

View file

@ -1060,26 +1060,6 @@ extern char *mousenames[];
extern char *duke3dgrp, *duke3dgrpstring; extern char *duke3dgrp, *duke3dgrpstring;
extern char mod_dir[BMAX_PATH]; extern char mod_dir[BMAX_PATH];
// Hash functions
struct HASH_item // size is 12/24 bits.
{
const char *string;
int key;
struct HASH_item *next;
};
struct HASH_table
{
int size;
struct HASH_item **items;
};
void HASH_init(struct HASH_table *t);
void HASH_free(struct HASH_table *t);
int HASH_findcase(struct HASH_table *t, const char *s);
int HASH_find(struct HASH_table *t, const char *s);
void HASH_replace(struct HASH_table *t, const char *s, int key);
void HASH_add(struct HASH_table *t, const char *s, int key);
struct HASH_table gamevarH; struct HASH_table gamevarH;
struct HASH_table arrayH; struct HASH_table arrayH;
struct HASH_table keywH; struct HASH_table keywH;

View file

@ -942,172 +942,21 @@ int HASH_find(struct HASH_table *t, const char *s);
void HASH_replace(struct HASH_table *t, const char *s, int key); void HASH_replace(struct HASH_table *t, const char *s, int key);
void HASH_add(struct HASH_table *t, const char *s, int key); void HASH_add(struct HASH_table *t, const char *s, int key);
*/ */
void HASH_init(struct HASH_table *t)
{
HASH_free(t);
t->items=Bcalloc(1,t->size * sizeof(struct HASH_item));
Bmemset(t->items,0,t->size * sizeof(struct HASH_item));
}
void HASH_free(struct HASH_table *t) struct HASH_table gamevarH = { MAXGAMEVARS, NULL };
{ struct HASH_table arrayH = { MAXGAMEARRAYS, NULL };
struct HASH_item *cur, *tmp; struct HASH_table labelH = { 11264, NULL };
int i; struct HASH_table keywH = { CON_END, NULL };
int num;
if (t->items==NULL)return; struct HASH_table sectorH = { SECTOR_END, NULL };
initprintf("*free\n"); struct HASH_table wallH = { WALL_END, NULL };
i=t->size-1; struct HASH_table userdefH = { USERDEFS_END, NULL };
do
{
cur=t->items[i];
num=0;
while (cur)
{
tmp=cur;
cur=cur->next;
// initprintf("Free %4d '%s'\n",tmp->key,(tmp->string)?tmp->string:".");
Bfree(tmp);
num++;
}
// initprintf("Bucket #%4d: %3d\n",i,num);
}
while (--i>=0);
Bfree(t->items);
t->items=0;
}
inline int HASH_getcode(const char *s) struct HASH_table projectileH = { PROJ_END, NULL };
{ struct HASH_table playerH = { PLAYER_END, NULL };
int i=0, fact=1; struct HASH_table inputH = { INPUT_END, NULL };
while (*s) struct HASH_table actorH = { ACTOR_END, NULL };
{ struct HASH_table tspriteH = { ACTOR_END, NULL };
i+=*s;
i+=1<<fact;
s++;
}
return i;
}
void HASH_add(struct HASH_table *t, const char *s, int key)
{
struct HASH_item *cur, *prev=NULL;
int code;
if (!s)return;
if (t->items==NULL) {initprintf("HASH_add: not initalized\n");return;}
code=HASH_getcode(s)%t->size;
cur=t->items[code];
if (!cur)
{
cur=Bcalloc(1,sizeof(struct HASH_item));
cur->string=s;
cur->key=key;
cur->next=NULL;
t->items[code]=cur;
return;
}
do
{
if (Bstrcmp(s,cur->string)==0)return;
prev=cur;
cur=cur->next;
}
while (cur);
cur=Bcalloc(1,sizeof(struct HASH_item));
cur->string=s;
cur->key=key;
cur->next=NULL;
prev->next=cur;
}
void HASH_replace(struct HASH_table *t, const char *s, int key)
{
struct HASH_item *cur, *prev=NULL;
int code;
if (t->items==NULL) {initprintf("HASH_add: not initalized\n");return;}
code=HASH_getcode(s)%t->size;
cur=t->items[code];
if (!cur)
{
cur=Bcalloc(1,sizeof(struct HASH_item));
cur->string=s;
cur->key=key;
cur->next=NULL;
t->items[code]=cur;
return;
}
do
{
if (Bstrcmp(s,cur->string)==0)
{
cur->key=key;
return;
}
prev=cur;
cur=cur->next;
}
while (cur);
cur=Bcalloc(1,sizeof(struct HASH_item));
cur->string=s;
cur->key=key;
cur->next=NULL;
prev->next=cur;
}
int HASH_find(struct HASH_table *t, const char *s)
{
struct HASH_item *cur;
// initprintf("{");
if (t->items==NULL) {initprintf("HASH_findyy: not initalized\n");return -1;}
cur=t->items[HASH_getcode(s)%t->size];
while (cur)
{
if (Bstrcmp(s,cur->string)==0)return cur->key;
cur=cur->next;
}
// initprintf("}");
return -1;
}
int HASH_findcase(struct HASH_table *t, const char *s)
{
struct HASH_item *cur;
// initprintf("{");
if (t->items==NULL) {initprintf("HASH_findcase: not initalized\n");return -1;}
cur=t->items[HASH_getcode(s)%t->size];
while (cur)
{
if (Bstrcasecmp(s,cur->string)==0)return cur->key;
cur=cur->next;
}
// initprintf("}");
return -1;
}
struct HASH_table gamevarH ={8096,NULL};
struct HASH_table arrayH ={2096,NULL};
struct HASH_table labelH ={8096,NULL};
struct HASH_table keywH ={ 800,NULL};
struct HASH_table sectorH ={ 200,NULL};
struct HASH_table wallH ={ 200,NULL};
struct HASH_table userdefH ={ 200,NULL};
struct HASH_table projectileH ={ 500,NULL};
struct HASH_table playerH ={ 500,NULL};
struct HASH_table inputH ={ 100,NULL};
struct HASH_table actorH ={ 500,NULL};
struct HASH_table tspriteH ={ 400,NULL};
void inithash() void inithash()
{ {
@ -1147,6 +996,7 @@ void inithash()
for (i=0;tsprlabels[i].lId >=0 ; i++) for (i=0;tsprlabels[i].lId >=0 ; i++)
HASH_add(&tspriteH,tsprlabels[i].name,i); HASH_add(&tspriteH,tsprlabels[i].name,i);
} }
void freehash() void freehash()
{ {
HASH_free(&gamevarH); HASH_free(&gamevarH);

View file

@ -248,7 +248,8 @@ enum playerlabels
PLAYER_TEAM, PLAYER_TEAM,
PLAYER_MAX_PLAYER_HEALTH, PLAYER_MAX_PLAYER_HEALTH,
PLAYER_MAX_SHIELD_AMOUNT, PLAYER_MAX_SHIELD_AMOUNT,
PLAYER_MAX_AMMO_AMOUNT PLAYER_MAX_AMMO_AMOUNT,
PLAYER_END
}; };
enum userdefslabels enum userdefslabels
@ -344,7 +345,8 @@ enum userdefslabels
USERDEFS_LEVELSTATS, USERDEFS_LEVELSTATS,
USERDEFS_CROSSHAIRSCALE, USERDEFS_CROSSHAIRSCALE,
USERDEFS_ALTHUD, USERDEFS_ALTHUD,
USERDEFS_DISPLAY_BONUS_SCREEN USERDEFS_DISPLAY_BONUS_SCREEN,
USERDEFS_END
}; };
enum sectorlabels enum sectorlabels
@ -371,7 +373,8 @@ enum sectorlabels
SECTOR_ALIGNTO, SECTOR_ALIGNTO,
SECTOR_LOTAG, SECTOR_LOTAG,
SECTOR_HITAG, SECTOR_HITAG,
SECTOR_EXTRA SECTOR_EXTRA,
SECTOR_END
}; };
enum walllabels enum walllabels
@ -392,7 +395,8 @@ enum walllabels
WALL_YPANNING, WALL_YPANNING,
WALL_LOTAG, WALL_LOTAG,
WALL_HITAG, WALL_HITAG,
WALL_EXTRA WALL_EXTRA,
WALL_END
}; };
enum actorlabels enum actorlabels
@ -448,6 +452,7 @@ enum actorlabels
ACTOR_XPANNING, ACTOR_XPANNING,
ACTOR_YPANNING, ACTOR_YPANNING,
ACTOR_HTFLAGS, ACTOR_HTFLAGS,
ACTOR_END
}; };
enum inputlabels enum inputlabels
@ -457,7 +462,8 @@ enum inputlabels
INPUT_FVEL, INPUT_FVEL,
INPUT_SVEL, INPUT_SVEL,
INPUT_BITS, INPUT_BITS,
INPUT_EXTBITS INPUT_EXTBITS,
INPUT_END
}; };
enum projectilelabels enum projectilelabels
@ -489,7 +495,8 @@ enum projectilelabels
PROJ_OFFSET, PROJ_OFFSET,
PROJ_BOUNCES, PROJ_BOUNCES,
PROJ_BSOUND, PROJ_BSOUND,
PROJ_RANGE // 28 PROJ_RANGE, // 28
PROJ_END
}; };
enum keywords enum keywords
@ -831,4 +838,5 @@ enum keywords
CON_SETGAMEPALETTE, // 334 CON_SETGAMEPALETTE, // 334
CON_SETDEFNAME, // 335 CON_SETDEFNAME, // 335
CON_SETCFGNAME, // 336 CON_SETCFGNAME, // 336
CON_END
}; };