libStatGen Software 1
StringIntMap Class Reference
Collaboration diagram for StringIntMap:

Public Member Functions

 StringIntMap (int startsize=0)
 
void Grow (int newsize)
 
void Clear ()
 
int Length () 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 Add (const ::String &s, int i)
 
int Find (const ::String &s, int defaultValue)
 
int Find (const ::String &s) const
 
int FindStem (const ::String &stem) const
 
StringIntMapoperator= (const StringIntMap &rhs)
 
const ::Stringoperator[] (int i) const
 
::Stringoperator[] (int i)
 
::StringString (int i)
 
int IncrementCount (const ::String &key)
 
int DecrementCount (const ::String &key)
 
int GetCount (const ::String &key) const
 
int GetCount (int index) const
 
void Delete (int index)
 

Static Public Member Functions

static void * CreateMap ()
 

Static Public Attributes

static int alloc = 8
 

Protected Attributes

::String ** strings
 
int * integers
 
int count
 
int size
 

Detailed Description

Definition at line 92 of file StringMap.h.

Constructor & Destructor Documentation

◆ StringIntMap()

StringIntMap::StringIntMap ( int  startsize = 0)

Definition at line 306 of file StringMap.cpp.

307{
308 count = 0;
309 size = (startsize + alloc) / alloc * alloc;
310 strings = new ::String * [size];
311 integers = new int[size];
312};

◆ ~StringIntMap()

StringIntMap::~StringIntMap ( )
virtual

Definition at line 314 of file StringMap.cpp.

315{
316 for (int i = 0; i < count; i++)
317 delete strings[i];
318 delete [] strings;
319 delete [] integers;
320}

Member Function Documentation

◆ Add()

int StringIntMap::Add ( const ::String s,
int  i 
)

Definition at line 352 of file StringMap.cpp.

353{
354 if (count == 0)
355 {
356 Grow(1);
357 strings[0] = new ::String(key);
358 integers[0] = integer;
359 return count++;
360 }
361
362 int left = 0;
363 int right = count - 1;
364
365 while (right > left)
366 {
367 int probe = (left + right) / 2;
368 int test = key.SlowCompare(*(strings[probe]));
369
370 if (test == 0)
371 {
372 integers[probe] = integer;
373 return probe;
374 }
375
376 if (test < 0)
377 right = probe - 1;
378 else
379 left = probe + 1;
380 }
381
382 int insertAt = left;
383 int test = key.SlowCompare(*(strings[insertAt]));
384
385 if (test == 0)
386 {
387 integers[insertAt] = integer;
388 return insertAt;
389 }
390
391 if (test > 0) insertAt++;
392
393 Grow(count + 1);
394
395 if (insertAt < count)
396 {
397 for (int i = count; i > insertAt; i--)
398 {
399 strings[i] = strings[i - 1];
400 integers[i] = integers[i - 1];
401 }
402 }
403
404 strings[insertAt] = new ::String(key);
405 integers[insertAt] = integer;
406 count++;
407
408 return insertAt;
409}

◆ Clear()

void StringIntMap::Clear ( )

Definition at line 523 of file StringMap.cpp.

524{
525 for (int i = 0; i < count; i++)
526 delete strings[i];
527 count = 0;
528}

◆ DecrementCount()

int StringIntMap::DecrementCount ( const ::String key)

Definition at line 547 of file StringMap.cpp.

548{
549 int index = Find(key);
550
551 if (index != -1)
552 return --(integers[index]);
553
554 SetInteger(key, -1);
555 return -1;
556}

◆ Delete()

void StringIntMap::Delete ( int  index)

Definition at line 558 of file StringMap.cpp.

559{
560 count--;
561
562 delete strings[index];
563
564 for (int i = index; i < count; i++)
565 {
566 strings[i] = strings[i+1];
567 integers[i] = integers[i+1];
568 }
569}

◆ Find() [1/2]

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

Definition at line 460 of file StringMap.cpp.

461{
462 if (!count) return -1;
463
464 int left = 0;
465 int right = count - 1;
466
467 while (right > left)
468 {
469 int probe = (left + right) / 2;
470 int test = s.SlowCompare(*(strings[probe]));
471
472 if (test == 0)
473 return probe;
474
475 if (test < 0)
476 right = probe - 1;
477 else
478 left = probe + 1;
479 }
480
481 int position = left;
482 int test = s.SlowCompare(*(strings[left]));
483
484 if (test == 0)
485 return position;
486
487 return -1;
488}

◆ Find() [2/2]

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

Definition at line 411 of file StringMap.cpp.

412{
413 if (!count)
414 return Add(s, defaultValue);
415
416 int left = 0;
417 int right = count - 1;
418
419 while (right > left)
420 {
421 int probe = (left + right) / 2;
422 int test = s.SlowCompare(*(strings[probe]));
423
424 if (test == 0)
425 return probe;
426
427 if (test < 0)
428 right = probe - 1;
429 else
430 left = probe + 1;
431 }
432
433 int position = left;
434 int test = s.SlowCompare(*(strings[left]));
435
436 if (test == 0)
437 return position;
438
439 if (test > 0)
440 position++;
441
442 Grow(count + 1);
443
444 if (position < count)
445 {
446 for (int i = count; i > position; i--)
447 {
448 strings[i] = strings[i - 1];
449 integers[i] = integers[i - 1];
450 }
451 }
452
453 strings[position] = new ::String(s);
454 integers[position] = defaultValue;
455 count++;
456
457 return position;
458}

◆ FindStem()

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

Definition at line 490 of file StringMap.cpp.

491{
492 if (!count) return -1;
493
494 int left = 0;
495 int right = count - 1;
496
497 while (right > left)
498 {
499 int probe = (left + right) / 2;
500 int test = strings[probe]->SlowCompareToStem(stem);
501
502 if (test == 0)
503 {
504 if ((left < probe && strings[probe-1]->SlowCompareToStem(stem) == 0) ||
505 (right > probe && strings[probe+1]->SlowCompareToStem(stem) == 0))
506 return -2;
507
508 return probe;
509 }
510
511 if (test > 0)
512 right = probe - 1;
513 else
514 left = probe + 1;
515 }
516
517 if (strings[left]->SlowCompareToStem(stem) == 0)
518 return left;
519
520 return -1;
521}

◆ GetCount() [1/2]

int StringIntMap::GetCount ( const ::String key) const

Definition at line 530 of file StringMap.cpp.

531{
532 int index = Find(key);
533 return index == -1 ? 0 : integers[index];
534}

◆ GetCount() [2/2]

int StringIntMap::GetCount ( int  index) const
inline

Definition at line 156 of file StringMap.h.

157 {
158 return integers[index];
159 }

◆ Grow()

void StringIntMap::Grow ( int  newsize)

Definition at line 322 of file StringMap.cpp.

323{
324 if (newsize >= size)
325 {
326 if ((newsize >> 1) >= size)
327 size = (newsize + alloc) / alloc * alloc;
328 else
329 {
330 size = alloc;
331 while (size <= newsize)
332 size *= 2;
333 }
334
335 ::String ** newStrings = new ::String * [size];
336 int * newIntegers = new int [size];
337
338 for (int i = 0; i < count; i++)
339 {
340 newStrings[i] = strings[i];
341 newIntegers[i] = integers[i];
342 }
343
344 delete [] strings;
345 delete [] integers;
346
347 strings = newStrings;
348 integers = newIntegers;
349 }
350}

◆ IncrementCount()

int StringIntMap::IncrementCount ( const ::String key)

Definition at line 536 of file StringMap.cpp.

537{
538 int index = Find(key);
539
540 if (index != -1)
541 return ++(integers[index]);
542
543 SetInteger(key, 1);
544 return 1;
545}

◆ Integer() [1/2]

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

Definition at line 116 of file StringMap.h.

117 {
118 int index = Find(key);
119 return (index >= 0) ? (int) integers[index] : -1;
120 }

◆ Integer() [2/2]

int StringIntMap::Integer ( int  i) const
inline

Definition at line 112 of file StringMap.h.

113 {
114 return integers[i];
115 }

◆ Length()

int StringIntMap::Length ( ) const
inline

Definition at line 107 of file StringMap.h.

108 {
109 return count;
110 }

◆ operator[]() [1/2]

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

Definition at line 142 of file StringMap.h.

143 {
144 return *(strings[i]);
145 }

◆ operator[]() [2/2]

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

Definition at line 138 of file StringMap.h.

139 {
140 return *(strings[i]);
141 }

◆ SetInteger() [1/2]

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

Definition at line 126 of file StringMap.h.

127 {
128 Add(key, value);
129 }

◆ SetInteger() [2/2]

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

Definition at line 122 of file StringMap.h.

123 {
124 integers[i] = value;
125 }

◆ String()

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

Definition at line 146 of file StringMap.h.

147 {
148 return *(strings[i]);
149 }

Member Data Documentation

◆ alloc

int StringIntMap::alloc = 8
static

Definition at line 100 of file StringMap.h.

◆ count

int StringIntMap::count
protected

Definition at line 97 of file StringMap.h.

◆ integers

int* StringIntMap::integers
protected

Definition at line 96 of file StringMap.h.

◆ size

int StringIntMap::size
protected

Definition at line 97 of file StringMap.h.

◆ strings

::String** StringIntMap::strings
protected

Definition at line 95 of file StringMap.h.


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