-
Notifications
You must be signed in to change notification settings - Fork 5
/
datastructures.cpp
1041 lines (833 loc) · 23.7 KB
/
datastructures.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "prism/datastructures.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <algorithm>
#include "prism/memoryhandler.h"
#include "prism/log.h"
#include "prism/system.h"
#include "prism/math.h"
using namespace std;
void convertIntegerToStringFast(std::string& oRet, int tValue)
{
static const char FAST_CONVERSION_DIGITS[] =
"0001020304050607080910111213141516171819"
"2021222324252627282930313233343536373839"
"4041424344454647484950515253545556575859"
"6061626364656667686970717273747576777879"
"8081828384858687888990919293949596979899";
static const auto BUFFER_SIZE = 22; // std::numeric_limits<unsigned long long>::digits10 + 3; // Vita hates this
char buffer[BUFFER_SIZE];
auto absoluteVal = static_cast<unsigned long long>(tValue);
const auto isNegative = tValue < 0;
if (isNegative) absoluteVal = 0 - absoluteVal;
char* ptr = buffer + (BUFFER_SIZE - 1);
while (absoluteVal >= 100) {
const auto index = static_cast<unsigned>((absoluteVal % 100) * 2);
absoluteVal /= 100;
*--ptr = FAST_CONVERSION_DIGITS[index + 1];
*--ptr = FAST_CONVERSION_DIGITS[index];
}
if (absoluteVal < 10) {
*--ptr = static_cast<char>('0' + absoluteVal);
}
else
{
auto index = static_cast<unsigned>(absoluteVal * 2);
*--ptr = FAST_CONVERSION_DIGITS[index + 1];
*--ptr = FAST_CONVERSION_DIGITS[index];
}
if (isNegative) *--ptr = '-';
oRet = std::string(ptr, buffer - ptr + BUFFER_SIZE - 1);
}
void convertFloatToStringFast(std::string& oRet, double tValue) {
static const auto BUFFER_SIZE = 50;
char buffer[BUFFER_SIZE];
sprintf(buffer, "%lf", tValue);
oRet = buffer;
}
void turnStringLowercase(char* tString) {
auto n = int(strlen(tString));
char* pos = tString;
int i;
for (i = 0; i < n; i++) {
*pos = (char)tolower((int)(*pos));
pos++;
}
}
void turnStringLowercase(string& tString)
{
transform(tString.begin(), tString.end(), tString.begin(), [](char c) {return (char)::tolower(c); });
}
void copyStringLowercase(char* tDst, const char* tSrc) {
auto n = int(strlen(tSrc));
int i;
for (i = 0; i <= n; i++) {
tDst[i] = (char)tolower((int)(tSrc[i]));
}
}
void copyStringLowercase(std::string& tDst, const char* tSrc)
{
auto n = int(strlen(tSrc));
tDst.reserve(n);
int i;
for (i = 0; i <= n; i++) {
tDst.push_back((char)tolower((int)(tSrc[i])));
}
}
void turnStringUppercase(std::string& tString)
{
transform(tString.begin(), tString.end(), tString.begin(), [](char c) {return (char)::toupper(c); });
}
char* copyToAllocatedString(char* tSrc) {
if (!tSrc) return NULL;
auto len = strlen(tSrc);
char* ret = (char*)allocMemory(int(len+1));
strcpy(ret, tSrc);
return ret;
}
void removeInvalidFileNameElementsFromString(std::string & tString)
{
transform(tString.begin(), tString.end(), tString.begin(), [](char c)
{
if (c >= 'a' && c <= 'z') return c;
if (c >= 'A' && c <= 'Z') return c;
if (c >= '0' && c <= '9') return c;
return '_';
});
}
int stringBeginsWithSubstring(const char * tString, const char * tSubstring)
{
auto n = strlen(tSubstring);
auto m = strlen(tString);
if (m < n) return 0;
size_t i;
for (i = 0; i < n; i++) {
if (tString[i] != tSubstring[i]) return 0;
}
return 1;
}
int stringBeginsWithSubstringCaseIndependent(const char * tString, const char * tSubstring)
{
string s(tString);
string substring(tSubstring);
transform(s.begin(), s.end(), s.begin(), [](char c) {return (char)::tolower(c); });
transform(substring.begin(), substring.end(), substring.begin(), [](char c) {return (char)::tolower(c); });
return stringBeginsWithSubstring(s.data(), substring.data());
}
int stringEqualCaseIndependent(const char * tString, const char * tOtherString)
{
string s(tString);
string otherString(tOtherString);
transform(s.begin(), s.end(), s.begin(), [](char c) {return (char)::tolower(c); });
transform(otherString.begin(), otherString.end(), otherString.begin(), [](char c) {return (char)::tolower(c); });
return s == otherString;
}
int isStringLowercase(const char* tString)
{
for (size_t i = 0; tString[i]; i++) {
if (isupper(tString[i])) return 0;
}
return 1;
}
vector<string> splitStringBySeparator(const string& tString, char tSeparator)
{
vector<string> ret;
size_t startPos = 0;
size_t pos = 0;
while ((pos = tString.find(tSeparator, startPos)) != string::npos) {
auto word = tString.substr(startPos, pos - startPos);
ret.push_back(word);
startPos = pos + 1;
}
if (startPos != string::npos) {
ret.push_back(tString.substr(startPos));
}
return ret;
}
static ListElement* newListElement(List* tList, void* tData, int tIsOwned) {
ListElement* e = (ListElement*)allocMemory(sizeof(ListElement));
e->mID = tList->mIDs++;
e->mData = tData;
e->mIsDataOwned = tIsOwned;
e->mNext = NULL;
e->mPrev = NULL;
return e;
}
static int list_push_front_internal(List* tList, void* tData, int tIsOwned) {
ListElement* e = newListElement(tList, tData, tIsOwned);
if(tList->mFirst != NULL) {
tList->mFirst->mPrev = e;
e->mNext = tList->mFirst;
}
if(tList->mLast == NULL) tList->mLast = e;
tList->mFirst = e;
tList->mSize++;
return e->mID;
}
int list_push_front(List* tList, void* tData) {
return list_push_front_internal(tList, tData, 0);
}
int list_push_front_owned(List* tList, void* tData){
return list_push_front_internal(tList, tData, 1);
}
static int list_push_back_internal(List* tList, void* tData, int tIsOwned) {
ListElement* e = newListElement(tList, tData, tIsOwned);
if(tList->mLast != NULL) {
tList->mLast->mNext = e;
e->mPrev = tList->mLast;
}
if(tList->mFirst == NULL) tList->mFirst = e;
tList->mLast = e;
tList->mSize++;
return e->mID;
}
int list_push_back(List* tList, void* tData) {
return list_push_back_internal(tList, tData, 0);
}
int list_push_back_owned(List* tList, void* tData) {
return list_push_back_internal(tList, tData, 1);
}
static void removeListElement(List* tList, ListElement* e){
if(e == tList->mFirst) {
tList->mFirst = e->mNext;
}
if(e == tList->mLast) {
tList->mLast = e->mPrev;
}
if(e->mPrev != NULL) {
e->mPrev->mNext = e->mNext;
}
if(e->mNext != NULL) {
e->mNext->mPrev = e->mPrev;
}
if(e->mIsDataOwned) {
freeMemory(e->mData);
}
freeMemory(e);
tList->mSize--;
}
void* list_get(List* tList, int tID) {
int left = tList->mSize;
ListElement* cur = tList->mFirst;
while(left--) {
if(cur->mID == tID) {
return cur->mData;
}
cur = cur->mNext;
}
return NULL;
}
void * list_get_by_ordered_index(List * tList, int tIndex)
{
if (tIndex >= list_size(tList)) {
logError("Trying to access invalid index.");
logErrorInteger(tIndex);
recoverFromError();
return NULL;
}
int left = tIndex;
ListElement* cur = tList->mFirst;
while (left--) {
cur = cur->mNext;
assert(cur);
}
return cur->mData;
}
void list_remove(List* tList, int tID){
int left = tList->mSize;
ListElement* cur = tList->mFirst;
while(left--) {
ListElement* next = cur->mNext;
if(cur->mID == tID) {
removeListElement(tList, cur);
}
cur = next;
}
}
static int trueFunction(void* tCaller, void* tData){
(void) tCaller;
(void) tData;
return 1;
}
void list_empty(List* tList){
list_remove_predicate(tList, trueFunction, NULL);
}
void list_map(List* tList, mapCB tCB, void* tCaller) {
int left = tList->mSize;
ListElement* cur = tList->mFirst;
while(left--) {
ListElement* next = cur->mNext;
tCB(tCaller, cur->mData);
cur = next;
}
}
void list_remove_predicate(List* tList, predicateCB tCB, void* tCaller){
int left = tList->mSize;
ListElement* cur = tList->mFirst;
while(left--) {
ListElement* next = cur->mNext;
if(tCB(tCaller, cur->mData)) {
removeListElement(tList, cur);
}
cur = next;
}
}
void list_remove_predicate_inverted(List* tList, predicateCB tCB, void* tCaller) {
int left = tList->mSize;
ListElement* cur = tList->mLast;
while (left--) {
ListElement* prev = cur->mPrev;
if (tCB(tCaller, cur->mData)) {
removeListElement(tList, cur);
}
cur = prev;
}
}
ListIterator list_find_first_predicate(List * tList, predicateCB tCB, void * tCaller)
{
int left = tList->mSize;
ListElement* cur = tList->mFirst;
while (left--) {
ListElement* next = cur->mNext;
if (tCB(tCaller, cur->mData)) {
return cur;
}
cur = next;
}
return NULL;
}
int list_size(List* tList){
return tList->mSize;
}
List new_list() {
List l;
l.mSize = 0;
l.mFirst = NULL;
l.mLast = NULL;
l.mIDs = 0;
return l;
}
void delete_list(List* tList){
list_empty(tList);
}
ListIterator list_iterator_begin(List* tList) {
return tList->mFirst;
}
ListIterator list_iterator_end(List * tList)
{
return tList->mLast;
}
void* list_iterator_get(ListIterator tIterator) {
return tIterator->mData;
}
void list_iterator_increase(ListIterator* tIterator) {
if((*tIterator)->mNext == NULL) {
logError("Trying to increase end iterator.");
recoverFromError();
}
*tIterator = (*tIterator)->mNext;
}
void list_iterator_remove(List* tList, ListIterator tIterator) {
removeListElement(tList, tIterator);
}
int list_has_next(ListIterator tIterator) {
return tIterator->mNext != NULL;
}
void * list_front(List * tList)
{
return list_get_by_ordered_index(tList, 0);
}
typedef struct {
void* mData;
int mFound;
} ListContainsCaller;
static void compareSingleListElement(void* tCaller, void* tData) {
ListContainsCaller* caller = (ListContainsCaller*)tCaller;
caller->mFound |= caller->mData == tData;
}
int list_contains(List* tList, void* tData) {
ListContainsCaller caller;
caller.mData = tData;
caller.mFound = 0;
list_map(tList, compareSingleListElement, &caller);
return caller.mFound;
}
Vector new_vector() {
Vector ret;
ret.mSize = 0;
ret.mAlloc = 2;
ret.mData = (VectorElement*)allocMemory(sizeof(VectorElement) * ret.mAlloc);
return ret;
}
void delete_vector(Vector* tVector) {
vector_empty(tVector);
freeMemory(tVector->mData);
tVector->mSize = 0;
tVector->mAlloc = 0;
}
void vector_empty(Vector* tVector) {
int i;
for(i = 0; i < tVector->mSize; i++) {
VectorElement* e = &tVector->mData[i];
if(e->mIsOwned) freeMemory(e->mData);
}
tVector->mSize = 0;
tVector->mAlloc = 2;
tVector->mData = (VectorElement*)reallocMemory(tVector->mData, sizeof(VectorElement)*tVector->mAlloc);
}
void vector_empty_to_previous_size(Vector* tVector) {
int i;
for(i = 0; i < tVector->mSize; i++) {
VectorElement* e = &tVector->mData[i];
if(e->mIsOwned) freeMemory(e->mData);
}
tVector->mAlloc = max(tVector->mSize, 2);
tVector->mSize = 0;
tVector->mData = (VectorElement*)reallocMemory(tVector->mData, sizeof(VectorElement)*tVector->mAlloc);
}
static void vector_push_back_internal(Vector* tVector, void* tData, int tIsOwned) {
if(tVector->mSize == tVector->mAlloc) {
tVector->mAlloc *= 2;
tVector->mData = (VectorElement*)reallocMemory(tVector->mData, sizeof(VectorElement)*tVector->mAlloc);
}
VectorElement* e = &tVector->mData[tVector->mSize];
e->mIsOwned = tIsOwned;
e->mData = tData;
tVector->mSize++;
}
void vector_push_back(Vector* tVector, void* tData) {
vector_push_back_internal(tVector, tData, 0);
}
void vector_push_back_owned(Vector* tVector, void* tData) {
vector_push_back_internal(tVector, tData, 1);
}
void* vector_push_back_new_buffer(Vector* tVector, int tSize) {
char* b = (char*)allocMemory(tSize);
vector_push_back_owned(tVector, b);
return b;
}
void* vector_get(Vector* tVector, int tIndex) {
return tVector->mData[tIndex].mData;
}
const void* vector_get(const Vector* tVector, int tIndex) {
return tVector->mData[tIndex].mData;
}
int vector_size(const Vector* tVector) {
return tVector->mSize;
}
void vector_remove(Vector* tVector, int ind) {
VectorElement* e = &tVector->mData[ind];
if (e->mIsOwned) {
freeMemory(e->mData);
}
int i;
for (i = ind; i < tVector->mSize - 1; i++) {
tVector->mData[i] = tVector->mData[i + 1];
}
tVector->mSize--;
}
static sortCB gSortFunc;
static void* gSortCaller;
static int vector_sort_cmp(const void* tData1, const void* tData2) {
const VectorElement* e1 = (VectorElement*)tData1;
const VectorElement* e2 = (VectorElement*)tData2;
return gSortFunc(gSortCaller, e1->mData, e2->mData);
}
void vector_sort(Vector* tVector, sortCB tCB, void* tCaller) {
gSortCaller = tCaller;
gSortFunc = tCB;
qsort(tVector->mData, tVector->mSize, sizeof(VectorElement), vector_sort_cmp);
}
void vector_map(Vector* tVector, mapCB tCB, void* tCaller) {
int i;
for(i = 0; i < tVector->mSize; i++) {
tCB(tCaller, tVector->mData[i].mData);
}
}
void* vector_get_back(Vector* tVector) {
return tVector->mData[tVector->mSize-1].mData;
}
void vector_pop_back(Vector* tVector) {
vector_remove(tVector, tVector->mSize-1);
}
#define MAP_MODULO 211
typedef struct {
char mKey[100];
void* mData;
int mIsOwned;
} StringMapBucketListEntry;
typedef struct {
List mEntries;
} StringMapBucket;
int gDebugStringMapAmount;
StringMap new_string_map() {
StringMap ret;
ret.mBuckets = allocMemory(sizeof(StringMapBucket) * MAP_MODULO);
int i;
for (i = 0; i < MAP_MODULO; i++) {
StringMapBucket* newBucket = &((StringMapBucket*)ret.mBuckets)[i];
newBucket->mEntries = new_list();
}
gDebugStringMapAmount++;
ret.mSize = 0;
return ret;
}
static void string_map_remove_element(StringMap* tMap, StringMapBucketListEntry* e) {
if (e->mIsOwned) {
freeMemory(e->mData);
}
tMap->mSize--;
}
static int clearStringMapBucketEntry(void* tCaller, void* tData) {
StringMap* map = (StringMap*)tCaller;
string_map_remove_element(map, (StringMapBucketListEntry*)tData);
return 1;
}
static void clearStringMapBucket(StringMap* tMap, StringMapBucket* e) {
list_remove_predicate(&e->mEntries, clearStringMapBucketEntry, tMap);
delete_list(&e->mEntries);
}
void delete_string_map(StringMap* tMap) {
string_map_empty(tMap);
freeMemory(tMap->mBuckets);
gDebugStringMapAmount--;
}
void string_map_empty(StringMap* tMap) {
int i;
for (i = 0; i < MAP_MODULO; i++) {
StringMapBucket* bucket = &((StringMapBucket*)tMap->mBuckets)[i];
clearStringMapBucket(tMap, bucket);
}
}
static int getBucketIDFromString(uint8_t* tKey) {
auto l = int(strlen((char*)tKey));
int i;
int base = 1;
int offset = 0;
for (i = 0; i < l; i++) {
offset = (offset + (tKey[i] * base)) % MAP_MODULO;
base = (base * 10) % MAP_MODULO;
}
return offset;
}
static void string_map_push_internal(StringMap* tMap, const char* tKey, void* tData, int tIsOwned) {
int offset = getBucketIDFromString((uint8_t*)tKey);
StringMapBucket* bucket = &((StringMapBucket*)tMap->mBuckets)[offset];
StringMapBucketListEntry* newEntry = (StringMapBucketListEntry*)allocMemory(sizeof(StringMapBucketListEntry));
strcpy(newEntry->mKey, tKey);
newEntry->mData = tData;
newEntry->mIsOwned = tIsOwned;
list_push_back_owned(&bucket->mEntries, newEntry);
tMap->mSize++;
}
void string_map_push_owned(StringMap* tMap, const char* tKey, void* tData) {
string_map_push_internal(tMap, tKey, tData, 1);
}
void string_map_push(StringMap* tMap, const char* tKey, void* tData) {
string_map_push_internal(tMap, tKey, tData, 0);
}
void string_map_remove(StringMap* tMap, const char* tKey) {
int offset = getBucketIDFromString((uint8_t*)tKey);
StringMapBucket* bucket = &((StringMapBucket*)tMap->mBuckets)[offset];
if (!list_size(&bucket->mEntries)) {
logError("Unable to find key in map.");
logErrorString(tKey);
recoverFromError();
}
ListIterator it = list_iterator_begin(&bucket->mEntries);
while (1) {
StringMapBucketListEntry* e = (StringMapBucketListEntry*)list_iterator_get(it);
if (!strcmp(tKey, e->mKey)) {
string_map_remove_element(tMap, e);
list_iterator_remove(&bucket->mEntries, it);
return;
}
if (!list_has_next(it)) {
logError("Unable to find key in map.");
logErrorString(tKey);
recoverFromError();
}
list_iterator_increase(&it);
}
}
void* string_map_get(StringMap* tMap, const char* tKey) {
int offset = getBucketIDFromString((uint8_t*)tKey);
StringMapBucket* bucket = &((StringMapBucket*)tMap->mBuckets)[offset];
if (!list_size(&bucket->mEntries)) {
logError("Unable to find key in map.");
logErrorString(tKey);
recoverFromError();
}
ListIterator it = list_iterator_begin(&bucket->mEntries);
while (1) {
StringMapBucketListEntry* e = (StringMapBucketListEntry*)list_iterator_get(it);
if (!strcmp(tKey, e->mKey)) {
return e->mData;
}
if (!list_has_next(it)) {
logError("Unable to find key in map.");
logErrorString(tKey);
recoverFromError();
}
list_iterator_increase(&it);
}
}
typedef struct {
void* mCaller;
stringMapMapCB mCB;
} StringMapCaller;
static void string_map_map_single_list_entry(void* tCaller, void* tData) {
StringMapCaller* caller = (StringMapCaller*)tCaller;
StringMapBucketListEntry* e = (StringMapBucketListEntry*)tData;
caller->mCB(caller->mCaller, e->mKey, e->mData);
}
static void string_map_map_single_bucket(StringMapCaller* tCaller, StringMapBucket* e) {
list_map(&e->mEntries, string_map_map_single_list_entry, tCaller);
}
void string_map_map(StringMap* tMap, stringMapMapCB tCB, void* tCaller) {
StringMapCaller caller;
caller.mCaller = tCaller;
caller.mCB = tCB;
int i;
for (i = 0; i < MAP_MODULO; i++) {
StringMapBucket* bucket = &((StringMapBucket*)tMap->mBuckets)[i];
string_map_map_single_bucket(&caller, bucket);
}
}
int string_map_contains(StringMap* tMap, const char* tKey) {
int offset = getBucketIDFromString((uint8_t*)tKey);
StringMapBucket* bucket = &((StringMapBucket*)tMap->mBuckets)[offset];
if (!list_size(&bucket->mEntries)) return 0;
ListIterator it = list_iterator_begin(&bucket->mEntries);
while (1) {
StringMapBucketListEntry* e = (StringMapBucketListEntry*)list_iterator_get(it);
if (!strcmp(tKey, e->mKey)) {
return 1;
}
if (!list_has_next(it)) {
break;
}
list_iterator_increase(&it);
}
return 0;
}
int string_map_size(StringMap* tMap) {
return tMap->mSize;
}
IntMap new_int_map()
{
return new_string_map();
}
void delete_int_map(IntMap * tMap)
{
delete_string_map(tMap);
}
void int_map_empty(IntMap * tMap)
{
string_map_empty(tMap);
}
static int gIntMapIndex;
void int_map_push_owned(IntMap * tMap, int tKey, void * tData)
{
char str[100];
sprintf(str, "%d", tKey);
string_map_push_owned(tMap, str, tData);
}
int int_map_push_back_owned(IntMap * tMap, void * tData)
{
int nkey = gIntMapIndex++;
int_map_push_owned(tMap, nkey, tData);
return nkey;
}
void int_map_push(IntMap * tMap, int tKey, void * tData)
{
char str[100];
sprintf(str, "%d", tKey);
string_map_push(tMap, str, tData);
}
int int_map_push_back(IntMap * tMap, void * tData)
{
int nkey = gIntMapIndex++;
int_map_push(tMap, nkey, tData);
return nkey;
}
void int_map_remove(IntMap * tMap, int tKey)
{
char str[100];
sprintf(str, "%d", tKey);
string_map_remove(tMap, str);
}
void* int_map_get(IntMap * tMap, int tKey)
{
char str[100];
sprintf(str, "%d", tKey);
return string_map_get(tMap, str);
}
typedef struct {
void* mCaller;
mapCB mCB;
} IntMapCaller;
static void int_map_map_single_list_entry(void* tCaller, void* tData) {
IntMapCaller* caller = (IntMapCaller*)tCaller;
StringMapBucketListEntry* e = (StringMapBucketListEntry*)tData;
caller->mCB(caller->mCaller, e->mData);
}
static void int_map_map_single_bucket(IntMapCaller* tCaller, StringMapBucket* e) {
list_map(&e->mEntries, int_map_map_single_list_entry, tCaller);
}
void int_map_map(IntMap* tMap, mapCB tCB, void* tCaller) {
IntMapCaller caller;
caller.mCaller = tCaller;
caller.mCB = tCB;
int i;
for (i = 0; i < MAP_MODULO; i++) {
StringMapBucket* bucket = &((StringMapBucket*)tMap->mBuckets)[i];
int_map_map_single_bucket(&caller, bucket);
}
}
typedef struct {
void* mCaller;
predicateCB mCB;
IntMap* mMap;
} IntPredicateCaller;
static int int_map_remove_predicate_single_list_entry(void* tCaller, void* tData) {
IntPredicateCaller* caller = (IntPredicateCaller*)tCaller;
StringMapBucketListEntry* e = (StringMapBucketListEntry*)tData;
int ret = caller->mCB(caller->mCaller, e->mData);
if (ret) {
string_map_remove_element(caller->mMap, e);
}
return ret;
}
static void int_map_remove_predicate_single_bucket(IntPredicateCaller* tCaller, StringMapBucket* e) {
list_remove_predicate(&e->mEntries, int_map_remove_predicate_single_list_entry, tCaller);
}
void int_map_remove_predicate(IntMap * tMap, predicateCB tCB, void * tCaller)
{
IntPredicateCaller caller;
caller.mCaller = tCaller;
caller.mCB = tCB;
caller.mMap = tMap;
int i;
for (i = 0; i < MAP_MODULO; i++) {
StringMapBucket* bucket = &((StringMapBucket*)tMap->mBuckets)[i];
int_map_remove_predicate_single_bucket(&caller, bucket);
}
}
int int_map_contains(IntMap * tMap, int tKey)
{
char str[100];
sprintf(str, "%d", tKey);
return string_map_contains(tMap, str);
}
int int_map_size(IntMap* tMap) {
return string_map_size(tMap);
}
static SuffixTreeEntryNode createEmptyNode() {
SuffixTreeEntryNode node;
node.mEntry = NULL;
node.mChildren = NULL;
return node;
}
SuffixTree new_suffix_tree()
{
SuffixTree ret;
ret.mSize = 0;
ret.mRoot = createEmptyNode();
return ret;
}
static void new_suffix_tree_from_string_map_copy_single(void* tCaller, char* tKey, void* tData) {
SuffixTree* tree = (SuffixTree*)tCaller;
suffix_tree_push(tree, tKey, tData);
}
SuffixTree new_suffix_tree_from_string_map(StringMap * tMap)
{
SuffixTree ret = new_suffix_tree();
string_map_map(tMap, new_suffix_tree_from_string_map_copy_single, &ret);
return ret;
}
#define SUFFIX_TREE_CHILDREN_AMOUNT (128 - 0x20)
static void delete_suffy_tree_recursively(SuffixTreeEntryNode* tNode) {
if (tNode->mEntry) {
SuffixTreeEntryData* entry = tNode->mEntry;
if (entry->mIsOwned) {
freeMemory(entry->mData);
}
freeMemory(tNode->mEntry);
}
if (tNode->mChildren) {
int i;
for (i = 0; i < SUFFIX_TREE_CHILDREN_AMOUNT; i++) {
delete_suffy_tree_recursively(&tNode->mChildren[i]);
}
freeMemory(tNode->mChildren);
}
}
void delete_suffix_tree(SuffixTree * tTree)
{
delete_suffy_tree_recursively(&tTree->mRoot);
}
static void suffix_tree_add_data_to_node(SuffixTreeEntryNode* tNode, void* tData, int tIsOwned) {
tNode->mEntry = (SuffixTreeEntryData*)allocMemory(sizeof(SuffixTreeEntryData));
tNode->mEntry->mData = tData;
tNode->mEntry->mIsOwned = tIsOwned;
}
static void suffix_tree_add_children_to_node(SuffixTreeEntryNode* tNode) {
tNode->mChildren = (SuffixTreeEntryNode*)allocClearedMemory(SUFFIX_TREE_CHILDREN_AMOUNT, sizeof(SuffixTreeEntryNode));
}
static void suffix_tree_push_recursive(SuffixTreeEntryNode* tNode, char* tRestString, void* tData, int tIsOwned) {
if (*tRestString == '\0') {
suffix_tree_add_data_to_node(tNode, tData, tIsOwned);
return;
}
if (!tNode->mChildren) {
suffix_tree_add_children_to_node(tNode);
}
char currentChar = *tRestString;
int index = currentChar - 0x20;
suffix_tree_push_recursive(&tNode->mChildren[index], tRestString + 1, tData, tIsOwned);
}
static void suffix_tree_push_general(SuffixTree* tTree, char* tKey, void* tData, int tIsOwned) {
suffix_tree_push_recursive(&tTree->mRoot, tKey, tData, tIsOwned);
}
void suffix_tree_push_owned(SuffixTree * tTree, char * tKey, void * tData)
{
suffix_tree_push_general(tTree, tKey, tData, 1);
}
void suffix_tree_push(SuffixTree * tTree, char * tKey, void * tData)
{