StringArray.h

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 
00032     StringArray(int startsize = 0);
00033     StringArray(StringArray & original);
00034     virtual ~StringArray();
00035 
00036     // Each line in a file is parsed into a separate array element
00037     //
00038 
00039     void Read(FILE * f);
00040     void Write(FILE * f);
00041     void WriteLine(FILE * f);
00042     void Read(const char * filename);
00043     void Write(const char * filename);
00044     void WriteLine(const char * filename);
00045 
00046 #ifdef __ZLIB_AVAILABLE__
00047     void Read(IFILE & f);
00048 #endif
00049 
00050     // Write all strings to the screen
00051     void Print();
00052     void PrintLine();
00053 
00054     void Grow(int newsize);
00055     void Clear();
00056 
00057     int Length() const
00058     {
00059         return count;
00060     }
00061     int Dimension(int newcount);
00062     int CharLength();
00063 
00064     String & operator [](int i)
00065     {
00066         return *(strings[i]);
00067     }
00068     const String & operator [](int i) const
00069     {
00070         return *(strings[i]);
00071     }
00072 
00073     // These functions divide a string into tokens and append these to the
00074     // array. Return value is the new array length
00075     //
00076 
00077     int AddColumns(const String & s, char ch = '\t');
00078     int AddTokens(const String & s, char ch);
00079     int AddTokens(const String & s, const String & separators = " \t\r\n");
00080 
00081     int ReplaceColumns(const String & s, char ch = '\t')
00082     {
00083         Clear();
00084         return AddColumns(s, ch);
00085     }
00086     int ReplaceTokens(const String & s, const String & separators = " \t\r\n")
00087     {
00088         Clear();
00089         return AddTokens(s, separators);
00090     }
00091 
00092     // These functions add, insert or remove a single array element
00093     //
00094 
00095     int  Add(const String & s);
00096     void InsertAt(int position, const String & s);
00097     void Delete(int position);
00098 
00099     // These functions manipulate a string as a stack
00100     //
00101 
00102     String & Last() const;
00103     int      Push(const String & s)
00104     {
00105         return Add(s);
00106     }
00107     String   Pop();
00108 
00109     // Linear search (N/2 comparisons on average) for a single element
00110     // If searching is required, StringMaps are a better option
00111     //
00112 
00113     int Find(const String & s) const;
00114     int FastFind(const String & s) const;
00115     int SlowFind(const String & s) const;
00116 
00117     // Alphetically orders strings
00118     //
00119     void Sort();
00120 
00121     // Trims strings to remove whitespace
00122     void Trim();
00123 
00124     StringArray & operator = (const StringArray & rhs);
00125 
00126     bool operator == (const StringArray & rhs);
00127     bool operator != (const StringArray & rhs)
00128     {
00129         return !(*this == rhs);
00130     }
00131 
00132     void Swap(StringArray & s);
00133 
00134 private:
00135     static int ComparisonForSort(const void * a, const void * b);
00136 };
00137 
00138 #endif
00139 
Generated on Wed Nov 17 15:38:29 2010 for StatGen Software by  doxygen 1.6.3