libStatGen Software 1
StringArray Class Reference
Collaboration diagram for StringArray:

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 ()
 
Stringoperator[] (int i)
 
const Stringoperator[] (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)
 
StringLast () 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 ()
 
StringArrayoperator= (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.

29{
30 count = startsize;
31 size = (startsize + alloc) / alloc * alloc;
32 strings = new String * [size];
33 for (int i = 0; i < count; i++)
34 strings[i] = new String;
35 for (int i = count; i < size; i++)
36 strings[i] = NULL;
37};

◆ StringArray() [2/2]

StringArray::StringArray ( StringArray original)

Definition at line 39 of file StringArray.cpp.

40{
41 count = rhs.count;
42 size = (rhs.count + alloc) / alloc * alloc;
43 strings = new String * [size];
44
45 for (int i = 0; i < count; i++)
46 strings[i] = new String(rhs[i]);;
47 for (int i = count; i < size; i++)
48 strings[i] = NULL;
49}

◆ ~StringArray()

StringArray::~StringArray ( )
virtual

Definition at line 51 of file StringArray.cpp.

52{
53 for (int i = 0; i < size; i++)
54 if (strings[i] != NULL)
55 delete strings[i];
56 else
57 break;
58
59 delete [] strings;
60}

Member Function Documentation

◆ Add()

int StringArray::Add ( const String s)

Definition at line 327 of file StringArray.cpp.

328{
329 Grow(count + 1);
330 if (strings[count] == NULL)
331 {
332 strings[count] = new String(s);
333 }
334 else
335 {
336 *strings[count] = s;
337 }
338 return ++count;
339}

◆ AddColumns() [1/2]

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

Definition at line 199 of file StringArray.cpp.

200{
201 maxColumns += count;
202
203 if (s.Length() > 0)
204 for (int pos = 0; pos <= s.Length() && maxColumns != count; pos++)
205 {
206 int oldpos = pos;
207 pos = s.FindChar(ch, pos);
208 if (pos == -1) pos = s.Length();
209 Grow(count + 1);
210
211 if (strings[count] == NULL)
212 strings[count] = new String(pos - oldpos);
213 strings[count]->SetLength(pos - oldpos);
214 memcpy((char *) *strings[count++], ((const char *) s) + oldpos, pos - oldpos);
215 };
216
217 return count;
218}

◆ AddColumns() [2/2]

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

Definition at line 178 of file StringArray.cpp.

179{
180 if (s.Length() > 0)
181 for (int pos = 0; pos <= s.Length(); pos++)
182 {
183 int oldpos = pos;
184 pos = s.FindChar(ch, pos);
185 if (pos == -1) pos = s.Length();
186 Grow(count + 1);
187
188 if (strings[count] == NULL)
189 {
190 strings[count] = new String(pos - oldpos);
191 }
192 strings[count]->SetLength(pos - oldpos);
193 memcpy((char *) *strings[count++], ((const char *) s) + oldpos, pos - oldpos);
194 }
195
196 return count;
197}

◆ AddTokens() [1/2]

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

Definition at line 220 of file StringArray.cpp.

221{
222 for (int pos = 0; pos < s.Length(); pos++)
223 {
224 while (pos < s.Length() && s[pos] == ch) pos++;
225 int oldpos = pos;
226
227 while (pos < s.Length() && s[pos] != ch) pos++;
228
229 if (oldpos < s.Length())
230 {
231 Grow(count + 1);
232 if (strings[count] == NULL)
233 {
234 strings[count] = new String(pos - oldpos);
235 }
236 strings[count]->SetLength(pos - oldpos);
237 memcpy((char *) *strings[count++], (const char *) s + oldpos, pos - oldpos);
238 }
239 }
240
241 return count;
242}

◆ AddTokens() [2/2]

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

Definition at line 244 of file StringArray.cpp.

245{
246 for (int pos = 0; pos < s.Length(); pos++)
247 {
248 while (pos < s.Length() && separators.FindChar(s[pos]) != -1) pos++;
249 int oldpos = pos;
250
251 while (pos < s.Length() && separators.FindChar(s[pos]) == -1) pos++;
252
253 if (oldpos < s.Length())
254 {
255 Grow(count + 1);
256 if (strings[count] == NULL)
257 strings[count] = new String(pos - oldpos);
258 strings[count]->SetLength(pos - oldpos);
259 memcpy((char *) *strings[count++], ((const char *) s) + oldpos, pos - oldpos);
260 }
261 }
262
263 return count;
264}

◆ CharLength()

int StringArray::CharLength ( )

Definition at line 62 of file StringArray.cpp.

63{
64 int charlen = 0;
65 for (int i = 0; i < count; i++)
66 charlen += strings[i]->Length();
67 return charlen;
68}

◆ Clear()

void StringArray::Clear ( )

Definition at line 158 of file StringArray.cpp.

159{
160 if (!lazyMemoryManagement)
161 {
162 for (int i = 0; i < size; i++)
163 {
164 if (strings[i] != NULL)
165 {
166 delete strings[i];
167 strings[i] = NULL;
168 }
169 else
170 {
171 break;
172 }
173 }
174 }
175 count = 0;
176}

◆ Delete()

void StringArray::Delete ( int  position)

Definition at line 363 of file StringArray.cpp.

364{
365 String * oldString = strings[index];
366
367 count--;
368 for (; index < count; index++)
369 strings[index] = strings[index + 1];
370 strings[count] = oldString;
371}

◆ Dimension()

int StringArray::Dimension ( int  newcount)

Definition at line 266 of file StringArray.cpp.

267{
268 if (newcount > count)
269 {
270 Grow(newcount);
271 for (int i = count; i < newcount; i++)
272 {
273 if (strings[i] == NULL)
274 strings[i] = new String;
275 else
276 strings[i]->Clear();
277 }
278 count = newcount;
279 }
280 else if (newcount < count)
281 {
282 if (!lazyMemoryManagement)
283 {
284 for (int i = newcount; i < size; i++)
285 {
286 if (strings[i] != NULL)
287 {
288 delete strings[i];
289 strings[i] = NULL;
290 }
291 else
292 {
293 break;
294 }
295 }
296 }
297 count = newcount;
298 }
299
300 return count;
301}

◆ FastFind()

int StringArray::FastFind ( const String s) const

Definition at line 311 of file StringArray.cpp.

312{
313 for (int i = 0; i < count; i++)
314 if (strings[i]->FastCompare(s) == 0)
315 return i;
316 return -1;
317}

◆ Find()

int StringArray::Find ( const String s) const

Definition at line 303 of file StringArray.cpp.

304{
305 for (int i = 0; i < count; i++)
306 if (*(strings[i]) == s)
307 return i;
308 return -1;
309}

◆ Grow()

void StringArray::Grow ( int  newsize)

Definition at line 134 of file StringArray.cpp.

135{
136 if (newsize >= size)
137 {
138 int oldsize = size;
139
140 if ((newsize >> 1) >= size)
141 size = (newsize + alloc) / alloc * alloc;
142 else
143 {
144 size = alloc;
145 while (size <= newsize)
146 size *= 2;
147 }
148 String ** tmp = new String * [size];
149 for (int i = 0; i < oldsize; i++)
150 tmp[i] = strings[i];
151 for (int i = oldsize; i < size; i++)
152 tmp[i] = NULL;
153 delete [] strings;
154 strings = tmp;
155 }
156}

◆ InsertAt()

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

Definition at line 341 of file StringArray.cpp.

342{
343 Grow(count + 1);
344
345 String * newString = strings[count];
346 if (newString == NULL)
347 newString = new String(s);
348 else
349 *newString = s;
350
351 for (int i = count; i > position; i--)
352 strings[i] = strings[i - 1];
353 strings[position] = newString;
354 count++;
355}

◆ Last()

String & StringArray::Last ( ) const

Definition at line 357 of file StringArray.cpp.

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

◆ Length()

int StringArray::Length ( ) const
inline

Definition at line 60 of file StringArray.h.

61 {
62 return count;
63 }

◆ operator!=()

bool StringArray::operator!= ( const StringArray rhs) const
inline

Definition at line 131 of file StringArray.h.

132 {
133 return !(*this == rhs);
134 }

◆ operator=()

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

Definition at line 373 of file StringArray.cpp.

374{
375 Dimension(rhs.count);
376 for (int i = 0; i < rhs.count; i++)
377 *strings[i] = *rhs.strings[i];
378 return *this;
379}

◆ operator==()

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

Definition at line 381 of file StringArray.cpp.

382{
383 if (count != rhs.count) return false;
384 for (int i = 0; i < rhs.count; i++)
385 if (*strings[i] != *rhs.strings[i])
386 return false;
387 return true;
388}

◆ operator[]() [1/2]

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

Definition at line 67 of file StringArray.h.

68 {
69 return *(strings[i]);
70 }

◆ operator[]() [2/2]

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

Definition at line 71 of file StringArray.h.

72 {
73 return *(strings[i]);
74 }

◆ Pop()

String StringArray::Pop ( )

Definition at line 403 of file StringArray.cpp.

404{
405 String result = *(strings[count - 1]);
406
407 Dimension(count - 1);
408
409 return result;
410}

◆ Print() [1/2]

void StringArray::Print ( )

Definition at line 418 of file StringArray.cpp.

419{
420 Print(stdout);
421}

◆ Print() [2/2]

void StringArray::Print ( FILE *  f)

Definition at line 423 of file StringArray.cpp.

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

◆ PrintLine() [1/2]

void StringArray::PrintLine ( )

Definition at line 429 of file StringArray.cpp.

430{
431 PrintLine(stdout);
432}

◆ PrintLine() [2/2]

void StringArray::PrintLine ( FILE *  f)

Definition at line 434 of file StringArray.cpp.

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

◆ Push()

int StringArray::Push ( const String s)
inline

Definition at line 107 of file StringArray.h.

108 {
109 return Add(s);
110 }

◆ Read() [1/3]

void StringArray::Read ( const char *  filename)

Definition at line 70 of file StringArray.cpp.

71{
72 IFILE f = ifopen(filename, "rb");
73 if (f == NULL) return;
74 Read(f);
75 ifclose(f);
76}
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
int ifclose(IFILE &file)
Close the file.
Definition: InputFile.h:580
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition: InputFile.h:37

◆ Read() [2/3]

void StringArray::Read ( FILE *  f)

Definition at line 94 of file StringArray.cpp.

95{
96 while (!feof(f))
97 {
98 Grow(count + 1);
99 if (strings[count] == NULL)
100 strings[count] = new String;
101 strings[count]->ReadLine(f);
102 count++;
103 }
104}

◆ Read() [3/3]

void StringArray::Read ( IFILE f)

Definition at line 118 of file StringArray.cpp.

119{
120 while (!ifeof(f))
121 {
122 Grow(count + 1);
123 if (strings[count] == NULL)
124 strings[count] = new String;
125 strings[count]->ReadLine(f);
126 if (ifeof(f) && strings[count]->Length()==0)
127 {
128 return;
129 }
130 count++;
131 }
132}
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.

86 {
87 Clear();
88 return AddColumns(s, ch);
89 }

◆ ReplaceTokens()

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

Definition at line 90 of file StringArray.h.

91 {
92 Clear();
93 return AddTokens(s, separators);
94 }

◆ SlowFind()

int StringArray::SlowFind ( const String s) const

Definition at line 319 of file StringArray.cpp.

320{
321 for (int i = 0; i < count; i++)
322 if (strings[i]->SlowCompare(s) == 0)
323 return i;
324 return -1;
325}

◆ Sort()

void StringArray::Sort ( )

Definition at line 390 of file StringArray.cpp.

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

◆ Swap()

void StringArray::Swap ( StringArray s)

Definition at line 440 of file StringArray.cpp.

441{
442 String ** temp = s.strings;
443 s.strings = strings;
444 strings = temp;
445
446 int swap = s.size;
447 s.size = size;
448 size = swap;
449
450 swap = s.count;
451 s.count = count;
452 count = swap;
453}

◆ Trim()

void StringArray::Trim ( )

Definition at line 412 of file StringArray.cpp.

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

◆ Write() [1/2]

void StringArray::Write ( const char *  filename)

Definition at line 78 of file StringArray.cpp.

79{
80 FILE * f = fopen(filename, "wt");
81 if (f == NULL) return;
82 Write(f);
83 fclose(f);
84}

◆ Write() [2/2]

void StringArray::Write ( FILE *  f)

Definition at line 106 of file StringArray.cpp.

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

◆ WriteLine() [1/2]

void StringArray::WriteLine ( const char *  filename)

Definition at line 86 of file StringArray.cpp.

87{
88 FILE * f = fopen(filename, "wt");
89 if (f == NULL) return;
90 WriteLine(f);
91 fclose(f);
92}

◆ WriteLine() [2/2]

void StringArray::WriteLine ( FILE *  f)

Definition at line 112 of file StringArray.cpp.

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

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: