1
1

Add additional functions to the hash to enable iterating thorugh the hash elements as in a list

This commit was SVN r954.
Этот коммит содержится в:
Vishal Sahay 2004-03-24 01:54:59 +00:00
родитель 7bb2e7e3ba
Коммит c00d43e77c
2 изменённых файлов: 193 добавлений и 24 удалений

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

@ -125,7 +125,8 @@ void* lam_hash_table_get_value_uint32(lam_hash_table_t* ht, uint32_t key)
#if LAM_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
lam_output(0, "lam_hash_table_get_value_uint32: lam_hash_table_init() has not been called");
lam_output(0, "lam_hash_table_get_value_uint32:"
"lam_hash_table_init() has not been called");
return NULL;
}
#endif
@ -140,14 +141,16 @@ void* lam_hash_table_get_value_uint32(lam_hash_table_t* ht, uint32_t key)
}
int lam_hash_table_set_value_uint32(lam_hash_table_t* ht, uint32_t key, void* value)
int lam_hash_table_set_value_uint32(lam_hash_table_t* ht,
uint32_t key, void* value)
{
lam_list_t* list = ht->ht_table + (key & ht->ht_mask);
lam_uint32_hash_node_t *node;
#if LAM_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
lam_output(0, "lam_hash_table_set_value_uint32: lam_hash_table_init() has not been called");
lam_output(0, "lam_hash_table_set_value_uint32:"
"lam_hash_table_init() has not been called");
return LAM_ERR_BAD_PARAM;
}
#endif
@ -181,7 +184,8 @@ int lam_hash_table_remove_value_uint32(lam_hash_table_t* ht, uint32_t key)
#if LAM_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
lam_output(0, "lam_hash_table_remove_value_uint32: lam_hash_table_init() has not been called");
lam_output(0, "lam_hash_table_remove_value_uint32:"
"lam_hash_table_init() has not been called");
return LAM_ERR_BAD_PARAM;
}
#endif
@ -234,7 +238,8 @@ void* lam_hash_table_get_value_uint64(lam_hash_table_t* ht, uint64_t key)
#if LAM_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
lam_output(0, "lam_hash_table_get_value_uint64: lam_hash_table_init() has not been called");
lam_output(0, "lam_hash_table_get_value_uint64:"
"lam_hash_table_init() has not been called");
return NULL;
}
#endif
@ -249,14 +254,16 @@ void* lam_hash_table_get_value_uint64(lam_hash_table_t* ht, uint64_t key)
}
int lam_hash_table_set_value_uint64(lam_hash_table_t* ht, uint64_t key, void* value)
int lam_hash_table_set_value_uint64(lam_hash_table_t* ht,
uint64_t key, void* value)
{
lam_list_t* list = ht->ht_table + (key & ht->ht_mask);
lam_uint64_hash_node_t *node;
#if LAM_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
lam_output(0, "lam_hash_table_set_value_uint64: lam_hash_table_init() has not been called");
lam_output(0, "lam_hash_table_set_value_uint64:"
"lam_hash_table_init() has not been called");
return LAM_ERR_BAD_PARAM;
}
#endif
@ -290,7 +297,8 @@ int lam_hash_table_remove_value_uint64(lam_hash_table_t* ht, uint64_t key)
#if LAM_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
lam_output(0, "lam_hash_table_remove_value_uint64: lam_hash_table_init() has not been called");
lam_output(0, "lam_hash_table_remove_value_uint64:"
"lam_hash_table_init() has not been called");
return LAM_ERR_BAD_PARAM;
}
#endif
@ -343,7 +351,8 @@ static lam_class_t lam_ptr_hash_node_t_class = {
};
static inline uint32_t lam_hash_value(size_t mask, const void *key, uint32_t keysize)
static inline uint32_t lam_hash_value(size_t mask, const void *key,
uint32_t keysize)
{
uint32_t h, i;
const unsigned char *p;
@ -355,14 +364,17 @@ static inline uint32_t lam_hash_value(size_t mask, const void *key, uint32_t key
return (h & mask);
}
void* lam_hash_table_get_value_ptr(lam_hash_table_t* ht, const void* key, size_t key_size)
void* lam_hash_table_get_value_ptr(lam_hash_table_t* ht, const void* key,
size_t key_size)
{
lam_list_t* list = ht->ht_table + lam_hash_value(ht->ht_mask, key, key_size);
lam_list_t* list = ht->ht_table + lam_hash_value(ht->ht_mask, key,
key_size);
lam_ptr_hash_node_t *node;
#if LAM_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
lam_output(0, "lam_hash_table_get_value_ptr: lam_hash_table_init() has not been called");
lam_output(0, "lam_hash_table_get_value_ptr:"
"lam_hash_table_init() has not been called");
return NULL;
}
#endif
@ -378,14 +390,17 @@ void* lam_hash_table_get_value_ptr(lam_hash_table_t* ht, const void* key, size_t
}
int lam_hash_table_set_value_ptr(lam_hash_table_t* ht, const void* key, size_t key_size, void* value)
int lam_hash_table_set_value_ptr(lam_hash_table_t* ht, const void* key,
size_t key_size, void* value)
{
lam_list_t* list = ht->ht_table + lam_hash_value(ht->ht_mask, key, key_size);
lam_list_t* list = ht->ht_table + lam_hash_value(ht->ht_mask, key,
key_size);
lam_ptr_hash_node_t *node;
#if LAM_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
lam_output(0, "lam_hash_table_set_value_ptr: lam_hash_table_init() has not been called");
lam_output(0, "lam_hash_table_set_value_ptr:"
"lam_hash_table_init() has not been called");
return LAM_ERR_BAD_PARAM;
}
#endif
@ -415,14 +430,17 @@ int lam_hash_table_set_value_ptr(lam_hash_table_t* ht, const void* key, size_t k
}
int lam_hash_table_remove_value_ptr(lam_hash_table_t* ht, const void* key, size_t key_size)
int lam_hash_table_remove_value_ptr(lam_hash_table_t* ht,
const void* key, size_t key_size)
{
lam_list_t* list = ht->ht_table + lam_hash_value(ht->ht_mask, key, key_size);
lam_list_t* list = ht->ht_table + lam_hash_value(ht->ht_mask,
key, key_size);
lam_ptr_hash_node_t *node;
#if LAM_ENABLE_DEBUG
if(ht->ht_table_size == 0) {
lam_output(0, "lam_hash_table_remove_value_ptr: lam_hash_table_init() has not been called");
lam_output(0, "lam_hash_table_remove_value_ptr: "
"lam_hash_table_init() has not been called");
return LAM_ERR_BAD_PARAM;
}
#endif
@ -440,7 +458,72 @@ int lam_hash_table_remove_value_ptr(lam_hash_table_t* ht, const void* key, size_
return LAM_SUCCESS;
}
}
return LAM_ERR_NOT_FOUND;
return LAM_ERR_NOT_FOUND;
}
int
lam_hash_table_get_first_key_uint32(lam_hash_table_t *ht, uint32_t *key,
void *value, void **node)
{
*node = (void *)(lam_uint32_hash_node_t*) lam_list_get_first(ht->ht_table);
if (NULL == *node)
return LAM_ERROR;
*key = ((lam_uint32_hash_node_t*)(*node))->hn_key;
return LAM_SUCCESS;
}
int
lam_hash_table_get_next_key_uint32(lam_hash_table_t *ht, uint32_t *key,
void *value, void *in_node, void **out_node)
{
lam_list_t* list = ht->ht_table + (((lam_uint32_hash_node_t*)
in_node)->hn_key & ht->ht_mask);
if (in_node == lam_list_get_end(list)) {
++list;
*out_node = (void *) lam_list_get_first(list);
if (NULL == *out_node)
return LAM_ERROR;
} else {
*out_node = (void *) lam_list_get_next(in_node);
}
*key = ((lam_uint32_hash_node_t*)(*out_node))->hn_key;
return LAM_SUCCESS;
}
int
lam_hash_table_get_first_key_uint64(lam_hash_table_t *ht, uint64_t *key,
void *value, void **node)
{
*node = (lam_uint64_hash_node_t*) lam_list_get_first(ht->ht_table);
if (NULL == *node)
return LAM_ERROR;
*key = ((lam_uint64_hash_node_t*)(*node))->hn_key;
return LAM_SUCCESS;
}
int
lam_hash_table_get_next_key_uint64(lam_hash_table_t *ht, uint64_t *key,
void *value, void *in_node, void **out_node)
{
lam_list_t* list = ht->ht_table + (((lam_uint64_hash_node_t*)
in_node)->hn_key & ht->ht_mask);
if (in_node == lam_list_get_end(list)) {
++list;
*out_node = (void *) lam_list_get_first(list);
if (NULL == *out_node)
return LAM_ERROR;
} else {
*out_node = (void *) lam_list_get_next(in_node);
}
*key = ((lam_uint64_hash_node_t*)(*out_node))->hn_key;
return LAM_SUCCESS;
}

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

@ -5,8 +5,10 @@
/** @file
*
* A hash table that may be indexed with either fixed length (e.g. uint32_t/uint64_t) or arbitrary
* size binary key values. However, only one key type may be used in a given table concurrently.
* A hash table that may be indexed with either fixed length
* (e.g. uint32_t/uint64_t) or arbitrary size binary key
* values. However, only one key type may be used in a given table
* concurrently.
*/
#ifndef LAM_HASH_TABLE_H
@ -18,19 +20,21 @@
extern lam_class_t lam_hash_table_t_class;
struct lam_hash_table_t
{
lam_object_t super; /**< subclass of lam_object_t */
lam_list_t ht_nodes; /**< free list of hash nodes */
lam_list_t *ht_table; /**< each item is an array of lam_fhnode_t nodes */
lam_list_t *ht_table; /**< each item is an array of
lam_fhnode_t nodes */
size_t ht_table_size; /**< size of table */
size_t ht_size; /**< number of values on table */
size_t ht_mask;
};
typedef struct lam_hash_table_t lam_hash_table_t;
#if defined(c_plusplus) || defined(__cplusplus)
extern "C" {
#endif
@ -174,6 +178,88 @@ int lam_hash_table_set_value_ptr(lam_hash_table_t *table, const void* key, size_
int lam_hash_table_remove_value_ptr(lam_hash_table_t *table, const void* key, size_t keylen);
/** The following functions are only for allowing iterating through
the hash table. The calls return along with a key, a pointer to
the hash node with the current key, so that subsequent calls do
not have to traverse all over again to the key (although it may
just be a simple thing - to go to the array element and then
traverse through the individual list). But lets take out this
inefficiency too. This is similar to having an STL iterator in
functionality */
/**
* Get the first 32 bit key from the hash table, which can be used later to
* get the next key
* @param table The hash table pointer (IN)
* @param key The first key (OUT)
* @param value The value corresponding to this key (OUT)
* @param node The pointer to the hash table internal node which stores
* the key-value pair (this is required for subsequent calls
* to get_next_key) (OUT)
* @return LAM error code
*
*/
int lam_hash_table_get_first_key_uint32(lam_hash_table_t *table, uint32_t *key,
void *value, void **node);
/**
* Get the next 32 bit key from the hash table, knowing the current key
* @param table The hash table pointer (IN)
* @param key The key (OUT)
* @param value The value corresponding to this key (OUT)
* @param in_node The node pointer from previous call to either get_first
or get_next (IN)
* @param out_node The pointer to the hash table internal node which stores
* the key-value pair (this is required for subsequent calls
* to get_next_key) (OUT)
* @return LAM error code
*
*/
int lam_hash_table_get_next_key_uint32(lam_hash_table_t *table, uint32_t *key,
void *value, void *in_node,
void **out_node);
/**
* Get the first 64 key from the hash table, which can be used later to
* get the next key
* @param table The hash table pointer (IN)
* @param key The first key (OUT)
* @param value The value corresponding to this key (OUT)
* @param node The pointer to the hash table internal node which stores
* the key-value pair (this is required for subsequent calls
* to get_next_key) (OUT)
* @return LAM error code
*
*/
int lam_hash_table_get_first_key_uint64(lam_hash_table_t *table, uint64_t *key,
void *value, void **node);
/**
* Get the next 64 bit key from the hash table, knowing the current key
* @param table The hash table pointer (IN)
* @param key The key (OUT)
* @param value The value corresponding to this key (OUT)
* @param in_node The node pointer from previous call to either get_first
or get_next (IN)
* @param out_node The pointer to the hash table internal node which stores
* the key-value pair (this is required for subsequent calls
* to get_next_key) (OUT)
* @return LAM error code
*
*/
int lam_hash_table_get_next_key_uint64(lam_hash_table_t *table, uint64_t *key,
void *value, void *in_node,
void **out_node);
#if defined(c_plusplus) || defined(__cplusplus)
}
#endif