Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
mystring.h
00001 /**************************************************************************
00002  * Copyright (c) 2002-2011 T. M. Murali                                   *
00003  *                                                                        *
00004  * This file is part of Biorithm.                                         *
00005  *                                                                        *
00006  * Biorithm is free software: you can redistribute it and/or modify       *
00007  * it under the terms of the GNU General Public License as published by   *
00008  * the Free Software Foundation, either version 3 of the License, or      *
00009  * (at your option) any later version.                                    *
00010  *                                                                        *
00011  * Biorithm is distributed in the hope that it will be useful,            *
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of         *
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
00014  * GNU General Public License for more details.                           *
00015  *                                                                        *
00016  * You should have received a copy of the GNU General Public License      *
00017  * along with Biorithm.  If not, see <http://www.gnu.org/licenses/>.      *
00018  *                                                                        *
00019  **************************************************************************/
00020 
00021 // Purpose: to define a string that has the readline() function like
00022 // LEDA's string (it has read_line().
00023 
00024 #ifndef _MYSTRING_H
00025 #define _MYSTRING_H
00026 
00027 // for isspace.
00028 #include <ctype.h>
00029 #include <iostream>
00030 #include <string>
00031 
00032 #include "constants.h"
00033 
00034 // Test for GCC >= 4.3. from http://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html. See definition of GCC_VERSION macro in libutil/constants.h
00035 #if GCC_VERSION >= 40300
00036 // see http://gcc.gnu.org/gcc-4.3/changes.html
00037 #include <tr1/unordered_set>
00038 #else
00039 #include <ext/hash_set>
00040 using namespace __gnu_cxx;
00041 // rename hash* to unordered*, so that I can consistently use unordered* in my code.
00042 // template < class T >
00043 // typedef hash_set< T > unordered_set;
00044 //typedef hash_map unordered_map;
00045 #define unordered_set hash_set;
00046 #define unordered_map hash_map;
00047 #endif
00048 
00049 using namespace std;
00050 
00051 struct eqstr
00052 {
00053   bool operator()(const string& s1, const string& s2) const
00054   {
00055     return(s1 == s2);
00056   }
00057 };
00058 
00059 struct ltstr
00060 {
00061   bool operator()(const string& s1, const string& s2) const
00062   {
00063     return(s1 < s2);
00064   }
00065 };
00066 
00067 class MyString : public string
00068 {
00069 public:
00070   MyString()
00071       : string()
00072     {}
00073   MyString(const char *s)
00074       : string(s)
00075     {}
00076   MyString(const string& s)
00077       : string(s)
00078     {}
00079   MyString(const MyString &s)
00080       : string(s)
00081     {}
00082   
00083   virtual ~MyString()
00084     {}
00085   
00086 
00087   MyString& operator=(const MyString& str)
00088     {
00089       if (&str != this)
00090         string::operator=(str);
00091       return *this;
00092     }
00093   MyString& operator=(const string& str)
00094     {
00095       if (&str != this)
00096         string::operator=(str);
00097       return *this;
00098     }
00099 
00100   
00101   void read(istream& s, char delim)
00102     {
00103       char buf[1024];
00104       char* q = buf+1023;
00105       bool go = true;
00106 
00107       string::operator=(""); // clear string
00108 
00109       while (s && go)
00110         {
00111           char* p;
00112           for(p = buf; p < q && s.get(*p); p++)
00113             {
00114               if (*p == delim || (delim == 0) && isspace(*p)) 
00115                 {
00116                   if (delim != '\n') s.putback(*p);
00117                   go = false;
00118                   break;
00119                 }
00120             } 
00121           *p = '\0';
00122           operator+=(buf);
00123         } 
00124     }
00125 
00126   void read(char delim = ' ')
00127     {
00128       read(cin,delim);
00129     }
00130 
00131   void read_line(istream& I)
00132     {
00133       read(I,'\n');
00134     }
00135 
00136   void read_line()
00137     {
00138       read_line(cin);
00139     }
00140 
00141   void read_file(istream& I)
00142     {
00143       read(I,(char)EOF);
00144     }
00145 
00146   void read_file()
00147     {
00148       read_file(cin);
00149     }
00150 
00151   // comparison operators
00152   friend bool operator==(const MyString &x, const MyString &y)
00153     {
00154       // don't call operator== because that causes inf. loop!
00155       return (0 == x.compare(y));
00156     }
00157 };
00158 
00159 // have to specialise template in the same namespace as its definition.
00160 #ifdef __GNUC__
00161 #if __GNUC__ < 3
00162   namespace Sgi { using ::hash_map; }; // inherit globals
00163 #else
00164 #if __GNUC_MINOR__ == 0
00165   namespace Sgi = std;               // GCC 3.0
00166 #else
00167 namespace Sgi = ::__gnu_cxx;       // GCC 3.1 and later
00168 #endif
00169 #endif
00170 #else      // ...  there are other compilers, right?
00171 namespace Sgi = std;
00172 #endif
00173 
00174 #if (GCC_VERSION < 40300)
00175 // i need to specialise some hash templates.
00176 namespace __gnu_cxx
00177 // namespace Sgi
00178  {
00179 // hash functions.
00180 template <> struct
00181 hash< string >
00182 {
00183   // DON'T use string::data()! that function sometimes returns a longer string.
00184   size_t operator()(const string& s) const { return __stl_hash_string(s.c_str()); }
00185 };
00186 
00187 template <> struct
00188 hash< MyString >
00189 {
00190   size_t operator()(const MyString& s) const { return __stl_hash_string(s.data()); }
00191 };
00192  }
00193 #endif // GCC_VERSION
00194 
00195 
00196 #endif // _MYSTRING_H 
 All Classes Functions Variables Typedefs Friends