Main Page   Data Structures   File List   Data Fields   Globals   Related Pages  

xhash_tmpl_c.inc

Go to the documentation of this file.
00001 /***************************************************************************
00002                                xhash_tmpl_c.inc
00003                              -------------------
00004     begin                : Sat Dec 08 2001
00005     copyright            : (C) 2001-2002 by Christian Hoenig & Gunter Ohrner
00006     email                : pdepp@CustomCDROM.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00056 // any Table Name Prefix defined?
00057 #ifdef XH_TABLE_NAME
00058 
00059 #include "common.h"
00060 
00061 #include <stdlib.h>
00062 #include <sys/types.h>
00063 #include <assert.h>
00064 
00065 #include "xmalloc.h"
00066 
00067 #ifndef XHPFX
00068 # define XHPFX XH_FUNC_PFX
00069 #endif
00070 
00071 #ifdef DOXYGEN_PREPROCESSING
00072 # include "xhash_tmpl_h.inc"
00073 #endif
00074 
00075 #undef KEY_VAL_PAIR
00076 
00077 #define KEY_VAL_PAIR fn(XH_TABLE_NAME, KeyValPair)
00078 
00080 struct KEY_VAL_PAIR
00081 {
00082   XH_KEY key;        
00083   XH_CARGO payload;  
00084 };
00085 
00087 typedef struct KEY_VAL_PAIR* KEY_VAL_PAIR;
00088 
00089 #ifndef XH_CREATE_KEY
00090 
00091 # define XH_CREATE_KEY(new)
00092 #endif
00093 #ifndef XH_COPY_KEY
00094 
00095 # define XH_COPY_KEY(dest, source) (dest = source)
00096 #endif
00097 #ifndef XH_FREE_KEY
00098 
00099 # define XH_FREE_KEY(a) 
00100 #endif
00101 
00102 #ifndef XH_CREATE_CARGO
00103 
00104 # define XH_CREATE_CARGO(new)
00105 #endif
00106 #ifndef XH_COPY_CARGO
00107 
00108 # define XH_COPY_CARGO(dest, source) (dest = source)
00109 #endif
00110 #ifndef XH_FREE_CARGO
00111 
00112 # define XH_FREE_CARGO(a)
00113 #endif
00114 
00115 
00116 // define mlist data types for external hashing:
00118 #define MlCargo KEY_VAL_PAIR
00120 #define XH_HASH_LIST_NAME fn(XH_TABLE_NAME, ExtHashList)
00122 #define ML_LIST_NAME XH_HASH_LIST_NAME
00124 #define XH_HASH_LIST_FUNC_PFX fn(XH_FUNC_PFX, ExtHashList)
00126 #define ML_FUNC_PFX XH_HASH_LIST_FUNC_PFX
00128 #define ML_INVALID NULL
00129 
00130 #undef ML_IS_VALID_CARGO
00131 
00132 #undef ML_CREATE_CARGO
00133 
00134 #define ML_CREATE_CARGO(new) (new = (MlCargo) pd_malloc(sizeof(struct KEY_VAL_PAIR)))
00135 
00136 #undef ML_COPY_CARGO
00137 
00138 #define ML_COPY_CARGO(dest, source) { \
00139                                       XH_COPY_KEY(dest->key, source->key); \
00140                                       XH_COPY_CARGO(dest->payload, source->payload);\
00141                                     }
00142 
00143 #undef ML_FREE_CARGO
00144 
00145 #define ML_FREE_CARGO(a) { \
00146                           XH_FREE_KEY(a->key); \
00147                           XH_FREE_CARGO(a->payload); \
00148                           pd_free(a); \
00149                          }
00150 
00151 // include mlist for external hashing
00152 #include "mlist_tmpl_h.inc"
00153 #include "mlist_tmpl_c.inc"
00154 
00156 struct XH_TABLE_NAME
00157 {
00158   size_t table_size;               
00159   XH_HASH_LIST_NAME* table;        
00160   XH_HASH_LIST_NAME cur_hash_list; 
00161 };
00162 
00163 // init hash table
00164 XH_TABLE_NAME fn(XHPFX,Init)(const size_t size)
00165 {
00166   size_t i;
00167   // alloc hash table control block
00168   XH_TABLE_NAME this = (XH_TABLE_NAME) pd_malloc(sizeof(struct XH_TABLE_NAME));
00169 
00170   // alloc hash table
00171   this->table = (XH_HASH_LIST_NAME*) pd_malloc(sizeof(XH_HASH_LIST_NAME) * size);
00172 
00173   for (i = 0; i < size; ++i)
00174     *((this->table) + i) = fn(XH_HASH_LIST_FUNC_PFX,Init)();
00175 
00176   this->table_size = size;
00177 
00178   return this;
00179 }
00180 
00181 // insert a key value pair
00182 int fn(XHPFX,Insert)(XH_TABLE_NAME this, XH_KEY key, XH_CARGO new_load)
00183 {
00184   struct KEY_VAL_PAIR kvp;
00185 
00186   // delete object with same key if existing but select the right
00187   // external hashing list in any case
00188   fn(XHPFX,Delete)(this, key);
00189 
00190 //  XH_CREATE_KEY(kvp.key);
00191 //  XH_COPY_KEY(kvp.key, key);
00192 //  XH_CREATE_CARGO(kvp.payload);
00193 //  XH_COPY_CARGO(kvp.payload, new_load);
00194   kvp.key = key;
00195   kvp.payload = new_load;
00196 
00197   fn(XH_HASH_LIST_FUNC_PFX,Append)(this->cur_hash_list, &kvp);
00198 
00199   return 0;
00200 }
00201 
00203 inline int fn(XHPFX,Select)(XH_TABLE_NAME this, const XH_KEY key)
00204 {
00205   size_t hash_val  = XH_CALC_HASH_VAL(key, this->table_size);
00206   size_t table_ofs = hash_val % this->table_size;
00207   XH_HASH_LIST_NAME* table_pos = this->table + table_ofs;
00208 
00209   this->cur_hash_list = *(table_pos);
00210 
00211   fn(XH_HASH_LIST_FUNC_PFX,First)(this->cur_hash_list);
00212 
00213   while(!fn(XH_HASH_LIST_FUNC_PFX,IsTail)(this->cur_hash_list))
00214   {
00215     KEY_VAL_PAIR kvp = fn(XH_HASH_LIST_FUNC_PFX,GetPayload)(this->cur_hash_list);
00216     if (XH_KEYS_EQUAL(key, kvp->key))
00217       return 0;
00218 
00219     fn(XH_HASH_LIST_FUNC_PFX,Next)(this->cur_hash_list);
00220   }
00221 
00222   return -1;
00223 }
00224 
00225 // delete an entry
00226 int fn(XHPFX,Delete)(XH_TABLE_NAME this, XH_KEY key)
00227 {
00228   if (fn(XHPFX,Select)(this, key) == -1)
00229     return -1;
00230   else
00231     return fn(XH_HASH_LIST_FUNC_PFX,Delete)(this->cur_hash_list);
00232 }
00233 
00234 // get a table entry
00235 XH_CARGO fn(XHPFX,Get)(XH_TABLE_NAME this, const XH_KEY key)
00236 {
00237   return (fn(XHPFX,Select)(this, key) == -1) ? XH_INVALID 
00238           : fn(XH_HASH_LIST_FUNC_PFX,GetPayload)(this->cur_hash_list)->payload;
00239 }
00240 
00241 // delete the table
00242 int fn(XHPFX,Terminate)(XH_TABLE_NAME this)
00243 {
00244   size_t i;
00245 
00246   for(i = 0; i < this->table_size; ++i)
00247   {
00248     this->cur_hash_list = *(this->table + i);
00249     fn(XH_HASH_LIST_FUNC_PFX,Terminate)(this->cur_hash_list);
00250   }
00251   pd_free(this->table);
00252   pd_free(this);
00253 
00254   return 0;
00255 }
00256 
00257 
00258 #undef KEY_VAL_PAIR
00259 #undef XH_CREATE_KEY
00260 #undef XH_COPY_KEY
00261 #undef XH_FREE_KEY
00262 #undef XH_CREATE_CARGO
00263 #undef XH_COPY_CARGO
00264 #undef XH_FREE_CARGO
00265 #undef MlCargo
00266 #undef XH_HASH_LIST_NAME
00267 // #undef ML_LIST_NAME
00268 #undef XH_HASH_LIST_FUNC_PFX
00269 //#undef ML_FUNC_PFX
00270 #undef ML_INVALID
00271 
00272 //#undef ML_IS_VALID_CARGO
00273 
00274 #undef ML_CREATE_CARGO
00275 #undef ML_COPY_CARGO
00276 #undef ML_FREE_CARGO
00277 
00278 #undef XHPFX
00279 
00280 #endif

Generated on Fri Jan 25 22:40:31 2002 for PDepp Webserver by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001