hashtable.h File Reference

#include "common.h"
#include "collection.h"
#include "config.h"

Go to the source code of this file.

Data Structures

struct  _cp_hashtable_entry
struct  _cp_hashtable

Defines

#define HASH_SEED   1000000001L
#define CP_HASHTABLE_DEFAULT_MIN_FILL_FACTOR   5
#define CP_HASHTABLE_DEFAULT_MAX_FILL_FACTOR   70
#define cp_hashtable_create_by_mode(mode, size_hint, cp_hashfn, compare_fn)   cp_hashtable_create_by_option((mode), (size_hint), (cp_hashfn), (compare_fn), NULL, NULL, NULL, NULL)
#define cp_hashtable_rdlock(table)   cp_hashtable_lock((table), COLLECTION_LOCK_READ)
#define cp_hashtable_wrlock(table)   cp_hashtable_lock((table), COLLECTION_LOCK_WRITE)
#define cp_hashtable_is_empty(table)   (cp_hashtable_count(table) == 0)

Typedefs

typedef unsigned long(*) cp_hashfunction (void *)
typedef CPROPS_DLL struct
_cp_hashtable_entry 
cp_hashtable_entry
typedef CPROPS_DLL struct
_cp_hashtable 
cp_hashtable

Functions

unsigned long cp_hash_int (void *i)
int cp_hash_compare_int (void *i, void *j)
unsigned long cp_hash_long (void *l)
int cp_hash_compare_long (void *i, void *j)
unsigned long cp_hash_addr (void *addr)
int cp_hash_compare_addr (void *a1, void *a2)
unsigned long cp_hash_string (void *str)
unsigned long cp_hash_istring (void *str)
void * cp_hash_copy_string (void *element)
int cp_hash_compare_string (void *s1, void *s2)
int cp_hash_compare_istring (void *s1, void *s2)
cp_hashtablecp_hashtable_create (unsigned long size_hint, cp_hashfunction hash_fn, cp_compare_fn compare_fn)
cp_hashtablecp_hashtable_create_copy_mode (unsigned long size_hint, cp_hashfunction hash_fn, cp_compare_fn compare_fn, cp_copy_fn copy_key, cp_destructor_fn free_key, cp_copy_fn copy_value, cp_destructor_fn free_value)
cp_hashtablecp_hashtable_create_by_option (int mode, unsigned long size_hint, cp_hashfunction hash_fn, cp_compare_fn compare_fn, cp_copy_fn copy_key, cp_destructor_fn free_key, cp_copy_fn copy_value, cp_destructor_fn free_value)
void cp_hashtable_destroy (cp_hashtable *table)
void cp_hashtable_destroy_shallow (cp_hashtable *table)
void cp_hashtable_destroy_deep (cp_hashtable *table)
void cp_hashtable_destroy_custom (cp_hashtable *table, cp_destructor_fn dk, cp_destructor_fn dv)
int cp_hashtable_lock (cp_hashtable *table, int type)
int cp_hashtable_unlock (cp_hashtable *table)
int cp_hashtable_get_mode (cp_hashtable *table)
int cp_hashtable_set_mode (cp_hashtable *table, int mode)
int cp_hashtable_unset_mode (cp_hashtable *table, int mode)
int cp_hashtable_set_min_size (cp_hashtable *table, int min_size)
int cp_hashtable_set_max_fill_factor (cp_hashtable *table, int fill_factor)
int cp_hashtable_set_min_fill_factor (cp_hashtable *table, int fill_factor)
void * cp_hashtable_get (cp_hashtable *table, void *key)
void * cp_hashtable_get_by_option (cp_hashtable *table, void *key, int option)
void * cp_hashtable_put_by_option (cp_hashtable *table, void *key, void *value, int option)
void * cp_hashtable_put (cp_hashtable *table, void *key, void *value)
void * cp_hashtable_put_safe (cp_hashtable *table, void *key, void *value)
void * cp_hashtable_put_copy (cp_hashtable *table, void *key, void *value)
void * cp_hashtable_remove (cp_hashtable *table, void *key)
int cp_hashtable_remove_all (cp_hashtable *table)
int cp_hashtable_remove_deep (cp_hashtable *table, void *key)
int cp_hashtable_contains (cp_hashtable *table, void *key)
void ** cp_hashtable_get_keys (cp_hashtable *table)
void ** cp_hashtable_get_values (cp_hashtable *table)
unsigned long cp_hashtable_count (cp_hashtable *table)
unsigned long cp_hashtable_choose_size (unsigned long size_request)


Detailed Description

generic synchronized hashtable.

Here is an example of using a cp_hashtable to create a lookup for 'bar' items using 'foo' keys:

 unsigned long foo_hash_code(void *fooptr) 
 {
     return ((foo *) fooptr)->id; 
 }

 // compare function
 int foo_compare(void *f1, void *f2)
 {
     return ((foo *) f1)->id != ((foo *) f2)->id;
 }

 ...

     cp_hashtable *t;
     foo *foo1, *f;
     bar *bar1, *bar2;

     t = cp_hashtable_create(10, foo_hash_code, foo_compare);
     if (t == NULL) 
     {
         perror("can\'t create cp_hashtable");
         exit(1);
     }

     cp_hashtable_put(foo1, bar1, t);

     ...
     f = foo_create(...);
     ...

     if ((bar2 = (bar *) cp_hashtable_get(f, t))) 
         printf("%s maps to %s\n", foo_get_name(f), bar_get_name(bar2));
     else 
         printf("%s is not mapped\n", foo_get_name(f));

     ...

     cp_hashtable_destroy(t);
 

Note the strcmp like semantics of the compare function. The comparison should return 0 for identical keys.

See also:
hash.h (util.collection) for function prototypes and convenience functions.

cp_hashtable_create, cp_hashtable_destroy, cp_hashtable_destroy_deep, cp_hashtable_put, cp_hashtable_get, cp_hashtable_contains, cp_hashtable_remove_deep

Definition in file hashtable.h.


Generated on Mon Dec 5 23:00:22 2011 for cprops by  doxygen 1.4.7