Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals

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


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 Sat Dec 1 10:25:30 2007 for cprops by  doxygen 1.3.9.1