00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 #include "wsint.h"
00071 #include "wshash.h"
00072
00073
00074
00075
00076 #define WS_HASH_TABLE_SIZE 256
00077
00078
00079 struct WsHashItemRec
00080 {
00081 struct WsHashItemRec *next;
00082 char *name;
00083 void *data;
00084 };
00085
00086 typedef struct WsHashItemRec WsHashItem;
00087
00088
00089 struct WsHashRec
00090 {
00091 WsHashItem *items[WS_HASH_TABLE_SIZE];
00092 WsHashItemDestructor destructor;
00093 void *destructor_context;
00094 };
00095
00096
00097
00098
00099 static size_t count_hash(const char *string);
00100
00101
00102
00103 WsHashPtr ws_hash_create(WsHashItemDestructor destructor, void *context)
00104 {
00105 WsHashPtr hash = ws_calloc(1, sizeof(*hash));
00106
00107 if (hash) {
00108 hash->destructor = destructor;
00109 hash->destructor_context = context;
00110 }
00111
00112 return hash;
00113 }
00114
00115
00116 void ws_hash_destroy(WsHashPtr hash)
00117 {
00118 if (hash == NULL)
00119 return;
00120
00121 ws_hash_clear(hash);
00122 ws_free(hash);
00123 }
00124
00125
00126 WsBool ws_hash_put(WsHashPtr hash, const char *name, void *data)
00127 {
00128 WsHashItem *i;
00129 size_t h = count_hash(name);
00130
00131 for (i = hash->items[h]; i; i = i->next) {
00132 if (strcmp(i->name, name) == 0) {
00133
00134
00135
00136 if (hash->destructor)
00137 (*hash->destructor)(i->data, hash->destructor_context);
00138
00139 i->data = data;
00140
00141 return WS_FALSE;
00142 }
00143 }
00144
00145
00146 i = ws_calloc(1, sizeof(*i));
00147
00148 if (i == NULL)
00149 return WS_FALSE;
00150
00151 i->name = ws_strdup(name);
00152 if (i->name == NULL) {
00153 ws_free(i);
00154 return WS_FALSE;
00155 }
00156
00157 i->data = data;
00158
00159
00160 i->next = hash->items[h];
00161 hash->items[h] = i;
00162
00163 return WS_TRUE;
00164 }
00165
00166
00167 void *ws_hash_get(WsHashPtr hash, const char *name)
00168 {
00169 WsHashItem *i;
00170 size_t h = count_hash(name);
00171
00172 for (i = hash->items[h]; i; i = i->next)
00173 if (strcmp(i->name, name) == 0)
00174 return i->data;
00175
00176 return NULL;
00177 }
00178
00179
00180 void ws_hash_clear(WsHashPtr hash)
00181 {
00182 WsHashItem *i, *n;
00183 size_t j;
00184
00185 for (j = 0; j < WS_HASH_TABLE_SIZE; j++) {
00186 for (i = hash->items[j]; i; i = n) {
00187 n = i->next;
00188 if (hash->destructor)
00189 (*hash->destructor)(i->data, hash->destructor_context);
00190
00191 ws_free(i->name);
00192 ws_free(i);
00193 }
00194 hash->items[j] = NULL;
00195 }
00196 }
00197
00198
00199
00200 static size_t count_hash(const char *string)
00201 {
00202 size_t val = 0;
00203 int i;
00204
00205 for (i = 0; string[i]; i++) {
00206 val <<= 3;
00207 val ^= string[i];
00208 val ^= (val & 0xff00) >> 5;
00209 val ^= (val & 0xff0000) >> 16;
00210 }
00211
00212 return val % WS_HASH_TABLE_SIZE;
00213 }
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.