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 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
|
/**
* Yudit Unicode Editor Source File
*
* GNU Copyright (C) 1997-2023 Gaspar Sinai <gaspar@yudit.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* dated June 1991. See file COPYYING for details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "SBHashtable.h"
#include "SObject.h"
#include "SExcept.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_DATA_BYTES 8
#define HASH_INITIAL_SIZE 7
#define PSIZE (sizeof(char*))
#define RESIZE_AT 70 // Percent
#define PRIME_GROW_PERC 70 // Percent.
static unsigned int largerPrime (unsigned int in);
static unsigned int sqrimax (unsigned int sq);
static char* getNullVector();
int SBHashtable::debugLevel = 0;
class SBucketItem : public SObject
{
public:
SBucketItem (const SString& key, const char* value, int len);
SBucketItem (const SBucketItem& i);
virtual ~SBucketItem() {}
virtual SObject* clone () const {
SBucketItem* n = new SBucketItem(*this);
CHECK_NEW(n);
return n;
}
// Hope for the best - this should be aligned...
union
{
double d;
void* v;
char c[MAX_DATA_BYTES];
} value;
SString key;
unsigned int hash;
};
#define SBucketVector SBucketItem**
static SBucketVector _subBucketAppend (SBucketVector bv, SBucketItem* bi);
static unsigned int _subBucketVectorSize (SBucketVector bv);
#define GET_BUCKET(_arr, _mod) *((SBucketItem***)&_arr[_mod*PSIZE])
//typedef SVector<SBucketItem> SBucketVector;
/**
* Create a single bucketitem
* @param k is the key
* @param v is the value
* @param len is the size. max is 8
*/
SBucketItem::SBucketItem (const SBucketItem& item) : SObject(), key (item.key)
{
hash =item.hash;
memcpy (&value, &item.value, MAX_DATA_BYTES);
}
/**
* Create a single bucketitem
* @param k is the key
* @param v is the value
* @param len is the size. max is 8
*/
SBucketItem::SBucketItem (const SString& k, const char* v, int len) : SObject(), key(k)
{
if (len > MAX_DATA_BYTES)
{
fprintf (stderr, "Internal error: SBucketItem:: bad data length\n");
// Dump here.
}
hash = key.hashCode ();
memcpy (&value, v, len);
}
/**
* Set the debug level
* @param level 0 means no debug printouts.
* @return the previous level
*/
int
SBHashtable::debug (int level)
{
int old = debugLevel;
debugLevel = level;
return old;
}
SObject*
SBHashtable::clone() const
{
SBHashtable * n = new SBHashtable (*this);
CHECK_NEW(n);
return n;
}
/**
* This is the vector class. I don't prefer using STL.
*/
SBHashtable::SBHashtable(void)
{
buffer = new SHShared(HASH_INITIAL_SIZE * PSIZE);
CHECK_NEW(buffer);
memset (buffer->array, 0, HASH_INITIAL_SIZE * PSIZE);
}
/**
* Create a vector from a const vector. This is
* used when you do
* SBHashtable v = old;
* @param v is the old
*/
SBHashtable::SBHashtable (const SBHashtable& v)
{
// Assign the common buffer and increment the reference count
buffer = (SHShared*) v.buffer;
buffer->count++;
}
/**
* Assign a vector from a const vector. This is
* used when you do
* SBHashtable v;
* v = old;
* @param v is the old
*/
SBHashtable&
SBHashtable::operator=(const SBHashtable& v)
{
refer (v);
return *this;
}
/**
* Assign a vector from a const vector by changing the reference.
* @param v is the input vector
*/
void
SBHashtable::refer(const SBHashtable& v)
{
if (&v==this) return;
cleanup ();
buffer = (SHShared*) v.buffer;
buffer->count++;
return ;
}
/**
* The destructor
*/
SBHashtable::~SBHashtable()
{
cleanup();
}
/**
* Return the bucket array size.
*/
unsigned int
SBHashtable::size( ) const
{
return buffer->arraySize/PSIZE;
}
/**
* Return the bucket array size.
* param sub is the subbucket index.
*/
unsigned int
SBHashtable::size(int sub) const
{
SBucketVector bv = GET_BUCKET (buffer->array, sub);
if (bv == 0) return 0;
return _subBucketVectorSize (bv);
}
/**
* Remake the hash
*/
void
SBHashtable::rehash()
{
if (buffer->count != 1)
{
fprintf (stderr, "Internal error: SBuffer::rehash\n");
exit (1);
}
SHShared *sb = buffer;
unsigned int bigger = largerPrime(buffer->arraySize/PSIZE);
if (debugLevel > 1)
{
fprintf (stderr, "SBHashtable::rehash from %u to %u\n",
(unsigned int)(buffer->arraySize/PSIZE), bigger);
}
buffer = new SHShared (bigger * PSIZE);
CHECK_NEW(buffer);
memset (buffer->array, 0, bigger * PSIZE);
SBucketVector bo;
SBucketVector bn;
for (unsigned int i=0; i<sb->arraySize/PSIZE; i++)
{
bo = GET_BUCKET (sb->array, i);
if (bo == 0) continue;
// Move the items over...
for (unsigned j=0; bo[j] != 0; j++)
{
SBucketItem* bi =bo[j];
unsigned int mod = bi->hash % (buffer->arraySize/PSIZE);
bn = GET_BUCKET (buffer->array, mod);
GET_BUCKET(buffer->array, mod) = _subBucketAppend (bn, bi);
buffer->vectorSize++;
}
delete bo;
}
delete sb;
}
/**
* Replace char's with len length at index
* @param index is the reference index of len blocks
* @param e is the pointer to the char array to be saved
* @param len is the length of the block.
*/
void
SBHashtable::put (const SString& index, const char* e, int len, bool replace)
{
// Resize buckets
if (buffer->vectorSize*PSIZE >= (buffer->arraySize * RESIZE_AT)/100)
{
rehash ();
}
unsigned int mod = index.hashCode() % (buffer->arraySize/PSIZE);
SBucketItem* sbi = new SBucketItem (index, e, len);
CHECK_NEW(sbi);
SBucketVector bv = GET_BUCKET (buffer->array, mod);
if (bv==0)
{
//fprintf (stderr, "NEW BV %u\n", mod);
bv = new SBucketItem*[2];
CHECK_NEW(bv);
bv[0] = sbi;
bv[1] = 0;
GET_BUCKET (buffer->array, mod)=bv;
buffer->vectorSize++;
return;
}
//fprintf (stderr, "OLD BV %u\n", mod);
// Find the old one and delete it.
if (replace)
{
for (unsigned int i=0; bv[i]!= 0; i++)
{
SBucketItem* bi = bv[i];
if (bi->key == index)
{
delete bi;
bv[i] = sbi;
return;
}
}
}
bv = _subBucketAppend (bv, sbi);
GET_BUCKET(buffer->array, mod) = bv;
buffer->vectorSize++;
//fprintf (stderr, "NOREPLACE AFTER %u (%u)\n", _subBucketVectorSize (bv), mod);
//fprintf (stderr, "NOREPLACE AFTER %u\n", _subBucketVectorSize (bv));
//fprintf (stderr, "NOREPLACE AFTER %u\n",
// _subBucketVectorSize (GET_BUCKET(buffer->array, mod)));
}
/**
* Remove char's with len length at index
* @param index is the reference index of len blocks
* @param len is the length of the block.
*/
void
SBHashtable::remove (const SString& index)
{
derefer ();
unsigned int mod = index.hashCode() % (buffer->arraySize/PSIZE);
SBucketVector bv = GET_BUCKET (buffer->array, mod);
if (bv == 0) return;
SBucketItem* bi;
for (unsigned int i=0; bv[i] != 0; i++)
{
bi = bv[i];
if (bi->key == index)
{
buffer->vectorSize--;
delete bi;
unsigned int j;
for (j=i; bv[j]!=0; j++)
{
bv[j] = bv[j+1];
}
if (j==1)
{
delete [] bv;
bv = NULL;
GET_BUCKET (buffer->array, mod) = 0;
break;
}
break;
}
}
}
/**
* Clear the array and make a new SHShared.
*/
void
SBHashtable::clear ()
{
cleanup ();
buffer = new SHShared(HASH_INITIAL_SIZE * PSIZE);
CHECK_NEW(buffer);
memset (buffer->array, 0, HASH_INITIAL_SIZE * PSIZE);
}
/**
* clean up. usually called before delete.
* remove the buffer or its reference. This is private!
*/
void
SBHashtable::cleanup ()
{
if (!buffer) return; // already clean.
if (buffer->count==1) {
SBucketVector bv;
for (unsigned int i=0; i<buffer->arraySize/PSIZE; i++)
{
bv = GET_BUCKET (buffer->array, i);
if (bv)
{
SBucketItem* bi;
for (unsigned int j=0; bv[j] != 0; j++)
{
bi = bv[j];
delete bi;
}
delete [] bv;
bv = NULL;
}
}
delete buffer;
} else {
buffer->count--;
}
buffer = 0;
}
/**
* Return the element at index.
* @param index is the reference index of len blocks
*/
const char*
SBHashtable::get (const SString& index) const
{
unsigned int mod = index.hashCode() % (buffer->arraySize/PSIZE);
SBucketVector bv = GET_BUCKET (buffer->array, mod);
if (bv == 0) return getNullVector();
SBucketItem* bi;
for (unsigned int i=0; bv[i] != 0; i++)
{
bi = bv[i];
if (bi->key == index)
{
return ((char*) &bi->value);
}
}
return getNullVector();
}
/**
* Return the element at index.
* @param index is the reference index of len blocks
*/
const char*
SBHashtable::get (unsigned int bucket, unsigned int subbucket) const
{
SBucketVector bv = GET_BUCKET (buffer->array, bucket);
if (bv == 0) return getNullVector();
for (unsigned int i=0; bv[i] != 0; i++)
{
if (i==subbucket)
{
SBucketItem* bi = bv[i];
return ((const char*) &bi->value);
}
}
return getNullVector();
}
/**
* This could be used if you don't want to disturb the rehash.
*/
void
SBHashtable::put (unsigned int bucket, unsigned int subbucket, const char* e, int len)
{
derefer ();
SBucketVector bv = GET_BUCKET (buffer->array, bucket);
if (bv == 0) return;
SBucketItem* bi = bv[subbucket];
memcpy (&bi->value, e, len);
}
/**
* Return the element at index.
* @param index is the reference index of len blocks
*/
const SString&
SBHashtable::key (unsigned int bucket, unsigned int subbucket) const
{
SBucketVector bv = GET_BUCKET (buffer->array, bucket);
if (bv == 0) return SStringNull;
SBucketItem* bi = bv[subbucket];
return (bi->key);
}
/**
* Get the list of keys
* @param keys is the string list of output
*/
void
SBHashtable::keys(SStringVector* keys) const
{
keys->clear();
for (unsigned int i=0; i<size(); i++)
{
for (unsigned int j=0; j<size(i); j++)
{
keys->append(key(i,j));
}
}
}
/**
* The array will change. If the buffer is shared copy the buffer.
*/
void
SBHashtable::derefer()
{
if (buffer->count==1) return;
buffer->count--;
SHShared* oldBuffer = buffer;
// This copies the array..
buffer = new SHShared (*oldBuffer);
CHECK_NEW(buffer);
for (unsigned int i=0; i<oldBuffer->arraySize/PSIZE; i++)
{
SBucketVector bv = GET_BUCKET (oldBuffer->array, i);
if (bv ==0)
{
GET_BUCKET (buffer->array, i) = 0;
continue;
}
// This will not work this way. The deferer routine
// that comes after this can not recreate objects -
// It can not put two things in the same array!
// Note that we don't count references of contained object.
// Copying read-only hash is ok, but read write can be expensive.
// GET_BUCKET (buffer->array, i) = new SBucketVector(*bv);
// Copy the whole array comment this
// Uncomment of the above one does not work.
unsigned int ssize = _subBucketVectorSize (bv);
SBucketVector nv = new SBucketItem* [ssize+1];
CHECK_NEW (nv);
for (unsigned int j=0; j<ssize; j++)
{
SBucketItem* item = bv[j];
nv[j] = new SBucketItem(*item);
}
nv[ssize] = 0;
GET_BUCKET (buffer->array, i) = nv;
}
}
/**
* These are needed for a real fast hash
*/
static unsigned int
_subBucketVectorSize (SBucketVector bv)
{
if (bv == 0) return 0;
unsigned int i;
for (i=0; bv[i]!=0; i++) {}
return i;
}
/**
* These are needed for a real fast hash
*/
static SBucketVector
_subBucketAppend (SBucketVector bv, SBucketItem* bi)
{
unsigned int count = _subBucketVectorSize (bv);
SBucketVector nv = new SBucketItem*[count +2];
CHECK_NEW (nv);
if (count != 0)
{
memcpy (nv, bv, count * sizeof (SBucketItem*));
}
if (bv != 0)
{
delete [] bv;
bv = NULL;
}
nv[count] = bi;
nv[count+1] = 0;
return nv;
}
/**
* Give me a lerger prime than number.
*/
static unsigned int largerPrime (unsigned int base)
{
/* It is enough to check if it is dividable by
the square root of the fn. */
unsigned int sqrb = sqrimax (base + (base*PRIME_GROW_PERC)/100);
unsigned int sqro = sqrimax (base) + 1;
if (sqro >= sqrb) sqrb = sqro+1;
unsigned int result = (sqrb * sqrb) -1;
unsigned int i;
while (true)
{
for (i=2; i<sqrb; i++)
{
if ((result%i)==0) break;
}
if (i==sqrb) break;
result--;
}
return result;
}
/**
* Get the square root approximately - do not use util version
* because that one returns less.
* @param sq is the square
*/
static unsigned int sqrimax (unsigned int sq)
{
if (sq==1) return 1;
int r=sq-1; int x=sq;
while (r < x)
{
x = r; r= (x+(sq/x))/2;
}
return r;
}
/*
* Compose a static null vector
*/
static char* getNullVector()
{
static union
{
double d;
void* v;
char c[MAX_DATA_BYTES];
} nullVector;
memset (&nullVector, 0, MAX_DATA_BYTES);
return (char*) &nullVector;
}
SOHashtable::SOHashtable (void) : SBHashtable()
{
}
SOHashtable::SOHashtable (const SOHashtable& base) : SBHashtable (base)
{
}
SOHashtable&
SOHashtable::operator=(const SOHashtable& v)
{
refer (v);
return *this;
}
SObject*
SOHashtable::clone() const
{
return new SOHashtable (*this);
}
SOHashtable::~SOHashtable ()
{
cleanup ();
}
const SObject*
SOHashtable::get (const SString key) const
{
return (SObject*) (*(SObject**)SBHashtable::get (key));
}
const SObject*
SOHashtable::get (unsigned int row, unsigned int col) const
{
return (SObject*) (*(SObject**)SBHashtable::get (row, col));
}
void
SOHashtable::put (const SString& key, const SObject& e, bool replace)
{
derefer();
if (replace)
{
SObject* old = (SObject*) get (key);
if (old)
{
delete old;
}
}
SObject* r = e.clone(); SBHashtable::put (key, (char*) &r, sizeof (SObject*), replace);
}
void
SOHashtable::put (unsigned int bucket, unsigned int subbucket, SObject& e)
{
derefer();
SObject* old = (SObject*) get (bucket, subbucket); if (old) delete old;
SObject* r = e.clone(); SBHashtable::put (bucket, subbucket, (char*) &r, sizeof (SObject*));
}
void
SOHashtable::remove (const SString& key)
{
derefer();
SObject* old = (SObject*) get (key); if (old) delete old;
SBHashtable::remove (key);
}
void
SOHashtable::clear ()
{
cleanup ();
SBHashtable::clear();
}
void
SOHashtable::refer (const SOHashtable& v)
{
if (&v != this)
{
cleanup();
SBHashtable::refer(v);
}
}
void
SOHashtable::derefer()
{
if (buffer->count==1) return;
SBHashtable::derefer();
const SObject* r; SString k;
for (unsigned int i=0; i<size(); i++)
{
for (unsigned int j=0; j<size(i); j++)
{
k = key (i, j);
r = (SObject*) get (i, j);
SObject* newr = r->clone();
SBHashtable::put (i, j, (char*) &newr, sizeof (newr));
}
}
}
void
SOHashtable::cleanup()
{
if (buffer->count!=1)
{
return;
}
SObject* r;
for (unsigned int i=0; i<size(); i++)
{
for (unsigned int j=0; j<size(i); j++)
{
r = (SObject*) get (i, j);
delete r;
}
}
SBHashtable::cleanup();
}
|