u_hash_table.h File Reference

Include dependency graph for u_hash_table.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

struct hash_tablehash_table_create (unsigned(*hash)(void *key), int(*compare)(void *key1, void *key2))
 Create an hash table.
enum pipe_error hash_table_set (struct hash_table *ht, void *key, void *value)
void * hash_table_get (struct hash_table *ht, void *key)
void hash_table_remove (struct hash_table *ht, void *key)
void hash_table_clear (struct hash_table *ht)
enum pipe_error hash_table_foreach (struct hash_table *ht, enum pipe_error(*callback)(void *key, void *value, void *data), void *data)
void hash_table_destroy (struct hash_table *ht)


Function Documentation

void hash_table_clear ( struct hash_table ht  ) 

Definition at line 222 of file u_hash_table.c.

References assert, hash_table::cso, cso_hash_first_node(), cso_hash_iter_is_null(), cso_hash_iter_key(), cso_hash_take(), and FREE.

00223 {
00224    struct cso_hash_iter iter;
00225    struct hash_table_item *item;
00226 
00227    assert(ht);
00228    
00229    iter = cso_hash_first_node(ht->cso);
00230    while (!cso_hash_iter_is_null(iter)) {
00231       item = (struct hash_table_item *)cso_hash_take(ht->cso, cso_hash_iter_key(iter));
00232       FREE(item);
00233       iter = cso_hash_first_node(ht->cso);
00234    }
00235 }

struct hash_table* hash_table_create ( unsigned(*)(void *key)  hash,
int(*)(void *key1, void *key2)  compare 
) [read]

Create an hash table.

Parameters:
hash hash function
compare should return 0 for two equal keys.

Definition at line 79 of file u_hash_table.c.

References hash_table::compare, hash_table::cso, cso_hash_create(), FREE, hash_table::hash, and MALLOC_STRUCT.

00081 {
00082    struct hash_table *ht;
00083    
00084    ht = MALLOC_STRUCT(hash_table);
00085    if(!ht)
00086       return NULL;
00087    
00088    ht->cso = cso_hash_create();
00089    if(!ht->cso) {
00090       FREE(ht);
00091       return NULL;
00092    }
00093    
00094    ht->hash = hash;
00095    ht->compare = compare;
00096    
00097    return ht;
00098 }

void hash_table_destroy ( struct hash_table ht  ) 

Definition at line 263 of file u_hash_table.c.

References assert, hash_table::cso, cso_hash_delete(), cso_hash_first_node(), cso_hash_iter_data(), cso_hash_iter_is_null(), cso_hash_iter_next(), and FREE.

00264 {
00265    struct cso_hash_iter iter;
00266    struct hash_table_item *item;
00267    
00268    assert(ht);
00269    
00270    iter = cso_hash_first_node(ht->cso);
00271    while (!cso_hash_iter_is_null(iter)) {
00272       item = (struct hash_table_item *)cso_hash_iter_data(iter);
00273       FREE(item);
00274       iter = cso_hash_iter_next(iter);
00275    }
00276 
00277    cso_hash_delete(ht->cso);
00278    
00279    FREE(ht);
00280 }

enum pipe_error hash_table_foreach ( struct hash_table ht,
enum pipe_error(*)(void *key, void *value, void *data)  callback,
void *  data 
)

Definition at line 239 of file u_hash_table.c.

References assert, hash_table::cso, cso_hash_first_node(), cso_hash_iter_data(), cso_hash_iter_is_null(), cso_hash_iter_next(), hash_table_item::key, PIPE_OK, and hash_table_item::value.

00242 {
00243    struct cso_hash_iter iter;
00244    struct hash_table_item *item;
00245    enum pipe_error result;
00246    
00247    assert(ht);
00248    
00249    iter = cso_hash_first_node(ht->cso);
00250    while (!cso_hash_iter_is_null(iter)) {
00251       item = (struct hash_table_item *)cso_hash_iter_data(iter);
00252       result = callback(item->key, item->value, data);
00253       if(result != PIPE_OK)
00254          return result;
00255       iter = cso_hash_iter_next(iter);
00256    }
00257 
00258    return PIPE_OK;
00259 }

void* hash_table_get ( struct hash_table ht,
void *  key 
)

Definition at line 179 of file u_hash_table.c.

References assert, hash_table::hash, hash_table_find_item(), and hash_table_item::value.

00181 {
00182    unsigned key_hash;
00183    struct hash_table_item *item;
00184 
00185    assert(ht);
00186 
00187    key_hash = ht->hash(key);
00188 
00189    item = hash_table_find_item(ht, key, key_hash);
00190    if(!item)
00191       return NULL;
00192    
00193    return item->value;
00194 }

void hash_table_remove ( struct hash_table ht,
void *  key 
)

Definition at line 198 of file u_hash_table.c.

References assert, hash_table::cso, cso_hash_erase(), cso_hash_iter_is_null(), FREE, hash_table::hash, hash_table_find_iter(), and hash_table_item().

00200 {
00201    unsigned key_hash;
00202    struct cso_hash_iter iter;
00203    struct hash_table_item *item;
00204 
00205    assert(ht);
00206 
00207    key_hash = ht->hash(key);
00208 
00209    iter = hash_table_find_iter(ht, key, key_hash);
00210    if(cso_hash_iter_is_null(iter))
00211       return;
00212    
00213    item = hash_table_item(iter);
00214    assert(item);
00215    FREE(item);
00216    
00217    cso_hash_erase(ht->cso, iter);
00218 }

enum pipe_error hash_table_set ( struct hash_table ht,
void *  key,
void *  value 
)

Definition at line 142 of file u_hash_table.c.

References assert, hash_table::cso, cso_hash_insert(), cso_hash_iter_is_null(), FREE, hash_table::hash, hash_table_find_item(), hash_table_item::key, MALLOC_STRUCT, PIPE_ERROR_OUT_OF_MEMORY, PIPE_OK, and hash_table_item::value.

00145 {
00146    unsigned key_hash;
00147    struct hash_table_item *item;
00148    struct cso_hash_iter iter;
00149 
00150    assert(ht);
00151 
00152    key_hash = ht->hash(key);
00153 
00154    item = hash_table_find_item(ht, key, key_hash);
00155    if(item) {
00156       /* TODO: key/value destruction? */
00157       item->value = value;
00158       return PIPE_OK;
00159    }
00160    
00161    item = MALLOC_STRUCT(hash_table_item);
00162    if(!item)
00163       return PIPE_ERROR_OUT_OF_MEMORY;
00164    
00165    item->key = key;
00166    item->value = value;
00167    
00168    iter = cso_hash_insert(ht->cso, key_hash, item);
00169    if(cso_hash_iter_is_null(iter)) {
00170       FREE(item);
00171       return PIPE_ERROR_OUT_OF_MEMORY;
00172    }
00173 
00174    return PIPE_OK;
00175 }


Generated on Tue Sep 29 06:25:28 2009 for Gallium3D by  doxygen 1.5.4