-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathishtar.h
1515 lines (1177 loc) · 37.9 KB
/
ishtar.h
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
/// Ishtar v1.0 - A single-file suite of tools for C++.
///
/// To include the library in your project, copy this file (ishtar.h) into the directory where you usually
/// keep your libraries, make a new translation unit (ishtar.cpp, for example), use `#define ISHTAR_IMPL` before the include statement,
/// and make sure to compile that translation unit with your project.
///
/// For example:
///
/// ```c++
/// #define ISHTAR_IMPL
/// #include "ishtar.h"
///
/// // ...
/// ```
///
/// If you have any inquires or potential bugs, please contact me: https://frodoalaska.github.io/
#pragma once
#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <cstring>
namespace ishtar { // Start of ishtar
////////////////////////////////////////////////////////////////////
/// Typedefs
/// char
typedef char i8;
/// short
typedef short i16;
/// int
typedef int i32;
/// long
typedef long i64;
/// unsigned char
typedef unsigned char u8;
/// unsigned short
typedef unsigned short u16;
/// unsigned int
typedef unsigned int u32;
/// unsigned long
typedef unsigned long u64;
/// size_t
typedef size_t sizei;
/// Typedefs
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/// Function pointers
/// Function for iterating over `LinkedList`, `Queue`, and `Stack`
template<typename T>
using ListForEachFn = void(*)(T& value);
/// Function for iterating over `DynamicArray` and `String`
template<typename T>
using ArrayForEachFn = void(*)(T& value, const sizei index);
/// Function for iterating over a `HashTable`
template<typename K, typename V>
using TableForEachFn = void(*)(const K& key, V& value);
/// Hashing function prototype
template<typename K>
using HashFn = const u64(*)(const K& key);
/// Allocation function prototype
template<typename T>
using AllocFn = T*(*)(const sizei count, const sizei element_size);
/// Free function prototype
template<typename T>
using FreeFn = void(*)(T* ptr);
/// Function pointers
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/// Defines
/// Underflowing a `sizei` type to get a "non position" which can indicate
/// a string error
#define STRING_NPOS ((sizei)-1)
/// `HashTable` uses this to determine to grow its size
#define HASH_TABLE_LOAD_FACTOR 0.7
/// Defines
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/// Node
/// A generic node class of type `T`
template<typename T>
class Node {
public:
/// Default CTOR
Node() = default;
/// Assigns `val` to the node and as well as `next` and `previous`
Node(const T& val, Node<T>* next = nullptr, Node<T>* previous = nullptr) {
value = val;
this->next = next;
this->previous = previous;
}
public:
T value;
Node<T>* next;
Node<T>* previous;
};
/// Node
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/// LinedList
template<typename T>
class LinkedList {
public:
/// Takes in the given `head` as the start of the list
LinkedList(Node<T>* head = nullptr) {
this->head = head;
tail = nullptr;
count = head != nullptr ? 1 : 0;
}
/// Will assign `val` to the head of the list
LinkedList(const T& val) {
head = new Node<T>(val);
tail = nullptr;
count = 1;
}
public:
Node<T>* head;
Node<T>* tail;
sizei count;
public:
/// Retrieves node at the given `index`.
/// NOTE: This function will assert if `index` is out of range
Node<T>* get_at(const sizei index) {
assert(index >= 0 && index < count);
Node<T>* node = head;
for(sizei i = 0; node && i < count; i++) {
if(i == index) {
return node;
}
node = node->next;
}
return nullptr;
}
/// Add the given `node` to the tail of the list
/// NOTE: If the given `node` is `nullptr` this function will do nothing
void append(Node<T>* node) {
// Not a valid node in the first place
if(!node) {
return;
}
// Still need to make sure the head is valid
if(!head) {
prepend(node);
}
// Place as tail if there is no tail
else if(!tail) {
node->previous = head;
node->next = nullptr;
tail = node;
head->next = tail;
}
// Otherwise, it's a normal node and append it to the tail
else {
node->next = nullptr;
node->previous = tail;
tail->next = node;
// The new node is now the tail
tail = node;
}
// New node added so increase the count
count++;
}
/// Add the given `node` to the head of the list.
/// NOTE: If the given `node` is `nullptr` this function will do nothing
void prepend(Node<T>* node) {
// Not a valid node in the first place
if(!node) {
return;
}
// The node becomes the new head if no head exists
if(!head) {
head = node;
head->next = tail;
head->previous = nullptr;
}
// Just a plain simple prepend operation
else {
node->next = head;
node->previous = nullptr;
head->previous = node;
// The node now becomes the new head
head = node;
}
// New node added so increase the count
count++;
}
/// Insert the given `node` between `prev` and `next`
/// NOTE: If the given `node` is `nullptr` this function will do nothing
void insert(Node<T>* node, Node<T>* prev, Node<T>* next) {
// Not a valid node in the first place
if(!node) {
return;
}
// Placing the node in between the `next` and `prev`
node->previous = prev;
node->next = next;
// Relinking the nodes
next->previous = node;
prev->next = node;
// New node added so increase the count
count++;
}
/// Remove the given `node` from the list completely.
/// NOTE: This function _will_ de-allocate the given node
void remove(Node<T>* node) {
// Not a valid node in the first place or there's just nothing to remove
if(!node || count == 0) {
return;
}
// Relinking the nodes
if(node->next) {
node->next->previous = node->previous;
}
if(node->previous) {
node->previous->next = node->next;
}
// Some memory cleanup
free(node);
// A node was removed so decrease the count (but don't go below zero)
count -= count == 0 ? 0 : 1;
}
/// Remove the node at the given `index` from the list completely.
/// NOTE: This function _will_ de-allocate the given node
void remove_at(const sizei index) {
// Must be a valid index
assert(index >= 0 && index < count);
// Just pop the head
if(index == 0) {
Node<T>* popped = pop_front();
remove(popped);
return;
}
// Just pop the tail
else if(index == (count - 1)) {
Node<T>* popped = pop_back();
remove(popped);
return;
}
// Otherwise, just remove the node
Node<T>* node = get_at(index);
remove(node);
}
/// Prepend a new node with the value `val` to the front of the list
/// NOTE: This function will allocate `Node<T>`
void emplace_front(const T& val) {
Node<T>* node = (Node<T>*)malloc(sizeof(Node<T>));
node->value = val;
prepend(node);
}
/// Append a new node with the value `val` to the back of the list
/// NOTE: This function will allocate `Node<T>`
void emplace_back(const T& val) {
Node<T>* node = (Node<T>*)malloc(sizeof(Node<T>));
node->value = val;
append(node);
}
/// Insert a new node with value of `val` at the given `index`.
/// NOTE: This function will allocate `Node<T>`.
/// NOTE: This function will also assert if the `index` is out of range.
void emplace_at(const T& val, const sizei index) {
// Can't insert past the end
assert(index >= 0 && index <= count);
Node<T>* node = (Node<T>*)malloc(sizeof(Node<T>));
node->value = val;
if(index == 0) {
prepend(node);
return;
}
else if(index == count) {
append(node);
return;
}
// Otherwise, find the node at `index` and insert
Node<T>* curr = get_at(index);
insert(node, curr->previous, curr);
}
/// Remove and return a valid node at the head of the list
Node<T>* pop_front() {
// No head exists in the first place
if(!head) {
return nullptr;
}
// A node was removed so decrease the count (but don't go below zero)
count -= count == 0 ? 0 : 1;
// Relocating the head
Node<T>* old_head = head;
old_head->next->previous = nullptr;
head = old_head->next;
return old_head;
}
/// Remove and return a valid node at the tail of the list
Node<T>* pop_back() {
// No tail exists in the first place
if(!tail) {
return nullptr;
}
// A node was removed so decrease the count (but don't go below zero)
count -= count == 0 ? 0 : 1;
// Relocating the tail
Node<T>* old_tail = tail;
old_tail->previous->next = nullptr;
tail = old_tail->previous;
return old_tail;
}
/// Return the value of the node at the given `index`
const T& peek_at(const sizei index) {
return get_at(index)->value;
}
/// De-allocate each node from the list completely
void clear() {
Node<T>* node = head;
// Clearing all the memory of each node
while(node != nullptr) {
// Moving the loop forwards
Node<T>* old_node = node;
node = node->next;
// Deleting the node from existence
delete old_node;
// One less node to worry about...
count--;
}
}
/// Call the given `func` with each node in the list
void for_each(const ListForEachFn<T>& func) {
if(!func) {
return;
}
for(auto node = head; node != nullptr; node = node->next) {
func(node->value);
}
}
};
/// LinedList
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/// Queue
/// A simple queue FIFO structure with nodes of type `T`
template<typename T>
class Queue {
public:
/// Default CTOR
Queue()
:count(0), head(nullptr), tail(nullptr)
{}
public:
sizei count;
Node<T>* head;
Node<T>* tail;
/// Add `node` to the queue.
/// NOTE: This function will do nothing if `node` is a `nullptr`.
void push(Node<T>* node) {
// The given node is invalid
if(!node) {
return;
}
// This is possibly the first element in the queue
if(!head) {
head = node;
head->next = nullptr;
}
// Place as tail if there is no tail
else if(!tail) {
node->next = nullptr;
tail = node;
head->next = tail;
}
// Otherwise, it's a normal node and push it to the queue
else {
tail->next = node;
node->next = nullptr;
// The new node is now the tail
tail = node;
}
// New node added so increase the count
count++;
}
/// Remove and return the current head node of the queue.
/// NOTE: This function will return a `nullptr` if the queue is empty
Node<T>* pop() {
// There's nothing in the queue
if(!head) {
return nullptr;
}
// Don't go below zero when popping
count -= count == 0 ? 0 : 1;
// Relocating the head and popping the old head
Node<T>* old_head = head;
head = head->next;
return old_head;
}
/// Push a new node with the value of `val` to the queue.
/// NOTE: This function will allocate memory.
void emplace(const T& val) {
Node<T>* node = (Node<T>*)malloc(sizeof(Node<T>));
node->value = val;
push(node);
}
/// Iterate through each element in the queue and call the given `func` function.
void for_each(const ListForEachFn<T>& func) {
if(!func) {
return;
}
for(auto node = head; node != nullptr; node = node->next) {
func(node->value);
}
}
};
/// Queue
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/// Stack
/// A simple stack LIFO structure with nodes of type `T`
template<typename T>
class Stack {
public:
/// Default CTOR
Stack()
:count(0), head(nullptr)
{}
public:
sizei count;
Node<T>* head;
/// Push `node` to the back of the stack
void push(Node<T>* node) {
if(!node) {
return;
}
// This is possibly the first element in the stack
if(!head) {
head = node;
head->next = nullptr;
}
// Otherwise, it's a normal node and push it to the stack
else {
node->next = head;
head = node;
}
// New node added so increase the count
count++;
}
/// Remove and return the node at the back of the stack.
/// NODE: This function will return `nullptr` if the stack is empty.
Node<T>* pop() {
// There's nothing in the stack
if(count == 0) {
return nullptr;
}
// Don't go below zero when popping
count -= count == 0 ? 0 : 1;
// Relocating the head and popping the old head
Node<T>* old_head = head;
head = head->next;
old_head->next = nullptr;
return old_head;
}
/// Push a new node with the value of `val` to the stack.
/// NOTE: This function will allocate memory.
void emplace(const T& val) {
Node<T>* node = (Node<T>*)malloc(sizeof(Node<T>));
node->value = val;
push(node);
}
/// Iterate through each node in the stack and call the given `func` function.
void for_each(const ListForEachFn<T>& func) {
if(!func) {
return;
}
for(auto node = head; node != nullptr; node = node->next) {
func(node);
}
}
};
/// Stack
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/// DynamicArray
/// A dynamically resizable array with each element being of type `T`
template<typename T>
class DynamicArray {
public:
/// Default CTOR.
/// NOTE: This CTOR will have a `capacity` of 5.
DynamicArray() {
capacity = 5;
size = 0;
data = (T*)malloc(sizeof(T) * capacity);
}
/// Set the `capacity` member of the dynamic array to `initial_capacity`
DynamicArray(const sizei initial_capacity) {
capacity = initial_capacity;
size = 0;
data = (T*)malloc(sizeof(T) * capacity);
}
/// Set the given `dat` with size of `dat_size` to the member `data` and `size`.
/// NOTE: The `capacity` will be: `dat_size + (dat_size / 2)`.
DynamicArray(T* dat, const sizei dat_size) {
size = dat_size;
capacity = size + (size / 2);
data = dat;
}
public:
T* data;
sizei capacity, size;
public:
/// Make room for `new_capacity` amount of elements in the array.
/// This will not add new elements in the array. It will only allocate enough
/// memory for `new_capacity` of elements to be added.
/// NOTE: Keep in mind that if the given `new_capacity` is _smaller_ than
/// the current `capacity`, the array will _shrink_ in size, naturally.
/// However, if the opposite is true, than the array will _bloat_ its size.
void reserve(const sizei new_capacity) {
// There's nothing to be done when the given `new_capacity` is
// the _same_ as the current `capacity`
if(new_capacity == capacity) {
return;
}
capacity = new_capacity;
data = (T*)realloc(data, sizeof(T) * capacity);
// Assert just in case the system runs out of memory
assert(data != nullptr);
}
/// Resize the array by `new_size`. This will add new elements to the array.
/// NOTE: This function will call `reserve` if the `new_size` is greater than `capacity`.
void resize(const sizei new_size) {
sizei old_size = size;
size = new_size;
// Grow the array by half the new size if need be
if(size >= capacity) {
reserve(capacity + (size / 2));
}
// We set _only_ the new elements in the array to a default value.
// This way, we won't need to touch the already-existing values.
data = memset(data + (sizeof(T) * old_size), 0, sizeof(T) * new_size);
}
/// Add a new element with a value of `val` to the array.
/// NOTE: This function will call `reserve` if the array has no more space for elements
void append(const T& val) {
size++;
// Grow the array if need be
if(size >= capacity) {
reserve(capacity + (size / 2));
}
// Add the new value to the array
data[size - 1] = val;
}
/// Remove and return the last added element in the array.
T& pop_back() {
// There's nothing in the array in the first place
if(size == 0) {
return data[size];
}
// We pop the element from the back and ignore it for now
T& element = data[size - 1];
size--;
return element;
}
/// Remove and return the first element in the array.
T& pop_front() {
T& element = data[0];
// Reshuffle the entire array
for(sizei i = 1; i < size; i++) {
data[i - 1] = data[i];
}
size--;
return element;
}
/// Check the value in the beginning of the array.
const T& peek_front() {
return data[0];
}
/// Check the value at the end of the array.
const T& peek_back() {
return data[size - 1];
}
/// Completely remove the element at `index` from the array.
/// NOTE: This function will not do any memory de-allocations.
void remove(const sizei index) {
for(sizei i = index; i < size; i++) {
data[i] = data[i + 1];
}
size--;
}
/// Create a new `DynamicArray` reaching from `begin` till `end` elements.
/// Both `begin` and `end` are inclusive.
/// NOTE: This function will allocate memory and will create a copy.
DynamicArray<T> slice(const sizei begin, const sizei end) {
sizei new_data_size = (end - begin) + 1; // Make sure that `end` is _inclusive_
T* new_data = (T*)malloc(sizeof(T) * new_data_size);
// Copying over the correct data
for(sizei i = begin, j = 0; i < new_data_size || j < new_data_size; i++, j++) {
new_data[j] = at(i);
}
return DynamicArray<T>(new_data, new_data_size);
}
/// Fill `count` amount of elements with values of `value`.
void fill(const sizei count, const T& value) {
for(sizei i = 0; i < count; i++) {
append(value);
}
}
/// Free the underlying array buffer and reset the `capacity` and `size` members.
void clear() {
free(data);
size = 0;
capacity = 0;
}
/// Retrieve element at `index`.
/// NOTE: This function will assert if the `index` is out of bounds.
const T& at(const sizei index) {
// Index out of bounds
assert((index >= 0 && index < size));
// Otherwise, just return the value
return data[index];
}
/// Iterate through each element in the array and call the function `func`.
void for_each(const ArrayForEachFn<T>& func) {
if(!func) {
return;
}
for(sizei i = 0; i < size; i++) {
func(data[i], i);
}
}
// Operator overloading for []
const T& operator[](const sizei index) {
return at(index);
}
const T& operator[](const sizei index) const {
return at(index);
}
};
/// DynamicArray
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/// String
// ASCII string class
class String {
public:
/// Default CTOR
String() = default;
/// Copy CTORs
String(const String& other);
String(String&& other);
/// Make a `String` out of the given `str`
String(const char* str);
/// Take in the given `str` with the length `str_len` and create a new `String`.
String(const char* str, const sizei str_len);
~String();
public:
sizei length = 0;
char* data = nullptr;
public:
/// Retrieve `char` at `index`.
/// NOTE: This function will assert if the `index` is out of bounds.
const char& at(const sizei index) const {
assert(index >= 0 && index < length);
return data[index];
}
char& operator[](const sizei index) {
return (char&)at(index); // Remove const because I'm mean
}
const char& operator[](const sizei index) const {
return at(index);
}
String& operator=(const String& other) {
copy(other);
return *this;
}
const bool operator==(const String& other) {
return compare(other);
}
const bool operator!=(const String& other) {
return !compare(other);
}
/// Copy the given `str` with the length of `str_len`.
void copy(const char* str, const sizei str_len);
/// Copy the given `String` `str`.
void copy(const String& str);
/// Copy the given C-string `str`.
void copy(const char* str);
/// Return `true` if the `String` is empty
const bool is_empty();
/// Add the given `other` `String` to the current `String`
void append(const String& other);
/// Add the given `other` C-string to the current `String`
void append(const char* other);
/// Add the given `ch` `char` to the end of the current `String`.
void append(const char& ch);
/// Add the given `ch` `char` at `index`.
void append_at(const sizei index, const char& ch);
/// Create a new `String` with the contents of the current `String`
/// starting from `begin` till `end`.
/// NOTE: The given `begin` is inclusive while `end` is exclusive.
String slice(const sizei begin, const sizei end);
/// Return `true` the current `String` matches the `other` `String`.
const bool compare(const String& other);
/// Completely reverse the contents of the current `String`.
/// NOTE: This will allocate a temporary buffer and free it.
void reverse();
/// Starting at `start`, go through the `String` and try to find `ch`.
/// Return the index of `ch` if it was found.
/// Otherwise, return `STRING_NPOS`.
const sizei find(const char& ch, const sizei start = 0);
/// Start at the end of the `String` and find the last instance of `ch`.
const sizei find_last_of(const char& ch);
/// Start at the start of the `String` and find the first instance of `ch`.
const sizei find_first_of(const char& ch);
/// Completely remove all of the `char`s starting from `begin` till `end`.
/// NOTE: The given `begin` is inclusive while `end` is exclusive.
void remove(const sizei begin, const sizei end);
/// Equivalent to `String[index] = ch`.
void replace_at(const sizei index, const char& ch);
/// Go through the entire `String`, find `ch1`, and replace it with `ch2`.
/// NOTE: If `ch1` was not found, this function will do nothing.
void replace(const char& ch1, const char& ch2);
/// Replace every instance of `ch1` in the `String` with `ch2`.
/// NOTE: If `ch1` was not found, this function will do nothing.
void replace_all_of(const char& ch1, const char& ch2);
/// Return `true` if `ch` was found in the current `String`.
const bool has(const char& ch);
/// Return `true` if `ch` was found at exactly `index`.
const bool has_at(const sizei index, const char& ch);
/// Convert the `String` into a C-string format.
const char* c_str() const;
/// Fill `len` amount of elements with `ch`.
void fill(const sizei len, const char& ch);
/// Iterate over each `char` in the `String` and call the given
/// `func` function.
void for_each(const ArrayForEachFn<char>& func);
private:
const sizei string_length(const char* str) {
return strlen(str);
}
};
/// String
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/// HashTable functions
/// Return a scrambled (hashed) `u64` number from `str`.
const u64 hash_key(const char* str);
/// Return a scrambled (hashed) `u64` number from `str`.
const u64 hash_key(const String& str);
/// Return a scrambled (hashed) `u64` number from `key` of type `T`.
template<typename K>
const u64 hash_key(const K& key) {
u32 hash = 2166136261u;
hash ^= key;
hash *= 1677719;
return hash;
}
/// HashTable functions
/// HashTable
/// A hash table with key of type `K` and value of type `V`.
template<typename K, typename V>
class HashTable {
public:
/// Default CTOR.
/// Starts off with a `capacity` of 5.
HashTable()
:size(0), capacity(5), entries(new TableEntry*[capacity]), hash_fn(hash_key)
{}
/// Copy CTORs.
HashTable(const HashTable&) = default;
HashTable(HashTable&&) = default;
/// Create a new `HashTable` with the `capacity` memeber set to `initial_capacity`, using
/// the `hash_fn` to hash each entry's key.
HashTable(const sizei initial_capacity, const HashFn<K>& hash_fn = hash_key) {
size = 0;
capacity = initial_capacity;
entries = new TableEntry*[capacity];
this->hash_fn = hash_fn;
}
private:
struct TableEntry {
K key;
V value;
TableEntry* next = nullptr;
sizei index = 0;
};
public:
sizei size;
sizei capacity;
TableEntry** entries = nullptr;
HashFn<K> hash_fn;
public:
/// De-allocate any memory used by the `HashTable`.
void clear() {
if(!entries) {
return;