libStatGen Software  1
StringMap.h
1 /*
2  * Copyright (C) 2010 Regents of the University of Michigan
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __STRINGMAP_H__
19 #define __STRINGMAP_H__
20 
21 #include "StringBasics.h"
22 
23 class StringMap
24 {
25 protected:
26  ::String ** strings;
27  void ** objects;
28  int count, size;
29 
30 public:
31  static int alloc;
32 
33  StringMap(int startsize = 0);
34  virtual ~StringMap();
35 
36  void Grow(int newsize);
37  void Clear();
38  int Length() const
39  {
40  return count;
41  }
42 
43  void * Object(int i) const
44  {
45  return objects[i];
46  }
47  void * Object(const ::String & key) const
48  {
49  int index = Find(key);
50  return (index >= 0) ? objects[index] : NULL;
51  }
52  void * Object(const ::String & key, void *(*create_object)())
53  {
54  return objects[Find(key, create_object)];
55  }
56 
57  void SetObject(int i, void * object)
58  {
59  objects[i] = object;
60  }
61  void SetObject(const ::String & key, void * object)
62  {
63  Add(key, object);
64  }
65 
66  int Add(const ::String & s, void * object = NULL);
67  int Find(const ::String & s, void *(*create_object)() = NULL);
68  int Find(const ::String & s) const;
69  int FindStem(const ::String & stem) const;
70  int FindFirstStem(const ::String & stem) const;
71 
72  StringMap & operator = (const StringMap & rhs);
73 
74  const ::String & operator [](int i) const
75  {
76  return *(strings[i]);
77  }
78  ::String & operator [](int i)
79  {
80  return *(strings[i]);
81  }
82  ::String & String(int i)
83  {
84  return *(strings[i]);
85  }
86 
87  static void * CreateMap();
88 
89  void Delete(int index);
90 };
91 
93 {
94 protected:
95  ::String ** strings;
96  int * integers;
97  int count, size;
98 
99 public:
100  static int alloc;
101 
102  StringIntMap(int startsize = 0);
103  virtual ~StringIntMap();
104 
105  void Grow(int newsize);
106  void Clear();
107  int Length() const
108  {
109  return count;
110  }
111 
112  int Integer(int i) const
113  {
114  return integers[i];
115  }
116  int Integer(const ::String & key) const
117  {
118  int index = Find(key);
119  return (index >= 0) ? (int) integers[index] : -1;
120  }
121 
122  void SetInteger(int i, int value)
123  {
124  integers[i] = value;
125  }
126  void SetInteger(const ::String & key, int value)
127  {
128  Add(key, value);
129  }
130 
131  int Add(const ::String & s, int i);
132  int Find(const ::String & s, int defaultValue);
133  int Find(const ::String & s) const;
134  int FindStem(const ::String & stem) const;
135 
136  StringIntMap & operator = (const StringIntMap & rhs);
137 
138  const ::String & operator [](int i) const
139  {
140  return *(strings[i]);
141  }
142  ::String & operator [](int i)
143  {
144  return *(strings[i]);
145  }
146  ::String & String(int i)
147  {
148  return *(strings[i]);
149  }
150 
151  static void * CreateMap();
152 
153  int IncrementCount(const ::String & key);
154  int DecrementCount(const ::String & key);
155  int GetCount(const ::String & key) const;
156  int GetCount(int index) const
157  {
158  return integers[index];
159  }
160 
161  void Delete(int index);
162 };
163 
164 #endif
165 
StringIntMap
Definition: StringMap.h:93
String
Definition: StringBasics.h:39
StringMap
Definition: StringMap.h:24