From 2016032263cfe839492aed39a037ed76a66d1d1f Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Mon, 22 Jan 2018 18:26:10 +0000 Subject: [PATCH] Clean up of hash code --- src/grib_hash_keys.c | 27 ++++++++++++++------------- src/grib_itrie_keys.c | 29 +++++++++++++++-------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/grib_hash_keys.c b/src/grib_hash_keys.c index 7d70c18ff..c2ccbf974 100644 --- a/src/grib_hash_keys.c +++ b/src/grib_hash_keys.c @@ -11000,9 +11000,6 @@ void grib_hash_keys_delete(grib_itrie *t) int grib_hash_keys_get_id(grib_itrie* t,const char* key) { - const char *k=key; - grib_itrie* last=t; - const struct grib_keys_hash* hash=grib_keys_hash_get(key,strlen(key)); if (hash) { @@ -11011,19 +11008,23 @@ int grib_hash_keys_get_id(grib_itrie* t,const char* key) } /* printf("+++ \"%s\"\n",key); */ + { + const char *k=key; + grib_itrie* last=t; - GRIB_MUTEX_INIT_ONCE(&once,&init); - GRIB_MUTEX_LOCK(&mutex); + GRIB_MUTEX_INIT_ONCE(&once,&init); + GRIB_MUTEX_LOCK(&mutex); - while(*k && t) t = t->next[mapping[(int)*k++]]; + while(*k && t) t = t->next[mapping[(int)*k++]]; - if(t != NULL && t->id != -1) { - GRIB_MUTEX_UNLOCK(&mutex); - return t->id+TOTAL_KEYWORDS+1; - } else { - int ret=grib_hash_keys_insert(last,key); - GRIB_MUTEX_UNLOCK(&mutex); - return ret+TOTAL_KEYWORDS+1; + if(t != NULL && t->id != -1) { + GRIB_MUTEX_UNLOCK(&mutex); + return t->id+TOTAL_KEYWORDS+1; + } else { + int ret=grib_hash_keys_insert(last,key); + GRIB_MUTEX_UNLOCK(&mutex); + return ret+TOTAL_KEYWORDS+1; + } } } diff --git a/src/grib_itrie_keys.c b/src/grib_itrie_keys.c index c0f17aafe..0b3220d57 100644 --- a/src/grib_itrie_keys.c +++ b/src/grib_itrie_keys.c @@ -339,10 +339,7 @@ void grib_hash_keys_delete(grib_itrie *t) int grib_hash_keys_get_id(grib_itrie* t,const char* key) { - const char *k=key; - grib_itrie* last=t; - - struct grib_keys_hash* hash=grib_keys_hash_get(key,strlen(key)); + const struct grib_keys_hash* hash=grib_keys_hash_get(key,strlen(key)); if (hash) { /* printf("%s found %s (%d)\n",key,hash->name,hash->id); */ @@ -350,19 +347,23 @@ int grib_hash_keys_get_id(grib_itrie* t,const char* key) } /* printf("+++ \"%s\"\n",key); */ + { + const char *k=key; + grib_itrie* last=t; - GRIB_MUTEX_INIT_ONCE(&once,&init); - GRIB_MUTEX_LOCK(&mutex); + GRIB_MUTEX_INIT_ONCE(&once,&init); + GRIB_MUTEX_LOCK(&mutex); - while(*k && t) t = t->next[mapping[(int)*k++]]; + while(*k && t) t = t->next[mapping[(int)*k++]]; - if(t != NULL && t->id != -1) { - GRIB_MUTEX_UNLOCK(&mutex); - return t->id+TOTAL_KEYWORDS+1; - } else { - int ret=grib_hash_keys_insert(last,key); - GRIB_MUTEX_UNLOCK(&mutex); - return ret+TOTAL_KEYWORDS+1; + if(t != NULL && t->id != -1) { + GRIB_MUTEX_UNLOCK(&mutex); + return t->id+TOTAL_KEYWORDS+1; + } else { + int ret=grib_hash_keys_insert(last,key); + GRIB_MUTEX_UNLOCK(&mutex); + return ret+TOTAL_KEYWORDS+1; + } } }