StringMap Class Reference
Collaboration diagram for StringMap:
[legend]

## Public Member Functions

| StringMap (int startsize=0)
void | Grow (int newsize)
void | Clear ()
int | Length () 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
int | FindStem (const ::String &stem) const
int | FindFirstStem (const ::String &stem) const
StringMap & | operator= (const StringMap &rhs)
const ::String & | operator[] (int i) const
::String & | operator[] (int i)
::String & | String (int i)
void | Delete (int index)

## Static Public Member Functions

static void * | CreateMap ()

## Static Public Attributes

static int | alloc = 8

## Protected Attributes

::String ** | strings
void ** | objects
int | count
int | size

## Detailed Description

Definition at line 23 of file StringMap.h.

## Constructor & Destructor Documentation

## StringMap()

StringMap::StringMap | ( | int | startsize = `0` | ) |

Definition at line 22 of file StringMap.cpp.

{
count = 0;
size = (startsize + alloc) / alloc * alloc;
strings = new ::String * [size];
objects = new void * [size];
};

## ~StringMap()

StringMap::~StringMap | ( |  | ) |

virtual

Definition at line 30 of file StringMap.cpp.

{
for (int i = 0; i < count; i++)
delete strings[i];
delete [] strings;
delete [] objects;
}

## Member Function Documentation

## Add()

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

Definition at line 70 of file StringMap.cpp.

{
if (count == 0)
{
Grow(1);
strings[0] = new ::String(key);
objects[0] = object;
return count++;
}
int left = 0;
int right = count - 1;
while (right > left)
{
int probe = (left + right) / 2;
int test = key.SlowCompare(*(strings[probe]));
if (test == 0)
{
objects[probe] = object;
return probe;
}
if (test < 0)
right = probe - 1;
else
left = probe + 1;
}
int insertAt = left;
int test = key.SlowCompare(*(strings[insertAt]));
if (test == 0)
{
objects[insertAt] = object;
return insertAt;
}
if (test > 0) insertAt++;
Grow(count + 1);
if (insertAt < count)
{
for (int i = count; i > insertAt; i--)
{
strings[i] = strings[i - 1];
objects[i] = objects[i - 1];
}
}
strings[insertAt] = new ::String(key);
objects[insertAt] = object;
count++;
return insertAt;
}

## Clear()

void StringMap::Clear | ( |  | ) |

Definition at line 281 of file StringMap.cpp.

{
for (int i = 0; i < count; i++)
delete strings[i];
count = 0;
}

## CreateMap()

void * StringMap::CreateMap | ( |  | ) |

static

Definition at line 276 of file StringMap.cpp.

{
return (void *) new StringMap();
}
StringMap
Definition: StringMap.h:24

## Delete()

void StringMap::Delete | ( | int | index | ) |

Definition at line 288 of file StringMap.cpp.

{
count--;
delete strings[index];
for (int i = index; i < count; i++)
{
strings[i] = strings[i+1];
objects[i] = objects[i+1];
}
}

## Find() [1/2]

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

Definition at line 181 of file StringMap.cpp.

{
if (!count) return -1;
int left = 0;
int right = count - 1;
while (right > left)
{
int probe = (left + right) / 2;
int test = s.SlowCompare(*(strings[probe]));
if (test == 0)
return probe;
if (test < 0)
right = probe - 1;
else
left = probe + 1;
}
int position = left;
int test = s.SlowCompare(*(strings[left]));
if (test == 0)
return position;
return -1;
}

## Find() [2/2]

int StringMap::Find | ( | const ::String & | s,
|  | void *(*)() | create_object = `NULL`
| ) |  |

Definition at line 129 of file StringMap.cpp.

{
if (!count)
return create_object == NULL ? -1 : Add(s, create_object());
int left = 0;
int right = count - 1;
while (right > left)
{
int probe = (left + right) / 2;
int test = s.SlowCompare(*(strings[probe]));
if (test == 0)
return probe;
if (test < 0)
right = probe - 1;
else
left = probe + 1;
}
int position = left;
int test = s.SlowCompare(*(strings[left]));
if (test == 0)
return position;
if (create_object == NULL)
return -1;
if (test > 0)
position++;
Grow(count + 1);
if (position < count)
{
for (int i = count; i > position; i--)
{
strings[i] = strings[i - 1];
objects[i] = objects[i - 1];
}
}
strings[position] = new ::String(s);
objects[position] = create_object();
count++;
return position;
}

## FindFirstStem()

int StringMap::FindFirstStem | ( | const ::String & | stem | ) | const

Definition at line 244 of file StringMap.cpp.

{
if (!count) return -1;
int left = 0;
int right = count - 1;
while (right > left)
{
int probe = (left + right) / 2;
int test = strings[probe]->SlowCompareToStem(stem);
if (test == 0)
{
while (left < probe && strings[probe-1]->SlowCompareToStem(stem) == 0)
probe--;
return probe;
}
if (test > 0)
right = probe - 1;
else
left = probe + 1;
}
if (strings[left]->SlowCompareToStem(stem) == 0)
return left;
return -1;
}

## FindStem()

int StringMap::FindStem | ( | const ::String & | stem | ) | const

Definition at line 211 of file StringMap.cpp.

{
if (!count) return -1;
int left = 0;
int right = count - 1;
while (right > left)
{
int probe = (left + right) / 2;
int test = strings[probe]->SlowCompareToStem(stem);
if (test == 0)
{
if ((left < probe && strings[probe-1]->SlowCompareToStem(stem) == 0) ||
(right > probe && strings[probe+1]->SlowCompareToStem(stem) == 0))
return -2;
return probe;
}
if (test > 0)
right = probe - 1;
else
left = probe + 1;
}
if (strings[left]->SlowCompareToStem(stem) == 0)
return left;
return -1;
}

## Grow()

void StringMap::Grow | ( | int | newsize | ) |

Definition at line 38 of file StringMap.cpp.

{
if (newsize >= size)
{
if ((newsize >> 1) >= size)
size = (newsize + alloc) / alloc * alloc;
else
{
size = alloc;
while (size <= newsize)
size *= 2;
}
size = (newsize + alloc) / alloc * alloc;
::String ** newStrings = new ::String * [size];
void ** newObjects = new void * [size];
for (int i = 0; i < count; i++)
{
newStrings[i] = strings[i];
newObjects[i] = objects[i];
}
delete [] strings;
delete [] objects;
strings = newStrings;
objects = newObjects;
}
}
String
Definition: StringBasics.h:39

## Length()

int StringMap::Length | ( |  | ) | const

inline

Definition at line 38 of file StringMap.h.

{
return count;
}

## Object() [1/3]

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

inline

Definition at line 47 of file StringMap.h.

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

## Object() [2/3]

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

inline

Definition at line 52 of file StringMap.h.

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

## Object() [3/3]

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

inline

Definition at line 43 of file StringMap.h.

{
return objects[i];
}

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

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

inline

Definition at line 78 of file StringMap.h.

{
return *(strings[i]);
}

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

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

inline

Definition at line 74 of file StringMap.h.

{
return *(strings[i]);
}

## SetObject() [1/2]

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

inline

Definition at line 61 of file StringMap.h.

{
Add(key, object);
}

## SetObject() [2/2]

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

inline

Definition at line 57 of file StringMap.h.

{
objects[i] = object;
}

## String()

::String & StringMap::String | ( | int | i | ) |

inline

Definition at line 82 of file StringMap.h.

{
return *(strings[i]);
}

## Member Data Documentation

## alloc

int StringMap::alloc = 8

static

Definition at line 31 of file StringMap.h.

## count

int StringMap::count

protected

Definition at line 28 of file StringMap.h.

## objects

void** StringMap::objects

protected

Definition at line 27 of file StringMap.h.

## size

int StringMap::size

protected

Definition at line 28 of file StringMap.h.

## strings

::String** StringMap::strings

protected

Definition at line 26 of file StringMap.h.

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

- general/StringMap.h
- general/StringMap.cpp