libStatGen Software 1
StringDoubleHash Class Reference
Inheritance diagram for StringDoubleHash:
Collaboration diagram for StringDoubleHash:

Public Member Functions

 StringDoubleHash (int startsize=32)
 
void Grow ()
 
void Shrink ()
 
void SetSize (int newsize)
 
void Clear ()
 
int Capacity () const
 
int Entries () const
 
double Double (int i) const
 
double Double (const String &key) const
 
void SetDouble (int i, double value)
 
void SetDouble (const String &key, double value)
 
int Add (const String &s, double value)
 
int Find (const String &s, double defaultValue)
 
int Find (const String &s) const
 
StringDoubleHashoperator= (const StringDoubleHash &rhs)
 
const Stringoperator[] (int i) const
 
Stringoperator[] (int i)
 
void Delete (unsigned int index)
 
void Delete (const String &key)
 
bool SlotInUse (int index) const
 
- Public Member Functions inherited from StringHashBase
void setCaseSensitive (bool caseSensitive)
 
virtual void SetSize (int newsize)=0
 

Protected Attributes

String ** strings
 
double * doubles
 
unsigned int * keys
 
unsigned int count
 
unsigned int size
 
unsigned int mask
 
- Protected Attributes inherited from StringHashBase
bool myCaseSensitive
 

Additional Inherited Members

- Protected Member Functions inherited from StringHashBase
bool stringsEqual (const String &string1, const String &string2) const
 
unsigned int getKey (const String &string) const
 

Detailed Description

Definition at line 309 of file StringHash.h.

Constructor & Destructor Documentation

◆ StringDoubleHash()

StringDoubleHash::StringDoubleHash ( int  startsize = 32)

Definition at line 410 of file StringHash.cpp.

412{
413 count = 0;
414 size = startsize;
415 mask = startsize - 1;
416
417 // In this implementation, the size of hash tables must be a power of two
418 if (startsize & mask)
419 error("StringDoubleHash: Hash table size must be a power of two.\n");
420
421 strings = new String * [size];
422 doubles = new double [size];
423 keys = new unsigned int [size];
424
425 for (unsigned int i = 0; i < size; i++)
426 strings[i] = NULL;
427};

◆ ~StringDoubleHash()

StringDoubleHash::~StringDoubleHash ( )
virtual

Definition at line 429 of file StringHash.cpp.

430{
431 for (unsigned int i = 0; i < size; i++)
432 if (strings[i] != NULL)
433 delete strings[i];
434
435 if(strings) delete [] strings;
436 if(doubles) delete [] doubles;
437 if(keys) delete [] keys;
438}

Member Function Documentation

◆ Add()

int StringDoubleHash::Add ( const String s,
double  value 
)

Definition at line 477 of file StringHash.cpp.

478{
479 unsigned int key = getKey(string);
480 unsigned int h = Iterate(key, string);
481
482 if (strings[h] == NULL)
483 Insert(h, key, string);
484
485 doubles[h] = value;
486
487 if (count * 2 > size)
488 {
489 Grow();
490 return Iterate(key, string);
491 }
492
493 return h;
494}

◆ Capacity()

int StringDoubleHash::Capacity ( ) const
inline

Definition at line 335 of file StringHash.h.

336 {
337 return size;
338 }

◆ Clear()

void StringDoubleHash::Clear ( )

Definition at line 637 of file StringHash.cpp.

638{
639 for (unsigned int i = 0; i < size; i++)
640 if (strings[i] != NULL)
641 {
642 delete strings[i];
643 strings[i] = NULL;
644 }
645
646 count = 0;
647
648 if (size > 256)
649 SetSize(256);
650}

◆ Delete() [1/2]

void StringDoubleHash::Delete ( const String key)
inline

Definition at line 381 of file StringHash.h.

382 {
383 Delete(Find(key));
384 }

◆ Delete() [2/2]

void StringDoubleHash::Delete ( unsigned int  index)

Definition at line 527 of file StringHash.cpp.

528{
529 if (index >= size || strings[index] == NULL)
530 return;
531
532 delete strings[index];
533 strings[index] = NULL;
534 count--;
535
536 if (count * 8 < size && size > 32)
537 Shrink();
538 else
539 {
540 // rehash the next strings until we find empty slot
541 index = (index + 1) & mask;
542
543 while (strings[index] != NULL)
544 {
545 if ((keys[index] & mask) != index)
546 {
547 unsigned int h = Iterate(keys[index], *strings[index]);
548
549 if (h != (unsigned int) index)
550 {
551 keys[h] = keys[index];
552 strings[h] = strings[index];
553 doubles[h] = doubles[index];
554
555 strings[index] = NULL;
556 }
557 }
558
559 index = (index + 1) & mask;
560 }
561 }
562}

◆ Double() [1/2]

double StringDoubleHash::Double ( const String key) const
inline

Definition at line 348 of file StringHash.h.

349 {
350 int index = Find(key);
351
352 return index >= 0 ? doubles[index] : _NAN_;
353 }

◆ Double() [2/2]

double StringDoubleHash::Double ( int  i) const
inline

Definition at line 344 of file StringHash.h.

345 {
346 return doubles[i];
347 }

◆ Entries()

int StringDoubleHash::Entries ( ) const
inline

Definition at line 339 of file StringHash.h.

340 {
341 return count;
342 }

◆ Find() [1/2]

int StringDoubleHash::Find ( const String s) const

Definition at line 516 of file StringHash.cpp.

517{
518 unsigned int key = getKey(string);
519 unsigned int h = Iterate(key, string);
520
521 if (strings[h] == NULL)
522 return -1;
523
524 return h;
525}

◆ Find() [2/2]

int StringDoubleHash::Find ( const String s,
double  defaultValue 
)

Definition at line 496 of file StringHash.cpp.

497{
498 unsigned int key = getKey(string);
499 unsigned int h = Iterate(key, string);
500
501 if (strings[h] == NULL)
502 {
503 Insert(h, key, string);
504 doubles[h] = defaultValue;
505
506 if (count * 2 > size)
507 {
508 Grow();
509 return Iterate(key, string);
510 }
511 }
512
513 return h;
514}

◆ Grow()

void StringDoubleHash::Grow ( )
inline

Definition at line 322 of file StringHash.h.

323 {
324 SetSize(size * 2);
325 }

◆ operator=()

StringDoubleHash & StringDoubleHash::operator= ( const StringDoubleHash rhs)

Definition at line 699 of file StringHash.cpp.

700{
701 Clear();
702
703 for (int i = 0; i < rhs.Capacity(); i++)
704 if (rhs.SlotInUse(i))
705 Add(*(rhs.strings[i]), rhs.doubles[i]);
706
707 return *this;
708}

◆ operator[]() [1/2]

String & StringDoubleHash::operator[] ( int  i)
inline

Definition at line 374 of file StringHash.h.

375 {
376 return *(strings[i]);
377 }

◆ operator[]() [2/2]

const String & StringDoubleHash::operator[] ( int  i) const
inline

Definition at line 370 of file StringHash.h.

371 {
372 return *(strings[i]);
373 }

◆ SetDouble() [1/2]

void StringDoubleHash::SetDouble ( const String key,
double  value 
)
inline

Definition at line 359 of file StringHash.h.

360 {
361 Add(key, value);
362 }

◆ SetDouble() [2/2]

void StringDoubleHash::SetDouble ( int  i,
double  value 
)
inline

Definition at line 355 of file StringHash.h.

356 {
357 doubles[i] = value;
358 }

◆ SetSize()

void StringDoubleHash::SetSize ( int  newsize)
virtual

Implements StringHashBase.

Definition at line 440 of file StringHash.cpp.

441{
442 int newmask = newsize - 1;
443
444 String ** newstrings = new String * [newsize];
445 double * newdoubles = new double [newsize];
446 unsigned int * newkeys = new unsigned int [newsize];
447
448 for (int i = 0; i < newsize; i++)
449 newstrings[i] = NULL;
450
451 for (unsigned int i = 0; i < size; i++)
452 if (strings[i] != NULL)
453 {
454 unsigned int key = keys[i];
455 unsigned int h = key & newmask;
456
457 while (newstrings[h] != NULL &&
458 (newkeys[h] != key || (!stringsEqual(*(newstrings[h]), *(strings[i])))))
459 h = (h + 1) & newmask;
460
461 newkeys[h] = key;
462 newstrings[h] = strings[i];
463 newdoubles[h] = doubles[i];
464 }
465
466 if(strings) delete [] strings;
467 if(doubles) delete [] doubles;
468 if(keys) delete [] keys;
469
470 strings = newstrings;
471 doubles = newdoubles;
472 keys = newkeys;
473 size = newsize;
474 mask = newmask;
475}

◆ Shrink()

void StringDoubleHash::Shrink ( )
inline

Definition at line 326 of file StringHash.h.

327 {
328 SetSize(size / 2);
329 }

◆ SlotInUse()

bool StringDoubleHash::SlotInUse ( int  index) const
inline

Definition at line 386 of file StringHash.h.

387 {
388 return strings[index] != NULL;
389 }

Member Data Documentation

◆ count

unsigned int StringDoubleHash::count
protected

Definition at line 315 of file StringHash.h.

◆ doubles

double* StringDoubleHash::doubles
protected

Definition at line 313 of file StringHash.h.

◆ keys

unsigned int* StringDoubleHash::keys
protected

Definition at line 314 of file StringHash.h.

◆ mask

unsigned int StringDoubleHash::mask
protected

Definition at line 316 of file StringHash.h.

◆ size

unsigned int StringDoubleHash::size
protected

Definition at line 315 of file StringHash.h.

◆ strings

String** StringDoubleHash::strings
protected

Definition at line 312 of file StringHash.h.


The documentation for this class was generated from the following files: