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

## Public Member Functions

| StringArray (int startsize=0)
| StringArray (StringArray &original)
void | Read (FILE *f)
void | Write (FILE *f)
void | WriteLine (FILE *f)
void | Read (const char *filename)
void | Write (const char *filename)
void | WriteLine (const char *filename)
void | Read (IFILE &f)
void | Print ()
void | PrintLine ()
void | Print (FILE *f)
void | PrintLine (FILE *f)
void | Grow (int newsize)
void | Clear ()
int | Length () const
int | Dimension (int newcount)
int | CharLength ()
String & | operator[] (int i)
const String & | operator[] (int i) const
int | AddColumns (const String &s, char ch='\t')
int | AddColumns (const String &s, char ch, int maxColumns)
int | AddTokens (const String &s, char ch)
int | AddTokens (const String &s, const String &separators=" \t\r\n")
int | ReplaceColumns (const String &s, char ch='\t')
int | ReplaceTokens (const String &s, const String &separators=" \t\r\n")
int | Add (const String &s)
void | InsertAt (int position, const String &s)
void | Delete (int position)
String & | Last () const
int | Push (const String &s)
String | Pop ()
int | Find (const String &s) const
int | FastFind (const String &s) const
int | SlowFind (const String &s) const
void | Sort ()
void | Trim ()
StringArray & | operator= (const StringArray &rhs)
bool | operator== (const StringArray &rhs) const
bool | operator!= (const StringArray &rhs) const
void | Swap (StringArray &s)

## Static Public Attributes

static int | alloc = 32
static bool | lazyMemoryManagement = false

## Protected Attributes

String ** | strings
int | size
int | count

## Detailed Description

Definition at line 23 of file StringArray.h.

## Constructor & Destructor Documentation

## StringArray() [1/2]

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

Definition at line 28 of file StringArray.cpp.

{
count = startsize;
size = (startsize + alloc) / alloc * alloc;
strings = new String * [size];
for (int i = 0; i < count; i++)
strings[i] = new String;
for (int i = count; i < size; i++)
strings[i] = NULL;
};

## StringArray() [2/2]

StringArray::StringArray | ( | StringArray & | original | ) |

Definition at line 39 of file StringArray.cpp.

{
count = rhs.count;
size = (rhs.count + alloc) / alloc * alloc;
strings = new String * [size];
for (int i = 0; i < count; i++)
strings[i] = new String(rhs[i]);;
for (int i = count; i < size; i++)
strings[i] = NULL;
}

## ~StringArray()

StringArray::~StringArray | ( |  | ) |

virtual

Definition at line 51 of file StringArray.cpp.

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

## Member Function Documentation

## Add()

int StringArray::Add | ( | const String & | s | ) |

Definition at line 327 of file StringArray.cpp.

{
Grow(count + 1);
if (strings[count] == NULL)
{
strings[count] = new String(s);
}
else
{
*strings[count] = s;
}
return ++count;
}

## AddColumns() [1/2]

int StringArray::AddColumns | ( | const String & | s,
|  | char | ch,
|  | int | maxColumns )

Definition at line 199 of file StringArray.cpp.

{
maxColumns += count;
if (s.Length() > 0)
for (int pos = 0; pos <= s.Length() && maxColumns != count; pos++)
{
int oldpos = pos;
pos = s.FindChar(ch, pos);
if (pos == -1) pos = s.Length();
Grow(count + 1);
if (strings[count] == NULL)
strings[count] = new String(pos - oldpos);
strings[count]->SetLength(pos - oldpos);
memcpy((char *) *strings[count++], ((const char *) s) + oldpos, pos - oldpos);
};
return count;
}

## AddColumns() [2/2]

int StringArray::AddColumns | ( | const String & | s,
|  | char | ch = '\t' )

Definition at line 178 of file StringArray.cpp.

{
if (s.Length() > 0)
for (int pos = 0; pos <= s.Length(); pos++)
{
int oldpos = pos;
pos = s.FindChar(ch, pos);
if (pos == -1) pos = s.Length();
Grow(count + 1);
if (strings[count] == NULL)
{
strings[count] = new String(pos - oldpos);
}
strings[count]->SetLength(pos - oldpos);
memcpy((char *) *strings[count++], ((const char *) s) + oldpos, pos - oldpos);
}
return count;
}

## AddTokens() [1/2]

int StringArray::AddTokens | ( | const String & | s,
|  | char | ch )

Definition at line 220 of file StringArray.cpp.

{
for (int pos = 0; pos < s.Length(); pos++)
{
while (pos < s.Length() && s[pos] == ch) pos++;
int oldpos = pos;
while (pos < s.Length() && s[pos] != ch) pos++;
if (oldpos < s.Length())
{
Grow(count + 1);
if (strings[count] == NULL)
{
strings[count] = new String(pos - oldpos);
}
strings[count]->SetLength(pos - oldpos);
memcpy((char *) *strings[count++], (const char *) s + oldpos, pos - oldpos);
}
}
return count;
}

## AddTokens() [2/2]

int StringArray::AddTokens | ( | const String & | s,
|  | const String & | separators = " \t\r\n" )

Definition at line 244 of file StringArray.cpp.

{
for (int pos = 0; pos < s.Length(); pos++)
{
while (pos < s.Length() && separators.FindChar(s[pos]) != -1) pos++;
int oldpos = pos;
while (pos < s.Length() && separators.FindChar(s[pos]) == -1) pos++;
if (oldpos < s.Length())
{
Grow(count + 1);
if (strings[count] == NULL)
strings[count] = new String(pos - oldpos);
strings[count]->SetLength(pos - oldpos);
memcpy((char *) *strings[count++], ((const char *) s) + oldpos, pos - oldpos);
}
}
return count;
}

## CharLength()

int StringArray::CharLength | ( |  | ) |

Definition at line 62 of file StringArray.cpp.

{
int charlen = 0;
for (int i = 0; i < count; i++)
charlen += strings[i]->Length();
return charlen;
}

## Clear()

void StringArray::Clear | ( |  | ) |

Definition at line 158 of file StringArray.cpp.

{
if (!lazyMemoryManagement)
{
for (int i = 0; i < size; i++)
{
if (strings[i] != NULL)
{
delete strings[i];
strings[i] = NULL;
}
else
{
break;
}
}
}
count = 0;
}

## Delete()

void StringArray::Delete | ( | int | position | ) |

Definition at line 363 of file StringArray.cpp.

{
String * oldString = strings[index];
count--;
for (; index < count; index++)
strings[index] = strings[index + 1];
strings[count] = oldString;
}

## Dimension()

int StringArray::Dimension | ( | int | newcount | ) |

Definition at line 266 of file StringArray.cpp.

{
if (newcount > count)
{
Grow(newcount);
for (int i = count; i < newcount; i++)
{
if (strings[i] == NULL)
strings[i] = new String;
else
strings[i]->Clear();
}
count = newcount;
}
else if (newcount < count)
{
if (!lazyMemoryManagement)
{
for (int i = newcount; i < size; i++)
{
if (strings[i] != NULL)
{
delete strings[i];
strings[i] = NULL;
}
else
{
break;
}
}
}
count = newcount;
}
return count;
}

## FastFind()

int StringArray::FastFind | ( | const String & | s | ) | const

Definition at line 311 of file StringArray.cpp.

{
for (int i = 0; i < count; i++)
if (strings[i]->FastCompare(s) == 0)
return i;
return -1;
}

## Find()

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

Definition at line 303 of file StringArray.cpp.

{
for (int i = 0; i < count; i++)
if (*(strings[i]) == s)
return i;
return -1;
}

## Grow()

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

Definition at line 134 of file StringArray.cpp.

{
if (newsize >= size)
{
int oldsize = size;
if ((newsize >> 1) >= size)
size = (newsize + alloc) / alloc * alloc;
else
{
size = alloc;
while (size <= newsize)
size *= 2;
}
String ** tmp = new String * [size];
for (int i = 0; i < oldsize; i++)
tmp[i] = strings[i];
for (int i = oldsize; i < size; i++)
tmp[i] = NULL;
delete [] strings;
strings = tmp;
}
}

## InsertAt()

void StringArray::InsertAt | ( | int | position,
|  | const String & | s )

Definition at line 341 of file StringArray.cpp.

{
Grow(count + 1);
String * newString = strings[count];
if (newString == NULL)
newString = new String(s);
else
*newString = s;
for (int i = count; i > position; i--)
strings[i] = strings[i - 1];
strings[position] = newString;
count++;
}

## Last()

String & StringArray::Last | ( |  | ) | const

Definition at line 357 of file StringArray.cpp.

{
if (!count) error("StringArray: Null String Access");
return *(strings[count - 1]);
}

## Length()

int StringArray::Length | ( |  | ) | const

inline

Definition at line 60 of file StringArray.h.

{
return count;
}

## operator!=()

bool StringArray::operator!= | ( | const StringArray & | rhs | ) | const

inline

Definition at line 131 of file StringArray.h.

{
return !(*this == rhs);
}

## operator=()

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

Definition at line 373 of file StringArray.cpp.

{
Dimension(rhs.count);
for (int i = 0; i < rhs.count; i++)
*strings[i] = *rhs.strings[i];
return *this;
}

## operator==()

bool StringArray::operator== | ( | const StringArray & | rhs | ) | const

Definition at line 381 of file StringArray.cpp.

{
if (count != rhs.count) return false;
for (int i = 0; i < rhs.count; i++)
if (*strings[i] != *rhs.strings[i])
return false;
return true;
}

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

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

inline

Definition at line 67 of file StringArray.h.

{
return *(strings[i]);
}

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

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

inline

Definition at line 71 of file StringArray.h.

{
return *(strings[i]);
}

## Pop()

String StringArray::Pop | ( |  | ) |

Definition at line 403 of file StringArray.cpp.

{
String result = *(strings[count - 1]);
Dimension(count - 1);
return result;
}

## Print() [1/2]

void StringArray::Print | ( |  | ) |

Definition at line 418 of file StringArray.cpp.

{
Print(stdout);
}

## Print() [2/2]

void StringArray::Print | ( | FILE * | f | ) |

Definition at line 423 of file StringArray.cpp.

{
for (int i = 0; i < count; i++)
fprintf(output, "%s\n", (const char *)(*strings[i]));
}

## PrintLine() [1/2]

void StringArray::PrintLine | ( |  | ) |

Definition at line 429 of file StringArray.cpp.

{
PrintLine(stdout);
}

## PrintLine() [2/2]

void StringArray::PrintLine | ( | FILE * | f | ) |

Definition at line 434 of file StringArray.cpp.

{
for (int i = 0; i < count; i++)
fprintf(output, "%s%c", (const char *)(*strings[i]), i == count - 1 ? '\n' : '\t');
}

## Push()

int StringArray::Push | ( | const String & | s | ) |

inline

Definition at line 107 of file StringArray.h.

{
return Add(s);
}

## Read() [1/3]

void StringArray::Read | ( | const char * | filename | ) |

Definition at line 70 of file StringArray.cpp.

{
IFILE f = ifopen(filename, "rb");
if (f == NULL) return;
Read(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
IFILE
InputFile * IFILE
Define IFILE as a pointer to an InputFile object.
Definition InputFile.h:551

## Read() [2/3]

void StringArray::Read | ( | FILE * | f | ) |

Definition at line 94 of file StringArray.cpp.

{
while (!feof(f))
{
Grow(count + 1);
if (strings[count] == NULL)
strings[count] = new String;
strings[count]->ReadLine(f);
count++;
}
}

## Read() [3/3]

void StringArray::Read | ( | IFILE & | f | ) |

Definition at line 118 of file StringArray.cpp.

{
while (!ifeof(f))
{
Grow(count + 1);
if (strings[count] == NULL)
strings[count] = new String;
strings[count]->ReadLine(f);
if (ifeof(f) && strings[count]->Length()==0)
{
return;
}
count++;
}
}
ifeof
int ifeof(IFILE file)
Check to see if we have reached the EOF (returns 0 if not EOF).
Definition InputFile.h:654

## ReplaceColumns()

int StringArray::ReplaceColumns | ( | const String & | s,
|  | char | ch = '\t' )

inline

Definition at line 85 of file StringArray.h.

{
Clear();
return AddColumns(s, ch);
}

## ReplaceTokens()

int StringArray::ReplaceTokens | ( | const String & | s,
|  | const String & | separators = " \t\r\n" )

inline

Definition at line 90 of file StringArray.h.

{
Clear();
return AddTokens(s, separators);
}

## SlowFind()

int StringArray::SlowFind | ( | const String & | s | ) | const

Definition at line 319 of file StringArray.cpp.

{
for (int i = 0; i < count; i++)
if (strings[i]->SlowCompare(s) == 0)
return i;
return -1;
}

## Sort()

void StringArray::Sort | ( |  | ) |

Definition at line 390 of file StringArray.cpp.

{
QuickSort(strings, count, sizeof(String *), ComparisonForSort);
}

## Swap()

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

Definition at line 440 of file StringArray.cpp.

{
String ** temp = s.strings;
s.strings = strings;
strings = temp;
int swap = s.size;
s.size = size;
size = swap;
swap = s.count;
s.count = count;
count = swap;
}

## Trim()

void StringArray::Trim | ( |  | ) |

Definition at line 412 of file StringArray.cpp.

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

## Write() [1/2]

void StringArray::Write | ( | const char * | filename | ) |

Definition at line 78 of file StringArray.cpp.

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

## Write() [2/2]

void StringArray::Write | ( | FILE * | f | ) |

Definition at line 106 of file StringArray.cpp.

{
for (int i = 0; i < count; i++)
strings[i]->WriteLine(f);
}

## WriteLine() [1/2]

void StringArray::WriteLine | ( | const char * | filename | ) |

Definition at line 86 of file StringArray.cpp.

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

## WriteLine() [2/2]

void StringArray::WriteLine | ( | FILE * | f | ) |

Definition at line 112 of file StringArray.cpp.

{
for (int i = 0; i < count; i++)
fprintf(f, "%s%c", (const char *)(*strings[i]), i == count-1 ? '\n' : '\t');
}

## Member Data Documentation

## alloc

int StringArray::alloc = 32

static

Definition at line 30 of file StringArray.h.

## count

int StringArray::count

protected

Definition at line 27 of file StringArray.h.

## lazyMemoryManagement

bool StringArray::lazyMemoryManagement = false

static

Definition at line 31 of file StringArray.h.

## size

int StringArray::size

protected

Definition at line 27 of file StringArray.h.

## strings

String** StringArray::strings

protected

Definition at line 26 of file StringArray.h.

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

- general/StringArray.h
- general/StringArray.cpp