Clean up of hash code

This commit is contained in:
Shahram Najm 2018-01-22 18:26:10 +00:00
parent 5c9a446a53
commit 2016032263
2 changed files with 29 additions and 27 deletions

View File

@ -11000,9 +11000,6 @@ void grib_hash_keys_delete(grib_itrie *t)
int grib_hash_keys_get_id(grib_itrie* t,const char* key) 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)); const struct grib_keys_hash* hash=grib_keys_hash_get(key,strlen(key));
if (hash) { if (hash) {
@ -11011,19 +11008,23 @@ int grib_hash_keys_get_id(grib_itrie* t,const char* key)
} }
/* printf("+++ \"%s\"\n",key); */ /* printf("+++ \"%s\"\n",key); */
{
const char *k=key;
grib_itrie* last=t;
GRIB_MUTEX_INIT_ONCE(&once,&init); GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&mutex); 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) { if(t != NULL && t->id != -1) {
GRIB_MUTEX_UNLOCK(&mutex); GRIB_MUTEX_UNLOCK(&mutex);
return t->id+TOTAL_KEYWORDS+1; return t->id+TOTAL_KEYWORDS+1;
} else { } else {
int ret=grib_hash_keys_insert(last,key); int ret=grib_hash_keys_insert(last,key);
GRIB_MUTEX_UNLOCK(&mutex); GRIB_MUTEX_UNLOCK(&mutex);
return ret+TOTAL_KEYWORDS+1; return ret+TOTAL_KEYWORDS+1;
}
} }
} }

View File

@ -339,10 +339,7 @@ void grib_hash_keys_delete(grib_itrie *t)
int grib_hash_keys_get_id(grib_itrie* t,const char* key) int grib_hash_keys_get_id(grib_itrie* t,const char* key)
{ {
const char *k=key; const struct grib_keys_hash* hash=grib_keys_hash_get(key,strlen(key));
grib_itrie* last=t;
struct grib_keys_hash* hash=grib_keys_hash_get(key,strlen(key));
if (hash) { if (hash) {
/* printf("%s found %s (%d)\n",key,hash->name,hash->id); */ /* 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); */ /* printf("+++ \"%s\"\n",key); */
{
const char *k=key;
grib_itrie* last=t;
GRIB_MUTEX_INIT_ONCE(&once,&init); GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&mutex); 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) { if(t != NULL && t->id != -1) {
GRIB_MUTEX_UNLOCK(&mutex); GRIB_MUTEX_UNLOCK(&mutex);
return t->id+TOTAL_KEYWORDS+1; return t->id+TOTAL_KEYWORDS+1;
} else { } else {
int ret=grib_hash_keys_insert(last,key); int ret=grib_hash_keys_insert(last,key);
GRIB_MUTEX_UNLOCK(&mutex); GRIB_MUTEX_UNLOCK(&mutex);
return ret+TOTAL_KEYWORDS+1; return ret+TOTAL_KEYWORDS+1;
}
} }
} }