-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Statics-SequenceAnalysis.cs
2004 lines (1776 loc) · 81.5 KB
/
Statics-SequenceAnalysis.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
namespace Towel;
/// <summary>Root type of the static functional methods in Towel.</summary>
public static partial class Statics
{
#region EquateSequence
/// <summary>Determines if two sequences are equal.</summary>
/// <typeparam name="T">The element type of the sequences.</typeparam>
/// <typeparam name="TA">The type of first sequence of the equate.</typeparam>
/// <typeparam name="TB">The type of second sequence of the equate.</typeparam>
/// <typeparam name="TEquate">The type of element equate function.</typeparam>
/// <param name="start">The inclusive starting index to equate from.</param>
/// <param name="end">The inclusive ending index to equate to.</param>
/// <param name="a">The first sequence of the equate.</param>
/// <param name="b">The second sequence of the equate.</param>
/// <param name="equate">The element equate function.</param>
/// <returns>True if the spans are equal; False if not.</returns>
public static bool EquateSequence<T, TA, TB, TEquate>(int start, int end, TA a = default, TB b = default, TEquate equate = default)
where TA : struct, IFunc<int, T>
where TB : struct, IFunc<int, T>
where TEquate : struct, IFunc<T, T, bool>
{
for (int i = start; i <= end; i++)
{
if (!equate.Invoke(a.Invoke(i), b.Invoke(i)))
{
return false;
}
}
return true;
}
/// <summary>Determines if two spans are equal.</summary>
/// <typeparam name="T">The element type of the spans.</typeparam>
/// <param name="a">The first span of the equate.</param>
/// <param name="b">The second span of the equate.</param>
/// <param name="equate">The element equate function.</param>
/// <returns>True if the spans are equal; False if not.</returns>
public static bool EquateSequence<T>(ReadOnlySpan<T> a, ReadOnlySpan<T> b, Func<T, T, bool>? equate = default) =>
EquateSequence<T, SFunc<T, T, bool>>(a, b, equate ?? Equate);
/// <summary>Determines if two spans are equal.</summary>
/// <typeparam name="T">The element type of the spans.</typeparam>
/// <typeparam name="TEquate">The type of element equate function.</typeparam>
/// <param name="a">The first span of the equate.</param>
/// <param name="b">The second span of the equate.</param>
/// <param name="equate">The element equate function.</param>
/// <returns>True if the spans are equal; False if not.</returns>
public static bool EquateSequence<T, TEquate>(ReadOnlySpan<T> a, ReadOnlySpan<T> b, TEquate equate = default)
where TEquate : struct, IFunc<T, T, bool>
{
if (a.IsEmpty && b.IsEmpty)
{
return true;
}
if (a.Length != b.Length)
{
return false;
}
for (int i = 0; i < a.Length; i++)
{
if (!equate.Invoke(a[i], b[i]))
{
return false;
}
}
return true;
}
#endregion
#region EquateSet
/// <summary>Determines if neither span contains an element the other does not.</summary>
/// <typeparam name="T">The element type of each span.</typeparam>
/// <param name="a">The first span.</param>
/// <param name="b">The second span.</param>
/// <param name="equate">The function for determining equality of values.</param>
/// <param name="hash">The function for hashing the values.</param>
/// <returns>True if neither span contains an element the other does not.</returns>
public static bool EquateSet<T>(ReadOnlySpan<T> a, ReadOnlySpan<T> b, Func<T, T, bool>? equate = default, Func<T, int>? hash = default) =>
EquateSet<T, SFunc<T, T, bool>, SFunc<T, int>>(a, b, equate ?? Equate, hash ?? Hash);
/// <summary>Determines if neither span contains an element the other does not.</summary>
/// <typeparam name="T">The element type of each span.</typeparam>
/// <typeparam name="TEquate">The type of function for determining equality of values.</typeparam>
/// <typeparam name="THash">The type of function for hashing the values.</typeparam>
/// <param name="a">The first span.</param>
/// <param name="b">The second span.</param>
/// <param name="equate">The function for determining equality of values.</param>
/// <param name="hash">The function for hashing the values.</param>
/// <returns>True if neither span contains an element the other does not.</returns>
public static bool EquateSet<T, TEquate, THash>(ReadOnlySpan<T> a, ReadOnlySpan<T> b, TEquate equate = default, THash hash = default)
where TEquate : struct, IFunc<T, T, bool>
where THash : struct, IFunc<T, int>
{
if (a.IsEmpty && b.IsEmpty)
{
return true;
}
SetHashLinked<T, TEquate, THash> a_counts = new(
equate: equate,
hash: hash,
expectedCount: a.Length);
SetHashLinked<T, TEquate, THash> b_counts = new(
equate: equate,
hash: hash,
expectedCount: a.Length);
foreach (T value in a)
{
a_counts.TryAdd(value);
}
foreach (T value in b)
{
if (!a_counts.Contains(value))
{
return false;
}
b_counts.TryAdd(value);
}
return a_counts.Count == b_counts.Count;
}
#endregion
#region EquateOccurences
/// <inheritdoc cref="IsReorderOf{T, TEquate, THash}(ReadOnlySpan{T}, ReadOnlySpan{T}, TEquate, THash)"/>
public static bool EquateOccurences<T>(ReadOnlySpan<T> a, ReadOnlySpan<T> b, Func<T, T, bool>? equate = default, Func<T, int>? hash = default) =>
IsReorderOf<T, SFunc<T, T, bool>, SFunc<T, int>>(a, b, equate ?? Equate, hash ?? Hash);
/// <inheritdoc cref="IsReorderOf{T, TEquate, THash}(ReadOnlySpan{T}, ReadOnlySpan{T}, TEquate, THash)"/>
public static bool EquateOccurences<T, TEquate, THash>(ReadOnlySpan<T> a, ReadOnlySpan<T> b, TEquate equate = default, THash hash = default)
where TEquate : struct, IFunc<T, T, bool>
where THash : struct, IFunc<T, int> =>
IsReorderOf(a, b, equate, hash);
#endregion
#region Maximum
#pragma warning disable CS1711, CS1572, CS1735, SA1617
/// <summary>Finds the maximum value in a sequence.</summary>
/// <typeparam name="T">The type of values in the sequence.</typeparam>
/// <typeparam name="TCompare">The type of function for comparing <typeparamref name="T"/> values.</typeparam>
/// <param name="compare">The function for comparing <typeparamref name="T"/> values.</param>
/// <param name="values">The values to find the maximum value in.</param>
/// <param name="span">The span of values to find the maximum value in.</param>
/// <returns>
/// - <see cref="int"/> Index: the index of the first occurence of the maximum value<br/>
/// - <typeparamref name="T"/> Value: the maximum value in the sequence
/// </returns>
[Obsolete(NotIntended, true)]
public static void XML_Maximum() => throw new DocumentationMethodException();
#pragma warning restore CS1711, CS1572, CS1735, SA1617
/// <inheritdoc cref="XML_Maximum"/>
public static (int Index, T Value) Maximum<T>(Func<T, T, CompareResult>? compare = null, params T[] values) =>
Maximum<T, SFunc<T, T, CompareResult>>(values, compare ?? Compare);
/// <inheritdoc cref="XML_Maximum"/>
public static (int Index, T Value) Maximum<T>(ReadOnlySpan<T> span, Func<T, T, CompareResult>? compare = null) =>
Maximum<T, SFunc<T, T, CompareResult>>(span, compare ?? Compare);
/// <inheritdoc cref="XML_Maximum"/>
public static (int Index, T Value) Maximum<T, TCompare>(ReadOnlySpan<T> span, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult>
{
if (span.IsEmpty)
{
throw new ArgumentException($"{nameof(span)}.{nameof(span.IsEmpty)}", nameof(span));
}
int index = 0;
for (int i = 1; i < span.Length; i++)
{
if (compare.Invoke(span[i], span[index]) is Greater)
{
index = i;
}
}
return (index, span[index]);
}
#endregion
#region MaximumValue
#pragma warning disable CS1711, CS1572, SA1617, CS1735
/// <summary>Finds the maximum between two values.</summary>
/// <typeparam name="T">The type of values to compare.</typeparam>
/// <typeparam name="TCompare">The type of function for comparing <typeparamref name="T"/> values.</typeparam>
/// <param name="compare">The function for comparing <typeparamref name="T"/> values.</param>
/// <param name="a">The first value to compare.</param>
/// <param name="b">The second value to compare.</param>
/// <returns>The maximum of the two values.</returns>
[Obsolete(NotIntended, true)]
public static void XML_MaximumValue_Two() => throw new DocumentationMethodException();
#pragma warning restore CS1711, CS1572, SA1617, CS1735
/// <inheritdoc cref="XML_MaximumValue_Two"/>
public static T MaximumValue<T>(T a, T b, Func<T, T, CompareResult>? compare = null) =>
MaximumValue<T, SFunc<T, T, CompareResult>>(a, b, compare ?? Compare);
/// <inheritdoc cref="XML_MaximumValue_Two"/>
public static T MaximumValue<T, TCompare>(T a, T b, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult> =>
compare.Invoke(b, a) is Greater ? b : a;
#pragma warning disable CS1711, CS1735, CS1572, SA1617
/// <summary>Finds the maximum value in a sequence.</summary>
/// <typeparam name="T">The type of values in the sequence.</typeparam>
/// <typeparam name="TCompare">The type of function for comparing <typeparamref name="T"/> values.</typeparam>
/// <param name="compare">The function for comparing <typeparamref name="T"/> values.</param>
/// <param name="values">The values to find the maximum value in.</param>
/// <param name="span">The span of values to find the maximum value in.</param>
/// <returns>The maximum value in the sequence.</returns>
[Obsolete(NotIntended, true)]
public static void XML_MaximumValue() => throw new DocumentationMethodException();
#pragma warning restore CS1711, CS1735, CS1572, SA1617
/// <inheritdoc cref="XML_MaximumValue"/>
public static T MaximumValue<T>(Func<T, T, CompareResult>? compare = null, params T[] values) =>
MaximumValue<T, SFunc<T, T, CompareResult>>(values, compare ?? Compare);
/// <inheritdoc cref="XML_MaximumValue"/>
public static T MaximumValue<T>(ReadOnlySpan<T> span, Func<T, T, CompareResult>? compare = null) =>
MaximumValue<T, SFunc<T, T, CompareResult>>(span, compare ?? Compare);
/// <inheritdoc cref="XML_MaximumValue"/>
public static T MaximumValue<T, TCompare>(ReadOnlySpan<T> span, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult>
{
if (span.IsEmpty)
{
throw new ArgumentException($"{nameof(span)}.{nameof(span.IsEmpty)}", nameof(span));
}
T max = span[0];
for (int i = 1; i < span.Length; i++)
{
if (compare.Invoke(span[i], max) is Greater)
{
max = span[i];
}
}
return max;
}
#endregion
#region MaximumIndex
/// <summary>Finds the maximum value in a sequence.</summary>
/// <typeparam name="T">The type of values in the sequence.</typeparam>
/// <typeparam name="TGet">The type of the get method.</typeparam>
/// <typeparam name="TCompare">The type of method for comparing <typeparamref name="T"/> values.</typeparam>
/// <param name="start">The inclusive starting index to find the maximum of.</param>
/// <param name="end">The inclusive ending index to find the maximum of.</param>
/// <param name="get">The the get method.</param>
/// <param name="compare">The method for comparing <typeparamref name="T"/> values.</param>
/// <returns>The index of the first occurence of the maximum value in the sequence.</returns>
public static int MaximumIndex<T, TGet, TCompare>(int start, int end, TGet get, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult>
where TGet : struct, IFunc<int, T>
{
T max = get.Invoke(start);
int maxi = start;
if (start <= end)
{
for (int i = start + 1; i <= end; i++)
{
T ivalue = get.Invoke(i);
if (compare.Invoke(ivalue, max) is Greater)
{
maxi = i;
max = ivalue;
}
}
}
else
{
for (int i = start - 1; i >= end; i--)
{
T ivalue = get.Invoke(i);
if (compare.Invoke(ivalue, max) is Greater)
{
maxi = i;
max = ivalue;
}
}
}
return maxi;
}
#pragma warning disable CS1711, CS1572, SA1617, CS1735
/// <summary>Finds the maximum value in a sequence.</summary>
/// <typeparam name="T">The type of values in the sequence.</typeparam>
/// <typeparam name="TCompare">The type of function for comparing <typeparamref name="T"/> values.</typeparam>
/// <param name="compare">The function for comparing <typeparamref name="T"/> values.</param>
/// <param name="values">The values to find the maximum value in.</param>
/// <param name="span">The span of values to find the maximum value in.</param>
/// <returns>The index of the first occurence of the maximum value in the sequence.</returns>
[Obsolete(NotIntended, true)]
public static void XML_MaximumIndex() => throw new DocumentationMethodException();
#pragma warning restore CS1711, CS1572, SA1617, CS1735
/// <inheritdoc cref="XML_MaximumIndex"/>
public static int MaximumIndex<T>(Func<T, T, CompareResult>? compare = null, params T[] values) =>
MaximumIndex<T, SFunc<T, T, CompareResult>>(values, compare ?? Compare);
/// <inheritdoc cref="XML_MaximumIndex"/>
public static int MaximumIndex<T>(ReadOnlySpan<T> span, Func<T, T, CompareResult>? compare = null) =>
MaximumIndex<T, SFunc<T, T, CompareResult>>(span, compare ?? Compare);
/// <inheritdoc cref="XML_MaximumIndex"/>
public static int MaximumIndex<T, TCompare>(ReadOnlySpan<T> span, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult>
{
if (span.IsEmpty)
{
throw new ArgumentException($"{nameof(span)}.{nameof(span.IsEmpty)}", nameof(span));
}
int max = 0;
for (int i = 1; i < span.Length; i++)
{
if (compare.Invoke(span[i], span[max]) is Greater)
{
max = i;
}
}
return max;
}
#endregion
#region Minimum
#pragma warning disable CS1711, CS1572, CS1735, SA1617
/// <summary>Finds the minimum value in a sequence.</summary>
/// <typeparam name="T">The type of values in the sequence.</typeparam>
/// <typeparam name="TCompare">The type of function for comparing <typeparamref name="T"/> values.</typeparam>
/// <param name="compare">The function for comparing <typeparamref name="T"/> values.</param>
/// <param name="values">The values to find the minimum value in.</param>
/// <param name="span">The span of values to find the minimum value in.</param>
/// <returns>
/// - <see cref="int"/> Index: the index of the first occurence of the minimum value<br/>
/// - <typeparamref name="T"/> Value: the minimum value in the sequence
/// </returns>
[Obsolete(NotIntended, true)]
public static void XML_Minimum() => throw new DocumentationMethodException();
#pragma warning restore CS1711, CS1572, CS1735, SA1617
/// <inheritdoc cref="XML_Minimum"/>
public static (int Index, T Value) Minimum<T>(Func<T, T, CompareResult>? compare = null, params T[] values) =>
Minimum<T, SFunc<T, T, CompareResult>>(values, compare ?? Compare);
/// <inheritdoc cref="XML_Minimum"/>
public static (int Index, T Value) Minimum<T>(ReadOnlySpan<T> span, Func<T, T, CompareResult>? compare = null) =>
Minimum<T, SFunc<T, T, CompareResult>>(span, compare ?? Compare);
/// <inheritdoc cref="XML_Minimum"/>
public static (int Index, T Value) Minimum<T, TCompare>(ReadOnlySpan<T> span, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult>
{
if (span.IsEmpty)
{
throw new ArgumentException($"{nameof(span)}.{nameof(span.IsEmpty)}", nameof(span));
}
int index = 0;
for (int i = 1; i < span.Length; i++)
{
if (compare.Invoke(span[i], span[index]) is Less)
{
index = i;
}
}
return (index, span[index]);
}
#endregion
#region MinimumValue
#pragma warning disable CS1711, CS1572, CS1735, SA1617
/// <summary>Finds the minimum between two values.</summary>
/// <typeparam name="T">The type of values to compare.</typeparam>
/// <typeparam name="TCompare">The type of function for comparing <typeparamref name="T"/> values.</typeparam>
/// <param name="compare">The function for comparing <typeparamref name="T"/> values.</param>
/// <param name="a">The first value to compare.</param>
/// <param name="b">The second value to compare.</param>
/// <returns>The minimum of the two values.</returns>
[Obsolete(NotIntended, true)]
public static void XML_MinimumValue_Two() => throw new DocumentationMethodException();
#pragma warning restore CS1711, CS1572, CS1735, SA1617
/// <inheritdoc cref="XML_MinimumValue_Two"/>
public static T MinimumValue<T>(T a, T b, Func<T, T, CompareResult>? compare = null) =>
MinimumValue<T, SFunc<T, T, CompareResult>>(a, b, compare ?? Compare);
/// <inheritdoc cref="XML_MinimumValue_Two"/>
public static T MinimumValue<T, TCompare>(T a, T b, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult> =>
compare.Invoke(b, a) is Less ? b : a;
#pragma warning disable CS1711, CS1735, CS1572, SA1617
/// <summary>Finds the minimum value in a sequence.</summary>
/// <typeparam name="T">The type of values in the sequence.</typeparam>
/// <typeparam name="TCompare">The type of function for comparing <typeparamref name="T"/> values.</typeparam>
/// <param name="compare">The function for comparing <typeparamref name="T"/> values.</param>
/// <param name="values">The values to find the minimum value in.</param>
/// <param name="span">The span of values to find the minimum value in.</param>
/// <returns>The minimum value in the sequence.</returns>
[Obsolete(NotIntended, true)]
public static void XML_MinimumValue() => throw new DocumentationMethodException();
#pragma warning restore CS1711, CS1735, CS1572, SA1617
/// <inheritdoc cref="XML_MinimumValue"/>
public static T MinimumValue<T>(Func<T, T, CompareResult>? compare = null, params T[] values) =>
MinimumValue<T, SFunc<T, T, CompareResult>>(values, compare ?? Compare);
/// <inheritdoc cref="XML_MinimumValue"/>
public static T MinimumValue<T>(ReadOnlySpan<T> span, Func<T, T, CompareResult>? compare = null) =>
MinimumValue<T, SFunc<T, T, CompareResult>>(span, compare ?? Compare);
/// <inheritdoc cref="XML_MinimumValue"/>
public static T MinimumValue<T, TCompare>(ReadOnlySpan<T> span, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult>
{
if (span.IsEmpty)
{
throw new ArgumentException($"{nameof(span)}.{nameof(span.IsEmpty)}", nameof(span));
}
T min = span[0];
for (int i = 1; i < span.Length; i++)
{
if (compare.Invoke(span[i], min) is Less)
{
min = span[i];
}
}
return min;
}
#endregion
#region MinimumIndex
#pragma warning disable CS1711, CS1572, CS1735, SA1617
/// <summary>Finds the minimum value in a sequence.</summary>
/// <typeparam name="T">The type of values in the sequence.</typeparam>
/// <typeparam name="TCompare">The type of function for comparing <typeparamref name="T"/> values.</typeparam>
/// <param name="compare">The function for comparing <typeparamref name="T"/> values.</param>
/// <param name="values">The values to find the minimum value in.</param>
/// <param name="span">The span of values to find the minimum value in.</param>
/// <returns>The index of the first occurence of the minimum value in the sequence.</returns>
[Obsolete(NotIntended, true)]
public static void XML_MinimumIndex() => throw new DocumentationMethodException();
#pragma warning restore CS1711, CS1572, CS1735, SA1617
/// <inheritdoc cref="XML_MinimumIndex"/>
public static int MinimumIndex<T>(Func<T, T, CompareResult>? compare = null, params T[] values) =>
MinimumIndex<T, SFunc<T, T, CompareResult>>(values, compare ?? Compare);
/// <inheritdoc cref="XML_MinimumIndex"/>
public static int MinimumIndex<T>(ReadOnlySpan<T> span, Func<T, T, CompareResult>? compare = null) =>
MinimumIndex<T, SFunc<T, T, CompareResult>>(span, compare ?? Compare);
/// <inheritdoc cref="XML_MinimumIndex"/>
public static int MinimumIndex<T, TCompare>(ReadOnlySpan<T> span, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult>
{
if (span.IsEmpty)
{
throw new ArgumentException($"{nameof(span)}.{nameof(span.IsEmpty)}", nameof(span));
}
int min = 0;
for (int i = 1; i < span.Length; i++)
{
if (compare.Invoke(span[i], span[min]) is Less)
{
min = i;
}
}
return min;
}
#endregion
#region Range
/// <summary>Gets the range (minimum and maximum) of a set of data.</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="minimum">The minimum of the set of data.</param>
/// <param name="maximum">The maximum of the set of data.</param>
/// <param name="stepper">The set of data to get the range of.</param>
/// <exception cref="ArgumentNullException">Throws when stepper is null.</exception>
/// <exception cref="ArgumentException">Throws when stepper is empty.</exception>
public static void Range<T>(out T minimum, out T maximum, Action<Action<T>> stepper) =>
Range(stepper, out minimum, out maximum);
/// <summary>Gets the range (minimum and maximum) of a set of data.</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="stepper">The set of data to get the range of.</param>
/// <param name="minimum">The minimum of the set of data.</param>
/// <param name="maximum">The maximum of the set of data.</param>
/// <exception cref="ArgumentNullException">Throws when stepper is null.</exception>
/// <exception cref="ArgumentException">Throws when stepper is empty.</exception>
public static void Range<T>(Action<Action<T>> stepper, out T minimum, out T maximum)
{
if (stepper is null) throw new ArgumentNullException(nameof(stepper));
// Note: can't use out parameters as capture variables
T? min = default;
T? max = default;
bool assigned = false;
stepper(a =>
{
if (assigned)
{
min = LessThan(a, min) ? a : min;
max = LessThan(max, a) ? a : max;
}
else
{
min = a;
max = a;
assigned = true;
}
});
if (!assigned)
{
throw new ArgumentException("The argument is empty.", nameof(stepper));
}
minimum = min!;
maximum = max!;
}
/// <summary>Finds the minimum and maximum values from a sequence of <typeparamref name="T"/> values.</summary>
/// <typeparam name="T">The type of values in the sequence.</typeparam>
/// <typeparam name="TCompare">The type of function for comparing <typeparamref name="T"/> values.</typeparam>
/// <param name="span">The sequence of <typeparamref name="T"/> values to filter.</param>
/// <param name="compare">The function for comparing <typeparamref name="T"/> values.</param>
/// <returns>
/// - (<see cref="int"/> Index, <typeparamref name="T"/> Value) Min<br/>
/// - (<see cref="int"/> Index, <typeparamref name="T"/> Value) Max
/// </returns>
public static ((int Index, T Value) Min, (int Index, T Value) Max) Range<T, TCompare>(ReadOnlySpan<T> span, TCompare compare = default)
where TCompare : struct, IFunc<T, T, CompareResult>
{
if (span.IsEmpty)
{
throw new ArgumentException($"{nameof(span)}.{nameof(span.IsEmpty)}", nameof(span));
}
int minIndex = 0;
int maxIndex = 0;
for (int i = 1; i < span.Length; i++)
{
if (compare.Invoke(span[i], span[minIndex]) is Less)
{
minIndex = i;
}
if (compare.Invoke(span[i], span[maxIndex]) is Greater)
{
maxIndex = i;
}
}
return ((minIndex, span[minIndex]), (maxIndex, span[maxIndex]));
}
#endregion
#region Mode
/// <summary>Gets the mode(s) of a data set.</summary>
/// <typeparam name="T">The generic type of the data.</typeparam>
/// <param name="step">The action to perform on every mode value found.</param>
/// <param name="a">The first value of the data set.</param>
/// <param name="b">The rest of the data set.</param>
public static void Mode<T>(Action<T> step, T a, params T[] b) =>
Mode<T>(x => { x(a); b.ToStepper()(x); }, step);
/// <summary>Gets the mode(s) of a data set.</summary>
/// <typeparam name="T">The generic type of the data.</typeparam>
/// <param name="step">The action to perform on every mode value found.</param>
/// <param name="equate">The equality delegate.</param>
/// <param name="a">The first value of the data set.</param>
/// <param name="b">The rest of the data set.</param>
public static void Mode<T>(Action<T> step, Func<T, T, bool> equate, T a, params T[] b) =>
Mode<T>(x => { x(a); b.ToStepper()(x); }, step, equate, null);
/// <summary>Gets the mode(s) of a data set.</summary>
/// <typeparam name="T">The generic type of the data.</typeparam>
/// <param name="step">The action to perform on every mode value found.</param>
/// <param name="hash">The hash code delegate</param>
/// <param name="a">The first value of the data set.</param>
/// <param name="b">The rest of the data set.</param>
public static void Mode<T>(Action<T> step, Func<T, int> hash, T a, params T[] b) =>
Mode<T>(x => { x(a); b.ToStepper()(x); }, step, null, hash);
/// <summary>Gets the mode(s) of a data set.</summary>
/// <typeparam name="T">The generic type of the data.</typeparam>
/// <param name="step">The action to perform on every mode value found.</param>
/// <param name="equate">The equality delegate.</param>
/// <param name="hash">The hash code delegate</param>
/// <param name="a">The first value of the data set.</param>
/// <param name="b">The rest of the data set.</param>
public static void Mode<T>(Action<T> step, Func<T, T, bool> equate, Func<T, int> hash, T a, params T[] b) =>
Mode<T>(x => { x(a); b.ToStepper()(x); }, step, equate, hash);
/// <summary>Gets the mode(s) of a data set.</summary>
/// <typeparam name="T">The generic type of the data.</typeparam>
/// <param name="stepper">The data set.</param>
/// <param name="step">The action to perform on every mode value found.</param>
/// <param name="equate">The equality delegate.</param>
/// <param name="hash">The hash code delegate</param>
public static void Mode<T>(Action<Action<T>> stepper, Action<T> step, Func<T, T, bool>? equate = null, Func<T, int>? hash = null)
{
int maxOccurences = -1;
IMap<int, T> map = MapHashLinked.New<int, T>(equate, hash);
stepper(a =>
{
if (map.Contains(a))
{
int occurences = ++map[a];
maxOccurences = Math.Max(occurences, maxOccurences);
}
else
{
map[a] = 1;
maxOccurences = Math.Max(1, maxOccurences);
}
});
map.Pairs(x =>
{
if (x.Value == maxOccurences)
{
step(x.Key);
}
});
}
#endregion
#region Mean
/// <summary>Computes the mean of a set of numerical values.</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="a">The first value of the set of data to compute the mean of.</param>
/// <param name="b">The remaining values in the data set to compute the mean of.</param>
/// <returns>The computed mean of the set of data.</returns>
public static T Mean<T>(T a, params T[] b) =>
Mean<T>(step => { step(a); b.ToStepper()(step); });
/// <summary>Computes the mean of a set of numerical values.</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="stepper">The set of data to compute the mean of.</param>
/// <returns>The computed mean of the set of data.</returns>
public static T Mean<T>(Action<Action<T>> stepper)
{
if (stepper is null) throw new ArgumentNullException(nameof(stepper));
T i = Constant<T>.Zero;
T sum = Constant<T>.Zero;
stepper(step =>
{
i = Addition(i, Constant<T>.One);
sum = Addition(sum, step);
});
if (Equate(i, Constant<T>.Zero))
{
throw new ArgumentException("The argument is empty.", nameof(stepper));
}
return Division(sum, i);
}
#endregion
#region Median
/// <summary>Computes the median of a set of data.</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="compare">The comparison algorithm to sort the data by.</param>
/// <param name="values">The set of data to compute the median of.</param>
/// <returns>The computed median value of the set of data.</returns>
public static T Median<T>(Func<T, T, CompareResult> compare, params T[] values)
{
if (compare is null) throw new ArgumentNullException(nameof(compare));
if (values is null) throw new ArgumentNullException(nameof(values));
// standard algorithm (sort and grab middle value)
SortMerge(values, compare);
if (values.Length % 2 is 1) // odd... just grab middle value
{
return values[values.Length / 2];
}
else // even... must perform a mean of the middle two values
{
T leftMiddle = values[(values.Length / 2) - 1];
T rightMiddle = values[values.Length / 2];
return Division(Addition(leftMiddle, rightMiddle), Constant<T>.Two);
}
}
/// <summary>Computes the median of a set of data.</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="compare">The comparison algorithm to sort the data by.</param>
/// <param name="stepper">The set of data to compute the median of.</param>
/// <returns>The computed median value of the set of data.</returns>
public static T Median<T>(Func<T, T, CompareResult> compare, Action<Action<T>> stepper)
{
if (stepper is null) throw new ArgumentNullException(nameof(stepper));
return Median<T>(compare, stepper.ToArray());
}
/// <summary>Computes the median of a set of data.</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="values">The set of data to compute the median of.</param>
/// <returns>The computed median value of the set of data.</returns>
public static T Median<T>(params T[] values)
{
return Median(Compare, values);
}
/// <summary>Computes the median of a set of data.</summary>
/// <typeparam name="T">The numeric type of the operation.</typeparam>
/// <param name="stepper">The set of data to compute the median of.</param>
/// <returns>The computed median value of the set of data.</returns>
public static T Median<T>(Action<Action<T>> stepper)
{
if (stepper is null) throw new ArgumentNullException(nameof(stepper));
return Median(Compare, stepper.ToArray());
}
#region Possible Optimization (Still in Development)
// public static T Median<T>(Func<T, T, CompareResult> compare, Hash<T> hash, Func<T, T, bool> equate, params T[] values)
// {
// // this is an optimized median algorithm, but it only works on odd sets without duplicates
// if (hash is not null && equate is not null && values.Length % 2 is 1 && !values.ToStepper().ContainsDuplicates(equate, hash))
// {
// int medianIndex = 0;
// OddNoDupesMedianImplementation(values, values.Length, ref medianIndex, compare);
// return values[medianIndex];
// }
// else
// {
// return Median(compare, values);
// }
// }
// public static T Median<T>(Func<T, T, CompareResult> compare, Hash<T> hash, Func<T, T, bool> equate, Stepper<T> stepper)
// {
// return Median(compare, hash, equate, stepper.ToArray());
// }
///// <summary>Fast algorithm for median computation, but only works on data with an odd number of values without duplicates.</summary>
// internal static void OddNoDupesMedianImplementation<T>(T[] a, int n, ref int k, Func<T, T, CompareResult> compare)
// {
// int L = 0;
// int R = n - 1;
// k = n / 2;
// int i; int j;
// while (L < R)
// {
// T x = a[k];
// i = L; j = R;
// OddNoDupesMedianImplementation_Split(a, n, x, ref i, ref j, compare);
// if (j <= k) L = i;
// if (i >= k) R = j;
// }
// }
// internal static void OddNoDupesMedianImplementation_Split<T>(T[] a, int n, T x, ref int i, ref int j, Func<T, T, CompareResult> compare)
// {
// do
// {
// while (compare(a[i], x) is Less) i++;
// while (compare(a[j], x) is Greater) j--;
// T t = a[i];
// a[i] = a[j];
// a[j] = t;
// } while (i < j);
// }
#endregion
#endregion
#region GeometricMean
/// <summary>Computes the geometric mean of a set of numbers.</summary>
/// <typeparam name="T">The numeric type of the computation.</typeparam>
/// <param name="stepper">The set of numbres to compute the geometric mean of.</param>
/// <returns>The computed geometric mean of the set of numbers.</returns>
public static T GeometricMean<T>(Action<Action<T>> stepper)
{
T multiple = Constant<T>.One;
T count = Constant<T>.Zero;
stepper(i =>
{
count = Addition(count, Constant<T>.One);
multiple = Multiplication(multiple, i);
});
return Root(multiple, count);
}
#endregion
#region Variance
/// <summary>Computes the variance of a set of numbers.</summary>
/// <typeparam name="T">The numeric type of the computation.</typeparam>
/// <param name="stepper">The set of numbers to compute the variance of.</param>
/// <returns>The computed variance of the set of numbers.</returns>
public static T Variance<T>(Action<Action<T>> stepper)
{
T mean = Mean(stepper);
T variance = Constant<T>.Zero;
T count = Constant<T>.Zero;
stepper(i =>
{
T i_minus_mean = Subtraction(i, mean);
variance = Addition(variance, Multiplication(i_minus_mean, i_minus_mean));
count = Addition(count, Constant<T>.One);
});
return Division(variance, count);
}
#endregion
#region StandardDeviation
/// <summary>Computes the standard deviation of a set of numbers.</summary>
/// <typeparam name="T">The numeric type of the computation.</typeparam>
/// <param name="stepper">The set of numbers to compute the standard deviation of.</param>
/// <returns>The computed standard deviation of the set of numbers.</returns>
public static T StandardDeviation<T>(Action<Action<T>> stepper) =>
SquareRoot(Variance(stepper));
#endregion
#region MeanDeviation
/// <summary>The mean deviation of a set of numbers.</summary>
/// <typeparam name="T">The numeric type of the computation.</typeparam>
/// <param name="stepper">The set of numbers to compute the mean deviation of.</param>
/// <returns>The computed mean deviation of the set of numbers.</returns>
public static T MeanDeviation<T>(Action<Action<T>> stepper)
{
T mean = Mean(stepper);
T temp = Constant<T>.Zero;
T count = Constant<T>.Zero;
stepper(i =>
{
temp = Addition(temp, AbsoluteValue(Subtraction(i, mean)));
count = Addition(count, Constant<T>.One);
});
return Division(temp, count);
}
#endregion
#region Quantiles
/// <summary>Computes the quantiles of a set of data.</summary>
/// <typeparam name="T">The generic data type.</typeparam>
/// <param name="quantiles">The number of quantiles to compute.</param>
/// <param name="stepper">The data stepper.</param>
/// <returns>The computed quantiles of the data set.</returns>
public static T[] Quantiles<T>(int quantiles, Action<Action<T>> stepper)
{
if (quantiles < 1)
{
throw new ArgumentOutOfRangeException(nameof(quantiles), quantiles, "!(" + nameof(quantiles) + " >= 1)");
}
int count = stepper.Count();
T[] ordered = new T[count];
int a = 0;
stepper(i => { ordered[a++] = i; });
SortQuick<T>(ordered, Compare);
T[] resultingQuantiles = new T[quantiles + 1];
resultingQuantiles[0] = ordered[0];
resultingQuantiles[^1] = ordered[^1];
T QUANTILES_PLUS_1 = Convert<int, T>(quantiles + 1);
T ORDERED_LENGTH = Convert<int, T>(ordered.Length);
for (int i = 1; i < quantiles; i++)
{
T I = Convert<int, T>(i);
T temp = Division(ORDERED_LENGTH, Multiplication<T>(QUANTILES_PLUS_1, I));
if (IsInteger(temp))
{
resultingQuantiles[i] = ordered[Convert<T, int>(temp)];
}
else
{
resultingQuantiles[i] = Division(Addition(ordered[Convert<T, int>(temp)], ordered[Convert<T, int>(temp) + 1]), Constant<T>.Two);
}
}
return resultingQuantiles;
}
#endregion
#region Correlation
#if false
// /// <summary>Computes the median of a set of values.</summary>
// internal static Compute.Delegates.Correlation Correlation_internal = (Stepper<T> a, Stepper<T> b) =>
// {
// throw new System.NotImplementedException("I introduced an error here when I removed the stepref off of structure. will fix soon");
// Compute.Correlation_internal =
// Meta.Compile<Compute.Delegates.Correlation>(
// string.Concat(
// @"(Stepper<", Meta.ConvertTypeToCsharpSource(typeof(T)), "> _a, Stepper<", Meta.ConvertTypeToCsharpSource(typeof(T)), @"> _b) =>
//{
// ", Meta.ConvertTypeToCsharpSource(typeof(T)), " a_mean = Compute<", Meta.ConvertTypeToCsharpSource(typeof(T)), @">.Mean(_a);
// ", Meta.ConvertTypeToCsharpSource(typeof(T)), " b_mean = Compute<", Meta.ConvertTypeToCsharpSource(typeof(T)), @">.Mean(_b);
// List<", Meta.ConvertTypeToCsharpSource(typeof(T)), "> a_temp = new List_Linked<", Meta.ConvertTypeToCsharpSource(typeof(T)), @">();
// _a((", Meta.ConvertTypeToCsharpSource(typeof(T)), @" i) => { a_temp.Add(i - b_mean); });
// List<", Meta.ConvertTypeToCsharpSource(typeof(T)), "> b_temp = new List_Linked<", Meta.ConvertTypeToCsharpSource(typeof(T)), @">();
// _b((", Meta.ConvertTypeToCsharpSource(typeof(T)), @" i) => { b_temp.Add(i - a_mean); });
// ", Meta.ConvertTypeToCsharpSource(typeof(T)), "[] a_cross_b = new ", Meta.ConvertTypeToCsharpSource(typeof(T)), @"[a_temp.Count * b_temp.Count];
// int count = 0;
// a_temp.Stepper((", Meta.ConvertTypeToCsharpSource(typeof(T)), @" i_a) =>
// {
// b_temp.Stepper((", Meta.ConvertTypeToCsharpSource(typeof(T)), @" i_b) =>
// {
// a_cross_b[count++] = i_a * i_b;
// });
// });
// a_temp.Stepper((ref ", Meta.ConvertTypeToCsharpSource(typeof(T)), @" i) => { i *= i; });
// b_temp.Stepper((ref ", Meta.ConvertTypeToCsharpSource(typeof(T)), @" i) => { i *= i; });
// ", Meta.ConvertTypeToCsharpSource(typeof(T)), @" sum_a_cross_b = 0;
// foreach (", Meta.ConvertTypeToCsharpSource(typeof(T)), @" i in a_cross_b)
// sum_a_cross_b += i;
// ", Meta.ConvertTypeToCsharpSource(typeof(T)), @" sum_a_temp = 0;
// a_temp.Stepper((", Meta.ConvertTypeToCsharpSource(typeof(T)), @" i) => { sum_a_temp += i; });
// ", Meta.ConvertTypeToCsharpSource(typeof(T)), @" sum_b_temp = 0;
// b_temp.Stepper((", Meta.ConvertTypeToCsharpSource(typeof(T)), @" i) => { sum_b_temp += i; });
// return sum_a_cross_b / Compute<", Meta.ConvertTypeToCsharpSource(typeof(T)), @">.sqrt(sum_a_temp * sum_b_temp);
//}"));
// return Compute.Correlation_internal(a, b);
// };
// public static T Correlation(Stepper<T> a, Stepper<T> b)
// {
// return Correlation_internal(a, b);
// }
#endif
#endregion
#region Occurences
/// <summary>Counts the number of occurences of each item.</summary>
/// <typeparam name="T">The generic type to count the occerences of.</typeparam>
/// <param name="a">The first value in the data.</param>
/// <param name="b">The rest of the data.</param>
/// <returns>The occurence map of the data.</returns>
public static IMap<int, T> Occurences<T>(T a, params T[] b) =>
Occurences<T>(step => { step(a); b.ToStepper()(step); });
/// <summary>Counts the number of occurences of each item.</summary>
/// <typeparam name="T">The generic type to count the occerences of.</typeparam>
/// <param name="equate">The equality delegate.</param>
/// <param name="a">The first value in the data.</param>
/// <param name="b">The rest of the data.</param>
/// <returns>The occurence map of the data.</returns>
public static IMap<int, T> Occurences<T>(Func<T, T, bool> equate, T a, params T[] b) =>
Occurences<T>(step => { step(a); b.ToStepper()(step); }, equate, null);
/// <summary>Counts the number of occurences of each item.</summary>
/// <typeparam name="T">The generic type to count the occerences of.</typeparam>
/// <param name="hash">The hash code delegate.</param>
/// <param name="a">The first value in the data.</param>
/// <param name="b">The rest of the data.</param>
/// <returns>The occurence map of the data.</returns>
public static IMap<int, T> Occurences<T>(Func<T, int> hash, T a, params T[] b) =>
Occurences<T>(step => { step(a); b.ToStepper()(step); }, null, hash);
/// <summary>Counts the number of occurences of each item.</summary>
/// <typeparam name="T">The generic type to count the occerences of.</typeparam>