hashtable.h

Go to the documentation of this file.
00001 #ifndef _CP_HASHTABLE_H
00002 #define _CP_HASHTABLE_H
00003 
00004 /*
00005  * hash table implementation
00006  */
00007 
00072 #include "common.h"
00073 
00074 __BEGIN_DECLS
00075 
00076 #include "collection.h"
00077 
00078 /*
00079  * cp_hashtable interface for members of mapping collections.
00080  */
00081 
00082 #include "config.h"
00083 
00087 #define HASH_SEED 1000000001L
00088 
00089 #ifndef CP_HASHTABLE_DEFAULT_MIN_FILL_FACTOR
00090 #define CP_HASHTABLE_DEFAULT_MIN_FILL_FACTOR   5
00091 #endif
00092 
00093 #ifndef CP_HASHTABLE_DEFAULT_MAX_FILL_FACTOR
00094 #define CP_HASHTABLE_DEFAULT_MAX_FILL_FACTOR  70
00095 #endif
00096 
00097 /* ----------------------------------------------------------------- 
00098  * Function prototypes
00099  * ----------------------------------------------------------------- */
00105 typedef unsigned long (*cp_hashfunction)(void *);
00106 
00107 
00108 /* ------------------------------------------------------------------------ 
00109  * hash function prototypes for primitives and cp_strings
00110  * ------------------------------------------------------------------------ */
00111 
00112 
00118 CPROPS_DLL 
00119 unsigned long cp_hash_int(void *key);
00120 
00121 
00131 CPROPS_DLL 
00132 int cp_hash_compare_int(void *key1, void *key2);
00133 
00137 CPROPS_DLL 
00138 unsigned long cp_hash_long(void *key);
00139 
00149 CPROPS_DLL 
00150 int cp_hash_compare_long(void *key1, void *key2);
00151 
00155 CPROPS_DLL 
00156 unsigned long cp_hash_addr(void *addr);
00157 
00167 CPROPS_DLL 
00168 int cp_hash_compare_addr(void *key1, void *key2);
00169 
00175 CPROPS_DLL 
00176 unsigned long cp_hash_string(void *key);
00177 
00183 CPROPS_DLL 
00184 unsigned long cp_hash_istring(void *key);
00185 
00186 
00190 CPROPS_DLL 
00191 void *cp_hash_copy_string(void *element);
00192 
00201 CPROPS_DLL 
00202 int cp_hash_compare_string(void *key1, void *key2);
00203 
00204 
00213 CPROPS_DLL 
00214 int cp_hash_compare_istring(void *key1, void *key2);
00215 
00216 
00217 
00225 typedef CPROPS_DLL struct _cp_hashtable_entry 
00226 {
00227     void *key;                   
00228     void *value;                 
00229     unsigned long hashcode;      
00230     struct _cp_hashtable_entry *next; 
00231 } cp_hashtable_entry;
00232 
00236 typedef CPROPS_DLL struct _cp_hashtable
00237 {
00238     cp_hashtable_entry **table;     
00239     long table_size;                
00241     unsigned long items;            
00242     int mode;                       
00243     cp_hashfunction   hash_fn;      
00244     cp_compare_fn     compare_fn;   
00245     cp_copy_fn        copy_key;     
00246     cp_copy_fn        copy_value;   
00247     cp_destructor_fn  free_key;
00248     cp_destructor_fn  free_value;
00249 
00250     cp_lock           *lock;        
00251     cp_thread         txowner;      
00252     int               txtype;       
00254     int min_size;                   
00255     int fill_factor_min;            
00256     int fill_factor_max;            
00258     cp_hashtable_entry **resize_table; 
00259     int resizing;                   
00260     unsigned long resize_len;       
00261     cp_thread resize_thread;        
00262     cp_mutex *resize_lock;          
00263 } cp_hashtable;
00264 
00265 
00281 CPROPS_DLL 
00282 cp_hashtable *
00283     cp_hashtable_create(unsigned long size_hint, 
00284                         cp_hashfunction hashfn, 
00285                         cp_compare_fn compare_fn);
00286 
00290 #define cp_hashtable_create_by_mode(mode, size_hint, cp_hashfn, compare_fn) \
00291         cp_hashtable_create_by_option((mode), (size_hint), (cp_hashfn), (compare_fn), NULL, NULL, NULL, NULL)
00292 
00303 CPROPS_DLL 
00304 cp_hashtable *
00305     cp_hashtable_create_copy_mode(unsigned long size_hint, 
00306                                   cp_hashfunction hash_fn, 
00307                                   cp_compare_fn compare_fn, 
00308                                   cp_copy_fn copy_key, 
00309                                   cp_destructor_fn free_key,
00310                                   cp_copy_fn copy_value,
00311                                   cp_destructor_fn free_value);    
00312 
00324 CPROPS_DLL 
00325 cp_hashtable *
00326     cp_hashtable_create_by_option(int mode, unsigned long size_hint, 
00327                                   cp_hashfunction hash_fn, 
00328                                   cp_compare_fn compare_fn, 
00329                                   cp_copy_fn copy_key, 
00330                                   cp_destructor_fn free_key,
00331                                   cp_copy_fn copy_value,
00332                                   cp_destructor_fn free_value);
00333 
00338 CPROPS_DLL 
00339 void cp_hashtable_destroy(cp_hashtable *table);
00340 
00347 CPROPS_DLL 
00348 void cp_hashtable_destroy_shallow(cp_hashtable *table);
00349 
00354 CPROPS_DLL 
00355 void cp_hashtable_destroy_deep(cp_hashtable *table);
00356 
00357 
00362 CPROPS_DLL 
00363 void cp_hashtable_destroy_custom(cp_hashtable *table, cp_destructor_fn dk, cp_destructor_fn dv);
00364 
00382 CPROPS_DLL 
00383 int cp_hashtable_lock(cp_hashtable *table, int type);
00384 
00386 CPROPS_DLL 
00387 int cp_hashtable_unlock(cp_hashtable *table);
00388 
00391 #define cp_hashtable_rdlock(table) cp_hashtable_lock((table), COLLECTION_LOCK_READ)
00392 
00394 #define cp_hashtable_wrlock(table) cp_hashtable_lock((table), COLLECTION_LOCK_WRITE)
00395 
00400 CPROPS_DLL 
00401 int cp_hashtable_get_mode(cp_hashtable *table);
00402 
00417 CPROPS_DLL 
00418 int cp_hashtable_set_mode(cp_hashtable *table, int mode);
00419 
00420 
00424 CPROPS_DLL 
00425 int cp_hashtable_unset_mode(cp_hashtable *table, int mode);
00426 
00427 
00431 CPROPS_DLL 
00432 int cp_hashtable_set_min_size(cp_hashtable *table, int min_size);
00433 
00438 CPROPS_DLL 
00439 int cp_hashtable_set_max_fill_factor(cp_hashtable *table, int fill_factor);
00440 
00445 CPROPS_DLL 
00446 int cp_hashtable_set_min_fill_factor(cp_hashtable *table, int fill_factor);
00447 
00455 CPROPS_DLL 
00456 void *cp_hashtable_get(cp_hashtable *table, void *key);
00457 
00462 CPROPS_DLL 
00463 void *cp_hashtable_get_by_option(cp_hashtable *table, void *key, int mode);
00464 
00468 CPROPS_DLL 
00469 void *cp_hashtable_put_by_option(cp_hashtable *table, void *key, void *value, int mode);
00470 
00476 CPROPS_DLL 
00477 void *cp_hashtable_put(cp_hashtable *table, void *key, void *value);
00478 
00483 CPROPS_DLL 
00484 void *cp_hashtable_put_safe(cp_hashtable *table, void *key, void *value);
00485 
00490 CPROPS_DLL 
00491 void *cp_hashtable_put_copy(cp_hashtable *table, void *key, void *value);
00492 
00501 CPROPS_DLL 
00502 void *cp_hashtable_remove(cp_hashtable *table, void *key);
00503 
00505 CPROPS_DLL 
00506 int cp_hashtable_remove_all(cp_hashtable *table);
00507 
00516 CPROPS_DLL 
00517 int cp_hashtable_remove_deep(cp_hashtable *table, void *key);
00518 
00526 CPROPS_DLL 
00527 int cp_hashtable_contains(cp_hashtable *table, void *key);
00528 
00534 CPROPS_DLL 
00535 void **cp_hashtable_get_keys(cp_hashtable *table);
00536 
00542 CPROPS_DLL 
00543 void **cp_hashtable_get_values(cp_hashtable *table);
00544 
00549 CPROPS_DLL 
00550 unsigned long cp_hashtable_count(cp_hashtable *table);
00551 
00557 #define cp_hashtable_is_empty(table) (cp_hashtable_count(table) == 0)
00558 
00562 CPROPS_DLL 
00563 unsigned long cp_hashtable_choose_size(unsigned long size_request);
00564 
00565 __END_DECLS
00567 #endif
00568 

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