StringHash Class Reference
Inheritance diagram for StringHash:
[legend]
Collaboration diagram for StringHash:
[legend]

## Public Member Functions

| StringHash (int startsize=32)
void | Grow ()
void | Shrink ()
void | SetSize (int newsize)
void | Clear ()
int | Capacity () const
int | Entries () const
void * | Object (int i) const
void * | Object (const String &key) const
void * | Object (const String &key, void *(*create_object)())
void | SetObject (int i, void *object)
void | SetObject (const String &key, void *object)
int | Add (const String &s, void *object=NULL)
int | Find (const String &s, void *(*create_object)()=NULL)
int | Find (const String &s) const
StringHash & | operator= (const StringHash &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
void | Print ()
void | Print (FILE *file)
void | Print (const char *filename)
String | StringList (char separator=',')
void | ReadLinesFromFile (FILE *file)
void | ReadLinesFromFile (const char *filename)
void | ReadLinesFromFile (IFILE &file)
void | Swap (StringHash &s)
Public Member Functions inherited from StringHashBase
void | setCaseSensitive (bool caseSensitive)
virtual void | SetSize (int newsize)=0

## Static Public Member Functions

static void * | CreateHash ()

## Protected Attributes

String ** | strings
void ** | objects
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 65 of file StringHash.h.

## Constructor & Destructor Documentation

## StringHash()

StringHash::StringHash | ( | int | startsize = `32` | ) |

Definition at line 22 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("StringHash: Hash table size must be a power of two.\n");
strings = new String * [size];
objects = new void * [size];
keys = new unsigned int [size];
for (unsigned int i = 0; i < size; i++)
{
strings[i] = NULL;
objects[i] = NULL;
}
};
StringHashBase
Definition: StringHash.h:27
String
Definition: StringBasics.h:39

## ~StringHash()

StringHash::~StringHash | ( |  | ) |

virtual

Definition at line 44 of file StringHash.cpp.

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

## Member Function Documentation

## Add()

int StringHash::Add | ( | const String & | s,
|  | void * | object = `NULL`
| ) |  |

Definition at line 112 of file StringHash.cpp.

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

## Capacity()

int StringHash::Capacity | ( |  | ) | const

inline

Definition at line 91 of file StringHash.h.

{
return size;
}

## Clear()

void StringHash::Clear | ( |  | ) |

Definition at line 55 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);
}

## CreateHash()

void * StringHash::CreateHash | ( |  | ) |

static

Definition at line 164 of file StringHash.cpp.

{
return (void *) new StringHash();
}
StringHash
Definition: StringHash.h:66

## Delete() [1/2]

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

inline

Definition at line 145 of file StringHash.h.

{
Delete(Find(key));
}

## Delete() [2/2]

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

Definition at line 169 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];
objects[h] = objects[index];
strings[index] = NULL;
objects[index] = NULL;
}
}
index = (index + 1) & mask;
}
}
}

## Entries()

int StringHash::Entries | ( |  | ) | const

inline

Definition at line 95 of file StringHash.h.

{
return count;
}

## Find() [1/2]

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

Definition at line 154 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 StringHash::Find | ( | const String & | s,
|  | void *(*)() | create_object = `NULL`
| ) |  |

Definition at line 131 of file StringHash.cpp.

{
unsigned int key = getKey(string);
unsigned int h = Iterate(key, string);
if (strings[h] == NULL && create_object == NULL)
return -1;
if (strings[h] == NULL && create_object != NULL)
{
Insert(h, key, string);
objects[h] = create_object();
if (count * 2 > size)
{
Grow();
return Iterate(key, string);
}
}
return h;
}

## Grow()

void StringHash::Grow | ( |  | ) |

inline

Definition at line 78 of file StringHash.h.

{
SetSize(size * 2);
}

## Object() [1/3]

void * StringHash::Object | ( | const String & | key | ) | const

inline

Definition at line 104 of file StringHash.h.

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

## Object() [2/3]

void * StringHash::Object | ( | const String & | key,
|  | void *(*)() | create_object
| ) |  |

inline

Definition at line 110 of file StringHash.h.

{
int index = Find(key, create_object);
return objects[index];
}

## Object() [3/3]

void * StringHash::Object | ( | int | i | ) | const

inline

Definition at line 100 of file StringHash.h.

{
return objects[i];
}

## operator=()

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

Definition at line 652 of file StringHash.cpp.

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

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

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

inline

Definition at line 136 of file StringHash.h.

{
return *(strings[i]);
}

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

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

inline

Definition at line 132 of file StringHash.h.

{
return *(strings[i]);
}

## Print() [1/3]

void StringHash::Print | ( |  | ) |

Definition at line 564 of file StringHash.cpp.

{
Print(stdout);
}

## Print() [2/3]

void StringHash::Print | ( | const char * | filename | ) |

Definition at line 569 of file StringHash.cpp.

{
FILE * output = fopen(filename, "wt");
if (output == NULL)
return;
Print(output);
fclose(output);
}

## Print() [3/3]

void StringHash::Print | ( | FILE * | file | ) |

Definition at line 578 of file StringHash.cpp.

{
for (unsigned int i = 0; i < size; i++)
if (SlotInUse(i))
strings[i]->WriteLine(output);
}

## ReadLinesFromFile() [1/3]

void StringHash::ReadLinesFromFile | ( | const char * | filename | ) |

Definition at line 207 of file StringHash.cpp.

{
IFILE f = ifopen(filename, "rb");
if (f == NULL) return;
ReadLinesFromFile(f);
ifclose(f);
}
ifopen
IFILE ifopen(const char *filename, const char *mode, InputFile::ifileCompression compressionMode=InputFile::DEFAULT)
Open a file with the specified name and mode, using a filename of "-" to indicate stdin/stdout.
Definition: InputFile.h:562
ifclose
int ifclose(IFILE &file)
Close the file.
Definition: InputFile.h:580
InputFile
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition: InputFile.h:37

## ReadLinesFromFile() [2/3]

void StringHash::ReadLinesFromFile | ( | FILE * | file | ) |

Definition at line 215 of file StringHash.cpp.

{
String buffer;
while (!feof(f))
{
buffer.ReadLine(f);
Add(buffer.Trim());
}
}

## ReadLinesFromFile() [3/3]

void StringHash::ReadLinesFromFile | ( | IFILE & | file | ) |

Definition at line 226 of file StringHash.cpp.

{
String buffer;
while (!ifeof(f))
{
buffer.ReadLine(f);
Add(buffer.Trim());
}
}
ifeof
int ifeof(IFILE file)
Check to see if we have reached the EOF (returns 0 if not EOF).
Definition: InputFile.h:654

## SetObject() [1/2]

void StringHash::SetObject | ( | const String & | key,
|  | void * | object
| ) |  |

inline

Definition at line 121 of file StringHash.h.

{
Add(key, object);
}

## SetObject() [2/2]

void StringHash::SetObject | ( | int | i,
|  | void * | object
| ) |  |

inline

Definition at line 117 of file StringHash.h.

{
objects[i] = object;
}

## SetSize()

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

virtual

Implements StringHashBase.

Definition at line 70 of file StringHash.cpp.

{
int newmask = newsize - 1;
String ** newstrings = new String * [newsize];
void ** newobjects = new void * [newsize];
unsigned int * newkeys = new unsigned int [newsize];
for (int i = 0; i < newsize; i++)
{
newstrings[i] = NULL;
newobjects[i] = NULL;
}
if (count)
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];
newobjects[h] = objects[i];
}
if(strings) delete [] strings;
if(objects) delete [] objects;
if(keys) delete [] keys;
strings = newstrings;
objects = newobjects;
keys = newkeys;
size = newsize;
mask = newmask;
}

## Shrink()

void StringHash::Shrink | ( |  | ) |

inline

Definition at line 82 of file StringHash.h.

{
SetSize(size / 2);
}

## SlotInUse()

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

inline

Definition at line 150 of file StringHash.h.

{
return strings[index] != NULL;
}

## StringList()

String StringHash::StringList | ( | char | separator = `','` | ) |

Definition at line 585 of file StringHash.cpp.

{
String list;
for (unsigned int i = 0; i < size; i++)
if (SlotInUse(i))
list += *strings[i] + separator;
list.SetLength(list.Length() - 1);
return list;
}

## Swap()

void StringHash::Swap | ( | StringHash & | s | ) |

Definition at line 710 of file StringHash.cpp.

{
String ** tstrings = s.strings;
s.strings = strings;
strings = tstrings;
void ** tobjects = s.objects;
s.objects = objects;
objects = tobjects;
unsigned int * tkeys = s.keys;
s.keys = keys;
keys = tkeys;
unsigned int temp = s.count;
s.count = count;
count = temp;
temp = s.size;
s.size = size;
size = temp;
temp = s.mask;
s.mask = mask;
mask = temp;
}

## Member Data Documentation

## count

unsigned int StringHash::count

protected

Definition at line 71 of file StringHash.h.

## keys

unsigned int* StringHash::keys

protected

Definition at line 70 of file StringHash.h.

## mask

unsigned int StringHash::mask

protected

Definition at line 72 of file StringHash.h.

## objects

void** StringHash::objects

protected

Definition at line 69 of file StringHash.h.

## size

unsigned int StringHash::size

protected

Definition at line 71 of file StringHash.h.

## strings

String** StringHash::strings

protected

Definition at line 68 of file StringHash.h.

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

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