libStatGen Software 1
StringIntHash Class Reference
Inheritance diagram for StringIntHash:
Collaboration diagram for StringIntHash:

Public Member Functions

 StringIntHash (int startsize=32)
 
void Grow ()
 
void Shrink ()
 
void SetSize (int newsize)
 
void Clear ()
 
int Capacity () const
 
int Entries () const
 
int Integer (int i) const
 
int Integer (const String &key) const
 
void SetInteger (int i, int value)
 
void SetInteger (const String &key, int value)
 
int IncrementCount (const String &key)
 
int IncrementCount (const String &key, int amount)
 
int DecrementCount (const String &key)
 
int GetCount (const String &key) const
 
int GetCount (int index) const
 
int Add (const String &s, int integer)
 
int Find (const String &s, int defaultValue)
 
int Find (const String &s) const
 
StringIntHashoperator= (const StringIntHash &rhs)
 
bool operator== (const StringIntHash &rhs) const
 
const Stringoperator[] (int i) const
 
Stringoperator[] (int i)
 
void Delete (unsigned int index)
 
void Delete (const String &key)
 
bool SlotInUse (int index) const
 
- Public Member Functions inherited from StringHashBase
void setCaseSensitive (bool caseSensitive)
 
virtual void SetSize (int newsize)=0
 

Protected Attributes

String ** strings
 
int * integers
 
unsigned int * keys
 
unsigned int count
 
unsigned int size
 
unsigned int mask
 
- Protected Attributes inherited from StringHashBase
bool myCaseSensitive
 

Additional Inherited Members

- Protected Member Functions inherited from StringHashBase
bool stringsEqual (const String &string1, const String &string2) const
 
unsigned int getKey (const String &string) const
 

Detailed Description

Definition at line 193 of file StringHash.h.

Constructor & Destructor Documentation

◆ StringIntHash()

StringIntHash::StringIntHash ( int  startsize = 32)

Definition at line 239 of file StringHash.cpp.

241{
242 count = 0;
243 size = startsize;
244 mask = startsize - 1;
245
246 // In this implementation, the size of hash tables must be a power of two
247 if (startsize & mask)
248 error("StringIntHash: Hash table size must be a power of two.\n");
249
250 strings = new String * [size];
251 integers = new int [size];
252 keys = new unsigned int [size];
253
254 for (unsigned int i = 0; i < size; i++)
255 strings[i] = NULL;
256};

◆ ~StringIntHash()

StringIntHash::~StringIntHash ( )
virtual

Definition at line 258 of file StringHash.cpp.

259{
260 for (unsigned int i = 0; i < size; i++)
261 if (strings[i] != NULL)
262 delete strings[i];
263
264 if(strings) delete [] strings;
265 if(integers) delete [] integers;
266 if(keys) delete [] keys;
267}

Member Function Documentation

◆ Add()

int StringIntHash::Add ( const String s,
int  integer 
)

Definition at line 321 of file StringHash.cpp.

322{
323 unsigned int key = getKey(string);
324 unsigned int h = Iterate(key, string);
325
326 if (strings[h] == NULL)
327 Insert(h, key, string);
328
329 integers[h] = value;
330
331 if (count * 2 > size)
332 {
333 Grow();
334 return Iterate(key, string);
335 }
336
337 return h;
338}

◆ Capacity()

int StringIntHash::Capacity ( ) const
inline

Definition at line 219 of file StringHash.h.

220 {
221 return size;
222 }

◆ Clear()

void StringIntHash::Clear ( )

Definition at line 306 of file StringHash.cpp.

307{
308 for (unsigned int i = 0; i < size; i++)
309 if (strings[i] != NULL)
310 {
311 delete strings[i];
312 strings[i] = NULL;
313 }
314
315 count = 0;
316
317 if (size > 256)
318 SetSize(256);
319}

◆ DecrementCount()

int StringIntHash::DecrementCount ( const String key)

Definition at line 626 of file StringHash.cpp.

627{
628 int index = Find(key);
629
630 if (index != -1)
631 return --(integers[index]);
632
633 SetInteger(key, -1);
634 return -1;
635}

◆ Delete() [1/2]

void StringIntHash::Delete ( const String key)
inline

Definition at line 275 of file StringHash.h.

276 {
277 Delete(Find(key));
278 }

◆ Delete() [2/2]

void StringIntHash::Delete ( unsigned int  index)

Definition at line 371 of file StringHash.cpp.

372{
373 if (index >= size || strings[index] == NULL)
374 return;
375
376 delete strings[index];
377 strings[index] = NULL;
378 count--;
379
380 if (count * 8 < size && size > 32)
381 Shrink();
382 else
383 {
384 // rehash the next strings until we find empty slot
385 index = (index + 1) & mask;
386
387 while (strings[index] != NULL)
388 {
389 if ((keys[index] & mask) != index)
390 {
391 unsigned int h = Iterate(keys[index], *strings[index]);
392
393 if (h != (unsigned int) index)
394 {
395 keys[h] = keys[index];
396 strings[h] = strings[index];
397 integers[h] = integers[index];
398
399 strings[index] = NULL;
400 }
401 }
402
403 index = (index + 1) & mask;
404 }
405 }
406}

◆ Entries()

int StringIntHash::Entries ( ) const
inline

Definition at line 223 of file StringHash.h.

224 {
225 return count;
226 }

◆ Find() [1/2]

int StringIntHash::Find ( const String s) const

Definition at line 360 of file StringHash.cpp.

361{
362 unsigned int key = getKey(string);
363 unsigned int h = Iterate(key, string);
364
365 if (strings[h] == NULL)
366 return -1;
367
368 return h;
369}

◆ Find() [2/2]

int StringIntHash::Find ( const String s,
int  defaultValue 
)

Definition at line 340 of file StringHash.cpp.

341{
342 unsigned int key = getKey(string);
343 unsigned int h = Iterate(key, string);
344
345 if (strings[h] == NULL)
346 {
347 Insert(h, key, string);
348 integers[h] = defaultValue;
349
350 if (count * 2 > size)
351 {
352 Grow();
353 return Iterate(key, string);
354 }
355 }
356
357 return h;
358}

◆ GetCount() [1/2]

int StringIntHash::GetCount ( const String key) const

Definition at line 598 of file StringHash.cpp.

599{
600 int index = Find(key);
601 return index == -1 ? 0 : integers[index];
602}

◆ GetCount() [2/2]

int StringIntHash::GetCount ( int  index) const
inline

Definition at line 252 of file StringHash.h.

253 {
254 return integers[index];
255 }

◆ Grow()

void StringIntHash::Grow ( )
inline

Definition at line 206 of file StringHash.h.

207 {
208 SetSize(size * 2);
209 }

◆ IncrementCount() [1/2]

int StringIntHash::IncrementCount ( const String key)

Definition at line 604 of file StringHash.cpp.

605{
606 int index = Find(key);
607
608 if (index != -1)
609 return ++(integers[index]);
610
611 SetInteger(key, 1);
612 return 1;
613}

◆ IncrementCount() [2/2]

int StringIntHash::IncrementCount ( const String key,
int  amount 
)

Definition at line 615 of file StringHash.cpp.

616{
617 int index = Find(key);
618
619 if (index != -1)
620 return (integers[index] += amount);
621
622 SetInteger(key, amount);
623 return amount;
624}

◆ Integer() [1/2]

int StringIntHash::Integer ( const String key) const
inline

Definition at line 232 of file StringHash.h.

233 {
234 int index = Find(key);
235
236 return index >= 0 ? integers[index] : -1;
237 }

◆ Integer() [2/2]

int StringIntHash::Integer ( int  i) const
inline

Definition at line 228 of file StringHash.h.

229 {
230 return integers[i];
231 }

◆ operator=()

StringIntHash & StringIntHash::operator= ( const StringIntHash rhs)

Definition at line 663 of file StringHash.cpp.

664{
665 Clear();
666
667 for (int i = 0; i < rhs.Capacity(); i++)
668 if (rhs.SlotInUse(i))
669 Add(*(rhs.strings[i]), rhs.integers[i]);
670
671 return *this;
672}

◆ operator==()

bool StringIntHash::operator== ( const StringIntHash rhs) const

Definition at line 674 of file StringHash.cpp.

675{
676 if (Capacity() != rhs.Capacity()) return false;
677 if (Entries() != rhs.Entries()) return false;
678 for (int i = 0; i < rhs.Capacity(); i++)
679 {
680 if(rhs.SlotInUse(i) != SlotInUse(i))
681 {
682 return(false);
683 }
684 if (rhs.SlotInUse(i))
685 {
686 if(*(strings[i]) != *(rhs.strings[i]))
687 {
688 return(false);
689 }
690 if(rhs.integers[i] != integers[i])
691 {
692 return(false);
693 }
694 }
695 }
696 return(true);
697}

◆ operator[]() [1/2]

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

Definition at line 268 of file StringHash.h.

269 {
270 return *(strings[i]);
271 }

◆ operator[]() [2/2]

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

Definition at line 264 of file StringHash.h.

265 {
266 return *(strings[i]);
267 }

◆ SetInteger() [1/2]

void StringIntHash::SetInteger ( const String key,
int  value 
)
inline

Definition at line 243 of file StringHash.h.

244 {
245 Add(key, value);
246 }

◆ SetInteger() [2/2]

void StringIntHash::SetInteger ( int  i,
int  value 
)
inline

Definition at line 239 of file StringHash.h.

240 {
241 integers[i] = value;
242 }

◆ SetSize()

void StringIntHash::SetSize ( int  newsize)
virtual

Implements StringHashBase.

Definition at line 269 of file StringHash.cpp.

270{
271 int newmask = newsize - 1;
272
273 String ** newstrings = new String * [newsize];
274 int * newintegers = new int [newsize];
275 unsigned int * newkeys = new unsigned int [newsize];
276
277 for (int i = 0; i < newsize; i++)
278 newstrings[i] = NULL;
279
280 for (unsigned int i = 0; i < size; i++)
281 if (strings[i] != NULL)
282 {
283 unsigned int key = keys[i];
284 unsigned int h = key & newmask;
285
286 while (newstrings[h] != NULL &&
287 (newkeys[h] != key || (!stringsEqual(*(newstrings[h]), *(strings[i])))))
288 h = (h + 1) & newmask;
289
290 newkeys[h] = key;
291 newstrings[h] = strings[i];
292 newintegers[h] = integers[i];
293 }
294
295 if(strings) delete [] strings;
296 if(integers) delete [] integers;
297 if(keys) delete [] keys;
298
299 strings = newstrings;
300 integers = newintegers;
301 keys = newkeys;
302 size = newsize;
303 mask = newmask;
304}

◆ Shrink()

void StringIntHash::Shrink ( )
inline

Definition at line 210 of file StringHash.h.

211 {
212 SetSize(size / 2);
213 }

◆ SlotInUse()

bool StringIntHash::SlotInUse ( int  index) const
inline

Definition at line 280 of file StringHash.h.

281 {
282 return strings[index] != NULL;
283 }

Member Data Documentation

◆ count

unsigned int StringIntHash::count
protected

Definition at line 199 of file StringHash.h.

◆ integers

int* StringIntHash::integers
protected

Definition at line 197 of file StringHash.h.

◆ keys

unsigned int* StringIntHash::keys
protected

Definition at line 198 of file StringHash.h.

◆ mask

unsigned int StringIntHash::mask
protected

Definition at line 200 of file StringHash.h.

◆ size

unsigned int StringIntHash::size
protected

Definition at line 199 of file StringHash.h.

◆ strings

String** StringIntHash::strings
protected

Definition at line 196 of file StringHash.h.


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