libStatGen Software  1
StringArray.h
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __STRING_ARRAY_H__
19 #define __STRING_ARRAY_H__
20 
21 #include "StringBasics.h"
22 
24 {
25 protected:
26  String ** strings;
27  int size, count;
28 
29 public:
30  static int alloc;
31  static bool lazyMemoryManagement;
32 
33  StringArray(int startsize = 0);
34  StringArray(StringArray & original);
35  virtual ~StringArray();
36 
37  // Each line in a file is parsed into a separate array element
38  //
39 
40  void Read(FILE * f);
41  void Write(FILE * f);
42  void WriteLine(FILE * f);
43  void Read(const char * filename);
44  void Write(const char * filename);
45  void WriteLine(const char * filename);
46 
47  void Read(IFILE & f);
48 
49  // Write all strings to the screen
50  void Print();
51  void PrintLine();
52 
53  // Write all strings to a file
54  void Print(FILE * f);
55  void PrintLine(FILE * f);
56 
57  void Grow(int newsize);
58  void Clear();
59 
60  int Length() const
61  {
62  return count;
63  }
64  int Dimension(int newcount);
65  int CharLength();
66 
67  String & operator [](int i)
68  {
69  return *(strings[i]);
70  }
71  const String & operator [](int i) const
72  {
73  return *(strings[i]);
74  }
75 
76  // These functions divide a string into tokens and append these to the
77  // array. Return value is the new array length
78  //
79 
80  int AddColumns(const String & s, char ch = '\t');
81  int AddColumns(const String & s, char ch, int maxColumns);
82  int AddTokens(const String & s, char ch);
83  int AddTokens(const String & s, const String & separators = " \t\r\n");
84 
85  int ReplaceColumns(const String & s, char ch = '\t')
86  {
87  Clear();
88  return AddColumns(s, ch);
89  }
90  int ReplaceTokens(const String & s, const String & separators = " \t\r\n")
91  {
92  Clear();
93  return AddTokens(s, separators);
94  }
95 
96  // These functions add, insert or remove a single array element
97  //
98 
99  int Add(const String & s);
100  void InsertAt(int position, const String & s);
101  void Delete(int position);
102 
103  // These functions manipulate a string as a stack
104  //
105 
106  String & Last() const;
107  int Push(const String & s)
108  {
109  return Add(s);
110  }
111  String Pop();
112 
113  // Linear search (N/2 comparisons on average) for a single element
114  // If searching is required, StringMaps are a better option
115  //
116 
117  int Find(const String & s) const;
118  int FastFind(const String & s) const;
119  int SlowFind(const String & s) const;
120 
121  // Alphetically orders strings
122  //
123  void Sort();
124 
125  // Trims strings to remove whitespace
126  void Trim();
127 
128  StringArray & operator = (const StringArray & rhs);
129 
130  bool operator == (const StringArray & rhs) const;
131  bool operator != (const StringArray & rhs) const
132  {
133  return !(*this == rhs);
134  }
135 
136  void Swap(StringArray & s);
137 
138 private:
139  static int ComparisonForSort(const void * a, const void * b);
140 };
141 
142 #endif
143 
String
Definition: StringBasics.h:39
InputFile
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition: InputFile.h:37
StringArray
Definition: StringArray.h:24