1 #Region "Microsoft.VisualBasic::6fcc748629a24d0ca0195968e3ab945f, Microsoft.VisualBasic.Core\Extensions\Math\StatisticsMathExtensions\Linq\EnumerableStatsMode.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module EnumerableStatsMode
35     
36     '         Function: (+4 OverloadsMode, (+2 OverloadsModes
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