vhash.h

Go to the documentation of this file.
00001 
00008 #ifndef VHASH_H
00009 #define VHASH_H
00010 
00011 #include <stdlib.h>
00012 
00013 #include "vlist.h"
00014 
00015 #ifdef _WIN32_WCE
00016 #define VHASH_CDECL __cdecl
00017 #else
00018 #define VHASH_CDECL 
00019 #endif
00020 
00021 
00022 #ifdef __cplusplus
00023 extern "C" {
00024 #endif /* __cplusplus */
00025 
00039 typedef void *(VHASH_CDECL *vmalloc_t) (size_t);
00040 typedef void (VHASH_CDECL *vfree_t) (void*);
00041 typedef struct vhash_s {
00042     struct hash_node_s * table;
00043     unsigned long count;
00044     unsigned long table_size;
00045     long key_length;
00046     char * key_string;
00047 #if 0
00048     void *(VHASH_CDECL *malloc) (size_t);
00049     void (VHASH_CDECL *free) (void*);
00050 #else
00051         vmalloc_t malloc;
00052         vfree_t free;
00053 
00054 #endif
00055 } vhash_t;
00056 
00060 #define VHASH_STATUS_FAILED 0
00064 #define VHASH_STATUS_SUCCESS 1
00068 #define VHASH_STATUS_INSERTED 2
00069 
00073 typedef int vhash_status_t;
00074 
00082 _W3DTK_API vhash_t * new_vhash(
00083     unsigned long table_size,
00084         vmalloc_t vmalloc,
00085         vfree_t vfree);
00086         
00087 #if 0
00088     void *(VHASH_CDECL *vhash_malloc) (size_t),
00089     void (VHASH_CDECL *vhash_free) (void *));
00090 #endif
00091 
00096 _W3DTK_API void delete_vhash(vhash_t * vhash);
00097 
00098 
00099 _W3DTK_API void vhash_flush(vhash_t * vhash);
00100 
00101 
00102 _W3DTK_API vmalloc_t vhash_malloc(vhash_t * vhash);
00103 _W3DTK_API vfree_t vhash_free(vhash_t * vhash);
00110 _W3DTK_API vhash_status_t vhash_rebuild_table(
00111     vhash_t * vhash,
00112     unsigned long table_size);
00113 
00119 _W3DTK_API unsigned long vhash_count(vhash_t* vhash);
00120 
00129 _W3DTK_API void vhash_map_function(
00130     vhash_t * v,
00131     void(*function)(void*, void*, void*),
00132     void * user_data);
00133 
00137 _W3DTK_API typedef struct vhash_pair_s {
00138     void * key;
00139     void * item;
00140 } vhash_pair_t;
00141 
00145 _W3DTK_API typedef struct vhash_string_key_pair_s {
00146     const char * key;
00147     void * item;
00148 } vhash_string_key_pair_t;
00149 
00157 _W3DTK_API vhash_status_t vhash_to_vlist(
00158     vhash_t * vhash,
00159     vlist_t * vlist,
00160     void *(VHASH_CDECL *vhash_pair_malloc) (size_t));
00161 
00162 _W3DTK_API int vhash_item_set_to_vlist(
00163         vhash_t * vhash,
00164         vlist_t * vlist,
00165         void * in_key);
00166 
00175 _W3DTK_API vhash_status_t vhash_replace_item(
00176     vhash_t * v,
00177     void * in_key, 
00178     void * new_item,
00179     void ** replaced_item);
00180 
00188 _W3DTK_API vhash_status_t vhash_insert_item(
00189     vhash_t * v,
00190     void * in_key,
00191     void * item);
00192 
00200 _W3DTK_API vhash_status_t vhash_remove_item(
00201     vhash_t * v,
00202     void * in_key,
00203     void ** removed_item);
00204 
00212 _W3DTK_API vhash_status_t vhash_lookup_item(
00213     const vhash_t * v,
00214     void * in_key,
00215     void ** out_item);
00216 
00227 _W3DTK_API vhash_status_t vhash_lookup_nth_item(
00228     const vhash_t * v,
00229     void * in_key,
00230     int n,
00231     void ** out_item);
00232 
00242 _W3DTK_API void vhash_string_key_map_function(
00243     vhash_t * v,
00244     void(*function)(void*, const char *, void*),
00245     void * user_data);
00246 
00258 _W3DTK_API vhash_status_t vhash_replace_string_key_item(
00259     vhash_t * v,
00260     const char * string_key, 
00261     void * new_item,
00262     void ** replaced_item);
00263 
00272 _W3DTK_API vhash_status_t vhash_insert_string_key_item(
00273     vhash_t * v,
00274     const char * string_key, 
00275     void * item);
00276 
00285 _W3DTK_API vhash_status_t vhash_remove_string_key_item(
00286     vhash_t * v,
00287     const char * string_key,
00288     void ** removed_item);
00289 
00298 _W3DTK_API vhash_status_t vhash_lookup_string_key_item(
00299     vhash_t * v,
00300     const char * string_key,
00301     void ** out_item);
00302 
00314 _W3DTK_API vhash_status_t vhash_lookup_nth_string_key_item(
00315     vhash_t * v,
00316     const char * string_key,
00317     int n,
00318     void ** out_item);
00319 
00320 
00328 _W3DTK_API vhash_status_t vhash_string_keys_to_vlist(
00329     vhash_t * vhash,
00330     vlist_t * vlist,
00331     void *(VHASH_CDECL *vhash_pair_malloc) (size_t));
00332 
00333 
00334 /*THIS IS OBSOLETE*/
00335 _W3DTK_API void vhash_insert(
00336     vhash_t * vhash,
00337     void * key, void * item);
00338 
00339 /*THIS IS OBSOLETE*/
00340 _W3DTK_API void * vhash_remove(
00341     vhash_t * vhash,
00342     void * key);
00343 
00344 /*THIS IS OBSOLETE*/
00345 _W3DTK_API void * vhash_lookup(
00346     vhash_t * vhash,
00347     void * key);
00348 
00349 
00350 /*THIS IS OBSOLETE*/
00351 _W3DTK_API void vhash_insert_string_key(
00352     vhash_t * v,
00353     const char * string, 
00354     void * item);
00355 
00356 /*THIS IS OBSOLETE*/
00357 _W3DTK_API void * vhash_remove_string_key(
00358     vhash_t * v,
00359     const char * string);
00360 
00361 /*THIS IS OBSOLETE*/
00362 _W3DTK_API void * vhash_lookup_string_key(
00363     vhash_t * v,
00364     const char * string);
00365 
00366 #ifdef __cplusplus
00367 } /* extern "C" */
00368 #endif /* __cplusplus */
00369 
00370 /* template wrapper */
00371 #ifdef __cplusplus
00372 extern "C++" {
00373         
00374 template <typename T1, typename T2> class VHash {
00375 
00376 public:
00377 
00378         class Pair {
00379                 public:
00380                         T1 Key() {return (T1)(V_POINTER_SIZED_INT)vpair.key;};
00381                         T2 Item() {return (T2)(V_POINTER_SIZED_INT)vpair.item;};
00382                 private:
00383                         vhash_pair_t vpair;
00384         };
00385 
00386 
00387         typedef VList<Pair*> PairList;
00388 
00389     VHash(int initial_size=16)
00390         { m_vhash = new_vhash(initial_size,malloc,free); size_assert(); };
00391 
00392     VHash(void * (VHASH_CDECL *  vhash_malloc) (size_t), void (VHASH_CDECL *vhash_free) (void *))
00393         {m_vhash = new_vhash(16, vhash_malloc,vhash_free); size_assert(); };
00394 
00395     ~VHash() {delete_vhash(m_vhash);};
00396 
00397     void Flush() { vhash_flush(m_vhash); };
00398 
00399     unsigned long Count() { return (vhash_count(m_vhash)); };
00400 
00401         PairList * GetPairList() {
00402 
00403                 struct vlist_s *lv = new_vlist(vhash_malloc(m_vhash),vhash_free(m_vhash));
00404 
00405                 if (VHASH_STATUS_FAILED == vhash_to_vlist(m_vhash, lv, vhash_malloc(m_vhash))) {
00406                         delete_vlist(lv);
00407                         return 0;
00408                 }
00409                 PairList * pl = new PairList(lv);
00410                 pl->SetFreeItemsOnDelete();
00411                 return pl;
00412         };
00413         
00414         void MapFunction(void(*function)(T1, T2, void const * const), void const * const user_data) {
00415                 MapFunctionData data(function,user_data);
00416                 vhash_map_function(m_vhash, &map_function_helper, (void*)&data);
00417         };
00418 
00419     vhash_status_t ReplaceItem(T1 key, T2 item, T2 *replaced_item){
00420                 void * tmp_item = 0;
00421                 vhash_status_t retval = vhash_replace_item(m_vhash, (void*)(V_POINTER_SIZED_INT)key, (void*)(V_POINTER_SIZED_INT)item, &tmp_item);
00422                 if (VHASH_STATUS_SUCCESS == retval)
00423                         *replaced_item = (T2)(V_POINTER_SIZED_INT)tmp_item;
00424                 return retval;
00425         };
00426 
00427     vhash_status_t InsertItem(T1 key, T2 item){
00428                 return vhash_insert_item(m_vhash, (void*)(V_POINTER_SIZED_INT)key, (void*)(V_POINTER_SIZED_INT)item);
00429         };
00430 
00431     vhash_status_t LookupItem(T1 key, T2 *looked_up_item){
00432                 void * tmp_item = 0;
00433                 vhash_status_t retval = vhash_lookup_item(m_vhash, (void*)(V_POINTER_SIZED_INT)key, &tmp_item);
00434                 if (VHASH_STATUS_SUCCESS == retval)
00435                         *looked_up_item = (T2)(V_POINTER_SIZED_INT)tmp_item;
00436                 return retval;
00437         };
00438 
00439     T2 Lookup(T1 key)
00440         {return (T2)(V_POINTER_SIZED_INT)vhash_lookup(m_vhash, (void*)(V_POINTER_SIZED_INT)key); };
00441         
00442         
00443     vhash_status_t RemoveItem(T1 key, T2 *removed_item){
00444                 void * tmp_item = 0;
00445                 vhash_status_t retval = vhash_remove_item(m_vhash, (void*)(V_POINTER_SIZED_INT)key, &tmp_item);
00446                 if (VHASH_STATUS_SUCCESS == retval)
00447                         *removed_item = (T2)(V_POINTER_SIZED_INT)tmp_item;
00448                 return retval;
00449         };
00450                 
00451 
00452         vhash_status_t LookupNthItem(T1 key,int n, T2 *out_item){
00453                 void * tmp_item = 0;
00454                 vhash_status_t retval = vhash_lookup_nth_item(m_vhash, (void*)(V_POINTER_SIZED_INT)key, n, &tmp_item);
00455                 if (VHASH_STATUS_SUCCESS == retval)
00456                         *out_item = (T2)(V_POINTER_SIZED_INT)tmp_item;
00457                 return retval;
00458         };
00459 
00460 private:
00461     struct vhash_s *m_vhash;
00462 
00463         typedef  void(*MapFunctionType)(T1, T2, void const * const);
00464         
00465         class MapFunctionData {
00466                 public: 
00467                 MapFunctionData(MapFunctionType function=0, void const * const data=0) {
00468                         m_function=function;
00469                         m_data=(void*)data;
00470                 };
00471                 MapFunctionType m_function;
00472                 void * m_data;
00473         };
00474 
00475         static void map_function_helper(void* in_a, void* in_b, void*in_data) {
00476                 MapFunctionData *data = (MapFunctionData *)in_data;
00477                 T1 a = (T1)(V_POINTER_SIZED_INT)in_a;
00478                 T2 b = (T2)(V_POINTER_SIZED_INT)in_b;
00479                 data->m_function(a,b,data->m_data);
00480         };
00481 
00482     /* VHash cannot use any type that cannot be converted to and from a (void *) */
00483     void size_assert() {
00484         VLIST_COMPILE_TIME_ASSERT(sizeof(T1) <= sizeof(void *));
00485         VLIST_COMPILE_TIME_ASSERT(sizeof(T2) <= sizeof(void *));
00486     };
00487 };
00488 
00489 template <typename T1> class VStringHash {
00490 public:
00491 
00492         class Pair {
00493                 public:
00494                         const char * Key() {return vpair.key;};
00495                         T1 Item() {return (T1)(V_POINTER_SIZED_INT)vpair.item;};
00496                 private:
00497                         vhash_string_key_pair_t vpair;
00498         };
00499 
00500 
00501         typedef VList<Pair*> PairList;
00502 
00503     VStringHash(int initial_size=16)
00504         { m_vhash = new_vhash(initial_size,malloc,free); size_assert(); };
00505 
00506     VStringHash(void * (VHASH_CDECL *  vhash_malloc) (size_t), void (VHASH_CDECL *vhash_free) (void *))
00507         {m_vhash = new_vhash(16, vhash_malloc,vhash_free); size_assert(); };
00508 
00509     ~VStringHash() {delete_vhash(m_vhash);};
00510 
00511     void Flush() { vhash_flush(m_vhash); };
00512 
00513     unsigned long Count() { return (vhash_count(m_vhash)); };
00514         
00515     vhash_status_t ReplaceItem(const char *key, T1 item, T1 *replaced_item){
00516                 void * tmp_item = 0;
00517                 vhash_status_t retval = vhash_replace_string_key_item(m_vhash, (const char *)key, (void*)(V_POINTER_SIZED_INT)item, &tmp_item);
00518                 if (VHASH_STATUS_SUCCESS == retval)
00519                         *replaced_item = (T1)(V_POINTER_SIZED_INT)tmp_item;
00520                 return retval;
00521         };
00522 
00523     vhash_status_t InsertItem(const char *key, T1 item)
00524         {return vhash_insert_string_key_item(m_vhash, (const char *)key, (void*)(V_POINTER_SIZED_INT)item); };
00525         
00526     vhash_status_t LookupItem(const char *key, T1 *looked_up_item){
00527                 void * tmp_item = 0;
00528                 vhash_status_t retval = vhash_lookup_string_key_item(m_vhash, (const char*)key, &tmp_item);
00529                 if (VHASH_STATUS_SUCCESS == retval)
00530                         *looked_up_item = (T1)(V_POINTER_SIZED_INT)tmp_item;
00531                 return retval;  };
00532 
00533     T1 Lookup(const char *key)
00534                 {return (T1)(V_POINTER_SIZED_INT)vhash_lookup_string_key(m_vhash, (const char*)key); };
00535         
00536         
00537     vhash_status_t RemoveItem(const char *key, T1 *removed_item){
00538                 void * tmp_item = 0;
00539                 vhash_status_t retval = vhash_remove_string_key_item(m_vhash, (const char*)key, &tmp_item);
00540                 if (VHASH_STATUS_SUCCESS == retval)
00541                         *removed_item = (T1)(V_POINTER_SIZED_INT)tmp_item;
00542                 return retval;
00543         };
00544 
00545         
00546         vhash_status_t LookupItem(const char *key, int n, T1 *out_item){
00547                 void * tmp_item = 0;
00548                 vhash_status_t retval = vhash_lookup_nth_string_key_item(m_vhash, (const char*)key, n, &tmp_item);
00549                 if (VHASH_STATUS_SUCCESS == retval)
00550                         *out_item = (T1)(V_POINTER_SIZED_INT)tmp_item;
00551                 return retval;
00552         };
00553 
00554         PairList * GetPairList() {
00555 
00556                 struct vlist_s *lv = new_vlist(vhash_malloc(m_vhash),vhash_free(m_vhash));
00557 
00558                 if (VHASH_STATUS_FAILED == vhash_string_keys_to_vlist(m_vhash, lv, vhash_malloc(m_vhash))) {
00559                         delete_vlist(lv);
00560                         return 0;
00561                 }
00562                 PairList * pl = new PairList(lv);
00563                 pl->SetFreeItemsOnDelete();
00564                 return pl;
00565         };
00566         
00567         void MapFunction(void(*function)(T1, const char *, void const * const), void const * const user_data) {
00568                 MapFunctionData data(function,user_data);
00569                 vhash_string_key_map_function(m_vhash, &map_function_helper, (void*)&data);
00570         };
00571 
00572 private:
00573     struct vhash_s *m_vhash;
00574 
00575         typedef  void(*MapFunctionType)(T1, const char *, void const * const);
00576         
00577         class MapFunctionData {
00578                 public: 
00579                 MapFunctionData(MapFunctionType function=0, void const * const data=0) {
00580                         m_function=function;
00581                         m_data=(void*)data;
00582                 };
00583                 MapFunctionType m_function;
00584                 void * m_data;
00585         };
00586 
00587         static void map_function_helper(void* in_a, const char * in_b, void*in_data) {
00588                 MapFunctionData *data = (MapFunctionData *)in_data;
00589                 T1 a = (T1)(V_POINTER_SIZED_INT)in_a;
00590                 data->m_function(a,in_b,data->m_data);
00591         };
00592 
00593     /* VHash cannot use any type that cannot be converted to and from a (void *) */
00594     void size_assert() {
00595         VLIST_COMPILE_TIME_ASSERT(sizeof(T1) <= sizeof(void *));
00596     };
00597 };
00598 } /* extern "C++" */
00599 #endif
00600 #endif /*VHASH_H*/
00601 
00602 

Generated on Tue Jan 6 22:41:37 2009 for Autodesk DWF 3D Toolkit by  doxygen 1.4.5