Menu

[9e5f1b]: / Sasa / Operators.cs  Maximize  Restore  History

Download this file

895 lines (835 with data), 34.9 kB

  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
using System;
using System.Collections.Generic;
using System.Text;
using Sasa.Emit;
using System.Reflection;
namespace Sasa
{
/// <summary>
/// Operators defined for type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type on which the operators are defined.</typeparam>
/// <remarks>
/// When writing generic code, it's pretty common to yearn for some generic operator interface so
/// you don't have to copy-paste the same function over and over to provide overloads for all the
/// CLR's primitive types. Omitting a standard operator interface was one of .NET's biggest mistakes
/// in my opinion.
///
/// Enter <see cref="Operators{T}"/>, a static class library that exposes the delegates for the
/// standard mathematical and logical operators on any given type <typeparamref name="T"/>.
/// If <typeparamref name="T"/> does not contain a particular operator overload, the
/// corresponding delegate for that operation will be null, so you just need to check for
/// their presence as a precondition.
/// </remarks>
public static class Operators<T>
{
static Operators()
{
Add = Operators<T, T, T>.Add;
Subtract = Operators<T, T, T>.Subtract;
Divide = Operators<T, T, T>.Divide;
Multiply = Operators<T, T, T>.Multiply;
Modulo = Operators<T, T, T>.Modulo;
Negate = Operators<T, T>.Negate;
Not = Operators<T, T>.Not;
Or = Operators<T, T, T>.Or;
Xor = Operators<T, T, T>.Xor;
And = Operators<T, T, T>.And;
LeftShift = Operators<T, T>.LeftShift;
RightShift = Operators<T, T>.RightShift;
GreaterThan = Operators<T, T>.GreaterThan;
LessThan = Operators<T, T>.LessThan;
GreaterThanOrEqual = Operators<T, T>.GreaterThanOrEqual;
LessThanOrEqual = Operators<T, T>.LessThanOrEqual;
Increment = Operators<T, T>.Increment;
Decrement = Operators<T, T>.Decrement;
Plus = Operators<T, T>.Plus;
True = Func.Operator<Func<T, bool>>(Operators.True);
False = Func.Operator<Func<T, bool>>(Operators.False);
Equal = Operators<T, T>.Equal;
NotEqual = Operators<T, T>.NotEqual;
MaxValue = Select("MaxValue", int.MaxValue, long.MaxValue, uint.MaxValue, ulong.MaxValue, char.MaxValue, byte.MaxValue, decimal.MaxValue, float.MaxValue, double.MaxValue, short.MaxValue, ushort.MaxValue);
MinValue = Select("MinValue", int.MinValue, long.MinValue, uint.MinValue, ulong.MinValue, char.MinValue, byte.MinValue, decimal.MinValue, float.MinValue, double.MinValue, short.MinValue, ushort.MinValue);
}
/// <summary>
/// The minimum value, if applicable.
/// </summary>
public static readonly T MinValue;
/// <summary>
/// The max possible value, if applicable.
/// </summary>
public static readonly T MaxValue;
static T Select(string name, int i, long l, uint ui, ulong ul, char c, byte b, decimal dc, float f, double db, short s, ushort us) =>
typeof(T) == typeof(int) ? (T)(object)i:
typeof(T) == typeof(long) ? (T)(object)l:
typeof(T) == typeof(uint) ? (T)(object)ui:
typeof(T) == typeof(ulong) ? (T)(object)ul:
typeof(T) == typeof(char) ? (T)(object)c:
typeof(T) == typeof(byte) ? (T)(object)b:
typeof(T) == typeof(decimal)? (T)(object)dc:
typeof(T) == typeof(float) ? (T)(object)f:
typeof(T) == typeof(double) ? (T)(object)db:
typeof(T) == typeof(short) ? (T)(object)s:
typeof(T) == typeof(ushort) ? (T)(object)us:
(T)(typeof(T).GetRuntimeProperty(name)?.GetValue(null) ?? default(T));
/// <summary>
/// Adds two arguments.
/// </summary>
/// <remarks>
/// The standard addition operation is straightforward, taking two T arguments and returning their addition:
/// <code>
/// var x = Operators&lt;int&gt;.Add(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // 3
/// </code>
/// </remarks>
public static readonly Func<T, T, T> Add;
/// <summary>
/// Subtracts two arguments.
/// </summary>
/// <remarks>
/// The subtraction operator takes two T arguments, and returns their subtraction:
/// <code>
/// var x = Operators&lt;int&gt;.Subtract(1, 3);
/// Console.WriteLine(x);
/// // output:
/// // -2
/// </code>
/// </remarks>
public static readonly Func<T, T, T> Subtract;
/// <summary>
/// Divides two arguments.
/// </summary>
/// <remarks>
/// The divide operator two T arguments and returns a T argument corresponding to their division:
/// <code>
/// var x = Operators&lt;int&gt;.Divide(6, 2);
/// Console.WriteLine(x);
/// // output:
/// // 3
/// </code>
/// </remarks>
public static readonly Func<T, T, T> Divide;
/// <summary>
/// Multiplies two arguments.
/// </summary>
/// <remarks>
/// The multiply operator takes two T arguments, and returns their multiplication:
/// <code>
/// var x = Operators&lt;int&gt;.Multiply(9, 2);
/// Console.WriteLine(x);
/// // output:
/// // 18
/// </code>
/// </remarks>
public static readonly Func<T, T, T> Multiply;
/// <summary>
/// Takes the remainder of two arguments.
/// </summary>
/// <remarks>
/// The modulo operator takes two T arguments, and returns the modulus/remainder of dividing the two arguments:
/// <code>
/// var x = Operators&lt;int&gt;.Modulo(9, 2);
/// Console.WriteLine(x);
/// // output:
/// // 1
/// </code>
/// </remarks>
public static readonly Func<T, T, T> Modulo;
/// <summary>
/// Negates an argument.
/// </summary>
/// <remarks>
/// The negate operator takes a T argument, and returns its negation:
/// <code>
/// var x = Operators&lt;int&gt;.Negate(9);
/// Console.WriteLine(x);
/// // output:
/// // -9
/// </code>
/// </remarks>
public static readonly Func<T, T> Negate;
/// <summary>
/// Bitwise complement.
/// </summary>
/// <remarks>
/// The 'not' operator takes a T argument, and returns logical/bitwise not of the value:
/// <code>
/// // given twos-complement, not(-1) == 0
/// var x = Operators&lt;int&gt;.Not(-1);
/// Console.WriteLine(x);
/// // output:
/// // 0
/// </code>
/// </remarks>
public static readonly Func<T, T> Not;
/// <summary>
/// Increments an argument.
/// </summary>
/// <remarks>
/// The increment operator a T argument and returns an "incremented" T:
/// <code>
/// var x = Operators&lt;int&gt;.Increment(98);
/// Console.WriteLine(x);
/// // output:
/// // 99
/// </code>
/// </remarks>
public static readonly Func<T, T> Increment;
/// <summary>
/// Decrements an argument.
/// </summary>
/// <remarks>
/// The decrement operator takes a T argument, and returns a "decremented" T argument:
/// <code>
/// var x = Operators&lt;double&gt;.Decrement(1.5);
/// Console.WriteLine(x);
/// // output:
/// // 0.5
/// </code>
/// </remarks>
public static readonly Func<T, T> Decrement;
/// <summary>
/// Or's an argument.
/// </summary>
/// <remarks>
/// The 'or' operator takes two T arguments, and returns their logical/bitwise-or:
/// <code>
/// var x = Operators&lt;int&gt;.Or(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // 3
/// </code>
/// </remarks>
public static readonly Func<T, T, T> Or;
/// <summary>
/// Xor's its argument.
/// </summary>
/// <remarks>
/// The xor operator takes two T arguments, and returns their logical/bitwise exclusive-or:
/// <code>
/// var x = Operators&lt;int&gt;.Xor(1, 3);
/// Console.WriteLine(x);
/// // output:
/// // 2
/// </code>
/// </remarks>
public static readonly Func<T, T, T> Xor;
/// <summary>
/// And's its argument.
/// </summary>
/// <remarks>
/// The 'And' operation takes two T arguments, and returns a T argument which is the bitwise/logical
/// 'and' of the two arguments:
/// <code>
/// var x = Operators&lt;int&gt;.And(1, 3);
/// Console.WriteLine(x);
/// // output:
/// // 1
/// </code>
/// </remarks>
public static readonly Func<T, T, T> And;
/// <summary>
/// Left shifts an argument.
/// </summary>
/// <remarks>
/// The left shift operator a T argument, followed by an Int32 argument, and returns a T "left-shifted" by the Int32:
/// <code>
/// var x = Operators&lt;int&gt;.LeftShift(1, 3);
/// Console.WriteLine(x);
/// // output:
/// // 8
/// </code>
/// </remarks>
public static readonly Func<T, int, T> LeftShift;
/// <summary>
/// Right shifts an argument.
/// </summary>
/// <remarks>
/// The right shift operator a T argument, followed by an Int32 argument, and returns a T "right-shifted" by the Int32:
/// <code>
/// var x = Operators&lt;int&gt;.RightShift(8, 3);
/// Console.WriteLine(x);
/// // output:
/// // 1
/// </code>
/// </remarks>
public static readonly Func<T, int, T> RightShift;
/// <summary>
/// Converts the type to a boolean.
/// </summary>
/// <remarks>
/// The boolean conversion operator:
/// <code>
/// Console.WriteLine(Operators&lt;bool&gt;.True(false));
/// Console.WriteLine(Operators&lt;bool&gt;.True(true));
/// // output:
/// // false
/// // true
/// </code>
/// </remarks>
public static readonly Func<T, bool> True;
/// <summary>
/// Converts the type to a boolean.
/// </summary>
/// <remarks>
/// The boolean conversion operator:
/// <code>
/// Console.WriteLine(Operators&lt;bool&gt;.False(false));
/// Console.WriteLine(Operators&lt;bool&gt;.False(true));
/// // output:
/// // true
/// // false
/// </code>
/// </remarks>
public static readonly Func<T, bool> False;
/// <summary>
/// Unary plus operator.
/// </summary>
/// <remarks>
/// The unary plus operator:
/// <code>
/// var x = Operators&lt;Foo&gt;.Plus(new Foo());
/// Console.WriteLine(x);
/// // output:
/// // 1
/// </code>
/// </remarks>
public static readonly Func<T, T> Plus;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes two T arguments, and returns true if the first is greater than the second:
/// <code>
/// var x = Operators&lt;int&gt;.GreaterThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // false
/// </code>
/// </remarks>
public static readonly Func<T, T, bool> GreaterThan;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes two T arguments, and returns true if the first is less than the second:
/// <code>
/// var x = Operators&lt;int&gt;.LessThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // true
/// </code>
/// </remarks>
public static readonly Func<T, T, bool> LessThan;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes two T arguments, and returns true if the first is greater than or equal to the second:
/// <code>
/// var x = Operators&lt;int&gt;.GreaterThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // false
/// </code>
/// </remarks>
public static readonly Func<T, T, bool> GreaterThanOrEqual;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes two T arguments, and returns true if the first is less than or equal to the second:
/// <code>
/// var x = Operators&lt;int&gt;.LessThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // true
/// </code>
/// </remarks>
public static readonly Func<T, T, bool> LessThanOrEqual;
/// <summary>
/// Compare arguments for equality.
/// </summary>
/// <remarks>
/// The == operator takes two T arguments, and returns true if they are equal:
/// <code>
/// var x = Operators&lt;int&gt;.Equal(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // false
/// </code>
/// </remarks>
public static readonly Func<T, T, bool> Equal;
/// <summary>
/// Compare arguments for inequality.
/// </summary>
/// <remarks>
/// The == operator takes two T arguments, and returns true if they are not equal:
/// <code>
/// var x = Operators&lt;int&gt;.NotEqual(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // true
/// </code>
/// </remarks>
public static readonly Func<T, T, bool> NotEqual;
}
/// <summary>
/// Operators defined for types <typeparamref name="T0"/> and <typeparamref name="T1"/>.
/// </summary>
/// <typeparam name="T0">The first operator type.</typeparam>
/// <typeparam name="T1">The second operator type.</typeparam>
/// <remarks>
/// When writing generic code, it's pretty common to yearn for some generic operator interface so
/// you don't have to copy-paste the same function over and over to provide overloads for all the
/// CLR's primitive types. Omitting a standard operator interface was one of .NET's biggest mistakes
/// in my opinion.
///
/// Enter <see cref="Operators{T0, T1}"/>, a static class library that exposes delegates for the
/// overloadable operators defined on types <typeparamref name="T0"/> and <typeparamref name="T1"/>.
/// If neither type contains an operator overload for a given operation, the corresponding delegate
/// for that operation will be null, so you just need to check for their presence as a precondition.
/// </remarks>
public static class Operators<T0, T1>
{
static Operators()
{
Increment = Func.Operator<Func<T0, T1>>(Operators.Increment);
Decrement = Func.Operator<Func<T0, T1>>(Operators.Decrement);
Negate = Func.Operator<Func<T0, T1>>(Operators.UnaryNegation);
Not = Func.Operator<Func<T0, T1>>(Operators.OnesComplement);
LeftShift = Func.Operator<Func<T0, int, T1>>(Operators.LeftShift);
RightShift = Func.Operator<Func<T0, int, T1>>(Operators.RightShift);
GreaterThan = Func.Operator<Func<T0, T1, bool>>(Operators.GreaterThan);
LessThan = Func.Operator<Func<T0, T1, bool>>(Operators.LessThan);
GreaterThanOrEqual = Func.Operator<Func<T0, T1, bool>>(Operators.GreaterThanOrEqual);
LessThanOrEqual = Func.Operator<Func<T0, T1, bool>>(Operators.LessThanOrEqual);
Equal = Func.Operator<Func<T0, T1, bool>>(Operators.Equality);
NotEqual = Func.Operator<Func<T0, T1, bool>>(Operators.Inequality);
Plus = Func.Operator<Func<T0, T1>>(Operators.UnaryPlus);
Explicit = Func.Operator<Func<T0, T1>>(Operators.Explicit);
Implicit = Func.Operator<Func<T0, T1>>(Operators.Implicit);
}
/// <summary>
/// Negates an argument.
/// </summary>
/// <remarks>
/// The negate operator takes a T argument, and returns its negation:
/// <code>
/// var x = Operators&lt;int, int&gt;.Negate(9);
/// Console.WriteLine(x);
/// // output:
/// // -9
/// </code>
/// </remarks>
public static readonly Func<T0, T1> Negate;
/// <summary>
/// Implicitly converts an argument type.
/// </summary>
/// <remarks>
/// The implicit conversion operator converts a <typeparamref name="T0"/> to a <typeparamref name="T1"/>:
/// <code>
/// var x = Operators&lt;int, int&gt;.Implicit(9);
/// Console.WriteLine(x);
/// // output:
/// // -9
/// </code>
/// </remarks>
public static readonly Func<T0, T1> Implicit;
/// <summary>
/// Explicitly converts an argument type.
/// </summary>
/// <remarks>
/// The explicit conversion operator converts a <typeparamref name="T0"/> to a <typeparamref name="T1"/>:
/// <code>
/// var x = Operators&lt;int, float&gt;.Explicit(9);
/// Console.WriteLine(x);
/// // output:
/// // -9
/// </code>
/// </remarks>
public static readonly Func<T0, T1> Explicit;
/// <summary>
/// Bitwise complement.
/// </summary>
/// <remarks>
/// The 'not' operator takes a T argument, and returns logical/bitwise not of the value:
/// <code>
/// // given twos-complement, not(-1) == 0
/// var x = Operators&lt;int, int&gt;.Not(-1);
/// Console.WriteLine(x);
/// // output:
/// // 0
/// </code>
/// </remarks>
public static readonly Func<T0, T1> Not;
/// <summary>
/// Increments an argument.
/// </summary>
/// <remarks>
/// The increment operator a T argument and returns an "incremented" T:
/// <code>
/// var x = Operators&lt;int, int&gt;.Increment(98);
/// Console.WriteLine(x);
/// // output:
/// // 99
/// </code>
/// </remarks>
public static readonly Func<T0, T1> Increment;
/// <summary>
/// Decrements an argument.
/// </summary>
/// <remarks>
/// The decrement operator takes a T argument, and returns a "decremented" T argument:
/// <code>
/// var x = Operators&lt;double, double&gt;.Decrement(1.5);
/// Console.WriteLine(x);
/// // output:
/// // 0.5
/// </code>
/// </remarks>
public static readonly Func<T0, T1> Decrement;
/// <summary>
/// Left shifts an argument.
/// </summary>
/// <remarks>
/// The left shift operator a T argument, followed by an Int32 argument, and returns a T "left-shifted" by the Int32:
/// <code>
/// var x = Operators&lt;int, int&gt;.LeftShift(1, 3);
/// Console.WriteLine(x);
/// // output:
/// // 8
/// </code>
/// </remarks>
public static readonly Func<T0, int, T1> LeftShift;
/// <summary>
/// Right shifts an argument.
/// </summary>
/// <remarks>
/// The right shift operator a T argument, followed by an Int32 argument, and returns a T "right-shifted" by the Int32:
/// <code>
/// var x = Operators&lt;int, int&gt;.RightShift(8, 3);
/// Console.WriteLine(x);
/// // output:
/// // 1
/// </code>
/// </remarks>
public static readonly Func<T0, int, T1> RightShift;
/// <summary>
/// Unary plus operator.
/// </summary>
/// <remarks>
/// The unary plus operator:
/// <code>
/// var x = Operators&lt;int, int&gt;.Plus(-1);
/// Console.WriteLine(x);
/// // output:
/// // -1
/// </code>
/// </remarks>
public static readonly Func<T0, T1> Plus;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes two T arguments, and returns true if the first is greater than the second:
/// <code>
/// var x = Operators&lt;int, int&gt;.GreaterThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // false
/// </code>
/// </remarks>
public static readonly Func<T0, T1, bool> GreaterThan;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes two T arguments, and returns true if the first is less than the second:
/// <code>
/// var x = Operators&lt;int, int&gt;.LessThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // true
/// </code>
/// </remarks>
public static readonly Func<T0, T1, bool> LessThan;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes two T arguments, and returns true if the first is greater than or equal to the second:
/// <code>
/// var x = Operators&lt;int, int&gt;.GreaterThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // false
/// </code>
/// </remarks>
public static readonly Func<T0, T1, bool> GreaterThanOrEqual;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes two T arguments, and returns true if the first is less than or equal to the second:
/// <code>
/// var x = Operators&lt;int, int&gt;.LessThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // true
/// </code>
/// </remarks>
public static readonly Func<T0, T1, bool> LessThanOrEqual;
/// <summary>
/// Compare arguments for equality.
/// </summary>
/// <remarks>
/// The == operator takes two T arguments, and returns true if they are equal:
/// <code>
/// var x = Operators&lt;int, int&gt;.Equal(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // false
/// </code>
/// </remarks>
public static readonly Func<T0, T1, bool> Equal;
/// <summary>
/// Compare arguments for inequality.
/// </summary>
/// <remarks>
/// The == operator takes two T arguments, and returns true if they are not equal:
/// <code>
/// var x = Operators&lt;int, int&gt;.NotEqual(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // true
/// </code>
/// </remarks>
public static readonly Func<T0, T1, bool> NotEqual;
}
/// <summary>
/// Operators defined for types <typeparamref name="T0"/>, <typeparamref name="T1"/>
/// <typeparamref name="T2"/>.
/// </summary>
/// <typeparam name="T0">The first operator type.</typeparam>
/// <typeparam name="T1">The second operator type.</typeparam>
/// <typeparam name="T2">The third operator type.</typeparam>
/// <remarks>
/// When writing generic code, it's pretty common to yearn for some generic operator interface so
/// you don't have to copy-paste the same function over and over to provide overloads for all the
/// CLR's primitive types. Omitting a standard operator interface was one of .NET's biggest mistakes
/// in my opinion.
///
/// Enter <see cref="Operators{T0, T1, T2}"/>, a static class library that exposes delegates for the
/// overloadable operators defined on types <typeparamref name="T0"/> and <typeparamref name="T1"/>.
/// If neither type contains an operator overload for a given operation, the corresponding delegate
/// for that operation will be null, so you just need to check for their presence as a precondition.
/// </remarks>
public static class Operators<T0, T1, T2>
{
static Operators()
{
Add = Func.Operator<Func<T0, T1, T2>>(Operators.Addition);
Subtract = Func.Operator<Func<T0, T1, T2>>(Operators.Subtraction);
Divide = Func.Operator<Func<T0, T1, T2>>(Operators.Division);
Multiply = Func.Operator<Func<T0, T1, T2>>(Operators.Multiply);
Modulo = Func.Operator<Func<T0, T1, T2>>(Operators.Modulus);
Or = Func.Operator<Func<T0, T1, T2>>(Operators.BitwiseOr);
And = Func.Operator<Func<T0, T1, T2>>(Operators.BitwiseAnd);
Xor = Func.Operator<Func<T0, T1, T2>>(Operators.ExclusiveOr);
GreaterThan = Func.Operator<Func<T0, T1, T2>>(Operators.GreaterThan);
GreaterThanOrEqual = Func.Operator<Func<T0, T1, T2>>(Operators.GreaterThanOrEqual);
LessThan = Func.Operator<Func<T0, T1, T2>>(Operators.LessThan);
LessThanOrEqual = Func.Operator<Func<T0, T1, T2>>(Operators.LessThanOrEqual);
Equal = Func.Operator<Func<T0, T1, T2>>(Operators.Equality);
NotEqual = Func.Operator<Func<T0, T1, T2>>(Operators.Inequality);
}
/// <summary>
/// Adds two arguments.
/// </summary>
/// <remarks>
/// The standard addition operation takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and
/// returns their addition as type <typeparamref name="T2"/>:
/// <code>
/// var x = Operators&lt;int, long, double&gt;.Add(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // 3.0
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> Add;
/// <summary>
/// Subtracts two arguments.
/// </summary>
/// <remarks>
/// The subtraction operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their subtraction as type <typeparamref name="T2"/>:
/// <code>
/// var x = Operators&lt;int, int, int&gt;.Subtract(1, 3);
/// Console.WriteLine(x);
/// // output:
/// // -2
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> Subtract;
/// <summary>
/// Divides two arguments.
/// </summary>
/// <remarks>
/// The divide operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their
/// division as type <typeparamref name="T2"/>:
/// <code>
/// var x = Operators&lt;long, int, int&gt;.Divide(6, 2);
/// Console.WriteLine(x);
/// // output:
/// // 3
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> Divide;
/// <summary>
/// Multiplies two arguments.
/// </summary>
/// <remarks>
/// The multiply operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their multiplication as type <typeparamref name="T2"/>:
/// <code>
/// var x = Operators&lt;int, int, uint&gt;.Multiply(9, 2);
/// Console.WriteLine(x);
/// // output:
/// // 18
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> Multiply;
/// <summary>
/// Takes the remainder of two arguments.
/// </summary>
/// <remarks>
/// The modulo operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their modulus as type <typeparamref name="T2"/>:
/// <code>
/// var x = Operators&lt;int, int, int&gt;.Modulo(9, 2);
/// Console.WriteLine(x);
/// // output:
/// // 1
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> Modulo;
/// <summary>
/// Or's an argument.
/// </summary>
/// <remarks>
/// The 'or' operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their logical/bitwise-or as type <typeparamref name="T2"/>:
/// <code>
/// var x = Operators&lt;int&gt;.Or(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // 3
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> Or;
/// <summary>
/// Xor's its argument.
/// </summary>
/// <remarks>
/// The xor operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their logical/bitwise exclusive-or as type <typeparamref name="T2"/>:
/// <code>
/// var x = Operators&lt;int, int, uint&gt;.Xor(1, 3);
/// Console.WriteLine(x);
/// // output:
/// // 2
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> Xor;
/// <summary>
/// And's its argument.
/// </summary>
/// <remarks>
/// The 'And' operation takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their
/// bitwise/logical 'and' as type <typeparamref name="T2"/>:
/// <code>
/// var x = Operators&lt;int, uint, uint&gt;.And(1, 3);
/// Console.WriteLine(x);
/// // output:
/// // 1
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> And;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their &gt; as type <typeparamref name="T2"/>, where T2 must implement the true/false operators:
/// <code>
/// var x = Operators&lt;int&gt;.GreaterThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // false
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> GreaterThan;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their &lt; as type <typeparamref name="T2"/>, where T2 must implement the true/false operators:
/// <code>
/// var x = Operators&lt;int&gt;.LessThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // true
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> LessThan;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their &gt;= as type <typeparamref name="T2"/>, where T2 must implement the true/false operators:
/// <code>
/// var x = Operators&lt;int&gt;.GreaterThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // false
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> GreaterThanOrEqual;
/// <summary>
/// Compare argument magnitudes.
/// </summary>
/// <remarks>
/// The &gt; operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their &lt;= as type <typeparamref name="T2"/>, where T2 must implement the true/false operators:
/// <code>
/// var x = Operators&lt;int&gt;.LessThan(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // true
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> LessThanOrEqual;
/// <summary>
/// Compare arguments for equality.
/// </summary>
/// <remarks>
/// The == operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their equality as type <typeparamref name="T2"/>, where T2 must implement the true/false operators:
/// <code>
/// var x = Operators&lt;int&gt;.Equal(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // false
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> Equal;
/// <summary>
/// Compare arguments for inequality.
/// </summary>
/// <remarks>
/// The != operator takes arguments <typeparamref name="T0"/> and <typeparamref name="T1"/> and returns their inequality as type <typeparamref name="T2"/>, where T2 must implement the true/false operators:
/// <code>
/// var x = Operators&lt;int&gt;.NotEqual(1, 2);
/// Console.WriteLine(x);
/// // output:
/// // true
/// </code>
/// </remarks>
public static readonly Func<T0, T1, T2> NotEqual;
}
}