-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patht.h
826 lines (676 loc) · 19.3 KB
/
t.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
#include <cstddef>
#include <cstring>
#include <initializer_list>
#include <iostream>
#include <memory>
#include <string>
#include <cstdlib>
#include <cstdint>
#include <map>
#include <tuple>
typedef uint64_t uint_pr;
typedef std::string str;
typedef float f32;
typedef double f64;
template <typename T>
inline typename std::enable_if<std::is_arithmetic<T>::value, str>::type
proto_str(T value) {
return std::to_string(value);
}
inline str proto_str(const str s) {
return "\"" + s + "\"";
}
inline str proto_str(const char c) {
return str(1, c);
}
inline str proto_str(const bool b) {
return b ? "true" : "false";
}
inline void panic(int line, const str sourcefile, const str msg) {
// should unwind the call stack and then show the error message
std::cout << sourcefile << ":" << line << ":" << " " << msg << std::endl;
std::exit(EXIT_FAILURE);
}
template<typename Fn>
struct Defer {
Fn f;
Defer(Fn f) : f(f) {}
~Defer() { f(); }
};
template<typename Fn>
Defer<Fn> defer_func(Fn f) {
return Defer<Fn>(f);
}
#define DEFER_1(x, y) x##y
#define DEFER_2(x, y) DEFER_1(x, y)
#define DEFER_3(x) DEFER_2(x, __COUNTER__)
#define defer(code) auto DEFER_3(_defer_) = defer_func([&](){code;})
template <typename InnerType>
class Enum {
private:
InnerType* start;
InnerType* end_;
uint_pr index = 0;
public:
explicit Enum(InnerType* s, InnerType* e) : start(s), end_(e) {}
class Iterator {
private:
uint_pr index;
InnerType* item;
public:
Iterator(uint_pr in, InnerType* i) : index(in), item(i) {}
std::pair<uint_pr, InnerType&> operator*() const { return {index, *item}; }
Iterator& operator++() { index++; item++; return *this; }
bool operator!=(const Iterator& i) { return item != i.item; }
};
Iterator begin() const { return Iterator(0, start); };
Iterator end() const { return Iterator(static_cast<uint_pr>(-1), end_); };
};
template<typename T>
class Option {
private:
T data;
public:
enum {
Some,
None
} tag;
Option(T item) : data(item) {
tag = Some;
}
Option() : data() {
tag = None;
}
const bool is_some() const {
return tag == Some;
}
bool is_some() {
return tag == Some;
}
const bool is_none() const {
return tag == None;
}
bool is_none() {
return tag == None;
}
inline T& unwrap() {
// should use a panic() function to fail if Option.is_none() is true
if (this->is_none()) {
panic(__LINE__, __FILE_NAME__, "attempted to unwrap an Option::None.");
}
return data;
}
inline const T& unwrap() const {
// should use a panic() function to fail if Option.is_none() is true
if (this->is_none()) {
panic(__LINE__, __FILE_NAME__, "attempted to unwrap an Option::None.");
}
return data;
}
};
template<typename T>
inline str proto_str(const Option<T> op) {
str s = "";
if (op.is_some()) {
s += "some ";
s += proto_str(op.unwrap());
} else {
s = "none";
}
return s;
}
template<typename T>
class Slice {
private:
T* start;
uint_pr length;
uint_pr arr_capacity;
bool allocates;
public:
Slice(T* s, uint_pr len, uint_pr cap)
: start(s), length(len), arr_capacity(cap), allocates(false) {
// std::cout << "Slice(T* s, uint_pr len, uint_pr cap) constructor called\n";
}
Slice(uint_pr cap)
: length(0), arr_capacity(cap), allocates(true) {
start = (T*) malloc(sizeof(T) * cap);
std::memset(start, 0, cap);
// std::cout << "Slice(uint_pr cap) constructor called\n";
// std::cout << "allocated memory for " << cap << " items...\n";
}
Enum<T> enumerate() const {
return Enum(start, start + length);
}
Slice() : Slice(16) {
// std::cout << "Slice() constructor called\n";
}
// Copy constructor
Slice(const Slice& other)
: start(other.start), length(other.length),
arr_capacity(other.arr_capacity), allocates(false) {
// std::cout << "Slice(const Slice& other) constructor called\n";
}
Slice& operator=(const Slice& other) {
if (this == &other) { return *this; }
// std::cout << "operator=(const Slice& other) constructor called\n";
start = other.start;
length = other.length;
arr_capacity = other.arr_capacity;
allocates = false;
return *this;
}
~Slice() {
if (allocates) {
free(start);
// std::cout << "cleaning up Slice" << std::endl;
}
}
constexpr uint_pr len() const noexcept {
return length;
}
constexpr uint_pr cap() const noexcept {
return arr_capacity;
}
void append(T item) {
if (length >= arr_capacity) {
// Double the current capacity and align to the next power of two
uint_pr new_capacity = next_power_of_two(arr_capacity * 2);
T* new_array = (T*) malloc(sizeof(T) * new_capacity);
std::memcpy(new_array, start, arr_capacity * sizeof(T));
if (allocates) {
free(start);
}
start = new_array;
arr_capacity = new_capacity;
allocates = true;
// std::cout << "allocated memory for " << arr_capacity << " items...\n";
}
start[length] = item;
length += 1;
}
// Helper function to find the next power of two
static uint_pr next_power_of_two(uint_pr n) {
if (n == 0) return 1;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n |= n >> 32;
return n + 1;
}
T& operator[](uint_pr index) {
return start[index];
}
const T& operator[](uint_pr index) const {
return start[index];
}
Option<T> get(uint_pr index) {
if (index >= length) {
return Option<T>();
}
return Option<T>(start[index]);
}
const Option<T> get(uint_pr index) const {
if (index >= length) {
return Option<T>();
}
return Option<T>(start[index]);
}
inline Slice<T> make_slice(uint_pr start, uint_pr end_exclusive) {
if (start >= length || end_exclusive > length || start >= end_exclusive) {
panic(__LINE__, __FILE__, "Invalid slice bounds");
}
return Slice<T>(this->start + start, end_exclusive - start, arr_capacity - start);
}
inline const Slice<T> make_slice(uint_pr start, uint_pr end_exclusive) const {
if (start >= length || end_exclusive > length || start >= end_exclusive) {
panic(__LINE__, __FILE__, "Invalid slice bounds");
}
return Slice<T>(this->start + start, end_exclusive - start, arr_capacity - start);
}
inline Slice<T> make_slice_from(uint_pr start) {
if (start >= length) {
panic(__LINE__, __FILE__, "Invalid slice bounds");
}
return Slice<T>(this->start + start, length - start, arr_capacity - start);
}
inline const Slice<T> make_slice_from(uint_pr start) const {
if (start >= length) {
panic(__LINE__, __FILE__, "Invalid slice bounds");
}
return Slice<T>(this->start + start, length - start, arr_capacity - start);
}
// Begin and end methods for range-based for loops
inline T* begin() noexcept { return start; }
inline T* end() noexcept { return start + length; }
inline const T* begin() const noexcept { return start; }
inline const T* end() const noexcept { return start + length; }
};
template<typename T, uint_pr N>
class Array {
private:
T data[N];
public:
explicit Array(std::initializer_list<T> init) {
std::copy(init.begin(), init.end(), data);
}
Enum<T> enumerate() const {
return Enum<T>((T*)data + 0, (T*)data + N);
}
T& operator[](std::size_t index) {
return data[index];
}
const T& operator[](uint_pr index) const {
return data[index];
}
Option<T> get(uint_pr index) {
if (index >= N) {
return Option<T>();
}
return Option<T>(data[index]);
}
const Option<T> get(uint_pr index) const {
if (index >= N) {
return Option<T>();
}
return Option<T>(data[index]);
}
inline Slice<T> make_slice(uint_pr start, uint_pr end_exclusive) {
if (start >= N || end_exclusive > N || start >= end_exclusive) {
panic(__LINE__, __FILE__, "Invalid slice bounds");
}
return Slice<T>(data + start, end_exclusive - start, len() - start);
}
inline const Slice<T> make_slice(uint_pr start, uint_pr end_exclusive) const {
if (start >= N || end_exclusive > N || start >= end_exclusive) {
panic(__LINE__, __FILE__, "Invalid slice bounds");
}
// HACK:
// when you make a slice of a constant array, we make a copy of the array;
// std::copy(data, data + len(), start_copy);
// OR UNSAFE HACK:
// grab data and unsafe cast it to a T*. this is Sparta!
return Slice<T>((T*)data + start, end_exclusive - start, len() - start);
}
inline Slice<T> make_slice_from(uint_pr start) {
if (start >= N) {
panic(__LINE__, __FILE__, "Invalid slice bounds");
}
return Slice<T>(data + start, len() - start, len() - start);
}
inline const Slice<T> make_slice_from(uint_pr start) const {
if (start >= N) {
panic(__LINE__, __FILE__, "Invalid slice bounds");
}
// HACK:
// when you make a slice of a constant array, we make a copy of the array;
// std::copy(data, data + len(), start_copy);
// OR UNSAFE HACK:
// grab data and unsafe cast it to a T*. this is Sparta!
return Slice<T>((T*)data, len() - start, len() - start);
}
inline constexpr uint_pr len() const noexcept {
return N;
}
// Begin and end methods for range-based for loops
inline T* begin() noexcept { return data; }
inline T* end() noexcept { return data + N; }
inline const T* begin() const noexcept { return data; }
inline const T* end() const noexcept { return data + N; }
};
template<typename Key, typename Value>
class HashMap {
// private:
public:
std::map<Key, Value> contents;
HashMap(std::initializer_list<typename std::pair<const Key, Value>> init) : contents(init) {}
inline constexpr uint_pr len() const noexcept {
return contents.size();
}
Value& operator[](Key key) {
return contents[key];
}
const Value& operator[](Key key) const {
return contents[key];
}
void insert(Key k, Value v) {
contents.insert({k, v});
}
Option<Value> get(Key key) {
const auto exists = contents.find(key);
if (exists == contents.end()) {
return Option<Value>();
}
return Option<Value>(exists->second);
}
const Option<Value> get(Key key) const {
const auto exists = contents.find(key);
if (exists == contents.end()) {
return Option<Value>();
}
return Option<Value>(exists->second);
}
bool contains(Key key) const {
return get(key).is_some();
}
str as_str() {
str s = "{ ";
auto i = 0;
for (const auto& [key, val] : *this) {
defer(i += 1);
s += "(";
s += proto_str(key);
s += ", ";
s += proto_str(val);
s += ")";
if (i + 1 < len()) {
s += ", ";
}
}
s += " }";
return s;
}
// Begin and end methods for range-based for loops
inline auto begin() noexcept { return contents.begin(); }
inline auto end() noexcept { return contents.end(); }
inline const auto begin() const noexcept { return contents.begin(); }
inline const auto end() const noexcept { return contents.end(); }
};
template<typename T>
inline str proto_str(const Slice<T> slice) {
int count = 0;
str s = "[";
for (T a : slice) {
s += proto_str(a);
count++;
if (!(count == slice.len())) {
s += ", ";
}
}
s += "]";
return s;
}
template<typename T, uint_pr N>
inline str proto_str(const Array<T, N> arr) {
int count = 0;
str s = "[";
for (T a : arr) {
s += proto_str(a);
count++;
if (!(count == N)) {
s += ", ";
}
}
s += "]";
return s;
}
inline void proto_println(const str s) {
std::cout << s << std::endl;
}
inline void proto_print(const str s) {
std::cout << s;
}
template<typename T>
inline str proto_str(const T* t) {
return "*Ptr<" + std::to_string(reinterpret_cast<uintptr_t>(t)) + ">";
}
#include <type_traits>
#include <utility>
// Helper template to check if a type has an as_str() method
template <typename T>
class has_as_str {
private:
template <typename U>
static auto test(int) -> decltype(std::declval<U>().as_str(), std::true_type());
template <typename>
static std::false_type test(...);
public:
static constexpr bool value = decltype(test<T>(0))::value;
};
// Overload for types with an as_str() method
template <typename T>
inline auto proto_str(T& t) -> typename std::enable_if<has_as_str<T>::value, str>::type {
return t.as_str();
}
#ifdef MAKE_CHAR
inline char make_char(int num) {
return char(num);
}
#endif
class BumpAllocator {
private:
void* memory;
void* cur;
uint_pr capacity;
public:
explicit BumpAllocator(uint_pr cap) {
capacity = cap;
memory = malloc(cap);
cur = memory;
}
~BumpAllocator() {
if (capacity != 0) {
deinit();
}
}
void deinit() {
capacity = 0;
free(memory);
std::cout << "cleaned up bump allocator..." << std::endl;
}
template<typename Type>
Option<Type*> allocate(std::initializer_list<Type> init) {
int type_size = sizeof(Type);
int align_size = alignof(Type);
std::size_t available_space = static_cast<std::size_t>((char*)memory + capacity - (char*)cur);
void* aligned = cur;
if (std::align(align_size, type_size, aligned, available_space)) {
if ((char*)aligned + type_size <= (char*)memory + capacity) {
// Allocation successful
Type* result = new (aligned) Type(init); // Placement new for exception safety
cur = (char*)aligned + type_size;
std::cout << "allocated: " << type_size << " bytes with an alignment of " << align_size << std::endl;
std::cout << "used: " << used() << "/" << capacity << " bytes\n\n";
return Option<Type*>(result);
}
}
return {};
}
template<typename Type>
Option<Type*> allocate(Type init) {
int type_size = sizeof(Type);
int align_size = alignof(Type);
std::size_t available_space = static_cast<std::size_t>((char*)memory + capacity - (char*)cur);
void* aligned = cur;
if (std::align(align_size, type_size, aligned, available_space)) {
if ((char*)aligned + type_size <= (char*)memory + capacity) {
// Allocation successful
Type* result = new (aligned) Type(init); // Placement new for exception safety
cur = (char*)aligned + type_size;
std::cout << "allocated: " << type_size << " bytes with an alignment of " << align_size << std::endl;
std::cout << "used: " << used() << "/" << capacity << " bytes\n\n";
return Option<Type*>(result);
}
}
return {};
}
void reset() {
cur = memory;
}
uint_pr used() const {
return (char *)cur - (char *)memory;
}
uint_pr available() const {
return capacity - used();
}
uint_pr cap() const {
return capacity;
}
};
template<typename Type>
class PoolBumpAllocator {
private:
BumpAllocator bump_alo;
uint_pr num_of_items;
public:
explicit PoolBumpAllocator<Type>(uint_pr count) :
num_of_items(count),
bump_alo(sizeof(Type) * count) {
}
~PoolBumpAllocator() {
bump_alo.deinit();
}
void deinit() {
bump_alo.deinit();
}
Option<Type *> allocate(std::initializer_list<Type> init) {
return bump_alo.allocate<Type>(init);
}
Option<Type *> allocate(Type init) {
return bump_alo.allocate<Type>(init);
}
void reset() {
bump_alo.reset();
}
uint_pr used() const {
return bump_alo.used();
}
uint_pr available() const {
return bump_alo.available();
}
uint_pr cap() const {
return bump_alo.cap();
}
uint_pr num_of_allocatable_items() const {
return num_of_items;
}
};
class ArenaAllocator {
};
template<typename IntType>
class Int {
private:
IntType num;
public:
Int() : num(0) {}
Int(IntType n) : num(n) {}
Int operator+(const Int& n) const {
return Int(this->num + n.num);
}
Int operator*(const Int& n) const {
return Int(this->num * n.num);
}
Int operator/(const Int& n) const {
return Int(this->num / n.num);
}
Int operator%(const Int& n) const {
return Int(this->num % n.num);
}
bool operator>(const Int& n) const {
return this->num > n.num;
}
bool operator<(const Int& n) const {
return this->num < n.num;
}
str as_str() const {
return std::to_string(num);
}
};
template<typename FloatType>
class Float {
private:
FloatType num;
public:
Float() : num(0) {}
Float(FloatType n) : num(n) {}
template<typename IntType>
Float(IntType int_n) : num((FloatType) int_n) {}
Float operator+(const Float& n) const {
return Float(this->num + n.num);
}
Float operator*(const Float& n) const {
return Float(this->num * n.num);
}
Float operator/(const Float& n) const {
return Float(this->num / n.num);
}
Float operator%(const Float& n) const {
return Float(this->num % n.num);
}
str as_str() const {
return std::to_string(num);
}
};
struct Char {
private:
char c;
public:
Char() : c('\0') {}
Char(char ch) : c(ch) {}
template<typename IntType>
Char(IntType ascii) : c(char(ascii)) {}
str operator+(const Char& ch) const {
return this->as_str() + ch.as_str();
}
template<typename IntType>
str operator*(const Int<IntType>& count) const {
str buf = "";
Int<IntType> iters = 0;
while (iters < count) {
buf += c;
iters = iters + 1;
}
return buf;
}
str as_str() const {
return str(1, c);
}
};
class Range {
private:
uint_pr start, end_excl;
public:
Range(uint_pr s, uint_pr e) : start(s), end_excl(e) {}
class Iterator {
private:
uint_pr value;
public:
Iterator(uint_pr val) : value(val) {}
uint_pr operator*() const { return value; }
Iterator& operator++() { value++; return *this; }
bool operator!=(const Iterator& i) { return value != i.value; }
};
Iterator begin() const { return Iterator(start); }
Iterator end() const { return Iterator(end_excl); }
};
template<typename... Types>
class Tuple {
private:
std::tuple<Types...> data;
public:
Tuple(Types... args) : data(std::move(args)...) {}
template<uint_pr Index>
auto& get() {
return std::get<Index>(data);
}
constexpr static uint_pr len() {
return sizeof...(Types);
}
// Helper function to concatenate strings with a separator
template<std::size_t... Is>
str proto_str_helper_tuple(std::index_sequence<Is...>) {
str content = "(";
((content += (Is == 0 ? "" : ", ") + proto_str(std::get<Is>(data))), ...);
content += ")";
return content;
}
str as_str() {
return proto_str_helper_tuple(std::make_index_sequence<len()>{});
}
};
template<typename... Types>
str proto_str(Tuple<Types...>& tuple) {
return tuple.as_str();
}