-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Statics.cs
1781 lines (1546 loc) · 65.3 KB
/
Statics.cs
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
using System.ComponentModel;
using System.IO;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Towel;
/// <summary>Root type of the static functional methods in Towel.</summary>
public static partial class Statics
{
#region Internals
#region Constants
/// <summary>Related: https://github.com/dotnet/roslyn/issues/49568</summary>
internal const string NotIntended = "This member is not intended to be used.";
/// <summary>The max <see cref="byte"/>s to stackalloc.</summary>
internal const int Stackalloc = 256;
#endregion
#region Optimizations
// These are some shared internal optimizations that I don't want to expose because it might confuse people.
// If you need to use it, just copy this code into your own project.
internal static class MultiplyAddImplementation<T>
{
/// <summary>a * b + c</summary>
internal static Func<T, T, T, T> Function = (a, b, c) =>
{
var A = Expression.Parameter(typeof(T));
var B = Expression.Parameter(typeof(T));
var C = Expression.Parameter(typeof(T));
var BODY = Expression.Add(Expression.Multiply(A, B), C);
Function = Expression.Lambda<Func<T, T, T, T>>(BODY, A, B, C).Compile();
return Function(a, b, c);
};
}
internal static class D_subtract_A_multiply_B_divide_C<T>
{
/// <summary>d - a * b / c</summary>
internal static Func<T, T, T, T, T> Function = (a, b, c, d) =>
{
var A = Expression.Parameter(typeof(T));
var B = Expression.Parameter(typeof(T));
var C = Expression.Parameter(typeof(T));
var D = Expression.Parameter(typeof(T));
var BODY = Expression.Subtract(D, Expression.Divide(Expression.Multiply(A, B), C));
Function = Expression.Lambda<Func<T, T, T, T, T>>(BODY, A, B, C, D).Compile();
return Function(a, b, c, d);
};
}
#endregion
#region Helpers
internal static T OperationOnStepper<T>(Action<Action<T>> stepper, Func<T, T, T> operation)
{
if (stepper is null) throw new ArgumentNullException(nameof(stepper));
T? result = default;
bool assigned = false;
stepper(a =>
{
if (assigned)
{
result = operation(result!, a);
}
else
{
result = a;
assigned = true;
}
});
return assigned
? result!
: throw new ArgumentException($"{nameof(stepper)} is empty.", nameof(stepper));
}
#endregion
#endregion
#region Swap
/// <summary>Swaps two values.</summary>
/// <typeparam name="T">The type of values to swap.</typeparam>
/// <param name="a">The first value of the swap.</param>
/// <param name="b">The second value of the swap.</param>
public static void Swap<T>(ref T a, ref T b) => (a, b) = (b, a);
/// <summary>Swaps two values.</summary>
/// <typeparam name="T">The type of values to swap.</typeparam>
/// <typeparam name="TGet">The type of the get method.</typeparam>
/// <typeparam name="TSet">The type of the set method.</typeparam>
/// <param name="a">The index of the first value to swap.</param>
/// <param name="b">The index of the second value to swap.</param>
/// <param name="get">The the get method.</param>
/// <param name="set">The the set method.</param>
public static void Swap<T, TGet, TSet>(int a, int b, TGet get, TSet set)
where TGet : struct, IFunc<int, T>
where TSet : struct, IAction<int, T>
{
T temp = get.Invoke(a);
set.Invoke(a, get.Invoke(b));
set.Invoke(b, temp);
}
#endregion
#region source...
#pragma warning disable IDE1006 // Naming Styles
/// <summary>Gets the directory path of the current location in source code.</summary>
/// <param name="default">Intended to leave default. This value is set by the compiler via <see cref="CallerFilePathAttribute"/>.</param>
/// <returns>The directory path of the current location in source code.</returns>
public static string? sourcedirectory([CallerFilePath] string @default = default!) => Path.GetDirectoryName(@default);
/// <summary>Gets the file path of the current location in source code.</summary>
/// <param name="default">Intended to leave default. This value is set by the compiler via <see cref="CallerFilePathAttribute"/>.</param>
/// <returns>The file path of the current location in source code.</returns>
public static string sourcefilepath([CallerFilePath] string @default = default!) => @default;
/// <summary>Gets the member name of the current location in source code.</summary>
/// <param name="default">Intended to leave default. This value is set by the compiler via <see cref="CallerMemberNameAttribute"/>.</param>
/// <returns>The member name of the current location in source code.</returns>
public static string sourcemembername([CallerMemberName] string @default = default!) => @default;
/// <summary>Gets the line number of the current location in source code.</summary>
/// <param name="default">Intended to leave default. This value is set by the compiler via <see cref="CallerLineNumberAttribute"/>.</param>
/// <returns>The line number of the current location in source code.</returns>
public static int sourcelinenumber([CallerLineNumber] int @default = default) => @default;
/// <summary>Gets the source code and evaluation of an expression.</summary>
/// <typeparam name="T">The type the expression will evaluate to.</typeparam>
/// <param name="expression">The expression to evaluate and get the source of.</param>
/// <param name="default">Intended to leave default. This value is set by the compiler via <see cref="CallerArgumentExpressionAttribute"/>.</param>
/// <returns>The source code and evaluation of the expression.</returns>
public static (T Value, string Source) sourceof<T>(T expression, [CallerArgumentExpression("expression")] string? @default = default) => (expression, @default!);
/// <summary>Gets the source code and evaluation of an expression.</summary>
/// <typeparam name="T">The type the expression will evaluate to.</typeparam>
/// <param name="expression">The expression to evaluate and get the source of.</param>
/// <param name="source">The source code of the expression.</param>
/// <param name="default">Intended to leave default. This value is set by the compiler via <see cref="CallerArgumentExpressionAttribute"/>.</param>
/// <returns>The evaluation of the expression.</returns>
public static T sourceof<T>(T expression, out string source, [CallerArgumentExpression("expression")] string? @default = default)
{
source = @default!;
return expression;
}
#pragma warning restore IDE1006 // Naming Styles
#endregion
#region TryParse
/// <summary>Tries to parse a <see cref="string"/> into a value of the type <typeparamref name="T"/>.</summary>
/// <typeparam name="T">The type to parse the <see cref="string"/> into a value of.</typeparam>
/// <param name="string">The <see cref="string"/> to parse into a value ot type <typeparamref name="T"/>.</param>
/// <returns>
/// - <see cref="bool"/> Success: true if the parse was successful or false if not<br/>
/// - <typeparamref name="T"/> Value: the value if the parse was successful or default if not
/// </returns>
public static (bool Success, T? Value) TryParse<T>(string @string) =>
(TryParseImplementation<T>.Function(@string, out T? value), value);
internal static class TryParseImplementation<T>
{
internal delegate TResult TryParse<T1, T2, TResult>(T1 arg1, out T2? arg2);
internal static TryParse<string, T, bool> Function = (string @string, out T? value) =>
{
static bool Fail(string @string, out T? value)
{
value = default;
return false;
}
if (typeof(T).IsEnum)
{
foreach (MethodInfo methodInfo in typeof(Enum).GetMethods(
BindingFlags.Static |
BindingFlags.Public))
{
if (methodInfo.Name == nameof(Enum.TryParse) &&
methodInfo.IsGenericMethod &&
methodInfo.IsStatic &&
methodInfo.IsPublic &&
methodInfo.ReturnType == typeof(bool))
{
MethodInfo genericMethodInfo = methodInfo.MakeGenericMethod(typeof(T));
ParameterInfo[] parameters = genericMethodInfo.GetParameters();
if (parameters.Length is 2 &&
parameters[0].ParameterType == typeof(string) &&
parameters[1].ParameterType == typeof(T).MakeByRefType())
{
Function = genericMethodInfo.CreateDelegate<TryParse<string, T, bool>>();
return Function(@string, out value);
}
}
}
throw new TowelBugException("The System.Enum.TryParse method was not found via reflection.");
}
else
{
MethodInfo? methodInfo = Meta.GetTryParseMethod<T>();
Function = methodInfo is null
? Fail
: methodInfo.CreateDelegate<TryParse<string, T, bool>>();
return Function(@string, out value);
}
};
}
#endregion
#region Hash
/// <summary>Static wrapper for the instance based "object.GetHashCode" function.</summary>
/// <typeparam name="T">The generic type of the hash operation.</typeparam>
/// <param name="value">The item to get the hash code of.</param>
/// <returns>The computed hash code using the base GetHashCode instance method.</returns>
public static int Hash<T>(T value)
{
if (value is null) throw new ArgumentNullException(nameof(value));
return value.GetHashCode();
}
#endregion
#region Convert
/// <summary>Converts <paramref name="a"/> from <typeparamref name="TA"/> to <typeparamref name="TB"/>.</summary>
/// <typeparam name="TA">The type of the value to convert.</typeparam>
/// <typeparam name="TB">The type to convert the value to.</typeparam>
/// <param name="a">The value to convert.</param>
/// <returns>The <paramref name="a"/> value of <typeparamref name="TB"/> type.</returns>
public static TB Convert<TA, TB>(TA a) =>
ConvertImplementation<TA, TB>.Function(a);
internal static class ConvertImplementation<TA, TB>
{
internal static Func<TA, TB> Function = a =>
{
ParameterExpression A = Expression.Parameter(typeof(TA));
Expression BODY = Expression.Convert(A, typeof(TB));
Function = Expression.Lambda<Func<TA, TB>>(BODY, A).Compile();
return Function(a);
};
}
#endregion
#region Join
/// <summary>Iterates a <see cref="System.Range"/> and joins the results of a System.Func<int, string> seperated by a <see cref="string"/> <paramref name="seperator"/>.</summary>
/// <param name="range">The range of values to use use on the <System.Func{int, string> <paramref name="func"/>.</param>
/// <param name="func">The System.Func<int, string>.</param>
/// <param name="seperator">The <see cref="string"/> seperator to join the values with.</param>
/// <returns>The resulting <see cref="string"/> of the join.</returns>
public static string Join(Range range, Func<int, string> func, string seperator) =>
string.Join(seperator, range.ToIEnumerable().Select(func));
#endregion
#region Equate
#if false
/// <summary>Checks for equality of two values [<paramref name="a"/> == <paramref name="b"/>].</summary>
/// <typeparam name="A">The type of the left operand.</typeparam>
/// <typeparam name="B">The type of the right operand.</typeparam>
/// <typeparam name="C">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the equality.</returns>
public static C Equate<A, B, C>(A a, B b) =>
EquateImplementation<A, B, C>.Function(a, b);
#endif
/// <summary>Checks for equality of two values [<paramref name="a"/> == <paramref name="b"/>].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the equality check.</returns>
public static bool Equate<T>(T a, T b) =>
EquateImplementation<T, T, bool>.Function(a, b);
/// <summary>Checks for equality among multiple values [<paramref name="a"/> == <paramref name="b"/> == <paramref name="c"/> == ...].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first operand of the equality check.</param>
/// <param name="b">The second operand of the equality check.</param>
/// <param name="c">The remaining operands of the equality check.</param>
/// <returns>True if all operands are equal or false if not.</returns>
public static bool Equate<T>(T a, T b, params T[] c)
{
if (c is null) throw new ArgumentNullException(nameof(c));
if (c.Length is 0) throw new ArgumentException("The array is empty.", nameof(c));
if (!Equate(a, b))
{
return false;
}
for (int i = 0; i < c.Length; i++)
{
if (!Equate(a, c[i]))
{
return false;
}
}
return true;
}
internal static class EquateImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
#warning TODO: kill this try catch
try
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.Equal(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
}
catch
{
}
if (typeof(TC) == typeof(bool))
{
EquateImplementation<TA, TB, bool>.Function =
(typeof(TA).IsValueType, typeof(TB).IsValueType) switch
{
(true, true) => (A, B) => A!.Equals(B),
(true, false) => (A, B) => A!.Equals(B),
(false, true) => (A, B) => B!.Equals(A),
(false, false) =>
(A, B) =>
(A, B) switch
{
(null, null) => true,
(_, null) => false,
(null, _) => false,
_ => A.Equals(B),
},
};
return Function!(a, b);
}
#warning TODO
throw new NotImplementedException();
};
}
#endregion
#region Inequate
/// <summary>Checks for inequality of two values [<paramref name="a"/> != <paramref name="b"/>].</summary>
/// <typeparam name="TA">The type of the left operand.</typeparam>
/// <typeparam name="TB">The type of the right operand.</typeparam>
/// <typeparam name="TC">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the inequality.</returns>
public static TC Inequate<TA, TB, TC>(TA a, TB b) =>
InequateImplementation<TA, TB, TC>.Function(a, b);
/// <summary>Checks for inequality of two values [<paramref name="a"/> != <paramref name="b"/>].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first operand of the inequality check.</param>
/// <param name="b">The second operand of the inequality check.</param>
/// <returns>The result of the inequality check.</returns>
public static bool Inequate<T>(T a, T b) =>
Inequate<T, T, bool>(a, b);
internal static class InequateImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.NotEqual(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
};
}
#endregion
#region LessThan
/// <summary>Checks if one value is less than another [<paramref name="a"/> < <paramref name="b"/>].</summary>
/// <typeparam name="TA">The type of the left operand.</typeparam>
/// <typeparam name="TB">The type of the right operand.</typeparam>
/// <typeparam name="TC">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the less than operation.</returns>
public static TC LessThan<TA, TB, TC>(TA a, TB b) =>
LessThanImplementation<TA, TB, TC>.Function(a, b);
/// <summary>Checks if one value is less than another [<paramref name="a"/> < <paramref name="b"/>].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first operand of the less than check.</param>
/// <param name="b">The second operand of the less than check.</param>
/// <returns>The result of the less than check.</returns>
public static bool LessThan<T>(T a, T b) =>
LessThan<T, T, bool>(a, b);
internal static class LessThanImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.LessThan(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
};
}
#endregion
#region GreaterThan
/// <summary>Checks if one value is greater than another [<paramref name="a"/> > <paramref name="b"/>].</summary>
/// <typeparam name="TA">The type of the left operand.</typeparam>
/// <typeparam name="TB">The type of the right operand.</typeparam>
/// <typeparam name="TC">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the greater than operation.</returns>
public static TC GreaterThan<TA, TB, TC>(TA a, TB b) =>
GreaterThanImplementation<TA, TB, TC>.Function(a, b);
/// <summary>Checks if one value is greater than another [<paramref name="a"/> > <paramref name="b"/>].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first operand of the greater than check.</param>
/// <param name="b">The second operand of the greater than check.</param>
/// <returns>The result of the greater than check.</returns>
public static bool GreaterThan<T>(T a, T b) =>
GreaterThan<T, T, bool>(a, b);
internal static class GreaterThanImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.GreaterThan(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
};
}
#endregion
#region LessThanOrEqual
/// <summary>Checks if one value is less than or equal to another [<paramref name="a"/> <= <paramref name="b"/>].</summary>
/// <typeparam name="TA">The type of the left operand.</typeparam>
/// <typeparam name="TB">The type of the right operand.</typeparam>
/// <typeparam name="TC">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the less than or equal to operation.</returns>
public static TC LessThanOrEqual<TA, TB, TC>(TA a, TB b) =>
LessThanOrEqualImplementation<TA, TB, TC>.Function(a, b);
/// <summary>Checks if one value is less than or equal to another [<paramref name="a"/> <= <paramref name="b"/>].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first operand of the less than or equal to check.</param>
/// <param name="b">The second operand of the less than or equal to check.</param>
/// <returns>The result of the less than or equal to check.</returns>
public static bool LessThanOrEqual<T>(T a, T b) =>
LessThanOrEqual<T, T, bool>(a, b);
internal static class LessThanOrEqualImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.LessThanOrEqual(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
};
}
#endregion
#region GreaterThanOrEqual
/// <summary>Checks if one value is less greater or equal to another [<paramref name="a"/> >= <paramref name="b"/>].</summary>
/// <typeparam name="TA">The type of the left operand.</typeparam>
/// <typeparam name="TB">The type of the right operand.</typeparam>
/// <typeparam name="TC">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the greater than or equal to operation.</returns>
public static TC GreaterThanOrEqual<TA, TB, TC>(TA a, TB b) =>
GreaterThanOrEqualImplementation<TA, TB, TC>.Function(a, b);
/// <summary>Checks if one value is greater than or equal to another [<paramref name="a"/> >= <paramref name="b"/>].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first operand of the greater than or equal to check.</param>
/// <param name="b">The second operand of the greater than or equal to check.</param>
/// <returns>The result of the greater than or equal to check.</returns>
public static bool GreaterThanOrEqual<T>(T a, T b) =>
GreaterThanOrEqual<T, T, bool>(a, b);
internal static class GreaterThanOrEqualImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.GreaterThanOrEqual(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
};
}
#endregion
#region Compare
#if false
/// <summary>Compares two values.</summary>
/// <typeparam name="A">The type of the left operand.</typeparam>
/// <typeparam name="B">The type of the right operand.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the comparison.</returns>
public static C Compare<A, B, C>(A a, B b) =>
CompareImplementation<A, B, C>.Function(a, b);
#endif
/// <summary>Compares two values.</summary>
/// <typeparam name="T">The type of values to compare.</typeparam>
/// <param name="a">The first value of the comparison.</param>
/// <param name="b">The second value of the comparison.</param>
/// <returns>The result of the comparison.</returns>
public static CompareResult Compare<T>(T a, T b) =>
CompareImplementation<T, T, CompareResult>.Function(a, b);
internal static class CompareImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
if (typeof(TC) == typeof(CompareResult))
{
if (typeof(TA) == typeof(TB) && a is IComparable<TB> &&
!(typeof(TA).IsPrimitive && typeof(TB).IsPrimitive))
{
CompareImplementation<TA, TA, CompareResult>.Function =
(a, b) => System.Collections.Generic.Comparer<TA>.Default.Compare(a, b).ToCompareResult();
}
else
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var lessThanPredicate =
typeof(TA).IsPrimitive && typeof(TB).IsPrimitive
? Expression.LessThan(A, B)
: Meta.GetLessThanMethod<TA, TB, bool>() is not null
? Expression.LessThan(A, B)
: Meta.GetGreaterThanMethod<TB, TA, bool>() is not null
? Expression.GreaterThan(B, A)
: null;
var greaterThanPredicate =
typeof(TA).IsPrimitive && typeof(TB).IsPrimitive
? Expression.GreaterThan(A, B)
: Meta.GetGreaterThanMethod<TA, TB, bool>() is not null
? Expression.GreaterThan(A, B)
: Meta.GetLessThanMethod<TB, TA, bool>() is not null
? Expression.LessThan(B, A)
: null;
if (lessThanPredicate is null || greaterThanPredicate is null)
{
throw new NotSupportedException("You attempted a comparison operation with unsupported types.");
}
var RETURN = Expression.Label(typeof(CompareResult));
var BODY = Expression.Block(
Expression.IfThen(
lessThanPredicate,
Expression.Return(RETURN, Expression.Constant(Less, typeof(CompareResult)))),
Expression.IfThen(
greaterThanPredicate,
Expression.Return(RETURN, Expression.Constant(Greater, typeof(CompareResult)))),
Expression.Return(RETURN, Expression.Constant(Equal, typeof(CompareResult))),
Expression.Label(RETURN, Expression.Constant(default(CompareResult), typeof(CompareResult))));
CompareImplementation<TA, TB, CompareResult>.Function = Expression.Lambda<Func<TA, TB, CompareResult>>(BODY, A, B).Compile();
}
return Function!(a, b);
}
#warning TODO
throw new NotImplementedException();
};
}
#endregion
#region Negation
/// <summary>Negates a value [-<paramref name="a"/>].</summary>
/// <typeparam name="TA">The type of the value to negate.</typeparam>
/// <typeparam name="TB">The resulting type of the negation.</typeparam>
/// <param name="a">The value to negate.</param>
/// <returns>The result of the negation [-<paramref name="a"/>].</returns>
public static TB Negation<TA, TB>(TA a) =>
NegationImplementation<TA, TB>.Function(a);
/// <summary>Negates a value [-<paramref name="a"/>].</summary>
/// <typeparam name="T">The type of the value to negate.</typeparam>
/// <param name="a">The value to negate.</param>
/// <returns>The result of the negation [-<paramref name="a"/>].</returns>
public static T Negation<T>(T a) =>
Negation<T, T>(a);
internal static class NegationImplementation<TA, TB>
{
internal static Func<TA, TB> Function = a =>
{
var A = Expression.Parameter(typeof(TA));
var BODY = Expression.Negate(A);
Function = Expression.Lambda<Func<TA, TB>>(BODY, A).Compile();
return Function(a);
};
}
#endregion
#region Addition
/// <summary>Adds two values [<paramref name="a"/> + <paramref name="b"/>].</summary>
/// <typeparam name="TA">The type of the left operand.</typeparam>
/// <typeparam name="TB">The type of the right operand.</typeparam>
/// <typeparam name="TC">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the addition [<paramref name="a"/> + <paramref name="b"/>].</returns>
public static TC Addition<TA, TB, TC>(TA a, TB b) =>
AdditionImplementation<TA, TB, TC>.Function(a, b);
/// <summary>Adds two values [<paramref name="a"/> + <paramref name="b"/>].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the addition [<paramref name="a"/> + <paramref name="b"/>].</returns>
public static T Addition<T>(T a, T b) =>
Addition<T, T, T>(a, b);
/// <summary>Adds multiple values [<paramref name="a"/> + <paramref name="b"/> + <paramref name="c"/> + ...].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="a">The first operand of the addition.</param>
/// <param name="b">The second operand of the addition.</param>
/// <param name="c">The third operand of the addition.</param>
/// <param name="d">The remaining operands of the addition.</param>
/// <returns>The result of the addition [<paramref name="a"/> + <paramref name="b"/> + <paramref name="c"/> + ...].</returns>
public static T Addition<T>(T a, T b, T c, params T[] d) =>
Addition<T>(step => { step(a); step(b); step(c); d.ToStepper()(step); });
/// <summary>Adds multiple values [step1 + step2 + step3 + ...].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="stepper">The stepper of the values to add.</param>
/// <returns>The result of the addition [step1 + step2 + step3 + ...].</returns>
public static T Addition<T>(Action<Action<T>> stepper) =>
OperationOnStepper(stepper, Addition);
internal static class AdditionImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.Add(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
};
}
#endregion
#region Subtraction
/// <summary>Subtracts two values [<paramref name="a"/> - <paramref name="b"/>].</summary>
/// <typeparam name="TA">The type of the left operand.</typeparam>
/// <typeparam name="TB">The type of the right operand.</typeparam>
/// <typeparam name="TC">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the subtraction [<paramref name="a"/> - <paramref name="b"/>].</returns>
public static TC Subtraction<TA, TB, TC>(TA a, TB b) =>
SubtractionImplementation<TA, TB, TC>.Function(a, b);
/// <summary>Subtracts two values [<paramref name="a"/> - <paramref name="b"/>].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the subtraction [<paramref name="a"/> - <paramref name="b"/>].</returns>
public static T Subtraction<T>(T a, T b) =>
Subtraction<T, T, T>(a, b);
/// <summary>Subtracts multiple values [<paramref name="a"/> - <paramref name="b"/> - <paramref name="c"/> - ...].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="a">The first operand.</param>
/// <param name="b">The second operand.</param>
/// <param name="c">The third operand.</param>
/// <param name="d">The remaining values.</param>
/// <returns>The result of the subtraction [<paramref name="a"/> - <paramref name="b"/> - <paramref name="c"/> - ...].</returns>
public static T Subtraction<T>(T a, T b, T c, params T[] d) =>
Subtraction<T>(step => { step(a); step(b); step(c); d.ToStepper()(step); });
/// <summary>Subtracts multiple numeric values [step1 - step2 - step3 - ...].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="stepper">The stepper containing the values.</param>
/// <returns>The result of the subtraction [step1 - step2 - step3 - ...].</returns>
public static T Subtraction<T>(Action<Action<T>> stepper) =>
OperationOnStepper(stepper, Subtraction);
internal static class SubtractionImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.Subtract(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
};
}
#endregion
#region Multiplication
/// <summary>Multiplies two values [<paramref name="a"/> * <paramref name="b"/>].</summary>
/// <typeparam name="TA">The type of the left operand.</typeparam>
/// <typeparam name="TB">The type of the right operand.</typeparam>
/// <typeparam name="TC">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the multiplication [<paramref name="a"/> * <paramref name="b"/>].</returns>
public static TC Multiplication<TA, TB, TC>(TA a, TB b) =>
MultiplicationImplementation<TA, TB, TC>.Function(a, b);
/// <summary>Multiplies two values [<paramref name="a"/> * <paramref name="b"/>].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the multiplication [<paramref name="a"/> * <paramref name="b"/>].</returns>
public static T Multiplication<T>(T a, T b) =>
Multiplication<T, T, T>(a, b);
/// <summary>Multiplies multiple values [<paramref name="a"/> * <paramref name="b"/> * <paramref name="c"/> * ...].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="a">The first operand.</param>
/// <param name="b">The second operand.</param>
/// <param name="c">The third operand.</param>
/// <param name="d">The remaining values.</param>
/// <returns>The result of the multiplication [<paramref name="a"/> * <paramref name="b"/> * <paramref name="c"/> * ...].</returns>
public static T Multiplication<T>(T a, T b, T c, params T[] d) =>
Multiplication<T>(step => { step(a); step(b); step(c); d.ToStepper()(step); });
/// <summary>Multiplies multiple values [step1 * step2 * step3 * ...].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="stepper">The stepper containing the values.</param>
/// <returns>The result of the multiplication [step1 * step2 * step3 * ...].</returns>
public static T Multiplication<T>(Action<Action<T>> stepper) =>
OperationOnStepper(stepper, Multiplication);
internal static class MultiplicationImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.Multiply(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
};
}
#endregion
#region Division
/// <summary>Divides two values [<paramref name="a"/> / <paramref name="b"/>].</summary>
/// <typeparam name="TA">The type of the left operand.</typeparam>
/// <typeparam name="TB">The type of the right operand.</typeparam>
/// <typeparam name="TC">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the division [<paramref name="a"/> / <paramref name="b"/>].</returns>
public static TC Division<TA, TB, TC>(TA a, TB b) =>
DivisionImplementation<TA, TB, TC>.Function(a, b);
/// <summary>Divides two values [<paramref name="a"/> / <paramref name="b"/>].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the division [<paramref name="a"/> / <paramref name="b"/>].</returns>
public static T Division<T>(T a, T b) =>
Division<T, T, T>(a, b);
/// <summary>Divides multiple values [<paramref name="a"/> / <paramref name="b"/> / <paramref name="c"/> / ...].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="a">The first operand of the division.</param>
/// <param name="b">The second operand of the division.</param>
/// <param name="c">The third operand of the division.</param>
/// <param name="d">The remaining values of the division.</param>
/// <returns>The result of the division [<paramref name="a"/> / <paramref name="b"/> / <paramref name="c"/> / ...].</returns>
public static T Division<T>(T a, T b, T c, params T[] d) =>
Division<T>(step => { step(a); step(b); step(c); d.ToStepper()(step); });
/// <summary>Divides multiple values [step1 / step2 / step3 / ...].</summary>
/// <typeparam name="T">The type of the operation.</typeparam>
/// <param name="stepper">The stepper containing the values.</param>
/// <returns>The result of the division [step1 / step2 / step3 / ...].</returns>
public static T Division<T>(Action<Action<T>> stepper) =>
OperationOnStepper(stepper, Division);
internal static class DivisionImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.Divide(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
};
}
#endregion
#region Remainder
/// <summary>Remainders two values [<paramref name="a"/> % <paramref name="b"/>].</summary>
/// <typeparam name="TA">The type of the left operand.</typeparam>
/// <typeparam name="TB">The type of the right operand.</typeparam>
/// <typeparam name="TC">The type of the return.</typeparam>
/// <param name="a">The left operand.</param>
/// <param name="b">The right operand.</param>
/// <returns>The result of the remainder operation [<paramref name="a"/> % <paramref name="b"/>].</returns>
public static TC Remainder<TA, TB, TC>(TA a, TB b) =>
RemainderImplementation<TA, TB, TC>.Function(a, b);
/// <summary>Modulos two numeric values [<paramref name="a"/> % <paramref name="b"/>].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first operand of the modulation.</param>
/// <param name="b">The second operand of the modulation.</param>
/// <returns>The result of the modulation.</returns>
public static T Remainder<T>(T a, T b) =>
Remainder<T, T, T>(a, b);
/// <summary>Modulos multiple numeric values [<paramref name="a"/> % <paramref name="b"/> % <paramref name="c"/> % ...].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first operand of the modulation.</param>
/// <param name="b">The second operand of the modulation.</param>
/// <param name="c">The third operand of the modulation.</param>
/// <param name="d">The remaining values of the modulation.</param>
/// <returns>The result of the modulation.</returns>
public static T Remainder<T>(T a, T b, T c, params T[] d) =>
Remainder<T>(step => { step(a); step(b); step(c); d.ToStepper()(step); });
/// <summary>Modulos multiple numeric values [step_1 % step_2 % step_3...].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="stepper">The stepper containing the values.</param>
/// <returns>The result of the modulation.</returns>
public static T Remainder<T>(Action<Action<T>> stepper) =>
OperationOnStepper(stepper, Remainder);
internal static class RemainderImplementation<TA, TB, TC>
{
internal static Func<TA, TB, TC> Function = (a, b) =>
{
var A = Expression.Parameter(typeof(TA));
var B = Expression.Parameter(typeof(TB));
var BODY = Expression.Modulo(A, B);
Function = Expression.Lambda<Func<TA, TB, TC>>(BODY, A, B).Compile();
return Function(a, b);
};
}
#endregion
#region Inversion
/// <summary>Inverts a numeric value [1 / a].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The numeric value to invert.</param>
/// <returns>The result of the inversion.</returns>
public static T Inversion<T>(T a) =>
Division(Constant<T>.One, a);
#endregion
#region Power
/// <summary>Powers two numeric values [a ^ b].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first operand of the power.</param>
/// <param name="b">The second operand of the power.</param>
/// <returns>The result of the power.</returns>
public static T Power<T>(T a, T b) =>
PowerImplementation<T>.Function(a, b);
/// <summary>Powers multiple numeric values [a ^ b ^ c...].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first operand of the power.</param>
/// <param name="b">The second operand of the power.</param>
/// <param name="c">The third operand of the power.</param>
/// <param name="d">The remaining values of the power.</param>
/// <returns>The result of the power.</returns>
public static T Power<T>(T a, T b, T c, params T[] d) =>
Power<T>(step => { step(a); step(b); step(c); d.ToStepper()(step); });
/// <summary>Powers multiple numeric values [step_1 ^ step_2 ^ step_3...].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="stepper">The stepper containing the values.</param>
/// <returns>The result of the power.</returns>
public static T Power<T>(Action<Action<T>> stepper) =>
OperationOnStepper(stepper, Power);
internal static class PowerImplementation<T>
{
internal static Func<T, T, T> Function = (a, b) =>
{
// Note: this code needs to die.. but this works until it gets a better version
// optimization for specific known types
if (TypeDescriptor.GetConverter(typeof(T)).CanConvertTo(typeof(double)))
{
var A = Expression.Parameter(typeof(T));
var B = Expression.Parameter(typeof(T));
var Math_Pow = typeof(Math).GetMethod(nameof(Math.Pow));
if (Math_Pow is not null)
{
Expression BODY = Expression.Convert(Expression.Call(Math_Pow, Expression.Convert(A, typeof(double)), Expression.Convert(B, typeof(double))), typeof(T));
Function = Expression.Lambda<Func<T, T, T>>(BODY, A, B).Compile();
return Function(a, b);
}
}
Function = (A, B) =>
{
if (IsInteger(B) && IsPositive(B) && LessThan(B, Convert<int, T>(int.MaxValue)))
{
T result = A;
int power = Convert<T, int>(B);
for (int i = 0; i < power; i++)
{
result = Multiplication(result, A);
}
return result;
}
else
{
#warning TODO
throw new NotImplementedException("This feature is still in development.");
}
};
return Function(a, b);
};
}
#endregion
#region SquareRoot
/// <summary>Square roots a numeric value [√a].</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The numeric value to square root.</param>
/// <returns>The result of the square root.</returns>
public static T SquareRoot<T>(T a) =>
SquareRootImplementation<T>.Function(a);
internal static class SquareRootImplementation<T>
{
internal static Func<T, T> Function = a =>
{
#region Optimization(int)
if (typeof(T) == typeof(int))
{
static int SquareRoot(int x)