Commit 294c3427 authored by Lionel Gauthier's avatar Lionel Gauthier

tabs 2 spaces

git-svn-id: http://svn.eurecom.fr/openair4G/trunk@5181 818b1a75-f10b-46b9-bf7c-635c3b92a50f
parent 4824fdf7
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "hashtable.h" #include "hashtable.h"
#include "assertions.h"
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
...@@ -37,7 +38,7 @@ void hash_free_int_func(void* memoryP){} ...@@ -37,7 +38,7 @@ void hash_free_int_func(void* memoryP){}
static hash_size_t def_hashfunc(const uint64_t keyP) static hash_size_t def_hashfunc(const uint64_t keyP)
{ {
return (hash_size_t)keyP; return (hash_size_t)keyP;
} }
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
...@@ -49,24 +50,26 @@ static hash_size_t def_hashfunc(const uint64_t keyP) ...@@ -49,24 +50,26 @@ static hash_size_t def_hashfunc(const uint64_t keyP)
*/ */
hash_table_t *hashtable_create(hash_size_t sizeP, hash_size_t (*hashfuncP)(const uint64_t ), void (*freefuncP)(void*)) hash_table_t *hashtable_create(hash_size_t sizeP, hash_size_t (*hashfuncP)(const uint64_t ), void (*freefuncP)(void*))
{ {
hash_table_t *hashtbl; hash_table_t *hashtbl = NULL;
if(!(hashtbl=malloc(sizeof(hash_table_t)))) return NULL; if(!(hashtbl=malloc(sizeof(hash_table_t)))) {
return NULL;
}
if(!(hashtbl->nodes=calloc(sizeP, sizeof(hash_node_t*)))) { if(!(hashtbl->nodes=calloc(sizeP, sizeof(hash_node_t*)))) {
free(hashtbl); free(hashtbl);
return NULL; return NULL;
} }
hashtbl->size=sizeP; hashtbl->size=sizeP;
if(hashfuncP) hashtbl->hashfunc=hashfuncP; if(hashfuncP) hashtbl->hashfunc=hashfuncP;
else hashtbl->hashfunc=def_hashfunc; else hashtbl->hashfunc=def_hashfunc;
if(freefuncP) hashtbl->freefunc=freefuncP; if(freefuncP) hashtbl->freefunc=freefuncP;
else hashtbl->freefunc=free; else hashtbl->freefunc=free;
return hashtbl; return hashtbl;
} }
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
/* /*
...@@ -75,71 +78,71 @@ hash_table_t *hashtable_create(hash_size_t sizeP, hash_size_t (*hashfuncP)(const ...@@ -75,71 +78,71 @@ hash_table_t *hashtable_create(hash_size_t sizeP, hash_size_t (*hashfuncP)(const
*/ */
hashtable_rc_t hashtable_destroy(hash_table_t *hashtblP) hashtable_rc_t hashtable_destroy(hash_table_t *hashtblP)
{ {
hash_size_t n; hash_size_t n;
hash_node_t *node, *oldnode; hash_node_t *node, *oldnode;
if (hashtblP == NULL) { if (hashtblP == NULL) {
return HASH_TABLE_BAD_PARAMETER_HASHTABLE; return HASH_TABLE_BAD_PARAMETER_HASHTABLE;
} }
for(n=0; n<hashtblP->size; ++n) { for(n=0; n<hashtblP->size; ++n) {
node=hashtblP->nodes[n]; node=hashtblP->nodes[n];
while(node) { while(node) {
oldnode=node; oldnode=node;
node=node->next; node=node->next;
if (oldnode->data) { if (oldnode->data) {
hashtblP->freefunc(oldnode->data); hashtblP->freefunc(oldnode->data);
} }
free(oldnode); free(oldnode);
} }
} }
free(hashtblP->nodes); free(hashtblP->nodes);
free(hashtblP); free(hashtblP);
return HASH_TABLE_OK; return HASH_TABLE_OK;
} }
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
hashtable_rc_t hashtable_is_key_exists (hash_table_t *hashtblP, const uint64_t keyP) hashtable_rc_t hashtable_is_key_exists (hash_table_t *hashtblP, const uint64_t keyP)
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
{ {
hash_node_t *node; hash_node_t *node;
hash_size_t hash; hash_size_t hash;
if (hashtblP == NULL) { if (hashtblP == NULL) {
return HASH_TABLE_BAD_PARAMETER_HASHTABLE; return HASH_TABLE_BAD_PARAMETER_HASHTABLE;
} }
hash=hashtblP->hashfunc(keyP)%hashtblP->size; hash=hashtblP->hashfunc(keyP)%hashtblP->size;
node=hashtblP->nodes[hash]; node=hashtblP->nodes[hash];
while(node) { while(node) {
if(node->key == keyP) { if(node->key == keyP) {
return HASH_TABLE_OK; return HASH_TABLE_OK;
} }
node=node->next; node=node->next;
} }
return HASH_TABLE_KEY_NOT_EXISTS; return HASH_TABLE_KEY_NOT_EXISTS;
} }
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
hashtable_rc_t hashtable_apply_funct_on_elements (hash_table_t *hashtblP, void functP(uint64_t keyP, void* dataP, void* parameterP), void* parameterP) hashtable_rc_t hashtable_apply_funct_on_elements (hash_table_t *hashtblP, void functP(uint64_t keyP, void* dataP, void* parameterP), void* parameterP)
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
{ {
hash_node_t *node = NULL; hash_node_t *node = NULL;
unsigned int i = 0; unsigned int i = 0;
unsigned int num_elements = 0; unsigned int num_elements = 0;
if (hashtblP == NULL) { if (hashtblP == NULL) {
return HASH_TABLE_BAD_PARAMETER_HASHTABLE; return HASH_TABLE_BAD_PARAMETER_HASHTABLE;
} }
while ((num_elements < hashtblP->num_elements) && (i < hashtblP->size)) { while ((num_elements < hashtblP->num_elements) && (i < hashtblP->size)) {
if (hashtblP->nodes[i] != NULL) { if (hashtblP->nodes[i] != NULL) {
node=hashtblP->nodes[i]; node=hashtblP->nodes[i];
while(node) { while(node) {
num_elements += 1; num_elements += 1;
functP(node->key, node->data, parameterP); functP(node->key, node->data, parameterP);
node=node->next; node=node->next;
} }
} }
i += 1; i += 1;
} }
return HASH_TABLE_OK; return HASH_TABLE_OK;
} }
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
/* /*
...@@ -148,35 +151,35 @@ hashtable_rc_t hashtable_apply_funct_on_elements (hash_table_t *hashtblP, void f ...@@ -148,35 +151,35 @@ hashtable_rc_t hashtable_apply_funct_on_elements (hash_table_t *hashtblP, void f
*/ */
hashtable_rc_t hashtable_insert(hash_table_t *hashtblP, const uint64_t keyP, void *dataP) hashtable_rc_t hashtable_insert(hash_table_t *hashtblP, const uint64_t keyP, void *dataP)
{ {
hash_node_t *node; hash_node_t *node;
hash_size_t hash; hash_size_t hash;
if (hashtblP == NULL) { if (hashtblP == NULL) {
return HASH_TABLE_BAD_PARAMETER_HASHTABLE; return HASH_TABLE_BAD_PARAMETER_HASHTABLE;
} }
hash=hashtblP->hashfunc(keyP)%hashtblP->size; hash=hashtblP->hashfunc(keyP)%hashtblP->size;
node=hashtblP->nodes[hash]; node=hashtblP->nodes[hash];
while(node) { while(node) {
if(node->key == keyP) { if(node->key == keyP) {
if (node->data) { if (node->data) {
hashtblP->freefunc(node->data); hashtblP->freefunc(node->data);
} }
node->data=dataP; node->data=dataP;
return HASH_TABLE_INSERT_OVERWRITTEN_DATA; return HASH_TABLE_INSERT_OVERWRITTEN_DATA;
} }
node=node->next; node=node->next;
} }
if(!(node=malloc(sizeof(hash_node_t)))) return -1; if(!(node=malloc(sizeof(hash_node_t)))) return -1;
node->key=keyP; node->key=keyP;
node->data=dataP; node->data=dataP;
if (hashtblP->nodes[hash]) { if (hashtblP->nodes[hash]) {
node->next=hashtblP->nodes[hash]; node->next=hashtblP->nodes[hash];
} else { } else {
node->next = NULL; node->next = NULL;
} }
hashtblP->nodes[hash]=node; hashtblP->nodes[hash]=node;
hashtblP->num_elements += 1; hashtblP->num_elements += 1;
return HASH_TABLE_OK; return HASH_TABLE_OK;
} }
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
/* /*
...@@ -185,29 +188,29 @@ hashtable_rc_t hashtable_insert(hash_table_t *hashtblP, const uint64_t keyP, voi ...@@ -185,29 +188,29 @@ hashtable_rc_t hashtable_insert(hash_table_t *hashtblP, const uint64_t keyP, voi
*/ */
hashtable_rc_t hashtable_remove(hash_table_t *hashtblP, const uint64_t keyP) hashtable_rc_t hashtable_remove(hash_table_t *hashtblP, const uint64_t keyP)
{ {
hash_node_t *node, *prevnode=NULL; hash_node_t *node, *prevnode=NULL;
hash_size_t hash; hash_size_t hash;
if (hashtblP == NULL) { if (hashtblP == NULL) {
return HASH_TABLE_BAD_PARAMETER_HASHTABLE; return HASH_TABLE_BAD_PARAMETER_HASHTABLE;
} }
hash=hashtblP->hashfunc(keyP)%hashtblP->size; hash=hashtblP->hashfunc(keyP)%hashtblP->size;
node=hashtblP->nodes[hash]; node=hashtblP->nodes[hash];
while(node) { while(node) {
if(node->key != keyP) { if(node->key != keyP) {
if(prevnode) prevnode->next=node->next; if(prevnode) prevnode->next=node->next;
else hashtblP->nodes[hash]=node->next; else hashtblP->nodes[hash]=node->next;
if (node->data) { if (node->data) {
hashtblP->freefunc(node->data); hashtblP->freefunc(node->data);
} }
free(node); free(node);
hashtblP->num_elements -= 1; hashtblP->num_elements -= 1;
return HASH_TABLE_OK; return HASH_TABLE_OK;
} }
prevnode=node; prevnode=node;
node=node->next; node=node->next;
} }
return HASH_TABLE_KEY_NOT_EXISTS; return HASH_TABLE_KEY_NOT_EXISTS;
} }
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
/* /*
...@@ -216,27 +219,27 @@ hashtable_rc_t hashtable_remove(hash_table_t *hashtblP, const uint64_t keyP) ...@@ -216,27 +219,27 @@ hashtable_rc_t hashtable_remove(hash_table_t *hashtblP, const uint64_t keyP)
*/ */
hashtable_rc_t hashtable_get(hash_table_t *hashtblP, const uint64_t keyP, void** dataP) hashtable_rc_t hashtable_get(hash_table_t *hashtblP, const uint64_t keyP, void** dataP)
{ {
hash_node_t *node; hash_node_t *node;
hash_size_t hash; hash_size_t hash;
if (hashtblP == NULL) { if (hashtblP == NULL) {
*dataP = NULL; *dataP = NULL;
return HASH_TABLE_BAD_PARAMETER_HASHTABLE; return HASH_TABLE_BAD_PARAMETER_HASHTABLE;
} }
hash=hashtblP->hashfunc(keyP)%hashtblP->size; hash=hashtblP->hashfunc(keyP)%hashtblP->size;
/* fprintf(stderr, "hashtable_get() key=%s, hash=%d\n", key, hash);*/ /* fprintf(stderr, "hashtable_get() key=%s, hash=%d\n", key, hash);*/
node=hashtblP->nodes[hash]; node=hashtblP->nodes[hash];
while(node) { while(node) {
if(node->key == keyP) { if(node->key == keyP) {
*dataP = node->data; *dataP = node->data;
return HASH_TABLE_OK; return HASH_TABLE_OK;
} }
node=node->next; node=node->next;
} }
*dataP = NULL; *dataP = NULL;
return HASH_TABLE_KEY_NOT_EXISTS; return HASH_TABLE_KEY_NOT_EXISTS;
} }
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
/* /*
...@@ -252,34 +255,34 @@ hashtable_rc_t hashtable_get(hash_table_t *hashtblP, const uint64_t keyP, void** ...@@ -252,34 +255,34 @@ hashtable_rc_t hashtable_get(hash_table_t *hashtblP, const uint64_t keyP, void**
hashtable_rc_t hashtable_resize(hash_table_t *hashtblP, hash_size_t sizeP) hashtable_rc_t hashtable_resize(hash_table_t *hashtblP, hash_size_t sizeP)
{ {
hash_table_t newtbl; hash_table_t newtbl;
hash_size_t n; hash_size_t n;
hash_node_t *node,*next; hash_node_t *node,*next;
if (hashtblP == NULL) { if (hashtblP == NULL) {
return HASH_TABLE_BAD_PARAMETER_HASHTABLE; return HASH_TABLE_BAD_PARAMETER_HASHTABLE;
} }
newtbl.size = sizeP; newtbl.size = sizeP;
newtbl.hashfunc = hashtblP->hashfunc; newtbl.hashfunc = hashtblP->hashfunc;
if(!(newtbl.nodes=calloc(sizeP, sizeof(hash_node_t*)))) return -1; if(!(newtbl.nodes=calloc(sizeP, sizeof(hash_node_t*)))) return -1;
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;
hashtable_insert(&newtbl, node->key, node->data); hashtable_insert(&newtbl, node->key, node->data);
// Lionel GAUTHIER: BAD CODE TO BE REWRITTEN // Lionel GAUTHIER: BAD CODE TO BE REWRITTEN
hashtable_remove(hashtblP, node->key); hashtable_remove(hashtblP, node->key);
} }
} }
free(hashtblP->nodes); free(hashtblP->nodes);
hashtblP->size=newtbl.size; hashtblP->size=newtbl.size;
hashtblP->nodes=newtbl.nodes; hashtblP->nodes=newtbl.nodes;
return HASH_TABLE_OK; return HASH_TABLE_OK;
} }
......
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