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     // Initialize hash with the contents of a file
00120     void ReadLinesFromFile(FILE * file);
00121     void ReadLinesFromFile(const char * filename);
00122 
00123 #ifdef __ZLIB_AVAILABLE__
00124     void ReadLinesFromFile(IFILE & file);
00125 #endif
00126 
00127     void Swap(StringHash & s);
00128 
00129 private:
00130 
00131     unsigned int Iterate(unsigned int key, const String & string) const
00132     {
00133         unsigned int h = key & mask;
00134 
00135         while (strings[h] != NULL &&
00136                 (keys[h] != key ||
00137                  strings[h]->SlowCompare(string) != 0))
00138             h = (h + 1) & mask;
00139 
00140         return h;
00141     }
00142 
00143     void Insert(unsigned int where, unsigned int key, const String & string)
00144     {
00145         strings[where] = new String;
00146         *(strings[where]) = string;
00147         keys[where] = key;
00148 
00149         count++;
00150     }
00151 };
00152 
00153 class StringIntHash
00154 {
00155 protected:
00156     String        ** strings;
00157     int           * integers;
00158     unsigned int      * keys;
00159     unsigned int count, size;
00160     unsigned int        mask;
00161 
00162 public:
00163     StringIntHash(int startsize = 32);
00164     virtual ~StringIntHash();
00165 
00166     void Grow()
00167     {
00168         SetSize(size * 2);
00169     }
00170     void Shrink()
00171     {
00172         SetSize(size / 2);
00173     }
00174 
00175     void SetSize(int newsize);
00176 
00177     void Clear();
00178 
00179     int  Capacity() const
00180     {
00181         return size;
00182     }
00183     int  Entries() const
00184     {
00185         return count;
00186     }
00187 
00188     int Integer(int i) const
00189     {
00190         return integers[i];
00191     }
00192     int Integer(const String & key) const
00193     {
00194         int index = Find(key);
00195 
00196         return index >= 0 ? integers[index] : -1;
00197     }
00198 
00199     void SetInteger(int i, int value)
00200     {
00201         integers[i] = value;
00202     }
00203     void SetInteger(const String & key, int value)
00204     {
00205         Add(key, value);
00206     }
00207 
00208     int IncrementCount(const String & key);
00209     int DecrementCount(const String & key);
00210     int GetCount(const String & key) const;
00211     int GetCount(int index) const
00212     {
00213         return integers[index];
00214     }
00215 
00216     int Add(const String & s, int integer);
00217     int Find(const String & s, int defaultValue);
00218     int Find(const String & s) const;
00219 
00220     StringIntHash & operator = (const StringIntHash & rhs);
00221 
00222     const String & operator [](int i) const
00223     {
00224         return *(strings[i]);
00225     }
00226     String & operator [](int i)
00227     {
00228         return *(strings[i]);
00229     }
00230 //      String & String(int i) { return *(strings[i]); }
00231 
00232     void Delete(unsigned int index);
00233     void Delete(const String & key)
00234     {
00235         Delete(Find(key));
00236     }
00237 
00238     bool SlotInUse(int index) const
00239     {
00240         return strings[index] != NULL;
00241     }
00242 
00243 private:
00244 
00245     unsigned int Iterate(unsigned int key, const String & string) const
00246     {
00247         unsigned int h = key & mask;
00248 
00249         while (strings[h] != NULL &&
00250                 (keys[h] != key ||
00251                  strings[h]->SlowCompare(string) != 0))
00252             h = (h + 1) & mask;
00253 
00254         return h;
00255     }
00256 
00257     void Insert(unsigned int where, unsigned int key, const String & string)
00258     {
00259         strings[where] = new String;
00260         *(strings[where]) = string;
00261         keys[where] = key;
00262 
00263         count++;
00264     }
00265 };
00266 
00267 class StringDoubleHash
00268 {
00269 protected:
00270     String        ** strings;
00271     double         * doubles;
00272     unsigned int      * keys;
00273     unsigned int count, size;
00274     unsigned int        mask;
00275 
00276 public:
00277     StringDoubleHash(int startsize = 32);
00278     virtual ~StringDoubleHash();
00279 
00280     void Grow()
00281     {
00282         SetSize(size * 2);
00283     }
00284     void Shrink()
00285     {
00286         SetSize(size / 2);
00287     }
00288 
00289     void SetSize(int newsize);
00290 
00291     void Clear();
00292 
00293     int  Capacity() const
00294     {
00295         return size;
00296     }
00297     int  Entries() const
00298     {
00299         return count;
00300     }
00301 
00302     double Double(int i) const
00303     {
00304         return doubles[i];
00305     }
00306     double Double(const String & key) const
00307     {
00308         int index = Find(key);
00309 
00310         return index >= 0 ? doubles[index] : _NAN_;
00311     }
00312 
00313     void SetDouble(int i, double value)
00314     {
00315         doubles[i] = value;
00316     }
00317     void SetDouble(const String & key, double value)
00318     {
00319         Add(key, value);
00320     }
00321 
00322     int Add(const String & s, double value);
00323     int Find(const String & s, double defaultValue);
00324     int Find(const String & s) const;
00325 
00326     StringDoubleHash & operator = (const StringDoubleHash & rhs);
00327 
00328     const String & operator [](int i) const
00329     {
00330         return *(strings[i]);
00331     }
00332     String & operator [](int i)
00333     {
00334         return *(strings[i]);
00335     }
00336 //      String & String(int i) { return *(strings[i]); }
00337 
00338     void Delete(unsigned int index);
00339     void Delete(const String & key)
00340     {
00341         Delete(Find(key));
00342     }
00343 
00344     bool SlotInUse(int index) const
00345     {
00346         return strings[index] != NULL;
00347     }
00348 
00349 private:
00350 
00351     unsigned int Iterate(unsigned int key, const String & string) const
00352     {
00353         unsigned int h = key & mask;
00354 
00355         while (strings[h] != NULL &&
00356                 (keys[h] != key ||
00357                  strings[h]->SlowCompare(string) != 0))
00358             h = (h + 1) & mask;
00359 
00360         return h;
00361     }
00362 
00363     void Insert(unsigned int where, unsigned int key, const String & string)
00364     {
00365         strings[where] = new String;
00366         *(strings[where]) = string;
00367         keys[where] = key;
00368 
00369         count++;
00370     }
00371 };
00372 
00373 #endif
Generated on Wed Nov 17 15:38:29 2010 for StatGen Software by  doxygen 1.6.3