1 |
#Region "Microsoft.VisualBasic::6fcc748629a24d0ca0195968e3ab945f, Microsoft.VisualBasic.Core\Extensions\Math\StatisticsMathExtensions\Linq\EnumerableStatsMode.vb"
|
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 |
#End Region
|
42 |
|
43 |
Imports System.Collections.Generic
|
44 |
Imports System.Linq
|
45 |
Imports System.Runtime.CompilerServices
|
46 |
|
47 |
Namespace Math.Statistics.Linq
|
48 |
|
49 |
Public Module EnumerableStatsMode
|
50 |
|
51 |
<Extension>
|
52 |
Public Function Modes(Of T As Structure)(source As IEnumerable(Of T?)) As IEnumerable(Of T)
|
53 |
Dim values As IEnumerable(Of T) = source.Coalesce()
|
54 |
If values.Any() Then
|
55 |
Return values.Modes
|
56 |
End If
|
57 |
|
58 |
Return Enumerable.Empty(Of T)()
|
59 |
End Function
|
60 |
|
61 |
<Extension>
|
62 |
Public Function Modes(Of T As Structure)(source As IEnumerable(Of T)) As IEnumerable(Of T)
|
63 |
Dim modes__1 As New List(Of T)()
|
64 |
|
65 |
Dim current As IEnumerable(Of T) = source
|
66 |
Dim mode As T? = current.Mode
|
67 |
While mode.HasValue AndAlso current.Count() > 1
|
68 |
modes__1.Add(CType(mode, T))
|
69 |
current = current.Where(Function(x) x.Equals(mode) = False).ToArray()
|
70 |
mode = current.Mode
|
71 |
End While
|
72 |
Return modes__1
|
73 |
End Function
|
74 |
|
75 |
<Extension>
|
76 |
Public Function Mode(Of T As Structure)(source As IEnumerable(Of T?)) As T
|
77 |
Dim values As IEnumerable(Of T) = source.Coalesce()
|
78 |
If values.Any() Then
|
79 |
Return values.Mode
|
80 |
End If
|
81 |
|
82 |
Return Nothing
|
83 |
End Function
|
84 |
|
85 |
<Extension>
|
86 |
Public Function Mode(Of T As Structure)(source As IEnumerable(Of T)) As T
|
87 |
Dim sortedList = From number In source Order By number Select number
|
88 |
|
89 |
Dim count As Integer = 0
|
90 |
Dim max As Integer = 0
|
91 |
Dim current As T = Nothing
|
92 |
Dim mode__1 As New T?()
|
93 |
|
94 |
For Each [next] As T In sortedList
|
95 |
If current.Equals([next]) = False Then
|
96 |
current = [next]
|
97 |
count = 1
|
98 |
Else
|
99 |
count += 1
|
100 |
End If
|
101 |
|
102 |
If count > max Then
|
103 |
max = count
|
104 |
mode__1 = current
|
105 |
End If
|
106 |
Next
|
107 |
|
108 |
If max > 1 Then
|
109 |
Return mode__1
|
110 |
End If
|
111 |
|
112 |
Return Nothing
|
113 |
End Function
|
114 |
|
115 |
<Extension>
|
116 |
Public Function Mode(Of TSource, TMode As Structure)(source As IEnumerable(Of TSource), selector As Func(Of TSource, TMode)) As TMode
|
117 |
Return source.[Select](selector).Mode
|
118 |
End Function
|
119 |
|
120 |
<Extension>
|
121 |
Public Function Mode(Of TSource, TMode As Structure)(source As IEnumerable(Of TSource), selector As Func(Of TSource, TMode?)) As TMode
|
122 |
Return source.[Select](selector).Mode
|
123 |
End Function
|
124 |
End Module
|
125 |
End Namespace
|