00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <iostream>
00027 #include <cassert>
00028 #include "mempool.h"
00029 #include "htable.h"
00030
00031
00032
00033 #define rot_right(a,k) (((a) >> k ) | ((a) << (32-(k))))
00034 #define rot_left(a,k) (((a) << k ) | ((a) >> (32-(k))))
00035
00036 using namespace std;
00037
00038 htable::htable(int n,int kl,HTYPE ht,size_t (*klf)(const char* )){
00039
00040 if (ht!=STRPTR && ht!=STR && kl==0){
00041 cerr << "htable: key length must be specified for non-string entries!";
00042 exit(1);
00043 }
00044
00045 memory=new mempool( sizeof(entry) , BlockSize );
00046
00047 table = new entry* [ size=n ];
00048
00049 memset(table,0,sizeof(entry *) * n );
00050
00051 keylen=(ht==INT || ht==INTPTR?kl/sizeof(int):kl);
00052
00053 htype=ht;
00054
00055 keys = accesses = collisions = 0;
00056
00057 keylenfunc=(klf?klf:&strlen);
00058
00059 }
00060
00061
00062 char *htable::search(char *item, HT_ACTION action)
00063
00064 {
00065 address h;
00066 entry *q,**p;
00067 int i;
00068
00069
00070 accesses++;
00071
00072 h = Hash(item);
00073
00074 i=(h % size);
00075
00076
00077
00078
00079
00080
00081
00082 p=&table[h % size];
00083 q=table[h % size];
00084
00085
00086
00087
00088
00089
00090 while (q != NULL && Comp((char *)q->key,(char *)item))
00091 {
00092
00093
00094
00095
00096 p = &(q->next);
00097 q = q->next;
00098
00099
00100 collisions++;
00101 }
00102
00103 if (
00104 q != NULL
00105 ||
00106 action == HT_FIND
00107 ||
00108 (q = (entry *)memory->allocate())
00109 ==
00110 NULL
00111 )
00112
00113 return((q!=NULL)?(char *)q->key:(char *)NULL);
00114
00115 *p = q;
00116
00117
00118
00119
00120 q->key = item;
00121 q->next = NULL;
00122 keys++;
00123
00124 return((char *)q->key);
00125 }
00126
00127
00128 char *htable::scan(HT_ACTION action){
00129
00130 char *k;
00131
00132 if (action == HT_INIT)
00133 {
00134 scan_i=0;scan_p=table[0];
00135 return NULL;
00136 }
00137
00138
00139 while ((scan_p==NULL) && (++scan_i<size)) scan_p=table[scan_i];
00140
00141 if (scan_p!=NULL)
00142 {
00143 k=scan_p->key;
00144 scan_p=(entry *)scan_p->next;
00145 return k;
00146 };
00147
00148 return NULL;
00149 }
00150
00151
00152 void htable::map(ostream& co,int cols){
00153
00154 entry *p;
00155 char* img=new char[cols+1];
00156
00157 img[cols]='\0';
00158 memset(img,'.',cols);
00159
00160 co << "htable memory map: . (0 items), - (<5), # (>5)\n";
00161
00162 for (int i=0; i<size;i++)
00163 {
00164 int n=0;p=table[i];
00165
00166 while(p!=NULL){
00167 n++;
00168 p=(entry *)p->next;
00169 };
00170
00171 if (i && (i % cols)==0){
00172 co << img << "\n";
00173 memset(img,'.',cols);
00174 }
00175
00176 if (n>0)
00177 img[i % cols]=n<=5?'-':'#';
00178
00179 }
00180
00181 img[size % cols]='\0';
00182 co << img << "\n";
00183
00184 delete []img;
00185 }
00186
00187
00188 void htable::stat(){
00189 cerr << "htable class statistics\n";
00190 cerr << "size " << size
00191 << " keys " << keys
00192 << " acc " << accesses
00193 << " coll " << collisions
00194 << " used memory " << used()/1024 << "Kb\n";
00195 }
00196
00197 htable::~htable()
00198 {
00199 delete [] table;
00200 delete memory;
00201 }
00202
00203 address htable::HashStr(char *key)
00204 {
00205 char *Key=(htype==STRPTR? *(char **)key:key);
00206 int length=(keylen?keylen:keylenfunc(Key));
00207
00208
00209
00210 register address h=0;
00211 register int i;
00212
00213 for (i=0,h=0;i<length;i++)
00214 h = h * Prime1 ^ (Key[i] - ' ');
00215 h %= Prime2;
00216
00217 return h;
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 address htable::HashInt(char *key)
00248 {
00249 int *Key=(htype==INTPTR? *(int **)key:(int *)key);
00250
00251
00252 address h;
00253 register int i;
00254
00255
00256 for (i=0,h=0;i<keylen;i++){
00257 h+=Key[i];
00258 h += ~(h << 15);
00259 h ^= (h >> 10);
00260 h += (h << 3);
00261 h ^= (h >> 6);
00262 h += ~(h << 11);
00263 h ^= (h >> 16);
00264 };
00265
00266 return h;
00267 }
00268
00269 int htable::CompStr(char *key1, char *key2)
00270 {
00271 assert(key1 && key2);
00272
00273 char *Key1=(htype==STRPTR?*(char **)key1:key1);
00274 char *Key2=(htype==STRPTR?*(char **)key2:key2);
00275
00276 assert(Key1 && Key2);
00277
00278 int length1=(keylen?keylen:keylenfunc(Key1));
00279 int length2=(keylen?keylen:keylenfunc(Key2));
00280
00281 if (length1!=length2) return 1;
00282
00283 register int i;
00284
00285 for (i=0;i<length1;i++)
00286 if (Key1[i]!=Key2[i]) return 1;
00287 return 0;
00288 }
00289
00290 int htable::CompInt(char *key1, char *key2)
00291 {
00292 assert(key1 && key2);
00293
00294 int *Key1=(htype==INTPTR?*(int **)key1:(int*)key1);
00295 int *Key2=(htype==INTPTR?*(int **)key2:(int*)key2);
00296
00297 assert(Key1 && Key2);
00298
00299 register int i;
00300
00301 for (i=0;i<keylen;i++)
00302 if (Key1[i]!=Key2[i]) return 1;
00303 return 0;
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343