1
1
- remove some unnecessary functions
- use OBJ_* macros
- fix ompi_hase_table_get_[first|next]_key_uint[32|64] functions

This commit was SVN r2732.
Этот коммит содержится в:
Jeff Squyres 2004-09-16 23:51:30 +00:00
родитель e7135621fe
Коммит 71054a834e

Просмотреть файл

@ -11,6 +11,7 @@
#include "include/constants.h" #include "include/constants.h"
#include "util/output.h" #include "util/output.h"
#include "class/ompi_list.h"
#include "class/ompi_hash_table.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; size_t i;
ompi_hash_table_remove_all(ht); ompi_hash_table_remove_all(ht);
for(i=0; i<ht->ht_table_size; i++) for(i=0; i<ht->ht_table_size; i++) {
OBJ_DESTRUCT(ht->ht_table+i); OBJ_DESTRUCT(ht->ht_table+i);
if(NULL != ht->ht_table) }
if(NULL != ht->ht_table) {
free(ht->ht_table); free(ht->ht_table);
}
OBJ_DESTRUCT(&ht->ht_nodes); 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_mask = power2-1;
ht->ht_table = malloc(power2 * sizeof(ompi_list_t)); 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; return OMPI_ERR_OUT_OF_RESOURCE;
}
for(i=ht->ht_table_size; i<power2; i++) { for(i=ht->ht_table_size; i<power2; i++) {
ompi_list_t* list = ht->ht_table+i; ompi_list_t* list = ht->ht_table+i;
OBJ_CONSTRUCT(list, ompi_list_t); OBJ_CONSTRUCT(list, ompi_list_t);
@ -93,6 +97,8 @@ int ompi_hash_table_remove_all(ompi_hash_table_t* ht)
return OMPI_SUCCESS; return OMPI_SUCCESS;
} }
/***************************************************************************/
/* /*
* ompi_uint32_hash_node_t * 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; 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 OBJ_CLASS_INSTANCE(ompi_uint32_hash_node_t,
{ ompi_list_item_t,
} NULL,
NULL);
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
};
void* ompi_hash_table_get_value_uint32(ompi_hash_table_t* ht, uint32_t key) 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; return OMPI_ERR_NOT_FOUND;
} }
/***************************************************************************/
/* /*
* ompi_uint64_hash_node_t * 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; 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 OBJ_CLASS_INSTANCE(ompi_uint64_hash_node_t,
{ ompi_list_item_t,
} NULL,
NULL);
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
};
void* ompi_hash_table_get_value_uint64(ompi_hash_table_t* ht, uint64_t key) 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); node = (ompi_uint64_hash_node_t*)ompi_list_remove_first(&ht->ht_nodes);
if(NULL == node) { if(NULL == node) {
node = OBJ_NEW(ompi_uint64_hash_node_t); node = OBJ_NEW(ompi_uint64_hash_node_t);
if(NULL == node) if(NULL == node) {
return OMPI_ERR_OUT_OF_RESOURCE; return OMPI_ERR_OUT_OF_RESOURCE;
}
} }
node->hn_key = key; node->hn_key = key;
node->hn_value = value; 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; return OMPI_ERR_NOT_FOUND;
} }
/***************************************************************************/
/* /*
* ompi_ptr_hash_node_t * 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) 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); free(hn->hn_key);
}
} }
static ompi_class_t ompi_ptr_hash_node_t_class = { static OBJ_CLASS_INSTANCE(ompi_ptr_hash_node_t,
"ompi_ptr_hash_node_t", ompi_list_item_t,
OBJ_CLASS(ompi_list_item_t), ompi_ptr_hash_node_construct,
(ompi_construct_t)ompi_ptr_hash_node_construct, ompi_ptr_hash_node_destruct);
(ompi_destruct_t)ompi_ptr_hash_node_destruct
};
static inline uint32_t ompi_hash_value(size_t mask, const void *key, static inline uint32_t ompi_hash_value(size_t mask, const void *key,
uint32_t keysize) uint32_t keysize)
{ {
uint32_t h, i; uint32_t h, i;
const unsigned char *p; 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, 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, ompi_list_t* list = ht->ht_table + ompi_hash_value(ht->ht_mask, key,
key_size); key_size);
ompi_ptr_hash_node_t *node; ompi_ptr_hash_node_t *node;
#if OMPI_ENABLE_DEBUG #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, 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, ompi_list_t* list = ht->ht_table + ompi_hash_value(ht->ht_mask, key,
key_size); key_size);
ompi_ptr_hash_node_t *node; ompi_ptr_hash_node_t *node;
#if OMPI_ENABLE_DEBUG #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); node = (ompi_ptr_hash_node_t*)ompi_list_remove_first(&ht->ht_nodes);
if(NULL == node) { if(NULL == node) {
node = OBJ_NEW(ompi_ptr_hash_node_t); node = OBJ_NEW(ompi_ptr_hash_node_t);
if(NULL == node) if(NULL == node) {
return OMPI_ERR_OUT_OF_RESOURCE; return OMPI_ERR_OUT_OF_RESOURCE;
}
} }
node->hn_key = malloc(key_size); node->hn_key = malloc(key_size);
node->hn_key_size = 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, 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, 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; ompi_ptr_hash_node_t *node;
#if OMPI_ENABLE_DEBUG #if OMPI_ENABLE_DEBUG
@ -467,82 +455,141 @@ int ompi_hash_table_remove_value_ptr(ompi_hash_table_t* ht,
int int
ompi_hash_table_get_first_key_uint32(ompi_hash_table_t *ht, uint32_t *key, 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; 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 */ /* Go through all the lists and return the first element off the
if (*node == ompi_list_get_end(ht->ht_table)) { first non-empty list */
return OMPI_ERROR;
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; /* The hash table is empty */
*value = list_node->hn_value;
return OMPI_SUCCESS; return OMPI_ERROR;
} }
int int
ompi_hash_table_get_next_key_uint32(ompi_hash_table_t *ht, uint32_t *key, 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*) size_t i;
in_node)->hn_key & ht->ht_mask); ompi_list_t *list;
if (in_node == ompi_list_get_last(list)) { ompi_list_item_t *item;
++list; ompi_uint32_hash_node_t *next;
*out_node = (void *) ompi_list_get_first(list);
if (*out_node == ompi_list_get_end(list)) { /* Try to simply get the next value in the list. If there isn't
return OMPI_ERROR; one, find the next non-empty list and take the first value */
}
} else { next = (ompi_uint32_hash_node_t*) in_node;
*out_node = (void *) ompi_list_get_next(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; return OMPI_SUCCESS;
} }
int int
ompi_hash_table_get_first_key_uint64(ompi_hash_table_t *ht, uint64_t *key, 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 */ /* Go through all the lists and return the first element off the
if (*node == ompi_list_get_end(ht->ht_table)) { first non-empty list */
return OMPI_ERROR;
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; /* The hash table is empty */
*value = ((ompi_uint64_hash_node_t*)(*node))->hn_value;
return OMPI_SUCCESS; return OMPI_ERROR;
} }
int int
ompi_hash_table_get_next_key_uint64(ompi_hash_table_t *ht, uint64_t *key, 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*) size_t i;
in_node)->hn_key & ht->ht_mask); ompi_list_t *list;
if (in_node == ompi_list_get_last(list)) { ompi_list_item_t *item;
++list; ompi_uint64_hash_node_t *next;
*out_node = (void *) ompi_list_get_first(list);
if (*out_node == ompi_list_get_end(list)) { /* Try to simply get the next value in the list. If there isn't
return OMPI_ERROR; one, find the next non-empty list and take the first value */
}
} else { next = (ompi_uint64_hash_node_t*) in_node;
*out_node = (void *) ompi_list_get_next(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;
} }