From 52c508423e97514b8db9eff21f9ae40be8e41dc9 Mon Sep 17 00:00:00 2001 From: Stefan Frederik Date: Sun, 31 Oct 2021 13:35:06 +0100 Subject: [PATCH] eliminated duplicated string hash functions --- src/hilight.c | 2 +- src/node_hash.c | 13 +------------ src/spice_netlist.c | 12 ------------ src/token.c | 7 +++---- src/xschem.h | 1 + 5 files changed, 6 insertions(+), 29 deletions(-) diff --git a/src/hilight.c b/src/hilight.c index 319adc00..b3d3063b 100644 --- a/src/hilight.c +++ b/src/hilight.c @@ -24,7 +24,7 @@ static unsigned int hi_hash(const char *tok) { - register unsigned int hash = 0; + register unsigned int hash = 14057; register char *str; register int c; diff --git a/src/node_hash.c b/src/node_hash.c index 21b081ac..90242691 100644 --- a/src/node_hash.c +++ b/src/node_hash.c @@ -22,17 +22,6 @@ #include "xschem.h" -static unsigned int nh_hash(const char *tok) -{ - register unsigned int hash = 0; - register int c; - - while ( (c = *tok++) ) - hash = c + hash * 65599; - return hash; -} - - struct node_hashentry **get_node_table_ptr(void) { return xctx->node_table; @@ -226,7 +215,7 @@ struct node_hashentry *node_hash_lookup(const char *token, const char *dir,int w else if(!strcmp(dir,"out") ) d.out=1; else if(!strcmp(dir,"inout") ) d.inout=1; d.port=port; - hashcode=nh_hash(token); + hashcode=str_hash(token); index=hashcode % HASHSIZE; entry=xctx->node_table[index]; preventry=&xctx->node_table[index]; diff --git a/src/spice_netlist.c b/src/spice_netlist.c index e1fdc3ff..51210e10 100644 --- a/src/spice_netlist.c +++ b/src/spice_netlist.c @@ -549,18 +549,6 @@ void spice_netlist(FILE *fd, int spice_stop ) if(!spice_stop && !xctx->netlist_count) redraw_hilights(0); /* draw_hilight_net(1); */ } -/* calculate the hash function relative to string s */ -static unsigned int str_hash(const char *tok) -{ - unsigned int hash = 0; - int c; - - while ( (c = *tok++) ) - hash = c + (hash << 6) + (hash << 16) - hash; - return hash; -} - - /* GENERIC PURPOSE HASH TABLE */ diff --git a/src/token.c b/src/token.c index bf03b69b..e92edfaf 100644 --- a/src/token.c +++ b/src/token.c @@ -36,10 +36,9 @@ static struct inst_hashentry *table[HASHSIZE]; enum status {TOK_BEGIN, TOK_TOKEN, TOK_SEP, TOK_VALUE, TOK_END, TOK_ENDTOK}; -/* calculate the hash function relative to string s */ -static unsigned int hash(char *tok) +unsigned int str_hash(const char *tok) { - register unsigned int hash = 0; + register unsigned int hash = 14057; register int c; while ( (c = *tok++) ) @@ -84,7 +83,7 @@ static struct inst_hashentry *inst_hash_lookup(struct inst_hashentry **table, ch int s; if(token==NULL) return NULL; - hashcode=hash(token); + hashcode=str_hash(token); idx=hashcode % HASHSIZE; entry=table[idx]; preventry=&table[idx]; diff --git a/src/xschem.h b/src/xschem.h index 99087bbd..8e58a4fb 100644 --- a/src/xschem.h +++ b/src/xschem.h @@ -1058,6 +1058,7 @@ extern void check_unique_names(int rename); extern void clear_instance_hash(); +extern unsigned int str_hash(const char *tok); extern void free_hash(struct hashentry **table); extern struct hashentry *str_hash_lookup(struct hashentry **table, const char *token, const char *value, int what); extern void free_int_hash(struct int_hashentry **table);