From 71054a834e39f49638a138f1ddd72c421de87b5f Mon Sep 17 00:00:00 2001 From: Jeff Squyres Date: Thu, 16 Sep 2004 23:51:30 +0000 Subject: [PATCH] - minor format changes - remove some unnecessary functions - use OBJ_* macros - fix ompi_hase_table_get_[first|next]_key_uint[32|64] functions This commit was SVN r2732. --- src/class/ompi_hash_table.c | 231 ++++++++++++++++++++++-------------- 1 file changed, 139 insertions(+), 92 deletions(-) diff --git a/src/class/ompi_hash_table.c b/src/class/ompi_hash_table.c index 210a4c8499..0cf374e1c8 100644 --- a/src/class/ompi_hash_table.c +++ b/src/class/ompi_hash_table.c @@ -11,6 +11,7 @@ #include "include/constants.h" #include "util/output.h" +#include "class/ompi_list.h" #include "class/ompi_hash_table.h" /* @@ -44,10 +45,12 @@ static void ompi_hash_table_destruct(ompi_hash_table_t* ht) { size_t i; ompi_hash_table_remove_all(ht); - for(i=0; iht_table_size; i++) + for(i=0; iht_table_size; i++) { OBJ_DESTRUCT(ht->ht_table+i); - if(NULL != ht->ht_table) + } + if(NULL != ht->ht_table) { free(ht->ht_table); + } OBJ_DESTRUCT(&ht->ht_nodes); } @@ -64,8 +67,9 @@ int ompi_hash_table_init(ompi_hash_table_t* ht, size_t table_size) ht->ht_mask = power2-1; ht->ht_table = malloc(power2 * sizeof(ompi_list_t)); - if(NULL == ht->ht_table) + if(NULL == ht->ht_table) { return OMPI_ERR_OUT_OF_RESOURCE; + } for(i=ht->ht_table_size; iht_table+i; OBJ_CONSTRUCT(list, ompi_list_t); @@ -93,6 +97,8 @@ int ompi_hash_table_remove_all(ompi_hash_table_t* ht) return OMPI_SUCCESS; } +/***************************************************************************/ + /* * ompi_uint32_hash_node_t */ @@ -105,20 +111,10 @@ struct ompi_uint32_hash_node_t }; typedef struct ompi_uint32_hash_node_t ompi_uint32_hash_node_t; -static void ompi_uint32_hash_node_construct(ompi_uint32_hash_node_t* hn) -{ -} - -static void ompi_uint32_hash_node_destruct(ompi_uint32_hash_node_t* hn) -{ -} - -static ompi_class_t ompi_uint32_hash_node_t_class = { - "ompi_uint32_hash_node_t", - &ompi_list_item_t_class, - (ompi_construct_t)ompi_uint32_hash_node_construct, - (ompi_destruct_t)ompi_uint32_hash_node_destruct -}; +static OBJ_CLASS_INSTANCE(ompi_uint32_hash_node_t, + ompi_list_item_t, + NULL, + NULL); void* ompi_hash_table_get_value_uint32(ompi_hash_table_t* ht, uint32_t key) @@ -205,6 +201,7 @@ int ompi_hash_table_remove_value_uint32(ompi_hash_table_t* ht, uint32_t key) return OMPI_ERR_NOT_FOUND; } +/***************************************************************************/ /* * ompi_uint64_hash_node_t @@ -218,20 +215,10 @@ struct ompi_uint64_hash_node_t }; typedef struct ompi_uint64_hash_node_t ompi_uint64_hash_node_t; -static void ompi_uint64_hash_node_construct(ompi_uint64_hash_node_t* hn) -{ -} - -static void ompi_uint64_hash_node_destruct(ompi_uint64_hash_node_t* hn) -{ -} - -static ompi_class_t ompi_uint64_hash_node_t_class = { - "ompi_uint64_hash_node_t", - OBJ_CLASS(ompi_list_item_t), - (ompi_construct_t)ompi_uint64_hash_node_construct, - (ompi_destruct_t)ompi_uint64_hash_node_destruct -}; +static OBJ_CLASS_INSTANCE(ompi_uint64_hash_node_t, + ompi_list_item_t, + NULL, + NULL); void* ompi_hash_table_get_value_uint64(ompi_hash_table_t* ht, uint64_t key) @@ -282,8 +269,9 @@ int ompi_hash_table_set_value_uint64(ompi_hash_table_t* ht, node = (ompi_uint64_hash_node_t*)ompi_list_remove_first(&ht->ht_nodes); if(NULL == node) { node = OBJ_NEW(ompi_uint64_hash_node_t); - if(NULL == node) + if(NULL == node) { return OMPI_ERR_OUT_OF_RESOURCE; + } } node->hn_key = key; node->hn_value = value; @@ -318,7 +306,7 @@ int ompi_hash_table_remove_value_uint64(ompi_hash_table_t* ht, uint64_t key) return OMPI_ERR_NOT_FOUND; } - +/***************************************************************************/ /* * ompi_ptr_hash_node_t @@ -342,20 +330,19 @@ static void ompi_ptr_hash_node_construct(ompi_ptr_hash_node_t* hn) static void ompi_ptr_hash_node_destruct(ompi_ptr_hash_node_t* hn) { - if(NULL != hn->hn_key) + if(NULL != hn->hn_key) { free(hn->hn_key); + } } -static ompi_class_t ompi_ptr_hash_node_t_class = { - "ompi_ptr_hash_node_t", - OBJ_CLASS(ompi_list_item_t), - (ompi_construct_t)ompi_ptr_hash_node_construct, - (ompi_destruct_t)ompi_ptr_hash_node_destruct -}; +static OBJ_CLASS_INSTANCE(ompi_ptr_hash_node_t, + ompi_list_item_t, + ompi_ptr_hash_node_construct, + ompi_ptr_hash_node_destruct); static inline uint32_t ompi_hash_value(size_t mask, const void *key, - uint32_t keysize) + uint32_t keysize) { uint32_t h, i; const unsigned char *p; @@ -368,10 +355,10 @@ static inline uint32_t ompi_hash_value(size_t mask, const void *key, } void* ompi_hash_table_get_value_ptr(ompi_hash_table_t* ht, const void* key, - size_t key_size) + size_t key_size) { ompi_list_t* list = ht->ht_table + ompi_hash_value(ht->ht_mask, key, - key_size); + key_size); ompi_ptr_hash_node_t *node; #if OMPI_ENABLE_DEBUG @@ -394,10 +381,10 @@ void* ompi_hash_table_get_value_ptr(ompi_hash_table_t* ht, const void* key, int ompi_hash_table_set_value_ptr(ompi_hash_table_t* ht, const void* key, - size_t key_size, void* value) + size_t key_size, void* value) { ompi_list_t* list = ht->ht_table + ompi_hash_value(ht->ht_mask, key, - key_size); + key_size); ompi_ptr_hash_node_t *node; #if OMPI_ENABLE_DEBUG @@ -420,8 +407,9 @@ int ompi_hash_table_set_value_ptr(ompi_hash_table_t* ht, const void* key, node = (ompi_ptr_hash_node_t*)ompi_list_remove_first(&ht->ht_nodes); if(NULL == node) { node = OBJ_NEW(ompi_ptr_hash_node_t); - if(NULL == node) + if(NULL == node) { return OMPI_ERR_OUT_OF_RESOURCE; + } } node->hn_key = malloc(key_size); node->hn_key_size = key_size; @@ -434,10 +422,10 @@ int ompi_hash_table_set_value_ptr(ompi_hash_table_t* ht, const void* key, int ompi_hash_table_remove_value_ptr(ompi_hash_table_t* ht, - const void* key, size_t key_size) + const void* key, size_t key_size) { ompi_list_t* list = ht->ht_table + ompi_hash_value(ht->ht_mask, - key, key_size); + key, key_size); ompi_ptr_hash_node_t *node; #if OMPI_ENABLE_DEBUG @@ -467,82 +455,141 @@ int ompi_hash_table_remove_value_ptr(ompi_hash_table_t* ht, int ompi_hash_table_get_first_key_uint32(ompi_hash_table_t *ht, uint32_t *key, - void **value, void **node) + void **value, void **node) { + size_t i; ompi_uint32_hash_node_t *list_node; - list_node = (ompi_uint32_hash_node_t*) ompi_list_get_first(ht->ht_table); - *node = list_node; - /* Quit if there is no element in the list */ - if (*node == ompi_list_get_end(ht->ht_table)) { - return OMPI_ERROR; + /* Go through all the lists and return the first element off the + first non-empty list */ + + for (i = 0; i < ht->ht_table_size; ++i) { + if (ompi_list_get_size(ht->ht_table + i) > 0) { + list_node = (ompi_uint32_hash_node_t*) + ompi_list_get_first(ht->ht_table + i); + *node = list_node; + *key = list_node->hn_key; + *value = list_node->hn_value; + return OMPI_SUCCESS; + } } - *key = list_node->hn_key; - *value = list_node->hn_value; - return OMPI_SUCCESS; + /* The hash table is empty */ + + return OMPI_ERROR; } int ompi_hash_table_get_next_key_uint32(ompi_hash_table_t *ht, uint32_t *key, - void **value, void *in_node, void **out_node) + void **value, void *in_node, + void **out_node) { - ompi_list_t* list = ht->ht_table + (((ompi_uint32_hash_node_t*) - in_node)->hn_key & ht->ht_mask); - if (in_node == ompi_list_get_last(list)) { - ++list; - *out_node = (void *) ompi_list_get_first(list); + size_t i; + ompi_list_t *list; + ompi_list_item_t *item; + ompi_uint32_hash_node_t *next; - if (*out_node == ompi_list_get_end(list)) { - return OMPI_ERROR; - } + /* Try to simply get the next value in the list. If there isn't + one, find the next non-empty list and take the first value */ - } else { - *out_node = (void *) ompi_list_get_next(in_node); + next = (ompi_uint32_hash_node_t*) in_node; + list = ht->ht_table + (next->hn_key & ht->ht_mask); + item = ompi_list_get_next(next); + if (ompi_list_get_end(list) == item) { + item = NULL; + for (i = (list - ht->ht_table) + 1; i < ht->ht_table_size; ++i) { + if (ompi_list_get_size(ht->ht_table + i) > 0) { + item = ompi_list_get_first(ht->ht_table + i); + break; + } + } + + /* If we didn't find another non-empty list after this one, + then we're at the end of the hash table */ + + if (NULL == item) { + return OMPI_ERROR; + } } - *key = ((ompi_uint32_hash_node_t*)(*out_node))->hn_key; - *value = ((ompi_uint32_hash_node_t*)(*out_node))->hn_value; + + /* We found it. Save the values (use "next" to avoid some + typecasting) */ + + next = *out_node = (void *) item; + *key = next->hn_key; + *value = next->hn_value; + return OMPI_SUCCESS; } int ompi_hash_table_get_first_key_uint64(ompi_hash_table_t *ht, uint64_t *key, - void **value, void **node) + void **value, void **node) { - *node = (ompi_uint64_hash_node_t*) ompi_list_get_first(ht->ht_table); + size_t i; + ompi_uint64_hash_node_t *list_node; - /* Quit if there is no element in the list */ - if (*node == ompi_list_get_end(ht->ht_table)) { - return OMPI_ERROR; + /* Go through all the lists and return the first element off the + first non-empty list */ + + for (i = 0; i < ht->ht_table_size; ++i) { + if (ompi_list_get_size(ht->ht_table + i) > 0) { + list_node = (ompi_uint64_hash_node_t*) + ompi_list_get_first(ht->ht_table + i); + *node = list_node; + *key = list_node->hn_key; + *value = list_node->hn_value; + return OMPI_SUCCESS; + } } - *key = ((ompi_uint64_hash_node_t*)(*node))->hn_key; - *value = ((ompi_uint64_hash_node_t*)(*node))->hn_value; - return OMPI_SUCCESS; + /* The hash table is empty */ + + return OMPI_ERROR; } int ompi_hash_table_get_next_key_uint64(ompi_hash_table_t *ht, uint64_t *key, - void **value, void *in_node, void **out_node) + void **value, void *in_node, + void **out_node) { - ompi_list_t* list = ht->ht_table + (((ompi_uint64_hash_node_t*) - in_node)->hn_key & ht->ht_mask); - if (in_node == ompi_list_get_last(list)) { - ++list; - *out_node = (void *) ompi_list_get_first(list); + size_t i; + ompi_list_t *list; + ompi_list_item_t *item; + ompi_uint64_hash_node_t *next; - if (*out_node == ompi_list_get_end(list)) { - return OMPI_ERROR; - } + /* Try to simply get the next value in the list. If there isn't + one, find the next non-empty list and take the first value */ - } else { - *out_node = (void *) ompi_list_get_next(in_node); + next = (ompi_uint64_hash_node_t*) in_node; + list = ht->ht_table + (next->hn_key & ht->ht_mask); + item = ompi_list_get_next(next); + if (ompi_list_get_end(list) == item) { + item = NULL; + for (i = (list - ht->ht_table) + 1; i < ht->ht_table_size; ++i) { + if (ompi_list_get_size(ht->ht_table + i) > 0) { + item = ompi_list_get_first(ht->ht_table + i); + break; + } + } + + /* If we didn't find another non-empty list after this one, + then we're at the end of the hash table */ + + if (NULL == item) { + return OMPI_ERROR; + } } - *key = ((ompi_uint64_hash_node_t*)(*out_node))->hn_key; - *value = ((ompi_uint32_hash_node_t*)(*out_node))->hn_value; - return OMPI_SUCCESS; + /* We found it. Save the values (use "next" to avoid some + typecasting) */ + + next = *out_node = (void *) item; + *key = next->hn_key; + *value = next->hn_value; + + return OMPI_SUCCESS; }