libStatGen Software 1
Loading...
Searching...
No Matches
StringMap Class Reference
Collaboration diagram for StringMap:

Public Member Functions

 StringMap (int startsize=0)
 
void Grow (int newsize)
 
void Clear ()
 
int Length () const
 
void * Object (int i) const
 
void * Object (const ::String &key) const
 
void * Object (const ::String &key, void *(*create_object)())
 
void SetObject (int i, void *object)
 
void SetObject (const ::String &key, void *object)
 
int Add (const ::String &s, void *object=NULL)
 
int Find (const ::String &s, void *(*create_object)()=NULL)
 
int Find (const ::String &s) const
 
int FindStem (const ::String &stem) const
 
int FindFirstStem (const ::String &stem) const
 
StringMapoperator= (const StringMap &rhs)
 
const ::Stringoperator[] (int i) const
 
::Stringoperator[] (int i)
 
::StringString (int i)
 
void Delete (int index)
 

Static Public Member Functions

static void * CreateMap ()
 

Static Public Attributes

static int alloc = 8
 

Protected Attributes

::String ** strings
 
void ** objects
 
int count
 
int size
 

Detailed Description

Definition at line 23 of file StringMap.h.

Constructor & Destructor Documentation

◆ StringMap()

StringMap::StringMap ( int  startsize = 0)

Definition at line 22 of file StringMap.cpp.

23{
24 count = 0;
25 size = (startsize + alloc) / alloc * alloc;
26 strings = new ::String * [size];
27 objects = new void * [size];
28};

◆ ~StringMap()

StringMap::~StringMap ( )
virtual

Definition at line 30 of file StringMap.cpp.

31{
32 for (int i = 0; i < count; i++)
33 delete strings[i];
34 delete [] strings;
35 delete [] objects;
36}

Member Function Documentation

◆ Add()

int StringMap::Add ( const ::String s,
void *  object = NULL 
)

Definition at line 70 of file StringMap.cpp.

71{
72 if (count == 0)
73 {
74 Grow(1);
75 strings[0] = new ::String(key);
76 objects[0] = object;
77 return count++;
78 }
79
80 int left = 0;
81 int right = count - 1;
82
83 while (right > left)
84 {
85 int probe = (left + right) / 2;
86 int test = key.SlowCompare(*(strings[probe]));
87
88 if (test == 0)
89 {
90 objects[probe] = object;
91 return probe;
92 }
93
94 if (test < 0)
95 right = probe - 1;
96 else
97 left = probe + 1;
98 }
99
100 int insertAt = left;
101 int test = key.SlowCompare(*(strings[insertAt]));
102
103 if (test == 0)
104 {
105 objects[insertAt] = object;
106 return insertAt;
107 }
108
109 if (test > 0) insertAt++;
110
111 Grow(count + 1);
112
113 if (insertAt < count)
114 {
115 for (int i = count; i > insertAt; i--)
116 {
117 strings[i] = strings[i - 1];
118 objects[i] = objects[i - 1];
119 }
120 }
121
122 strings[insertAt] = new ::String(key);
123 objects[insertAt] = object;
124 count++;
125
126 return insertAt;
127}

◆ Clear()

void StringMap::Clear ( )

Definition at line 281 of file StringMap.cpp.

282{
283 for (int i = 0; i < count; i++)
284 delete strings[i];
285 count = 0;
286}

◆ CreateMap()

void * StringMap::CreateMap ( )
static

Definition at line 276 of file StringMap.cpp.

277{
278 return (void *) new StringMap();
279}

◆ Delete()

void StringMap::Delete ( int  index)

Definition at line 288 of file StringMap.cpp.

289{
290 count--;
291
292 delete strings[index];
293
294 for (int i = index; i < count; i++)
295 {
296 strings[i] = strings[i+1];
297 objects[i] = objects[i+1];
298 }
299}

◆ Find() [1/2]

int StringMap::Find ( const ::String s) const

Definition at line 181 of file StringMap.cpp.

182{
183 if (!count) return -1;
184
185 int left = 0;
186 int right = count - 1;
187
188 while (right > left)
189 {
190 int probe = (left + right) / 2;
191 int test = s.SlowCompare(*(strings[probe]));
192
193 if (test == 0)
194 return probe;
195
196 if (test < 0)
197 right = probe - 1;
198 else
199 left = probe + 1;
200 }
201
202 int position = left;
203 int test = s.SlowCompare(*(strings[left]));
204
205 if (test == 0)
206 return position;
207
208 return -1;
209}

◆ Find() [2/2]

int StringMap::Find ( const ::String s,
void *(*)()  create_object = NULL 
)

Definition at line 129 of file StringMap.cpp.

130{
131 if (!count)
132 return create_object == NULL ? -1 : Add(s, create_object());
133
134 int left = 0;
135 int right = count - 1;
136
137 while (right > left)
138 {
139 int probe = (left + right) / 2;
140 int test = s.SlowCompare(*(strings[probe]));
141
142 if (test == 0)
143 return probe;
144
145 if (test < 0)
146 right = probe - 1;
147 else
148 left = probe + 1;
149 }
150
151 int position = left;
152 int test = s.SlowCompare(*(strings[left]));
153
154 if (test == 0)
155 return position;
156
157 if (create_object == NULL)
158 return -1;
159
160 if (test > 0)
161 position++;
162
163 Grow(count + 1);
164
165 if (position < count)
166 {
167 for (int i = count; i > position; i--)
168 {
169 strings[i] = strings[i - 1];
170 objects[i] = objects[i - 1];
171 }
172 }
173
174 strings[position] = new ::String(s);
175 objects[position] = create_object();
176 count++;
177
178 return position;
179}

◆ FindFirstStem()

int StringMap::FindFirstStem ( const ::String stem) const

Definition at line 244 of file StringMap.cpp.

245{
246 if (!count) return -1;
247
248 int left = 0;
249 int right = count - 1;
250
251 while (right > left)
252 {
253 int probe = (left + right) / 2;
254 int test = strings[probe]->SlowCompareToStem(stem);
255
256 if (test == 0)
257 {
258 while (left < probe && strings[probe-1]->SlowCompareToStem(stem) == 0)
259 probe--;
260
261 return probe;
262 }
263
264 if (test > 0)
265 right = probe - 1;
266 else
267 left = probe + 1;
268 }
269
270 if (strings[left]->SlowCompareToStem(stem) == 0)
271 return left;
272
273 return -1;
274}

◆ FindStem()

int StringMap::FindStem ( const ::String stem) const

Definition at line 211 of file StringMap.cpp.

212{
213 if (!count) return -1;
214
215 int left = 0;
216 int right = count - 1;
217
218 while (right > left)
219 {
220 int probe = (left + right) / 2;
221 int test = strings[probe]->SlowCompareToStem(stem);
222
223 if (test == 0)
224 {
225 if ((left < probe && strings[probe-1]->SlowCompareToStem(stem) == 0) ||
226 (right > probe && strings[probe+1]->SlowCompareToStem(stem) == 0))
227 return -2;
228
229 return probe;
230 }
231
232 if (test > 0)
233 right = probe - 1;
234 else
235 left = probe + 1;
236 }
237
238 if (strings[left]->SlowCompareToStem(stem) == 0)
239 return left;
240
241 return -1;
242}

◆ Grow()

void StringMap::Grow ( int  newsize)

Definition at line 38 of file StringMap.cpp.

39{
40 if (newsize >= size)
41 {
42 if ((newsize >> 1) >= size)
43 size = (newsize + alloc) / alloc * alloc;
44 else
45 {
46 size = alloc;
47 while (size <= newsize)
48 size *= 2;
49 }
50
51 size = (newsize + alloc) / alloc * alloc;
52
53 ::String ** newStrings = new ::String * [size];
54 void ** newObjects = new void * [size];
55
56 for (int i = 0; i < count; i++)
57 {
58 newStrings[i] = strings[i];
59 newObjects[i] = objects[i];
60 }
61
62 delete [] strings;
63 delete [] objects;
64
65 strings = newStrings;
66 objects = newObjects;
67 }
68}

◆ Length()

int StringMap::Length ( ) const
inline

Definition at line 38 of file StringMap.h.

39 {
40 return count;
41 }

◆ Object() [1/3]

void * StringMap::Object ( const ::String key) const
inline

Definition at line 47 of file StringMap.h.

48 {
49 int index = Find(key);
50 return (index >= 0) ? objects[index] : NULL;
51 }

◆ Object() [2/3]

void * StringMap::Object ( const ::String key,
void *(*)()  create_object 
)
inline

Definition at line 52 of file StringMap.h.

53 {
54 return objects[Find(key, create_object)];
55 }

◆ Object() [3/3]

void * StringMap::Object ( int  i) const
inline

Definition at line 43 of file StringMap.h.

44 {
45 return objects[i];
46 }

◆ operator[]() [1/2]

::String & StringMap::operator[] ( int  i)
inline

Definition at line 78 of file StringMap.h.

79 {
80 return *(strings[i]);
81 }

◆ operator[]() [2/2]

const ::String & StringMap::operator[] ( int  i) const
inline

Definition at line 74 of file StringMap.h.

75 {
76 return *(strings[i]);
77 }

◆ SetObject() [1/2]

void StringMap::SetObject ( const ::String key,
void *  object 
)
inline

Definition at line 61 of file StringMap.h.

62 {
63 Add(key, object);
64 }

◆ SetObject() [2/2]

void StringMap::SetObject ( int  i,
void *  object 
)
inline

Definition at line 57 of file StringMap.h.

58 {
59 objects[i] = object;
60 }

◆ String()

::String & StringMap::String ( int  i)
inline

Definition at line 82 of file StringMap.h.

83 {
84 return *(strings[i]);
85 }

Member Data Documentation

◆ alloc

int StringMap::alloc = 8
static

Definition at line 31 of file StringMap.h.

◆ count

int StringMap::count
protected

Definition at line 28 of file StringMap.h.

◆ objects

void** StringMap::objects
protected

Definition at line 27 of file StringMap.h.

◆ size

int StringMap::size
protected

Definition at line 28 of file StringMap.h.

◆ strings

::String** StringMap::strings
protected

Definition at line 26 of file StringMap.h.


The documentation for this class was generated from the following files: