-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestbench.cpp
1343 lines (1015 loc) · 32.7 KB
/
testbench.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
//
// Created by david on 28/12/2019.
//
#include "testbench.h"
#include <cmath>
#include <iostream>
#include <unordered_map>
#include <utility>
#include <vector>
#include <list>
#include <set>
#include <random>
#include <chrono>
#include <utils/Randoms.h>
#include <utils/BitSet.h>
#include <typing/TypeInfos.h>
#include <typing/iterator.h>
#include "typing/TypeInfo.h"
#include <boost/format.hpp>
#include <boost/any.hpp>
using std::cout, std::unordered_map, std::vector, std::set, std::list, std::string;
int getFIndex(unsigned long a) {
for (int i = 0; i < 64; i++) {
if (a & 1) return i;
a = a >> 1;
}
return -1;
}
int getCIndex(unsigned long a) {
return (int) log2(a);
}
unordered_map<unsigned long, int> init_map() {
unordered_map<unsigned long, int> res;
unsigned long a = 1;
for (int i = 0; i < 64; i++) {
res[a] = i;
a = a << 1;
}
return res;
}
int getLIndex(unsigned long a) {
static auto lookup_table = init_map();
return lookup_table[a];
}
void algo() {
const int COMPONENTS = 10000000;
const int CLICKS = 1000;
const int W = 1920;
const int H = 1080;
vector<BitSet> ys;
vector<BitSet> xs;
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> distx(0, W); // distribution in range [1, 6]
std::uniform_int_distribution<std::mt19937::result_type> disty(0, H); // distribution in range [1, 6]
std::uniform_int_distribution<std::mt19937::result_type> distw(10, 10); // distribution in range [1, 6]
std::uniform_int_distribution<std::mt19937::result_type> disth(10, 10); // distribution in range [1, 6]
cout << "Initializing sets\n";
for (int ix = 0; ix <= W; ix++)
xs.emplace_back(COMPONENTS);
for (int iy = 0; iy <= H; iy++)
ys.emplace_back(COMPONENTS);
cout << "Populating sets\n";
/*vector<Context *> contexts;
for (int i = 0; i < COMPONENTS; i++) {
contexts.push_back(new Context(Randoms::random_string()));
}*/
int area = 1;
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < COMPONENTS; i++) {
int x = distx(rng);
int y = disty(rng);
int w = distw(rng);
int h = disth(rng);
area += w + h;
for (int ix = x; ix < x + w && ix < W; ix++)
xs[ix].set(i);
for (int iy = y; iy < y + h && iy < H; iy++)
ys[iy].set(i);
}
auto finish = std::chrono::high_resolution_clock::now();
std::cout << "Area: " << area << " Time/100px: "
<< std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count() / (1000.0f * area / 100)
<< " in "
<< std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count() / 1000.0f
<< " ms\n";
int avg_fill = 0;
for (int ix = 0; ix < W; ix++)
avg_fill += xs[ix].count();
for (int iy = 0; iy < H; iy++)
avg_fill += ys[iy].count();
cout << "avg_fill:" << avg_fill / (W + H) << "\n";
vector<int> txs;
vector<int> tys;
for (int i = 0; i < CLICKS; i++) {
txs.push_back(distx(rng));
tys.push_back(disty(rng));
}
int intersections = 0;
cout << "Intersecting sets\n";
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < CLICKS; i++) {
intersections += xs[txs[i]].fintersect(ys[tys[i]]);
}
finish = std::chrono::high_resolution_clock::now();
std::cout << "Intersections: " << intersections / (1.0f * CLICKS) << " in "
<< std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count() / (1000.0f * CLICKS)
<< " ms\n";
}
void test_indexing() {
int SAMPLES;
std::cin >> SAMPLES;
vector<unsigned long> l;
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist6(0, 63); // distribution in range [1, 6]
unsigned long one = 1;
for (int i = 0; i < SAMPLES; i++)
l.push_back(one << dist6(rng));
// Record start time
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < SAMPLES; i++) {
if (getFIndex(l[i]) < 0)
cout << "negative\n";
}
auto finish = std::chrono::high_resolution_clock::now();
std::cout << "For: " << std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count() / 1000.0f
<< " ms\n";
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < SAMPLES; i++) {
if (getCIndex(l[i]) < 0)
cout << "negative\n";
}
finish = std::chrono::high_resolution_clock::now();
std::cout << "Calculated: "
<< std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count() / 1000.0f << " ms\n";
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < SAMPLES; i++) {
if (getLIndex(l[i]) < 0)
cout << "negative\n";
}
finish = std::chrono::high_resolution_clock::now();
std::cout << "Lookup: " << std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count() / 1000.0f
<< " ms\n";
start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < SAMPLES; i++) {
if (SAMPLES < 200) {
if (getFIndex(l[i]) < 0)
cout << "negative\n";
} else {
if (getCIndex(l[i]) < 0)
cout << "negative\n";
}
}
finish = std::chrono::high_resolution_clock::now();
std::cout << "Best: " << std::chrono::duration_cast<std::chrono::microseconds>(finish - start).count() / 1000.0f
<< " ms\n";
}
void indirect_tb() {
vector<int> original;
vector<int> first;
vector<int> second;
vector<int> third;
int N = 100000000;
for (int i = 0; i < N; i++) {
original.push_back(i);
first.push_back(original[i]);
second.push_back(first[i]);
third.push_back(second[i]);
}
int total = 0;
auto t0 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; i++) {
total += original[first[second[third[i]]]];
}
auto t1 = std::chrono::high_resolution_clock::now();
cout << "Avg time:"
<< static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count()) / 1000
<< ", ans: " << total << "\n";
total = 0;
t0 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; i++) {
total += original[first[i]];
}
t1 = std::chrono::high_resolution_clock::now();
cout << "Avg time:"
<< static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count()) / 1000
<< ", ans: " << total << "\n";
total = 0;
t0 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; i++) {
total += original[first[second[i]]];
}
t1 = std::chrono::high_resolution_clock::now();
cout << "Avg time:"
<< static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count()) / 1000
<< ", ans: " << total << "\n";
total = 0;
t0 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < N; i++) {
total += original[i];
}
t1 = std::chrono::high_resolution_clock::now();
cout << "Avg time:"
<< static_cast<int>(std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0).count()) / 1000
<< ", ans: " << total << "\n";
//fatal_error();
}
void TB::run_windowing() {
algo();
}
void TB::run_anyvector() {
struct banana {
int a;
int b;
int c;
};
using std::vector;
using boost::any, boost::any_cast;
vector<any> myvec;
myvec.reserve(10);
printf("\n first: %p \n second: %p \n 10th: %p \n", &myvec[0], &myvec[1], &myvec[2]);
struct banana ban = {1, 2, 3};
myvec[0] = (char) 5;
myvec[1] = ban;
myvec[2] = (double) 5.0;
printf("\n first: %p \n second: %p \n 10th: %p \n", &myvec[0], &myvec[1], &myvec[2]);
void *p = &myvec[1];
printf("\n a: %d \n b: %p \n c: %p \n", any_cast<struct banana>(myvec[1]).c, &myvec[1], &myvec[2]);
}
void TB::deallocating() {
using namespace std;
const int size = 100000000;
double *buffy = new double[size];
for (int i = 0; i < size; i++)
buffy[i] = i * 1.2;
cout << "deallocate?\n";
double sum = 0.0;
for (int i = 0; i < size; i++)
sum += buffy[i];
cout << sum << endl;
int a;
cin >> a;
void *p = buffy;
delete[] p;
cin >> a;
}
template<typename T, size_t offset>
T *access(void *pointy) {
return (T *) ((char *) pointy + offset);
}
void TB::fancy_types() {
void *mymem = new char[sizeof(int) + sizeof(double) + 10];
auto myint = &access<int, 0>;
auto mydouble = &access<double, sizeof(int)>;
auto mystring = &access<char, sizeof(int) + sizeof(double)>;
*myint(mymem) = 10;
*mydouble(mymem) = 10.0 / 7;
memset(mystring(mymem), 0, *myint(mymem));
strcpy(mystring(mymem), "hello");
cout << *myint(mymem) << " " << *mydouble(mymem) << " " << mystring(mymem);
}
namespace FancyTyping {
template<typename... Types>
size_t size_sum() {
return (sizeof(Types)+...);
}
template<>
size_t size_sum() {
return 0;
}
template<typename T, typename... Types>
T *access(void *mem) {
return (T *) ((char *) mem + size_sum<Types...>());
}
template<void *mem, typename T, typename... Types>
T *access() {
return (T *) ((char *) mem + size_sum<Types...>());
}
template<typename... Types>
void *allocate() {
return new char[size_sum<Types...>()];
}
template<typename... Types>
void copy(void *dest, void *src) {
memcpy(dest, src, size_sum<Types...>());
}
}
class FancyNode {
public:
static constexpr auto create = &FancyTyping::allocate<int, double>;
static constexpr auto copy = &FancyTyping::copy<int, double>;
static constexpr auto age = &FancyTyping::access<int>;
static constexpr auto kg = &FancyTyping::access<double, int>;
void *mem;
operator void *() {
return this;
}
FancyNode() {
mem = create();
}
FancyNode(void *mem) {
this->mem = create();
copy(this->mem, mem);
}
};
void TB::fancy_access() {
void *mem = FancyNode::create();
*FancyNode::age(mem) = 11;
*FancyNode::kg(mem) = 60.15 + *FancyNode::age(mem);
cout << *FancyNode::age(mem) << " " << *FancyNode::kg(mem) << "\n";
auto not_pointy = (FancyNode) mem;
cout << *not_pointy.age(not_pointy) << " " << *not_pointy.kg(not_pointy) << "\n";
cout << *not_pointy.age(not_pointy) << " " << *not_pointy.kg(not_pointy) << "\n";
//FancyNode dupl1(not_pointy);
FancyNode dupl2(mem);
/*
//*dupl1.age() = 7;
*dupl2.kg() = 30.2;
//cout << *dupl1.age() << " " << *dupl1.kg() << "\n";
cout << *dupl2.age() << " " << *dupl2.kg() << "\n";
not_pointy._age(¬_pointy);
*/
}
class FancierNode {
public:
void *mem;
FancyTypes::TypeInfo *t;
FancierNode(FancyTypes::TypeInfo &type) {
this->t = &type;
mem = t->create();
t->initialize(mem);
}
template<typename A>
A access() {
return (A) mem;
}
/*template<typename T, FancyTypes::t_iterator<T> f_iterator, FancyTypes::t_access<T> f_access, FancyTypes::TypeInfo &typeinfo>
operator FancyTypes::TypeAccess<T, f_iterator, f_access, typeinfo>() {
return (FancyTypes::TypeAccess<T, f_iterator, f_access, typeinfo>) mem;
}*/
};
void TB::typeinfo() {
using namespace FancyTypes;
#if 0
FancierNode node(int_i);
int_t myint = node;
myint = 5;
cout << "myint: " << myint << "\n";
auto myotherint = (int_t) node;
cout << "myotherint: " << myotherint << "\n";
myotherint = 7;
cout << "myint: " << myint << "\n";
#endif
FancierNode node(uninfo_i);
string_t st = string("hello");
cout << st << "\n";
uninfo_t info = node.mem;
info = {"Bernadette", 4, 1, 3};
uninfo_t info2 = {"Bernadette", 2, 1, 3};
uninfo_t info3 = *info + *info2;
vector<float> myfile(1000);
for (int i = 0; i < 1000; i++)
myfile[i] = i / 3.0f;
for (int y = 0; y < 10; y++)
for (int x = 0; x < 10; x++)
cout << myfile[y * 10 + x] << " ";
cout << "\n";
iterator dim2(myfile, 10, 10, 3);
for (int y = 0; y < 10; y++)
for (int x = 0; x < 10; x++)
cout << dim2(1, y, x) << " ";
/*auto l = I(info).length;
cout << info->length << " = " << l << "\n";*/
cout << info.toString() << "\n";
cout << info3.toString() << "\n";
cout << "glm tests \n";
vec3_t a({1.0, 0.5, 0.5});
vec3_t b({1.0, 0.5, 1.33});
vec3_t c = *a + *b;
cout << c;
cout << "\nDone.";
}
// Oh the lag..
/*
#define EVAL0(...) __VA_ARGS__
#define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__)))
#define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
#define EVAL3(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))
#define EVAL4(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__)))
#define EVAL(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__)))
#define MAP_END(...)
#define MAP_OUT
#define MAP_COMMA ,
#define MAP_GET_END2() 0, MAP_END
#define MAP_GET_END1(...) MAP_GET_END2
#define MAP_GET_END(...) MAP_GET_END1
#define MAP_NEXT0(test, next, ...) next MAP_OUT
#define MAP_NEXT1(test, next) MAP_NEXT0(test, next, 0)
#define MAP_NEXT(test, next) MAP_NEXT1(MAP_GET_END test, next)
#define MAP0(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP1)(f, peek, __VA_ARGS__)
#define MAP1(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP0)(f, peek, __VA_ARGS__)
#define MAP_LIST_NEXT1(test, next) MAP_NEXT0(test, MAP_COMMA next, 0)
#define MAP_LIST_NEXT(test, next) MAP_LIST_NEXT1(MAP_GET_END test, next)
#define MAP_LIST0(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST1)(f, peek, __VA_ARGS__)
#define MAP_LIST1(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST0)(f, peek, __VA_ARGS__)
#define MAP_2P0(f, x, y, peek, ...) f(x, y) MAP_NEXT(peek, MAP_2P1)(f, peek, __VA_ARGS__)
#define MAP_2P1(f, x, y, peek, ...) f(x, y) MAP_NEXT(peek, MAP_2P0)(f, peek, __VA_ARGS__)
#define MAP_2P_LIST0(f, x, y, peek, ...) f(x, y) MAP_LIST_NEXT(peek, MAP_2P_LIST1)(f, peek, __VA_ARGS__)
#define MAP_2P_LIST1(f, x, y, peek, ...) f(x, y) MAP_LIST_NEXT(peek, MAP_2P_LIST0)(f, peek, __VA_ARGS__)
#define MAP_2P(f, ...) EVAL(MAP_2P1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
#define MAP_2P_LIST(f, ...) EVAL(MAP_2P_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
#define MAP(f, ...) EVAL(MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
#define MAP_LIST(f, ...) EVAL(MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0))
#define STRINGIFY(x) #x
#define CALL(x, y) printf(STRINGIFY(x))
*/
void TB::macro_parenthesis() {
//MAP_2P_LIST(CALL, a, b, c, d);
}
template<typename T, template<typename...> typename Template>
struct is_specialization : std::false_type {
};
template<template<typename...> typename Template, typename... Args>
struct is_specialization<Template<Args...>, Template> : std::true_type {
};
//template <typename T, template <typename...> typename Template>
//using is_specialization_v = typename is_specialization<T, Template>::value;
template<int N, typename... Args>
struct nth_arg;
template<typename Head, typename... Rest>
struct nth_arg<0, Head, Rest...> {
typedef Head type;
};
template<int N, typename Head, typename... Rest>
struct nth_arg<N, Head, Rest...> {
typedef typename nth_arg<N - 1, Rest...>::type type;
};
template<typename S, template<typename...> typename T>
struct argument_extractor;
template<template<typename...> typename C, typename... Args>
struct argument_extractor<C<Args...>, C> {
template<int N>
using nth_arg = typename nth_arg<N, Args...>::type;
};
/*
inline typename std::enable_if<(not std::is_same<T, int>::value)
and (not std::is_same<T, float>::value), void>::type
*/
/*
template <template <typename> typename C, typename T>
constexpr inline typename std::enable_if<std::is_pointer<C<T>>::value, int>::type
gimme_info(){
auto content = gimme_info<std::remove_pointer_t<C<T>>>();
return 200 + content;
}
template <template <typename> typename C, typename T>
constexpr inline typename std::enable_if<not std::is_pointer<C<T>>::value, int>::type
gimme_info(){}
*/
template<typename, typename Condition=void>
struct gimme_info;
template<>
struct gimme_info<int> {
static constexpr int value = 1;
};
template<>
struct gimme_info<float> {
static constexpr int value = 2;
};
template<typename A, typename B, typename C>
struct mycomplexthing {
};
//std::enable_if_t<std::is_pointer<T>::value, int> = 0
template<typename T>
struct gimme_info<T, typename std::enable_if_t<std::is_pointer_v<T>>> {
static constexpr int value = 10 * gimme_info<std::remove_pointer_t<T>>::value;
};
template<typename C>
struct gimme_info<C, typename std::enable_if_t<is_specialization<C, std::vector>::value>> {
static constexpr int value =
5 + 10 * gimme_info<typename argument_extractor<C, std::vector>::template nth_arg<0>>::value;
};
template<typename C>
struct gimme_info<C, typename std::enable_if_t<is_specialization<C, mycomplexthing>::value>> {
static constexpr int value =
(((gimme_info<typename argument_extractor<C, mycomplexthing>::template nth_arg<0>>::value) * 10
+ gimme_info<typename argument_extractor<C, mycomplexthing>::template nth_arg<1>>::value) * 10
+ gimme_info<typename argument_extractor<C, mycomplexthing>::template nth_arg<2>>::value) * 10 + 6;
};
/*
// Value of <T>
template <typename T>
constexpr inline typename std::enable_if<not std::is_pointer<T>::value, int>::type
gimme_info();
template <>
constexpr int gimme_info<int>(){
return 1;
}
template <>
constexpr int gimme_info<float>(){
return 2;
}
// Value of <T*>
template <typename T>
constexpr inline typename std::enable_if<std::is_pointer<T>::value, int>::type
gimme_info(){
auto content = gimme_info<std::remove_pointer_t<T>>();
return 10 + content;
}
*/
// Value of <C<T>>
//template <typename C, typename T>
//constexpr inline typename std::enable_if<not std::is_pointer<C<T>>::value, int>::type
//constexpr inline int
//gimme_info();
/*
template <template <typename...> typename C, typename T>
constexpr inline typename std::enable_if<(not std::is_pointer<C<T>>::value) and std::is_same<C<T>, vector<T>>::value, int>::type
gimme_info(){
//auto content = gimme_info<T>();
return 100;// + content;
}
*/
/*
template <typename C>
constexpr inline typename std::enable_if_t<is_specialization<C, std::vector>::value, int>
gimme_info(){
auto content = gimme_info<argument_extractor<C, std::vector>::template nth_arg<1>>();
return 100 + content;
}
*/
/*
template<typename T>
struct banana{
template <typename C>
banana(){
}
}
*/
//template<typename C>
//using extract_args = argument_extractor<C>;
/*
template <template <typename> typename C, typename T>
constexpr inline typename std::enable_if<(not std::is_pointer<C<T>>::value) and std::is_same<C<T>, vector<T>>::value, int>::type
gimme_info(){
auto content = gimme_info<T>();
return 10 + content;
}
*/
void TB::templating() {
using boost::format;
cout << format("1 = %1%\n") % gimme_info<int>::value;
cout << format("2 = %1%\n") % gimme_info<float>::value;
cout << format("10 = %1%\n") % gimme_info<int *>::value;
cout << format("10 = %1%\n") % gimme_info<float *>::value;
//cout << format("err = %1%\n") % gimme_info<double>::value;
//cout << format("err = %1%\n") % gimme_info<double*>::value;
cout << format("15 = %1%\n") % gimme_info<vector<int>>::value;
cout << format("25 = %1%\n") % gimme_info<vector<float>>::value;
cout << format("105 = %1%\n") % gimme_info<vector<int *>>::value;
cout << format("205 = %1%\n") % gimme_info<vector<float *>>::value;
cout << format("150 = %1%\n") % gimme_info<vector<int> *>::value;
cout << format("250 = %1%\n") % gimme_info<vector<float> *>::value;
cout << format("1050 = %1%\n") % gimme_info<vector<int *> *>::value;
cout << format("2050 = %1%\n") % gimme_info<vector<float *> *>::value;
cout << format("115265 = %1%\n") % gimme_info<vector<mycomplexthing<int, vector<int>, float>>>::value;
}
void TB::birthday() {
using namespace std;
using boost::format;
long double k = 3.141592653589793238462643383279502884L;
long double best_error = k;
uint64_t start = 7;
for (uint64_t i = 1; i < 1000000; i++) {
long double cn = k * i;
long double err = 0.5L - abs(cn - floor(cn) - 0.5L);
if (err < best_error) {
best_error = err;
cout << i << " " << err << "\n";
cout << (i - 2) % 113 << "\n";
}
}
glm::vec3(0, 0, 0);
}
void fun(int *&myptr) {
cout << "Hello from &\n";
}
void fun(int *&&myptr) {
cout << "Hello from &&\n";
}
void TB::movingpointers() {
int *p = new int(5);
fun(p);
fun(std::move(p));
fun(new int(5));
fun(std::move(new int(5)));
//I love successful tests
}
#define _A(k, a, b) k::a + k::b
#define _B(k, a, b) k::a * k::b
#define MCAT(a, b, c) a ## b ## C
#define A(a, b) MCAT(k, ::, a) + MCAT(k, ::, b)
#define C(k, m) m
struct mystruct_macro_order {
constexpr static int k1 = 1;
constexpr static int k2 = 2;
};
void TB::mystruct_macro_order() {
//cout<<C(mystruct, C(mystruct, A(k1,k2)));
}
struct structuring_vector_struct {
int a;
int b;
int c;
int d;
int e;
};
void TB::structuring_vector() {
vector<int> v = {1, 2, 3, 4, 5};
auto &s = *(structuring_vector_struct *) v.data();
cout << s.a << s.b << s.c << s.d << s.e;
}
void isit(int *&lvalue) {
cout << "nope\n";
}
void isit(int *&&rvalue) {
cout << "yep\n";
}
template<typename T>
void isit_ref(T &&lr_ref) {
// static_assert(std::is_same_v<std::remove_reference_t<T>, int *>,
// "This function does not support the specified type.");
isit(std::forward<T>(lr_ref));
}
void TB::isRValue() {
int *k = new int(3);
isit(new int(5));
isit(k);
isit_ref(new int(10));
isit_ref(k);
//isit_ref(5);
}
struct scope_node {
vector<void *> inputs;
vector<void *> outputs;
};
struct scope_escape {
typedef scope_escape this_t;
scope_node *value;
void link(size_t *what, scope_node *to, size_t where) {
}
enum class Inputs : size_t {
a = 2, b, c
};
typedef struct {
typedef size_t a;
typedef int b;
typedef double c;
} Inputs_t;
enum class Outputs : size_t {
a = 2, b, c
};
typedef struct {
typedef size_t a;
typedef int b;
typedef double c;
} Outputs_t;
};
#define link_scope(dest, dest_where, src, src_where) \
dest.value->inputs[(size_t) decltype(dest)::Inputs::dest_where] = \
src.value->outputs[(size_t) decltype(src)::Outputs::src_where]
void TB::scope_escape_tb() {
typedef int myint;
scope_escape::this_t::this_t::Inputs_t::a a = (size_t) scope_escape::Inputs::a;
cout << (myint) a;
scope_escape n1, n2;
typedef decltype(n1)::Inputs n1_ii;
typedef decltype(n1)::Outputs n1_oi;
typedef decltype(n2)::Inputs n2_ii;
typedef decltype(n2)::Outputs n2_oi;
auto x = new scope_escape::Outputs_t::b();
//n1.value->inputs[n1.Inputs.a] = n2.value->outputs[n2.Outputs.b];
//n1.value->inputs[(size_t) decltype(n1)::input_indexes::a] =
// n2.value->outputs[(size_t) decltype(n2)::input_indexes::b];
link_scope(n1, a, n2, b);
}
struct SynSugar {
int a;
enum class idx : std::size_t {
a, b, c, __end
};
int r[(size_t) idx::__end] = {1, 2, 3};
SynSugar(int x) {
a = x;
}
void operator()() {
cout << "Hello world! " << (size_t) idx::a;
}
};
void TB::pointy_parant() {
SynSugar *a = new SynSugar(1);
(*a)();
SynSugar &b = *a;
b();
}
template<size_t index, size_t... values>
struct ct_lut;
template<size_t cur, size_t... values>
struct ct_lut<0, cur, values...> {
static constexpr size_t value = cur;
};
template<size_t index, size_t cur, size_t... values>
struct ct_lut<index, cur, values...> {
static constexpr size_t value = ct_lut<index - 1, values...>::value;
};
struct abstract_banana {
virtual void do_the_flop() = 0;
};
struct banana2 : abstract_banana {
int k = 0;
template<size_t N>
static constexpr size_t lut = ct_lut<N, 1, 2, 3, 4, 5>::value;
static constexpr size_t arr[5] = {1, 2, 3, 4, 5};
static const size_t *arr2[3];
static void print_bananas(bool &var) {
cout << arr[0];
var = false;
}
void do_the_flop() {
cout << "|_";
}
banana2 &do_the_self_flop() {
cout << "k: " << k << "\n";
k++;
return *this;
}
typedef struct {
typedef float z;
typedef int start;
typedef int end;
} Inputs_t;
};
typedef banana2 banana2;