boilerplate.hpp initial upload
[CPPToys.git] /
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
#include <cinttypes>
#include <cstdarg>
#include <cmath>
#include <cwchar>
#include <functional>
#include <string>
#include <random>
#include <memory>

typedef std::wstring wstring;
typedef std::string string;

typedef char c8;
typedef wchar_t c16;

const c8* _endl = "\n";
const c16* _wendl = L"\n";

typedef uint16_t u16;
typedef int16_t s16;
typedef uint32_t u32;
typedef int32_t s32;
typedef uint64_t u64;
typedef int64_t s64;

typedef float f32;
typedef double f64;
typedef long double f80;

std::mt19937 rng;
std::mt19937_64 rng64;

template<typename...  params>
wstring string_format( const wstring& format,  params... args )
{
    size_t size = swprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
    std::unique_ptr<c16[]> buf( new c16[ size ] );
    snwprintf( buf.get(), size, format.c_str(), args ... );
    return wstring( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}

template<typename ...  params>
string string_format( const std::string& format,  params... args )
{
    size_t size = snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
	std::unique_ptr<c8[]> buf( new c8[ size ] );
    snprintf( buf.get(), size, format.c_str(), args ... );
    return string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}

void printline() {
	printf(_endl);
}

template<typename...  params>
void printline(const c8* format,  params... args) {
	auto s = string_format(format, args ...);

	printf(s.data());
	printf(_endl);
}

template<typename...  params>
void printline(const c16* format,  params... args) {
	auto s = string_format(format, args ...);

	wprintf(s.data());
	wprintf(_wendl);
}

template<typename...  params>
void die(s32 status, string message, params... args) {
    if (message.length() > 0)
    {
        fprintf(stderr, string_format(message, args...).c_str());
    }

    exit(status);
}

template<typename...  params>
void die(s32 status, wstring message, params... args) {
    if (message.length() > 0)
    {
        fwprintf(stderr, string_format(message, args...).c_str());
    }

    exit(status);
}

void random_seed(u64 seed) {
    rng64.seed(seed);
}

u64 random_long() {
    return rng64();
}

u32 random_int() {
    return (u32)(rng64() >> 32);
}

u16 random_short() {
    return (u16)(rng64() >> 48);
}

// Produces a random integer in the range [range_min..range_max).
s64 random_range(s64 range_min, s64 range_max) {
    s64 width = range_max - range_min;

    s64 num = rng64();

    num %= width;

    num += range_min;

    if (num < range_min) {
        num += width;
    }

    return num;
}

// List type valid up to 65536 members.
template <typename T>
struct List {
private:
    static bool gtFunc(T l, T r) {
        return l > r;
    }

    static bool ltFunc(T l, T r) {
        return l < r;
    }

	u32 ITEMS_PER_BUCKET;
    u32 MAX_BUCKETS;

    T** buckets;

    u32 bucketCount;
    u64 itemCount;

    T** BucketPtr(u32 idx) {
        if (idx >= bucketCount) {
            die(1, L"Tried to access a list sub-array with an index greater than the current count.");
        }

        return buckets + idx;
    }

    T* GetBucket(u32 idx) {
        return *BucketPtr(idx);
    }

    void AddBucket() {
        u32 bucketIdx = bucketCount;
        bucketCount++;

        *BucketPtr(bucketIdx) = (T*)malloc(sizeof(T) * ITEMS_PER_BUCKET);
    }

    s64 qs_partition(u64 ltIdx, u64 rtIdx, u64 pvtIdx, bool (*comp_func)(T, T)) {
        // Fetch the value of the pivot point.
        T pvtVal = GetItem(pvtIdx);

        T swap1, swap2;
        // Swap the pivot point to the right-most index in the partition.
        swap1 = GetItem(rtIdx);
        swap2 = GetItem(pvtIdx);

        SetItem(pvtIdx, swap1);
        SetItem(rtIdx, swap2);

        // Store the left-most index in the partition; later we'll increment this as we
        // swap values left of the pivot.
        u64 strIdx = ltIdx;

        for (u64 idx = ltIdx; idx < rtIdx; idx++) {
            //
            if ((*comp_func)(GetItem(idx), pvtVal)) {
                swap1 = GetItem(strIdx);
                swap2 = GetItem(idx);

                SetItem(idx, swap1);
                SetItem(strIdx, swap2);

                strIdx++;
            }
        }

        swap1 = GetItem(rtIdx);
        swap2 = GetItem(strIdx);

        SetItem(strIdx, swap1);
        SetItem(rtIdx, swap2);

        return strIdx;
    }

    void qs_region(u64 ltIdx, u64 rtIdx, bool (*comp_func)(T, T)) {
        if (ltIdx < rtIdx && rtIdx < Count()) {
            // Select a random pivot point between ltIdx and rtIdx.  The location is not important,
            // and picking randomly may provide speedup in some cases.
            u64 pvtIdx = random_range(ltIdx, rtIdx);

            // L"Sift" the array around the pivot value, and record its new location.
            s64 newIdx = qs_partition(ltIdx, rtIdx, pvtIdx, comp_func);

            // Since the L"sifted" regions are not sorted, sort them.
            qs_region(ltIdx, newIdx - 1, comp_func);
            qs_region(newIdx + 1, rtIdx, comp_func);
        }
    }

public:
    static const u32 DEFAULT_ITEMS_PER_BUCKET = 256;
    static const u32 DEFAULT_MAX_BUCKETS = 256;

    bool (*comparer)(T, T);

    T* ItemPtr(u32 idx) {
        if (idx >= ITEMS_PER_BUCKET * MAX_BUCKETS) {
            die(1, L"Tried to access List item with index outside maximum bounds.");
        }

		u32 arIdx = idx / ITEMS_PER_BUCKET;
        u32 lfIdx = idx % ITEMS_PER_BUCKET;

        return GetBucket(arIdx) + lfIdx;
    }

    T GetItem(u32 idx) {
        return *ItemPtr(idx);
    }

    T operator[] (const u32& idx) {
        return GetItem(idx);
    }

    void SetItem(u32 idx, T value) {
        *ItemPtr(idx) = value;
    }

    void AddItem(T value) {
        u32 idx = itemCount;
        itemCount++;

        if (itemCount > ITEMS_PER_BUCKET * bucketCount) {
            AddBucket();
        }

        *ItemPtr(idx) = value;
    }

    u64 Count() {
        return itemCount;
    }

    u64 CurrentCapacity() {
        return (u64)ITEMS_PER_BUCKET * (u64)bucketCount;
    }

	u64 MaxCapacity() {
		return (u64)ITEMS_PER_BUCKET * (u64)MAX_BUCKETS;
	}

    bool IsSorted(bool (*comp_func)(T, T)) {
        T first = GetItem(0);
        for (u32 idx = 1; idx < Count(); idx++) {
            if ((*comp_func)(GetItem(idx), first)) {
                return false;
            }
        }

        return true;
    }

    bool IsSorted(bool reversed) {
        if (reversed) {
            return IsSorted(&gtFunc);
        }
        else {
            return IsSorted(&ltFunc);
        }
    }

    bool IsSorted() {
        return IsSorted(false);
    }

    void Sort(bool (*comp_func)(T, T))
    {
        qs_region(0, Count() - 1, comp_func);
    }

    void Sort(bool reversed) {
        if (reversed) {
            Sort(&gtFunc);
        }
        else {
            Sort(&ltFunc);
        }
    }

    void Sort() {
        Sort(false);
    }

    u64 SizeOf() {
        return sizeof(T) * bucketCount * ITEMS_PER_BUCKET;
    }

    void Clear() {
        for (u32 bIdx = 0; bIdx < bucketCount; bIdx++) {
            free(BucketPtr(bIdx));
        }

        bucketCount = 0;
        itemCount = 0;
    }

    List(u32 max_buckets, u32 items_per_bucket) :
		ITEMS_PER_BUCKET(items_per_bucket),
        MAX_BUCKETS(max_buckets),
        bucketCount(0),
        itemCount(0),
        comparer(&ltFunc)
        {
        buckets = (T**)malloc(sizeof(T*) * MAX_BUCKETS);
    }

    List() : List(DEFAULT_MAX_BUCKETS, DEFAULT_ITEMS_PER_BUCKET) {}

	List(u64 capacity, bool contiguous_small_list) : bucketCount(0), itemCount(0), comparer(&ltFunc) {
		if (capacity > 0xfffffffe00000001)
		{
			capacity = 0xfffffffe00000001;
		}

		double bit_width = std::log2(capacity);

		u32 items_per_bucket, max_buckets;

		if (bit_width > 62)
		{
			max_buckets = 0xffffffff;
		}
		else
		{
			if (contiguous_small_list && bit_width < 32)
			{
				max_buckets = 1;
			}
			else if (bit_width < 8)
			{
				max_buckets = 1;
			}
			else if (bit_width < 12)
			{
				max_buckets = 1 << 4;
			}
			else if (bit_width < 40)
			{
				max_buckets = 1ull << 8;
			}
			else if (bit_width < 46)
			{
				max_buckets = 1ull << 14;
			}
			else if (bit_width < 56)
			{
				max_buckets = 1ull << 24;
			}
			else
			{
				max_buckets = 1ull << ((u64)ceil(bit_width) / 2ull);
			}
		}

		items_per_bucket = (u32)(capacity / (u64)max_buckets);

		u64 real_capacity = (u64)max_buckets * (u64)items_per_bucket;

		if (real_capacity < capacity && items_per_bucket < 0xffffffff) {
			items_per_bucket++;
			real_capacity = (u64)max_buckets * (u64)items_per_bucket;
		}

		MAX_BUCKETS = max_buckets;
		ITEMS_PER_BUCKET = items_per_bucket;

		buckets = (T**)malloc(sizeof(T*) * MAX_BUCKETS);

		if (contiguous_small_list && capacity < (1ull << 32))
		{
			AddBucket();
			// itemCount = items_per_bucket;
		}
	}

    ~List() {
        for (u32 idx = 0; idx < bucketCount; idx++) {
            free(buckets[idx]);
        }

        free(buckets);
    }
};


typedef std::function<void()> Callback;
typedef Callback* CallbackPtr;

Callback* make_callback(Callback cb) {
    return new std::function<void()> (cb);
}

struct VacuumCleaner {
private:
    List<CallbackPtr> cleanup_tasks;

public:
    template <typename T>
    void AddItem(T* item) {
        cleanup_tasks.AddItem(make_callback([item]() {
            delete item;
        }
    ));
    }

    void Clean() {
        for (u32 idx = 0; idx < cleanup_tasks.Count(); idx++) {
            CallbackPtr cb = cleanup_tasks.GetItem(idx);

            (*cb)();

            delete cb;
        }

        cleanup_tasks.Clear();
    }

    VacuumCleaner() : cleanup_tasks(List<CallbackPtr>()) {}

    ~VacuumCleaner() {
        Clean();
    }
};