libStatGen Software
1
|
00001 /* 00002 * Copyright (C) 2010 Regents of the University of Michigan 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef __STRING_ARRAY_H__ 00019 #define __STRING_ARRAY_H__ 00020 00021 #include "StringBasics.h" 00022 00023 class StringArray 00024 { 00025 protected: 00026 String ** strings; 00027 int size, count; 00028 00029 public: 00030 static int alloc; 00031 static bool lazyMemoryManagement; 00032 00033 StringArray(int startsize = 0); 00034 StringArray(StringArray & original); 00035 virtual ~StringArray(); 00036 00037 // Each line in a file is parsed into a separate array element 00038 // 00039 00040 void Read(FILE * f); 00041 void Write(FILE * f); 00042 void WriteLine(FILE * f); 00043 void Read(const char * filename); 00044 void Write(const char * filename); 00045 void WriteLine(const char * filename); 00046 00047 void Read(IFILE & f); 00048 00049 // Write all strings to the screen 00050 void Print(); 00051 void PrintLine(); 00052 00053 // Write all strings to a file 00054 void Print(FILE * f); 00055 void PrintLine(FILE * f); 00056 00057 void Grow(int newsize); 00058 void Clear(); 00059 00060 int Length() const 00061 { 00062 return count; 00063 } 00064 int Dimension(int newcount); 00065 int CharLength(); 00066 00067 String & operator [](int i) 00068 { 00069 return *(strings[i]); 00070 } 00071 const String & operator [](int i) const 00072 { 00073 return *(strings[i]); 00074 } 00075 00076 // These functions divide a string into tokens and append these to the 00077 // array. Return value is the new array length 00078 // 00079 00080 int AddColumns(const String & s, char ch = '\t'); 00081 int AddColumns(const String & s, char ch, int maxColumns); 00082 int AddTokens(const String & s, char ch); 00083 int AddTokens(const String & s, const String & separators = " \t\r\n"); 00084 00085 int ReplaceColumns(const String & s, char ch = '\t') 00086 { 00087 Clear(); 00088 return AddColumns(s, ch); 00089 } 00090 int ReplaceTokens(const String & s, const String & separators = " \t\r\n") 00091 { 00092 Clear(); 00093 return AddTokens(s, separators); 00094 } 00095 00096 // These functions add, insert or remove a single array element 00097 // 00098 00099 int Add(const String & s); 00100 void InsertAt(int position, const String & s); 00101 void Delete(int position); 00102 00103 // These functions manipulate a string as a stack 00104 // 00105 00106 String & Last() const; 00107 int Push(const String & s) 00108 { 00109 return Add(s); 00110 } 00111 String Pop(); 00112 00113 // Linear search (N/2 comparisons on average) for a single element 00114 // If searching is required, StringMaps are a better option 00115 // 00116 00117 int Find(const String & s) const; 00118 int FastFind(const String & s) const; 00119 int SlowFind(const String & s) const; 00120 00121 // Alphetically orders strings 00122 // 00123 void Sort(); 00124 00125 // Trims strings to remove whitespace 00126 void Trim(); 00127 00128 StringArray & operator = (const StringArray & rhs); 00129 00130 bool operator == (const StringArray & rhs) const; 00131 bool operator != (const StringArray & rhs) const 00132 { 00133 return !(*this == rhs); 00134 } 00135 00136 void Swap(StringArray & s); 00137 00138 private: 00139 static int ComparisonForSort(const void * a, const void * b); 00140 }; 00141 00142 #endif 00143