Alphabetical Class List   File List   Compound Members  

vhash.h

00001 
00002 
00003 #ifndef VHASH_H
00004 #define VHASH_H
00005 
00006 #include <stdlib.h>
00007 
00008 #include "vlist.h"
00009 
00010 #ifdef __cplusplus
00011 extern "C" {
00012 #endif /* __cplusplus */
00013 
00014 /* WARNING this assumes all keys are unique*/
00015 /* if not remove the old key/item first*/
00016 
00017 /* WARNING use each vhash only for strings */
00018 /* or keys do not mix types*/
00019 
00020 /* NOTE after a large number of remove items*/
00021 /* the hash should be rebuild to restore speed*/
00022 
00023 /* NOTE this auto resizes at 50% capacity for*/
00024 /* best pefmance choose a size 2*max items expected*/
00025 
00026 typedef struct vhash_s {
00027 
00028     struct hash_node_s * table;
00029     unsigned long count;
00030     unsigned long table_size;
00031     long key_length;
00032     char * key_string;
00033     void *(*malloc) (size_t);
00034     void (*free) (void*);
00035 } vhash_t;
00036 #define VHASH_STATUS_FAILED 0
00037 #define VHASH_STATUS_SUCCESS 1
00038 #define VHASH_STATUS_INSERTED 2
00039 
00040 typedef int vhash_status_t;
00041 
00042 vhash_t * new_vhash(
00043     unsigned long table_size,
00044     void *(*vhash_malloc) (size_t),
00045     void (*vhash_free) (void *));
00046 
00047 void delete_vhash(vhash_t * vhash);
00048 
00049 vhash_status_t vhash_rebuild_table(
00050     vhash_t * vhash,
00051     unsigned long table_size);
00052 
00053 unsigned long vhash_count(vhash_t* vhash);
00054 
00055 /*args for function are item, key, user_dat*/
00056 /*your function returns VHASH_STATUS_SUCCESS to keep mapping*/
00057 /*your function returns VHASH_STATUS_FAILED to stop mapping*/
00058 void vhash_map_function(
00059     vhash_t * v,
00060     void(*function)(void*, void*, void*),
00061     void * user_data);
00062 
00063 typedef struct vhash_pair_s {
00064     void * key;
00065     void * item;
00066 } vhash_pair_t;
00067 
00068 vhash_status_t vhash_to_vlist(
00069     vhash_t * vhash,
00070     vlist_t * vlist,
00071     void *(*vhash_pair_malloc) (size_t));
00072 
00073 /* replaced_item is valid only if return status is VHASH_STATUS_SUCCESS */
00074 /* returns VHASH_STATUS_INSERTED is no item with same key existed*/
00075 vhash_status_t vhash_replace_item(
00076     vhash_t * v,
00077     void * in_key, 
00078     void * new_item,
00079     void ** replaced_item);
00080 
00081 vhash_status_t vhash_insert_item(
00082     vhash_t * v,
00083     void * in_key,
00084     void * item);
00085 
00086 vhash_status_t vhash_remove_item(
00087     vhash_t * v,
00088     void * in_key,
00089     void ** removed_item);
00090 
00091 vhash_status_t vhash_lookup_item(
00092     vhash_t * v,
00093     void * in_key,
00094     void ** out_item);
00095 
00096 /*args for function are item, key, user_dat*/
00097 /*your function returns VHASH_STATUS_SUCCESS to keep mapping*/
00098 /*your function returns VHASH_STATUS_FAILED to stop mapping*/
00099 void vhash_string_key_map_function(
00100     vhash_t * v,
00101     void(*function)(void*, const char *, void*),
00102     void * user_data);
00103 
00104 /* replaced_item is vailid only if return status is VHASH_STATUS_SUCCESS */
00105 vhash_status_t vhash_replace_string_key_item(
00106     vhash_t * v,
00107     const char * string_key, 
00108     void * new_item,
00109     void ** replaced_item);
00110 
00111 vhash_status_t vhash_insert_string_key_item(
00112     vhash_t * v,
00113     const char * string_key, 
00114     void * item);
00115 
00116 vhash_status_t vhash_remove_string_key_item(
00117     vhash_t * v,
00118     const char * string_key,
00119     void ** removed_item);
00120 
00121 vhash_status_t vhash_lookup_string_key_item(
00122     vhash_t * v,
00123     const char * string_key,
00124     void ** out_item);
00125 
00126 
00127 /* your function is called once for every item that has been inserted */
00128 void vhash_for_each(
00129     vhash_t * v,
00130     void(*function)(void *item, void *user_data),
00131     void * user_data);
00132 
00133 
00134 
00135 /*THIS IS OBSOLETE*/
00136 void vhash_insert(
00137     vhash_t * vhash,
00138     void * key, void * item);
00139 
00140 /*THIS IS OBSOLETE*/
00141 void * vhash_remove(
00142     vhash_t * vhash,
00143     void * key);
00144 
00145 /*THIS IS OBSOLETE*/
00146 void * vhash_lookup(
00147     vhash_t * vhash,
00148     void * key);
00149 
00150 
00151 /*THIS IS OBSOLETE*/
00152 void vhash_insert_string_key(
00153     vhash_t * v,
00154     const char * string, 
00155     void * item);
00156 
00157 /*THIS IS OBSOLETE*/
00158 void * vhash_remove_string_key(
00159     vhash_t * v,
00160     const char * string);
00161 
00162 /*THIS IS OBSOLETE*/
00163 void * vhash_lookup_string_key(
00164     vhash_t * v,
00165     const char * string);
00166 
00167 #ifdef __cplusplus
00168 } /* extern "C" */
00169 #endif /* __cplusplus */
00170 
00171 #endif /*VHASH_H*/
00172 
00173 


Copyright © 2003 Tech Soft 3D