Commit dbc78cfb authored by Lionel Gauthier's avatar Lionel Gauthier

Update

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@4795 818b1a75-f10b-46b9-bf7c-635c3b92a50f
parent fd2bda19
...@@ -12,6 +12,7 @@ typedef enum hashtable_return_code_e { ...@@ -12,6 +12,7 @@ typedef enum hashtable_return_code_e {
HASH_TABLE_KEY_NOT_EXISTS = 2, HASH_TABLE_KEY_NOT_EXISTS = 2,
HASH_TABLE_KEY_ALREADY_EXISTS = 3, HASH_TABLE_KEY_ALREADY_EXISTS = 3,
HASH_TABLE_BAD_PARAMETER_HASHTABLE = 4, HASH_TABLE_BAD_PARAMETER_HASHTABLE = 4,
HASH_TABLE_SYSTEM_ERROR = 5,
HASH_TABLE_CODE_MAX HASH_TABLE_CODE_MAX
} hashtable_rc_t; } hashtable_rc_t;
......
...@@ -91,6 +91,10 @@ hashtable_rc_t obj_hashtable_is_key_exists (obj_hash_table_t *hashtblP, void* ke ...@@ -91,6 +91,10 @@ hashtable_rc_t obj_hashtable_is_key_exists (obj_hash_table_t *hashtblP, void* ke
while(node) { while(node) {
if(node->key == keyP) { if(node->key == keyP) {
return HASH_TABLE_OK; return HASH_TABLE_OK;
} else if (node->key_size == key_sizeP) {
if (memcmp(node->key, keyP, key_sizeP) == 0) {
return HASH_TABLE_OK;
}
} }
node=node->next; node=node->next;
} }
...@@ -150,7 +154,7 @@ hashtable_rc_t obj_hashtable_remove(obj_hash_table_t *hashtblP, const void* keyP ...@@ -150,7 +154,7 @@ hashtable_rc_t obj_hashtable_remove(obj_hash_table_t *hashtblP, const void* keyP
hash=hashtblP->hashfunc(keyP, key_sizeP)%hashtblP->size; hash=hashtblP->hashfunc(keyP, key_sizeP)%hashtblP->size;
node=hashtblP->nodes[hash]; node=hashtblP->nodes[hash];
while(node) { while(node) {
if(node->key == keyP) { if ((node->key == keyP) || ((node->key_size == key_sizeP) && (memcmp(node->key, keyP, key_sizeP) == 0))){
if(prevnode) { if(prevnode) {
prevnode->next=node->next; prevnode->next=node->next;
} else { } else {
...@@ -186,6 +190,11 @@ hashtable_rc_t obj_hashtable_get(obj_hash_table_t *hashtblP, const void* keyP, i ...@@ -186,6 +190,11 @@ hashtable_rc_t obj_hashtable_get(obj_hash_table_t *hashtblP, const void* keyP, i
if(node->key == keyP) { if(node->key == keyP) {
*dataP = node->data; *dataP = node->data;
return HASH_TABLE_OK; return HASH_TABLE_OK;
} else if (node->key_size == key_sizeP) {
if (memcmp(node->key, keyP, key_sizeP) == 0) {
*dataP = node->data;
return HASH_TABLE_OK;
}
} }
node=node->next; node=node->next;
} }
...@@ -193,6 +202,29 @@ hashtable_rc_t obj_hashtable_get(obj_hash_table_t *hashtblP, const void* keyP, i ...@@ -193,6 +202,29 @@ hashtable_rc_t obj_hashtable_get(obj_hash_table_t *hashtblP, const void* keyP, i
return HASH_TABLE_KEY_NOT_EXISTS; return HASH_TABLE_KEY_NOT_EXISTS;
} }
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
/*
* Function to return all keys of an object hash table
*/
hashtable_rc_t obj_hashtable_get_keys(obj_hash_table_t *hashtblP, void ** keysP, unsigned int *sizeP)
{
size_t n = 0;
obj_hash_node_t *node = NULL;
obj_hash_node_t *next = NULL;
*sizeP = 0;
keysP = calloc(hashtblP->num_elements, sizeof(void *));
if (keysP) {
for(n=0; n<hashtblP->size; ++n) {
for(node=hashtblP->nodes[n]; node; node=next) {
keysP[*sizeP++] = node->key;
next = node->next;
}
}
return HASH_TABLE_OK;
}
return HASH_TABLE_SYSTEM_ERROR;
}
//-------------------------------------------------------------------------------------------------------------------------------
/* /*
* Resizing * Resizing
* The number of elements in a hash table is not always known when creating the table. * The number of elements in a hash table is not always known when creating the table.
...@@ -216,13 +248,12 @@ hashtable_rc_t obj_hashtable_resize(obj_hash_table_t *hashtblP, hash_size_t size ...@@ -216,13 +248,12 @@ hashtable_rc_t obj_hashtable_resize(obj_hash_table_t *hashtblP, hash_size_t size
newtbl.size = sizeP; newtbl.size = sizeP;
newtbl.hashfunc = hashtblP->hashfunc; newtbl.hashfunc = hashtblP->hashfunc;
if(!(newtbl.nodes=calloc(sizeP, sizeof(obj_hash_node_t*)))) return -1; if(!(newtbl.nodes=calloc(sizeP, sizeof(obj_hash_node_t*)))) return HASH_TABLE_SYSTEM_ERROR;
for(n=0; n<hashtblP->size; ++n) { for(n=0; n<hashtblP->size; ++n) {
for(node=hashtblP->nodes[n]; node; node=next) { for(node=hashtblP->nodes[n]; node; node=next) {
next = node->next; next = node->next;
obj_hashtable_insert(&newtbl, node->key, node->key_size, node->data); obj_hashtable_insert(&newtbl, node->key, node->key_size, node->data);
//WARNING Lionel GAUTHIER: BAD CODE TO BE REWRITTEN
obj_hashtable_remove(hashtblP, node->key, node->key_size); obj_hashtable_remove(hashtblP, node->key, node->key_size);
} }
} }
......
...@@ -26,11 +26,12 @@ typedef struct obj_hash_table_s { ...@@ -26,11 +26,12 @@ typedef struct obj_hash_table_s {
} obj_hash_table_t; } obj_hash_table_t;
obj_hash_table_t *obj_hashtable_create (hash_size_t size, hash_size_t (*hashfunc)(const void*, int ), void (*freekeyfunc)(void*), void (*freedatafunc)(void*)); obj_hash_table_t *obj_hashtable_create (hash_size_t size, hash_size_t (*hashfunc)(const void*, int ), void (*freekeyfunc)(void*), void (*freedatafunc)(void*));
hashtable_rc_t obj_hashtable_destroy(obj_hash_table_t *hashtblP); hashtable_rc_t obj_hashtable_destroy (obj_hash_table_t *hashtblP);
hashtable_rc_t obj_hashtable_is_key_exists (obj_hash_table_t *hashtblP, void* keyP, int key_sizeP); hashtable_rc_t obj_hashtable_is_key_exists (obj_hash_table_t *hashtblP, void* keyP, int key_sizeP);
hashtable_rc_t obj_hashtable_insert (obj_hash_table_t *hashtblP, void* keyP, int key_sizeP, void *dataP); hashtable_rc_t obj_hashtable_insert (obj_hash_table_t *hashtblP, void* keyP, int key_sizeP, void *dataP);
hashtable_rc_t obj_hashtable_remove (obj_hash_table_t *hashtblP, const void* keyP, int key_sizeP); hashtable_rc_t obj_hashtable_remove (obj_hash_table_t *hashtblP, const void* keyP, int key_sizeP);
hashtable_rc_t obj_hashtable_get (obj_hash_table_t *hashtblP, const void* keyP, int key_sizeP, void ** dataP); hashtable_rc_t obj_hashtable_get (obj_hash_table_t *hashtblP, const void* keyP, int key_sizeP, void ** dataP);
hashtable_rc_t obj_hashtable_get_keys(obj_hash_table_t *hashtblP, void ** keysP, unsigned int *sizeP);
hashtable_rc_t obj_hashtable_resize (obj_hash_table_t *hashtblP, hash_size_t sizeP); hashtable_rc_t obj_hashtable_resize (obj_hash_table_t *hashtblP, hash_size_t sizeP);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment