Loading...
Searching...
No Matches
StringDoubleHash Class Reference
Inheritance diagram for StringDoubleHash:
[legend]
Collaboration diagram for StringDoubleHash:
[legend]

## 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
StringDoubleHash & | operator= (const StringDoubleHash &rhs)
const String & | operator[] (int i) const
String & | operator[] (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)

## 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.

: StringHashBase()
{
count = 0;
size = startsize;
mask = startsize - 1;
// In this implementation, the size of hash tables must be a power of two
if (startsize & mask)
error("StringDoubleHash: Hash table size must be a power of two.\n");
strings = new String * [size];
doubles = new double [size];
keys = new unsigned int [size];
for (unsigned int i = 0; i < size; i++)
strings[i] = NULL;
};
StringHashBase
Definition StringHash.h:27
String
Definition StringBasics.h:39

## ~StringDoubleHash()

StringDoubleHash::~StringDoubleHash | ( |  | ) |

virtual

Definition at line 429 of file StringHash.cpp.

{
for (unsigned int i = 0; i < size; i++)
if (strings[i] != NULL)
delete strings[i];
if(strings) delete [] strings;
if(doubles) delete [] doubles;
if(keys) delete [] keys;
}

## Member Function Documentation

## Add()

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

Definition at line 477 of file StringHash.cpp.

{
unsigned int key = getKey(string);
unsigned int h = Iterate(key, string);
if (strings[h] == NULL)
Insert(h, key, string);
doubles[h] = value;
if (count * 2 > size)
{
Grow();
return Iterate(key, string);
}
return h;
}

## Capacity()

int StringDoubleHash::Capacity | ( |  | ) | const

inline

Definition at line 335 of file StringHash.h.

{
return size;
}

## Clear()

void StringDoubleHash::Clear | ( |  | ) |

Definition at line 637 of file StringHash.cpp.

{
for (unsigned int i = 0; i < size; i++)
if (strings[i] != NULL)
{
delete strings[i];
strings[i] = NULL;
}
count = 0;
if (size > 256)
SetSize(256);
}

## Delete() [1/2]

void StringDoubleHash::Delete | ( | const String & | key | ) |

inline

Definition at line 381 of file StringHash.h.

{
Delete(Find(key));
}

## Delete() [2/2]

void StringDoubleHash::Delete | ( | unsigned int | index | ) |

Definition at line 527 of file StringHash.cpp.

{
if (index >= size || strings[index] == NULL)
return;
delete strings[index];
strings[index] = NULL;
count--;
if (count * 8 < size && size > 32)
Shrink();
else
{
// rehash the next strings until we find empty slot
index = (index + 1) & mask;
while (strings[index] != NULL)
{
if ((keys[index] & mask) != index)
{
unsigned int h = Iterate(keys[index], *strings[index]);
if (h != (unsigned int) index)
{
keys[h] = keys[index];
strings[h] = strings[index];
doubles[h] = doubles[index];
strings[index] = NULL;
}
}
index = (index + 1) & mask;
}
}
}

## Double() [1/2]

double StringDoubleHash::Double | ( | const String & | key | ) | const

inline

Definition at line 348 of file StringHash.h.

{
int index = Find(key);
return index >= 0 ? doubles[index] : _NAN_;
}

## Double() [2/2]

double StringDoubleHash::Double | ( | int | i | ) | const

inline

Definition at line 344 of file StringHash.h.

{
return doubles[i];
}

## Entries()

int StringDoubleHash::Entries | ( |  | ) | const

inline

Definition at line 339 of file StringHash.h.

{
return count;
}

## Find() [1/2]

int StringDoubleHash::Find | ( | const String & | s | ) | const

Definition at line 516 of file StringHash.cpp.

{
unsigned int key = getKey(string);
unsigned int h = Iterate(key, string);
if (strings[h] == NULL)
return -1;
return h;
}

## Find() [2/2]

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

Definition at line 496 of file StringHash.cpp.

{
unsigned int key = getKey(string);
unsigned int h = Iterate(key, string);
if (strings[h] == NULL)
{
Insert(h, key, string);
doubles[h] = defaultValue;
if (count * 2 > size)
{
Grow();
return Iterate(key, string);
}
}
return h;
}

## Grow()

void StringDoubleHash::Grow | ( |  | ) |

inline

Definition at line 322 of file StringHash.h.

{
SetSize(size * 2);
}

## operator=()

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

Definition at line 699 of file StringHash.cpp.

{
Clear();
for (int i = 0; i < rhs.Capacity(); i++)
if (rhs.SlotInUse(i))
Add(*(rhs.strings[i]), rhs.doubles[i]);
return *this;
}

## operator[]() [1/2]

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

inline

Definition at line 374 of file StringHash.h.

{
return *(strings[i]);
}

## operator[]() [2/2]

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

inline

Definition at line 370 of file StringHash.h.

{
return *(strings[i]);
}

## SetDouble() [1/2]

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

inline

Definition at line 359 of file StringHash.h.

{
Add(key, value);
}

## SetDouble() [2/2]

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

inline

Definition at line 355 of file StringHash.h.

{
doubles[i] = value;
}

## SetSize()

void StringDoubleHash::SetSize | ( | int | newsize | ) |

virtual

Implements StringHashBase.

Definition at line 440 of file StringHash.cpp.

{
int newmask = newsize - 1;
String ** newstrings = new String * [newsize];
double * newdoubles = new double [newsize];
unsigned int * newkeys = new unsigned int [newsize];
for (int i = 0; i < newsize; i++)
newstrings[i] = NULL;
for (unsigned int i = 0; i < size; i++)
if (strings[i] != NULL)
{
unsigned int key = keys[i];
unsigned int h = key & newmask;
while (newstrings[h] != NULL &&
(newkeys[h] != key || (!stringsEqual(*(newstrings[h]), *(strings[i])))))
h = (h + 1) & newmask;
newkeys[h] = key;
newstrings[h] = strings[i];
newdoubles[h] = doubles[i];
}
if(strings) delete [] strings;
if(doubles) delete [] doubles;
if(keys) delete [] keys;
strings = newstrings;
doubles = newdoubles;
keys = newkeys;
size = newsize;
mask = newmask;
}

## Shrink()

void StringDoubleHash::Shrink | ( |  | ) |

inline

Definition at line 326 of file StringHash.h.

{
SetSize(size / 2);
}

## SlotInUse()

bool StringDoubleHash::SlotInUse | ( | int | index | ) | const

inline

Definition at line 386 of file StringHash.h.

{
return strings[index] != NULL;
}

## 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:

- general/StringHash.h
- general/StringHash.cpp