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

## Public Member Functions

| StringIntMap (int startsize=0)
void | Grow (int newsize)
void | Clear ()
int | Length () const
int | Integer (int i) const
int | Integer (const ::String &key) const
void | SetInteger (int i, int value)
void | SetInteger (const ::String &key, int value)
int | Add (const ::String &s, int i)
int | Find (const ::String &s, int defaultValue)
int | Find (const ::String &s) const
int | FindStem (const ::String &stem) const
StringIntMap & | operator= (const StringIntMap &rhs)
const ::String & | operator[] (int i) const
::String & | operator[] (int i)
::String & | String (int i)
int | IncrementCount (const ::String &key)
int | DecrementCount (const ::String &key)
int | GetCount (const ::String &key) const
int | GetCount (int index) const
void | Delete (int index)

## Static Public Member Functions

static void * | CreateMap ()

## Static Public Attributes

static int | alloc = 8

## Protected Attributes

::String ** | strings
int * | integers
int | count
int | size

## Detailed Description

Definition at line 92 of file StringMap.h.

## Constructor & Destructor Documentation

## StringIntMap()

StringIntMap::StringIntMap | ( | int | startsize = 0 | ) |

Definition at line 306 of file StringMap.cpp.

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

## ~StringIntMap()

StringIntMap::~StringIntMap | ( |  | ) |

virtual

Definition at line 314 of file StringMap.cpp.

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

## Member Function Documentation

## Add()

int StringIntMap::Add | ( | const ::String & | s,
|  | int | i )

Definition at line 352 of file StringMap.cpp.

{
if (count == 0)
{
Grow(1);
strings[0] = new ::String(key);
integers[0] = integer;
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)
{
integers[probe] = integer;
return probe;
}
if (test < 0)
right = probe - 1;
else
left = probe + 1;
}
int insertAt = left;
int test = key.SlowCompare(*(strings[insertAt]));
if (test == 0)
{
integers[insertAt] = integer;
return insertAt;
}
if (test > 0) insertAt++;
Grow(count + 1);
if (insertAt < count)
{
for (int i = count; i > insertAt; i--)
{
strings[i] = strings[i - 1];
integers[i] = integers[i - 1];
}
}
strings[insertAt] = new ::String(key);
integers[insertAt] = integer;
count++;
return insertAt;
}

## Clear()

void StringIntMap::Clear | ( |  | ) |

Definition at line 523 of file StringMap.cpp.

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

## DecrementCount()

int StringIntMap::DecrementCount | ( | const ::String & | key | ) |

Definition at line 547 of file StringMap.cpp.

{
int index = Find(key);
if (index != -1)
return --(integers[index]);
SetInteger(key, -1);
return -1;
}

## Delete()

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

Definition at line 558 of file StringMap.cpp.

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

## Find() [1/2]

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

Definition at line 460 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 StringIntMap::Find | ( | const ::String & | s,
|  | int | defaultValue )

Definition at line 411 of file StringMap.cpp.

{
if (!count)
return Add(s, defaultValue);
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 (test > 0)
position++;
Grow(count + 1);
if (position < count)
{
for (int i = count; i > position; i--)
{
strings[i] = strings[i - 1];
integers[i] = integers[i - 1];
}
}
strings[position] = new ::String(s);
integers[position] = defaultValue;
count++;
return position;
}

## FindStem()

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

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

## GetCount() [1/2]

int StringIntMap::GetCount | ( | const ::String & | key | ) | const

Definition at line 530 of file StringMap.cpp.

{
int index = Find(key);
return index == -1 ? 0 : integers[index];
}

## GetCount() [2/2]

int StringIntMap::GetCount | ( | int | index | ) | const

inline

Definition at line 156 of file StringMap.h.

{
return integers[index];
}

## Grow()

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

Definition at line 322 of file StringMap.cpp.

{
if (newsize >= size)
{
if ((newsize >> 1) >= size)
size = (newsize + alloc) / alloc * alloc;
else
{
size = alloc;
while (size <= newsize)
size *= 2;
}
::String ** newStrings = new ::String * [size];
int * newIntegers = new int [size];
for (int i = 0; i < count; i++)
{
newStrings[i] = strings[i];
newIntegers[i] = integers[i];
}
delete [] strings;
delete [] integers;
strings = newStrings;
integers = newIntegers;
}
}

## IncrementCount()

int StringIntMap::IncrementCount | ( | const ::String & | key | ) |

Definition at line 536 of file StringMap.cpp.

{
int index = Find(key);
if (index != -1)
return ++(integers[index]);
SetInteger(key, 1);
return 1;
}

## Integer() [1/2]

int StringIntMap::Integer | ( | const ::String & | key | ) | const

inline

Definition at line 116 of file StringMap.h.

{
int index = Find(key);
return (index >= 0) ? (int) integers[index] : -1;
}

## Integer() [2/2]

int StringIntMap::Integer | ( | int | i | ) | const

inline

Definition at line 112 of file StringMap.h.

{
return integers[i];
}

## Length()

int StringIntMap::Length | ( |  | ) | const

inline

Definition at line 107 of file StringMap.h.

{
return count;
}

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

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

inline

Definition at line 142 of file StringMap.h.

{
return *(strings[i]);
}

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

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

inline

Definition at line 138 of file StringMap.h.

{
return *(strings[i]);
}

## SetInteger() [1/2]

void StringIntMap::SetInteger | ( | const ::String & | key,
|  | int | value )

inline

Definition at line 126 of file StringMap.h.

{
Add(key, value);
}

## SetInteger() [2/2]

void StringIntMap::SetInteger | ( | int | i,
|  | int | value )

inline

Definition at line 122 of file StringMap.h.

{
integers[i] = value;
}

## String()

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

inline

Definition at line 146 of file StringMap.h.

{
return *(strings[i]);
}

## Member Data Documentation

## alloc

int StringIntMap::alloc = 8

static

Definition at line 100 of file StringMap.h.

## count

int StringIntMap::count

protected

Definition at line 97 of file StringMap.h.

## integers

int* StringIntMap::integers

protected

Definition at line 96 of file StringMap.h.

## size

int StringIntMap::size

protected

Definition at line 97 of file StringMap.h.

## strings

::String** StringIntMap::strings

protected

Definition at line 95 of file StringMap.h.

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

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