StringHash.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 __STRINGHASH_H__
00019 #define __STRINGHASH_H__
00020 
00021 #include "StringBasics.h"
00022 #include "Constant.h"
00023 #include "Hash.h"
00024 
00025 class StringHash
00026 {
00027 protected:
00028     String        ** strings;
00029     void          ** objects;
00030     unsigned int      * keys;
00031     unsigned int count, size;
00032     unsigned int        mask;
00033 
00034 public:
00035     StringHash(int startsize = 32);
00036     virtual ~StringHash();
00037 
00038     void Grow()
00039     {
00040         SetSize(size * 2);
00041     }
00042     void Shrink()
00043     {
00044         SetSize(size / 2);
00045     }
00046 
00047     void SetSize(int newsize);
00048 
00049     void Clear();
00050 
00051     int  Capacity() const
00052     {
00053         return size;
00054     }
00055     int  Entries() const
00056     {
00057         return count;
00058     }
00059 
00060     void * Object(int i) const
00061     {
00062         return objects[i];
00063     }
00064     void * Object(const String & key) const
00065     {
00066         int index = Find(key);
00067 
00068         return index >= 0 ? objects[index] : NULL;
00069     }
00070     void * Object(const String & key, void *(*create_object)())
00071     {
00072         int index = Find(key, create_object);
00073 
00074         return objects[index];
00075     }
00076 
00077     void SetObject(int i, void * object)
00078     {
00079         objects[i] = object;
00080     }
00081     void SetObject(const String & key, void * object)
00082     {
00083         Add(key, object);
00084     }
00085 
00086     int Add(const String & s, void * object = NULL);
00087     int Find(const String & s, void *(*create_object)() = NULL);
00088     int Find(const String & s) const;
00089 
00090     StringHash & operator = (const StringHash & rhs);
00091 
00092     const String & operator [](int i) const
00093     {
00094         return *(strings[i]);
00095     }
00096     String & operator [](int i)
00097     {
00098         return *(strings[i]);
00099     }
00100 //      String & String(int i) { return *(strings[i]); }
00101 
00102     static void * CreateHash();
00103 
00104     void Delete(unsigned int index);
00105     void Delete(const String & key)
00106     {
00107         Delete(Find(key));
00108     }
00109 
00110     bool SlotInUse(int index) const
00111     {
00112         return strings[index] != NULL;
00113     }
00114 
00115     void Print();
00116     void Print(FILE * file);
00117     void Print(const char * filename);
00118 
00119     String StringList(char separator = ',');
00120 
00121     // Initialize hash with the contents of a file
00122     void ReadLinesFromFile(FILE * file);
00123     void ReadLinesFromFile(const char * filename);
00124 
00125 #ifdef __ZLIB_AVAILABLE__
00126     void ReadLinesFromFile(IFILE & file);
00127 #endif
00128 
00129     void Swap(StringHash & s);
00130 
00131 private:
00132 
00133     unsigned int Iterate(unsigned int key, const String & string) const
00134     {
00135         unsigned int h = key & mask;
00136 
00137         while (strings[h] != NULL &&
00138                 (keys[h] != key ||
00139                  strings[h]->SlowCompare(string) != 0))
00140             h = (h + 1) & mask;
00141 
00142         return h;
00143     }
00144 
00145     void Insert(unsigned int where, unsigned int key, const String & string)
00146     {
00147         strings[where] = new String;
00148         *(strings[where]) = string;
00149         keys[where] = key;
00150 
00151         count++;
00152     }
00153 };
00154 
00155 class StringIntHash
00156 {
00157 protected:
00158     String        ** strings;
00159     int           * integers;
00160     unsigned int      * keys;
00161     unsigned int count, size;
00162     unsigned int        mask;
00163 
00164 public:
00165     StringIntHash(int startsize = 32);
00166     virtual ~StringIntHash();
00167 
00168     void Grow()
00169     {
00170         SetSize(size * 2);
00171     }
00172     void Shrink()
00173     {
00174         SetSize(size / 2);
00175     }
00176 
00177     void SetSize(int newsize);
00178 
00179     void Clear();
00180 
00181     int  Capacity() const
00182     {
00183         return size;
00184     }
00185     int  Entries() const
00186     {
00187         return count;
00188     }
00189 
00190     int Integer(int i) const
00191     {
00192         return integers[i];
00193     }
00194     int Integer(const String & key) const
00195     {
00196         int index = Find(key);
00197 
00198         return index >= 0 ? integers[index] : -1;
00199     }
00200 
00201     void SetInteger(int i, int value)
00202     {
00203         integers[i] = value;
00204     }
00205     void SetInteger(const String & key, int value)
00206     {
00207         Add(key, value);
00208     }
00209 
00210     int IncrementCount(const String & key);
00211     int IncrementCount(const String & key, int amount);
00212     int DecrementCount(const String & key);
00213     int GetCount(const String & key) const;
00214     int GetCount(int index) const
00215     {
00216         return integers[index];
00217     }
00218 
00219     int Add(const String & s, int integer);
00220     int Find(const String & s, int defaultValue);
00221     int Find(const String & s) const;
00222 
00223     StringIntHash & operator = (const StringIntHash & rhs);
00224 
00225     const String & operator [](int i) const
00226     {
00227         return *(strings[i]);
00228     }
00229     String & operator [](int i)
00230     {
00231         return *(strings[i]);
00232     }
00233 //      String & String(int i) { return *(strings[i]); }
00234 
00235     void Delete(unsigned int index);
00236     void Delete(const String & key)
00237     {
00238         Delete(Find(key));
00239     }
00240 
00241     bool SlotInUse(int index) const
00242     {
00243         return strings[index] != NULL;
00244     }
00245 
00246 private:
00247 
00248     unsigned int Iterate(unsigned int key, const String & string) const
00249     {
00250         unsigned int h = key & mask;
00251 
00252         while (strings[h] != NULL &&
00253                 (keys[h] != key ||
00254                  strings[h]->SlowCompare(string) != 0))
00255             h = (h + 1) & mask;
00256 
00257         return h;
00258     }
00259 
00260     void Insert(unsigned int where, unsigned int key, const String & string)
00261     {
00262         strings[where] = new String;
00263         *(strings[where]) = string;
00264         keys[where] = key;
00265 
00266         count++;
00267     }
00268 };
00269 
00270 class StringDoubleHash
00271 {
00272 protected:
00273     String        ** strings;
00274     double         * doubles;
00275     unsigned int      * keys;
00276     unsigned int count, size;
00277     unsigned int        mask;
00278 
00279 public:
00280     StringDoubleHash(int startsize = 32);
00281     virtual ~StringDoubleHash();
00282 
00283     void Grow()
00284     {
00285         SetSize(size * 2);
00286     }
00287     void Shrink()
00288     {
00289         SetSize(size / 2);
00290     }
00291 
00292     void SetSize(int newsize);
00293 
00294     void Clear();
00295 
00296     int  Capacity() const
00297     {
00298         return size;
00299     }
00300     int  Entries() const
00301     {
00302         return count;
00303     }
00304 
00305     double Double(int i) const
00306     {
00307         return doubles[i];
00308     }
00309     double Double(const String & key) const
00310     {
00311         int index = Find(key);
00312 
00313         return index >= 0 ? doubles[index] : _NAN_;
00314     }
00315 
00316     void SetDouble(int i, double value)
00317     {
00318         doubles[i] = value;
00319     }
00320     void SetDouble(const String & key, double value)
00321     {
00322         Add(key, value);
00323     }
00324 
00325     int Add(const String & s, double value);
00326     int Find(const String & s, double defaultValue);
00327     int Find(const String & s) const;
00328 
00329     StringDoubleHash & operator = (const StringDoubleHash & rhs);
00330 
00331     const String & operator [](int i) const
00332     {
00333         return *(strings[i]);
00334     }
00335     String & operator [](int i)
00336     {
00337         return *(strings[i]);
00338     }
00339 //      String & String(int i) { return *(strings[i]); }
00340 
00341     void Delete(unsigned int index);
00342     void Delete(const String & key)
00343     {
00344         Delete(Find(key));
00345     }
00346 
00347     bool SlotInUse(int index) const
00348     {
00349         return strings[index] != NULL;
00350     }
00351 
00352 private:
00353 
00354     unsigned int Iterate(unsigned int key, const String & string) const
00355     {
00356         unsigned int h = key & mask;
00357 
00358         while (strings[h] != NULL &&
00359                 (keys[h] != key ||
00360                  strings[h]->SlowCompare(string) != 0))
00361             h = (h + 1) & mask;
00362 
00363         return h;
00364     }
00365 
00366     void Insert(unsigned int where, unsigned int key, const String & string)
00367     {
00368         strings[where] = new String;
00369         *(strings[where]) = string;
00370         keys[where] = key;
00371 
00372         count++;
00373     }
00374 };
00375 
00376 #endif
Generated on Tue Aug 23 18:19:05 2011 for libStatGen Software by  doxygen 1.6.3