int hash_put( HASH *map, UCHAR *key, long int keysize, void *value ) { unsigned int index = 0; BUCKET *bucket = NULL; PAIR *pair = NULL; if( map == NULL || key == NULL || value == NULL ) { return 0; } index = hash_this( key,keysize) % map->count; bucket = &(map->buckets[index] ); /* Key already exists */ if( ( pair = hash_getpair( bucket, key, keysize)) != NULL ) { pair->value = value; return 1; } /* Create new obj_pair */ if( bucket->count == 0 ) { bucket->pairs = myalloc( sizeof(PAIR), "hash_put" ); bucket->count = 1; } else { bucket->pairs = myrealloc( bucket->pairs,( bucket->count + 1) * sizeof(PAIR), "hash_put" ); bucket->count++; } /* Store key pairs */ pair = &(bucket->pairs[bucket->count - 1] ); pair->key = key; pair->keysize = keysize; pair->value = value; return 1; }
int main(void) { hash_t *hash = hash_make(); char *key = NULL; int n = 100,*val; unsigned int count; hash_index_t *hi; unsigned int sum = 0; int i; for(i = 0;i<100000;++i) { key = malloc(sizeof(char) * 256); memset(key,0,256); val = malloc(sizeof(int)); *val = i; sprintf(key,"char%10d",i); hash_set(hash,key,HASH_KEY_STRING,val); void *entry_key; val = hash_get(hash,key,HASH_KEY_STRING,&entry_key); if(entry_key) { printf("key:%s\n",(char*)entry_key); } // if(val) // printf("val:%d\n",*val); } for (hi = hash_first(hash); hi ; hi = hash_next(hi)){ hash_this(hi,(const void **)&key,NULL,(void **)&val); hash_set(hash,key,HASH_KEY_STRING,NULL); printf("val:%d\n",*(int *)val); sum += *(int *)val; free(key); free(val); } printf("sum:%d\n",sum); count = hash_count(hash); printf("count:%u\n",count); hash_clear(hash); count = hash_count(hash); printf("count after clear:%u\n",count); hash_destroy(hash); return 0; }
void hash_del( HASH *map, UCHAR *key, long int keysize ) { unsigned int index = 0; BUCKET *bucket = NULL; PAIR *thispair = NULL; PAIR *oldpair = NULL; PAIR *newpair = NULL; PAIR *p_old = NULL; PAIR *p_new = NULL; unsigned int i = 0; if( map == NULL || key == NULL ) { return; } /* Compute bucket */ index = hash_this( key,keysize) % map->count; bucket = &(map->buckets[index] ); /* Not found */ if( ( thispair = hash_getpair( bucket,key,keysize)) == NULL ) { return; } if( bucket->count == 1 ) { myfree( bucket->pairs, "hash_rem" ); bucket->pairs = NULL; bucket->count = 0; } else if( bucket->count > 1 ) { /* Get new memory and remember the old one */ oldpair = bucket->pairs; newpair = myalloc( (bucket->count - 1) * sizeof(PAIR), "hash_rem" ); /* Copy pairs except the one to delete */ p_old = oldpair; p_new = newpair; for( i=0; i<bucket->count; i++ ) { if( p_old != thispair ) { memcpy( p_new++, p_old, sizeof(PAIR) ); } p_old++; } myfree( oldpair, "hash_rem" ); bucket->pairs = newpair; bucket->count--; } }
void *hash_get( const HASH *map, UCHAR *key, long int keysize ) { unsigned int index = 0; BUCKET *bucket = NULL; PAIR *pair = NULL; if( map == NULL || key == NULL ) { return NULL; } index = hash_this( key,keysize) % map->count; bucket = &(map->buckets[index] ); pair = hash_getpair( bucket,key,keysize ); if( pair == NULL ) { return NULL; } return pair->value; }