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