libStatGen Software  1
StringMap.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 __STRINGMAP_H__
00019 #define __STRINGMAP_H__
00020 
00021 #include "StringBasics.h"
00022 
00023 class StringMap
00024 {
00025 protected:
00026     ::String ** strings;
00027     void     ** objects;
00028     int         count, size;
00029 
00030 public:
00031     static int alloc;
00032 
00033     StringMap(int startsize = 0);
00034     virtual ~StringMap();
00035 
00036     void Grow(int newsize);
00037     void Clear();
00038     int  Length() const
00039     {
00040         return count;
00041     }
00042 
00043     void * Object(int i) const
00044     {
00045         return objects[i];
00046     }
00047     void * Object(const ::String & key) const
00048     {
00049         int index = Find(key);
00050         return (index >= 0) ? objects[index] : NULL;
00051     }
00052     void * Object(const ::String & key, void *(*create_object)())
00053     {
00054         return objects[Find(key, create_object)];
00055     }
00056 
00057     void SetObject(int i, void * object)
00058     {
00059         objects[i] = object;
00060     }
00061     void SetObject(const ::String & key, void * object)
00062     {
00063         Add(key, object);
00064     }
00065 
00066     int Add(const ::String & s, void * object = NULL);
00067     int Find(const ::String & s, void *(*create_object)() = NULL);
00068     int Find(const ::String & s) const;
00069     int FindStem(const ::String & stem) const;
00070     int FindFirstStem(const ::String & stem) const;
00071 
00072     StringMap & operator = (const StringMap & rhs);
00073 
00074     const ::String & operator [](int i) const
00075     {
00076         return *(strings[i]);
00077     }
00078     ::String & operator [](int i)
00079     {
00080         return *(strings[i]);
00081     }
00082     ::String & String(int i)
00083     {
00084         return *(strings[i]);
00085     }
00086 
00087     static void * CreateMap();
00088 
00089     void Delete(int index);
00090 };
00091 
00092 class StringIntMap
00093 {
00094 protected:
00095     ::String ** strings;
00096     int       * integers;
00097     int         count, size;
00098 
00099 public:
00100     static int alloc;
00101 
00102     StringIntMap(int startsize = 0);
00103     virtual ~StringIntMap();
00104 
00105     void Grow(int newsize);
00106     void Clear();
00107     int  Length() const
00108     {
00109         return count;
00110     }
00111 
00112     int Integer(int i) const
00113     {
00114         return integers[i];
00115     }
00116     int Integer(const ::String & key) const
00117     {
00118         int index = Find(key);
00119         return (index >= 0) ? (int) integers[index] : -1;
00120     }
00121 
00122     void SetInteger(int i, int value)
00123     {
00124         integers[i] = value;
00125     }
00126     void SetInteger(const ::String & key, int value)
00127     {
00128         Add(key, value);
00129     }
00130 
00131     int Add(const ::String & s, int i);
00132     int Find(const ::String & s, int defaultValue);
00133     int Find(const ::String & s) const;
00134     int FindStem(const ::String & stem) const;
00135 
00136     StringIntMap & operator = (const StringIntMap & rhs);
00137 
00138     const ::String & operator [](int i) const
00139     {
00140         return *(strings[i]);
00141     }
00142     ::String & operator [](int i)
00143     {
00144         return *(strings[i]);
00145     }
00146     ::String & String(int i)
00147     {
00148         return *(strings[i]);
00149     }
00150 
00151     static void * CreateMap();
00152 
00153     int IncrementCount(const ::String & key);
00154     int DecrementCount(const ::String & key);
00155     int GetCount(const ::String & key) const;
00156     int GetCount(int index) const
00157     {
00158         return integers[index];
00159     }
00160 
00161     void Delete(int index);
00162 };
00163 
00164 #endif
00165 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends