1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
|
/********************************************************************************
* Hash table class template (char* based) *
*********************************************************************************/
#ifndef GHash_HH
#define GHash_HH
#include "GBase.h"
/**
* This class maintains a fast-access hash table of entities
* indexed by a character string (essentially, maps strings to pointers)
*/
//#define HASH_DBG_PRINT 1
#define GSTR_HASH(s) strhash(s)
//#define GSTR_HASH(s) djb_hash(s)
//#define GSTR_HASH(s) fnv1a_hash(s)
//#define GSTR_HASH(s) murmur3(s)
template <class OBJ> class GHash {
protected:
struct GHashEntry {
char* key; // Key string
bool keyalloc; // shared key flag (to free/not the key)
int hash; // Hash value of key
pointer data; // Data
};
GHashEntry* hash; // Hash
int fCapacity; // table size
int fCount; // number of valid entries
int fCurrentEntry;
char* lastkeyptr; //pointer to last key string added
//---------- Raw data retrieval (including empty entries)
// Return key at position pos.
const char* Key(uint pos) const { return hash[pos].key; }
// return data OBJ* at given position
OBJ* Data(uint pos) const { return (OBJ*) hash[pos].data; }
// Return position of first filled slot, or >= fCapacity
int First() const;
// Return position of last filled slot or -1
int Last() const;
// Return position of next filled slot in hash table
// or a value greater than or equal to fCapacity if no filled
// slot was found
int Next(int pos) const;
//Return position of previous filled slot in hash table
//or a -1 if no filled slot was found
int Prev(int pos) const;
private:
GHash(const GHash&);
GHash &operator=(const GHash&);
GFreeProc* fFreeProc; //procedure to free item data
protected:
public:
static void DefaultFreeProc(pointer item) {
delete (OBJ*)item;
}
public:
GHash(GFreeProc* freeProc); // constructs of an empty hash
GHash(bool doFree=true); // constructs of an empty hash (free the item objects)
void setFreeItem(GFreeProc *freeProc) { fFreeProc=freeProc; }
void setFreeItem(bool doFree) { fFreeProc=(doFree)? &DefaultFreeProc : NULL; }
int Capacity() const { return fCapacity; } // table's size, including the empty slots.
void Resize(int m); // Resize the table to the given size.
int Count() const { return fCount; }// the total number of entries in the table.
// Insert a new entry into the table given key.
// If there is already an entry with that key, leave it unchanged
OBJ* Add(const char* ky, OBJ* ptr=NULL);
//same with Add, but frees the old element if it's a replacement
OBJ* fAdd(const char* ky, OBJ* ptr=NULL);
//same as Add, but the key pointer is stored directly, no string copy needed
//(shared-key-Add)
OBJ* shkAdd(const char* ky, OBJ* ptr);
// Replace data at key. If there was no existing entry,
// a new entry is inserted.
OBJ* Replace(const char* ky, OBJ* ptr);
// Remove a given key and its data
OBJ* Remove(const char* ky);
// Find data OBJ* given key.
OBJ* Find(const char* ky, char** keyptr=NULL);
bool hasKey(const char* ky);
char* getLastKey() { return lastkeyptr; }
OBJ* operator[](const char* ky) { return Find(ky); }
void startIterate(); //iterator-like initialization
char* NextKey(); //returns next valid key in the table (NULL if no more)
OBJ* NextData(); //returns next valid hash[].data
OBJ* NextData(char*& nextkey); //returns next valid hash[].data
//or NULL if no more
//nextkey is SET to the corresponding key
GHashEntry* NextEntry() { //returns a pointer to a GHashEntry
int pos=fCurrentEntry;
while (pos<fCapacity && hash[pos].hash<0) pos++;
if (pos==fCapacity) {
fCurrentEntry=fCapacity;
return NULL;
}
else {
fCurrentEntry=pos+1;
return &hash[pos];
}
}
/// Clear all entries
void Clear();
/// Destructor
virtual ~GHash();
};
//
//======================== method definitions ========================
//
/*
Notes:
- Since the algorithm doubles the table size when exceeding MAX_LOAD,
it would be prudent to keep MIN_LOAD less than 1/2 MAX_LOAD;
otherwise, the algorithm might flip between halving and doubling!
- We store the key hash value so that 99.999% of the time we can compare hash numbers;
only when hash numbers match we need to compare keys.
- Thus with a good hash function the fCount of calls to strcmp() should be
roughly the same as the fCount of successful lookups.
*/
// Initial table size (MUST be power of 2)
#define DEF_HASH_SIZE 32
// Maximum hash table load factor (%)
#define MAX_LOAD 80
// Minimum hash table load factor (%)
#define MIN_LOAD 10
// Probe Position [0..n-1]
#define HASH1(x,n) (((unsigned int)(x)*13)%(n))
// Probe Distance [1..n-1]
#define HASH2(x,n) (1|(((unsigned int)(x)*17)%((n)-1)))
#define FREEDATA (fFreeProc!=NULL)
/*******************************************************************************/
// Construct empty hash
template <class OBJ> GHash<OBJ>::GHash(GFreeProc* freeProc) {
GMALLOC(hash, sizeof(GHashEntry)*DEF_HASH_SIZE);
fCurrentEntry=-1;
fFreeProc=freeProc;
lastkeyptr=NULL;
for (uint i=0; i<DEF_HASH_SIZE; i++)
hash[i].hash=-1; //this will be an indicator for 'empty' entries
fCapacity=DEF_HASH_SIZE;
fCount=0;
}
template <class OBJ> GHash<OBJ>::GHash(bool doFree) {
GMALLOC(hash, sizeof(GHashEntry)*DEF_HASH_SIZE);
fCurrentEntry=-1;
lastkeyptr=NULL;
fFreeProc = (doFree)?&DefaultFreeProc : NULL;
for (uint i=0; i<DEF_HASH_SIZE; i++)
hash[i].hash=-1; //this will be an indicator for 'empty' entries
fCapacity=DEF_HASH_SIZE;
fCount=0;
}
// Resize table
template <class OBJ> void GHash<OBJ>::Resize(int m) {
int i,n,p,x,h;
GHashEntry *k;
GASSERT(fCount<=fCapacity);
if(m<DEF_HASH_SIZE) m=DEF_HASH_SIZE;
n=fCapacity;
while((n>>2)>m) n>>=1; // Shrink until n/4 <= m
while((n>>1)<m) n<<=1; // Grow until m <= n/2
GASSERT(m<=(n>>1));
GASSERT(DEF_HASH_SIZE<=n);
if(n!=fCapacity){
GASSERT(m<=n);
GMALLOC(k, sizeof(GHashEntry)*n);
for(i=0; i<n; i++) k[i].hash=-1;
for(i=0; i<fCapacity; i++){
h=hash[i].hash;
if(h>=0){
p=HASH1(h,n);
GASSERT(0<=p && p<n);
x=HASH2(h,n);
GASSERT(1<=x && x<n);
while(k[p].hash!=-1) p=(p+x)%n;
GASSERT(k[p].hash<0);
k[p]=hash[i];
}
}
GFREE(hash);
hash=k;
fCapacity=n;
}
}
// add a new entry, or update it if it already exists
template <class OBJ> OBJ* GHash<OBJ>::Add(const char* ky, OBJ* pdata) {
int p,i,x,h,n;
if(!ky) GError("GHash::insert: NULL key argument.\n");
GASSERT(fCount<fCapacity);
h=GSTR_HASH(ky);
GASSERT(0<=h);
p=HASH1(h,fCapacity);
GASSERT(0<=p && p<fCapacity);
x=HASH2(h,fCapacity);
GASSERT(1<=x && x<fCapacity);
i=-1;
n=fCapacity;
#ifdef HASH_DBG_PRINT
int iterations=0;
int init_p=p;
int init_x=x;
#endif
while(n && hash[p].hash!=-1) {
if ((i==-1)&&(hash[p].hash==-2)) i=p;
if (hash[p].hash==h && strcmp(hash[p].key,ky)==0) {
//replace hash data for this key!
lastkeyptr=hash[p].key;
OBJ* r = (OBJ*) hash[p].data;
hash[p].data = (void*) pdata;
#ifdef HASH_DBG_PRINT
GMessage("Add.R\t%s\t%d,%d,%d\t%d\t%d\t%d\n",
ky, h,init_p,init_x, iterations, fCount, fCapacity);
#endif
return r;
}
p=(p+x)%fCapacity;
n--;
}
if(i==-1) i=p;
#ifdef HASH_DBG_PRINT
GMessage("Add.N\t%s\t%d,%d,%d\t%d\t%d\t%d\n",
ky, h,init_p,init_x, iterations, fCount, fCapacity);
#endif
GTRACE(("GHash::insert: key=\"%s\"\n",ky));
//GMessage("GHash::insert: key=\"%s\"\n",ky);
GASSERT(0<=i && i<fCapacity);
GASSERT(hash[i].hash<0);
hash[i].hash=h;
hash[i].key=Gstrdup(ky);
hash[i].keyalloc=true;
lastkeyptr=hash[i].key;
hash[i].data= (void*) pdata;
fCount++;
if((100*fCount)>=(MAX_LOAD*fCapacity)) Resize(fCount);
GASSERT(fCount<fCapacity);
return pdata;
}
template <class OBJ> OBJ* GHash<OBJ>::fAdd(const char* ky, OBJ* pdata) {
int p,i,x,h,n;
if(!ky) GError("GHash::insert: NULL key argument.\n");
GASSERT(fCount<fCapacity);
h=GSTR_HASH(ky);
GASSERT(0<=h);
p=HASH1(h,fCapacity);
GASSERT(0<=p && p<fCapacity);
x=HASH2(h,fCapacity);
GASSERT(1<=x && x<fCapacity);
i=-1;
n=fCapacity;
#ifdef HASH_DBG_PRINT
int iterations=0;
int init_p=p;
int init_x=x;
#endif
while(n && hash[p].hash!=-1) {
if ((i==-1)&&(hash[p].hash==-2)) i=p;
if (hash[p].hash==h && strcmp(hash[p].key,ky)==0) {
//replace hash data for this key!
lastkeyptr=hash[p].key;
if (FREEDATA) (*fFreeProc)(hash[p].data);
hash[p].data = (void*) pdata;
#ifdef HASH_DBG_PRINT
GMessage("Add.R\t%s\t%d,%d,%d\t%d\t%d\t%d\n",
ky, h,init_p,init_x, iterations, fCount, fCapacity);
#endif
return pdata;
}
p=(p+x)%fCapacity;
#ifdef HASH_DBG_PRINT
++iterations;
#endif
n--;
}
if(i==-1) i=p;
#ifdef HASH_DBG_PRINT
GMessage("Add.N\t%s\t%d,%d,%d\t%d\t%d\t%d\n",
ky, h,init_p,init_x, iterations, fCount, fCapacity);
#endif
GTRACE(("GHash::insert: key=\"%s\"\n",ky));
//GMessage("GHash::insert: key=\"%s\"\n",ky);
GASSERT(0<=i && i<fCapacity);
GASSERT(hash[i].hash<0);
hash[i].hash=h;
hash[i].key=Gstrdup(ky);
hash[i].keyalloc=true;
lastkeyptr=hash[i].key;
hash[i].data= (void*) pdata;
fCount++;
if((100*fCount)>=(MAX_LOAD*fCapacity)) Resize(fCount);
GASSERT(fCount<fCapacity);
return pdata;
}
template <class OBJ> OBJ* GHash<OBJ>::shkAdd(const char* ky, OBJ* pdata) {
int p,i,x,h,n;
if(!ky) GError("GHash::insert: NULL key argument.\n");
GASSERT(fCount<fCapacity);
h=GSTR_HASH(ky);
GASSERT(0<=h);
p=HASH1(h,fCapacity);
GASSERT(0<=p && p<fCapacity);
x=HASH2(h,fCapacity);
GASSERT(1<=x && x<fCapacity);
i=-1;
n=fCapacity;
while(n && hash[p].hash!=-1){
if((i==-1)&&(hash[p].hash==-2)) i=p;
if(hash[p].hash==h && strcmp(hash[p].key,ky)==0){
//replace hash data for this key!
lastkeyptr=hash[p].key;
hash[p].data = (void*) pdata;
return (OBJ*)hash[p].data;
}
p=(p+x)%fCapacity;
n--;
}
if(i==-1) i=p;
GTRACE(("GHash::insert: key=\"%s\"\n",ky));
//GMessage("GHash::insert: key=\"%s\"\n",ky);
GASSERT(0<=i && i<fCapacity);
GASSERT(hash[i].hash<0);
hash[i].hash=h;
hash[i].key=(char *)ky;
lastkeyptr=hash[i].key;
hash[i].keyalloc=false;
hash[i].data= (void*) pdata;
fCount++;
if((100*fCount)>=(MAX_LOAD*fCapacity)) Resize(fCount);
GASSERT(fCount<fCapacity);
return pdata;
}
// Add or replace entry
template <class OBJ> OBJ* GHash<OBJ>::Replace(const char* ky, OBJ* pdata){
int p,i,x,h,n;
if(!ky){ GError("GHash::replace: NULL key argument.\n"); }
GASSERT(fCount<fCapacity);
h=GSTR_HASH(ky);
GASSERT(0<=h);
p=HASH1(h,fCapacity);
GASSERT(0<=p && p<fCapacity);
x=HASH2(h,fCapacity);
GASSERT(1<=x && x<fCapacity);
i=-1;
n=fCapacity;
while(n && hash[p].hash!=-1){
if((i==-1)&&(hash[p].hash==-2)) i=p;
if(hash[p].hash==h && strcmp(hash[p].key,ky)==0){
GTRACE(("GHash::replace: %08x: replacing: \"%s\"\n",this,ky));
if (FREEDATA) (*fFreeProc)(hash[p].data);
hash[p].data=pdata;
return hash[p].data;
}
p=(p+x)%fCapacity;
n--;
}
if(i==-1) i=p;
GTRACE(("GHash::replace: %08x: inserting: \"%s\"\n",this,ky));
GASSERT(0<=i && i<fCapacity);
GASSERT(hash[i].hash<0);
hash[i].hash=h;
hash[i].key=Gstrdup(ky);
hash[i].data=pdata;
fCount++;
if((100*fCount)>=(MAX_LOAD*fCapacity)) Resize(fCount);
GASSERT(fCount<fCapacity);
return pdata;
}
// Remove entry
template <class OBJ> OBJ* GHash<OBJ>::Remove(const char* ky){
int p,x,h,n;
if(!ky){ GError("GHash::remove: NULL key argument.\n"); }
OBJ* removed=NULL;
if(0<fCount){
h=GSTR_HASH(ky);
GASSERT(0<=h);
p=HASH1(h,fCapacity);
GASSERT(0<=p && p<fCapacity);
x=HASH2(h,fCapacity);
GASSERT(1<=x && x<fCapacity);
GASSERT(fCount<fCapacity);
n=fCapacity;
while(n && hash[p].hash!=-1){
if(hash[p].hash==h && strcmp(hash[p].key,ky)==0){
GTRACE(("GHash::remove: %08x removing: \"%s\"\n",this,ky));
hash[p].hash=-2;
if (hash[p].keyalloc) GFREE((hash[p].key));
if (FREEDATA) (*fFreeProc)(hash[p].data);
else removed=(OBJ*)hash[p].data;
hash[p].key=NULL;
hash[p].data=NULL;
fCount--;
if((100*fCount)<=(MIN_LOAD*fCapacity)) Resize(fCount);
GASSERT(fCount<fCapacity);
return removed;
}
p=(p+x)%fCapacity;
n--;
}
}
return removed;
}
// Find entry
template <class OBJ> bool GHash<OBJ>::hasKey(const char* ky) {
int p,x,h,n;
if(!ky){ GError("GHash::find: NULL key argument.\n"); }
if(0<fCount){
h=GSTR_HASH(ky);
GASSERT(0<=h);
p=HASH1(h,fCapacity);
GASSERT(0<=p && p<fCapacity);
x=HASH2(h,fCapacity);
GASSERT(1<=x && x<fCapacity);
GASSERT(fCount<fCapacity);
n=fCapacity;
while(n && hash[p].hash!=-1){
if(hash[p].hash==h && strcmp(hash[p].key,ky)==0){
return true;
}
p=(p+x)%fCapacity;
n--;
}
}
return false;
}
template <class OBJ> OBJ* GHash<OBJ>::Find(const char* ky, char** keyptr){
int p,x,h,n;
if(!ky){ GError("GHash::find: NULL key argument.\n"); }
if (fCount==0) return NULL;
h=GSTR_HASH(ky);
GASSERT(0<=h);
p=HASH1(h,fCapacity);
GASSERT(0<=p && p<fCapacity);
x=HASH2(h,fCapacity);
GASSERT(1<=x && x<fCapacity);
GASSERT(fCount<fCapacity);
n=fCapacity;
#ifdef HASH_DBG_PRINT
int iterations=0;
int init_p=p;
int init_x=x;
#endif
while(n && hash[p].hash!=-1){
if(hash[p].hash==h && strcmp(hash[p].key,ky)==0){
if (keyptr!=NULL) *keyptr = hash[p].key;
#ifdef HASH_DBG_PRINT
GMessage("Found \t%s\t%d,%d,%d\t%d\t%d\t%d\n",
ky, h,init_p,init_x, iterations, fCount, fCapacity);
#endif
return (OBJ*)hash[p].data;
}
p=(p+x)%fCapacity;
n--;
#ifdef HASH_DBG_PRINT
++iterations;
#endif
}
#ifdef HASH_DBG_PRINT
GMessage("Nfound\t%s\t%d,%d,%d\t%d\t%d\t%d\n",
ky, h,init_p,init_x, iterations, fCount, fCapacity);
#endif
return NULL;
}
template <class OBJ> void GHash<OBJ>::startIterate() {// initialize a key iterator; call
fCurrentEntry=0;
}
template <class OBJ> char* GHash<OBJ>::NextKey() {
int pos=fCurrentEntry;
while (pos<fCapacity && hash[pos].hash<0) pos++;
if (pos==fCapacity) {
fCurrentEntry=fCapacity;
return NULL;
}
else {
fCurrentEntry=pos+1;
return hash[pos].key;
}
}
template <class OBJ> OBJ* GHash<OBJ>::NextData() {
int pos=fCurrentEntry;
while (pos<fCapacity && hash[pos].hash<0) pos++;
if (pos==fCapacity) {
fCurrentEntry=fCapacity;
return NULL;
}
else {
fCurrentEntry=pos+1;
return (OBJ*)hash[pos].data;
}
}
template <class OBJ> OBJ* GHash<OBJ>::NextData(char* &nextkey) {
int pos=fCurrentEntry;
while (pos<fCapacity && hash[pos].hash<0) pos++;
if (pos==fCapacity) {
fCurrentEntry=fCapacity;
nextkey=NULL;
return NULL;
}
else {
fCurrentEntry=pos+1;
nextkey=hash[pos].key;
return (OBJ*)hash[pos].data;
}
}
// Get first non-empty entry
template <class OBJ> int GHash<OBJ>::First() const {
int pos=0;
while(pos<fCapacity){ if(0<=hash[pos].hash) break; pos++; }
GASSERT(fCapacity<=pos || 0<=hash[pos].hash);
return pos;
}
// Get last non-empty entry
template <class OBJ> int GHash<OBJ>::Last() const {
int pos=fCapacity-1;
while(0<=pos){ if(0<=hash[pos].hash) break; pos--; }
GASSERT(pos<0 || 0<=hash[pos].hash);
return pos;
}
// Find next valid entry
template <class OBJ> int GHash<OBJ>::Next(int pos) const {
GASSERT(0<=pos && pos<fCapacity);
while(++pos <= fCapacity-1){ if(0<=hash[pos].hash) break; }
GASSERT(fCapacity<=pos || 0<=hash[pos].hash);
return pos;
}
// Find previous valid entry
template <class OBJ> int GHash<OBJ>::Prev(int pos) const {
GASSERT(0<=pos && pos<fCapacity);
while(--pos >= 0){ if(0<=hash[pos].hash) break; }
GASSERT(pos<0 || 0<=hash[pos].hash);
return pos;
}
// Remove all
template <class OBJ> void GHash<OBJ>::Clear(){
int i;
for(i=0; i<fCapacity; i++){
if(hash[i].hash>=0){
if (hash[i].keyalloc) GFREE((hash[i].key));
if (FREEDATA)
(*fFreeProc)(hash[i].data);
}
}
GFREE(hash);
GMALLOC(hash, sizeof(GHashEntry)*DEF_HASH_SIZE);
//reinitialize it
for (i=0; i<DEF_HASH_SIZE; i++)
hash[i].hash=-1; //this will be an indicator for 'empty' entries
fCapacity=DEF_HASH_SIZE;
fCount=0;
}
// Destroy table
template <class OBJ> GHash<OBJ>::~GHash(){
for(int i=0; i<fCapacity; i++){
if(hash[i].hash>=0){
if (hash[i].keyalloc) GFREE((hash[i].key));
if (FREEDATA) (*fFreeProc)(hash[i].data);
}
}
GFREE(hash);
}
class GStrSet:public GHash<int> {
protected:
bool free_keys;
public:
GStrSet(bool shared_keys=false):GHash<int>(false), free_keys(!shared_keys) {
}
void Add(const char* str) {
if (free_keys) {
//allocates a copy of str
GHash<int>::Add(str, NULL);
}
else this->shkAdd(str, NULL);
}
void add(const char* str) { this->Add(str); }
void push(const char* str) { this->Add(str); }
bool has(const char* str) {
return hasKey(str);
}
};
#endif
|