1 | #Region "Microsoft.VisualBasic::e7acb2cc6f8ebc2d0e8963795c6463b0, Microsoft.VisualBasic.Core\Language\Value\Numeric\Numeric.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 Numeric |
35 | ' |
36 | ' Function: Equals, GreaterThan, GreaterThanOrEquals, LessThan, LessThanOrEquals |
37 | ' MaxIndex, MinIndex, NextInteger, (+2 Overloads) Reverse, ToUInt32 |
38 | ' ToUInt64 |
39 | ' |
40 | ' Class Precise |
41 | ' |
42 | ' |
43 | ' |
44 | ' |
45 | ' /********************************************************************************/ |
46 | |
47 | #End Region |
48 | |
49 | Imports System.Runtime.CompilerServices |
50 | |
51 | Namespace Language |
52 | |
53 | ''' <summary> |
54 | ''' Defines a generalized type-specific comparison method that a value type or class |
55 | ''' implements to order or sort its instances. |
56 | ''' </summary> |
57 | ''' <remarks> |
58 | ''' |
59 | ''' Summary: |
60 | ''' |
61 | ''' Compares the current instance with another object of the same type and returns |
62 | ''' an integer that indicates whether the current instance precedes, follows, or |
63 | ''' occurs in the same position in the sort order as the other object. |
64 | ''' |
65 | ''' Returns: |
66 | ''' |
67 | ''' A value that indicates the relative order of the objects being compared. The |
68 | ''' return value has these meanings: |
69 | ''' |
70 | ''' 1. Value Meaning Less than zero |
71 | ''' This instance precedes obj in the sort order. |
72 | ''' |
73 | ''' 2. Zero |
74 | ''' This instance occurs in the same position in the sort order as obj. |
75 | ''' |
76 | ''' 3. Greater than zero |
77 | ''' This instance follows obj in the sort order. |
78 | ''' |
79 | ''' Exceptions: |
80 | ''' |
81 | ''' T:System.ArgumentException: |
82 | ''' obj is not the same type as this instance. |
83 | ''' </remarks> |
84 | Public Module Numeric |
85 | |
86 | #Region "VB type char helper" |
87 | Public Const yes! = 1 |
88 | Public Const no! = 0 |
89 | #End Region |
90 | |
91 | ''' <summary> |
92 | ''' *<see cref="Numeric.MaxIndex"/>* The max element its index in the source collection. |
93 | ''' </summary> |
94 | ''' <typeparam name="T"></typeparam> |
95 | ''' <param name="source"></param> |
96 | ''' <returns></returns> |
97 | <Extension> |
98 | Public Function MaxIndex(Of T As IComparable)(source As IEnumerable(Of T)) As Integer |
99 | Dim i As Integer |
100 | Dim max As T = source.First |
101 | Dim maxInd As Integer = 0 |
102 | |
103 | For Each x As T In source.Skip(1) |
104 | i += 1 |
105 | |
106 | If x.CompareTo(max) > 0 Then |
107 | max = x |
108 | maxInd = i |
109 | End If |
110 | Next |
111 | |
112 | Return maxInd |
113 | End Function |
114 | |
115 | <Extension> |
116 | Public Function MinIndex(Of T As IComparable)(source As IEnumerable(Of T)) As Integer |
117 | Dim i As Integer |
118 | Dim min As T = source.First |
119 | Dim minInd As Integer = 0 |
120 | |
121 | For Each x As T In source.Skip(1) |
122 | i += 1 |
123 | |
124 | If x.CompareTo(min) < 0 Then |
125 | min = x |
126 | minInd = i |
127 | End If |
128 | Next |
129 | |
130 | Return minInd |
131 | End Function |
132 | |
133 | ''' <summary> |
134 | ''' = |
135 | ''' </summary> |
136 | ''' <typeparam name="T"></typeparam> |
137 | ''' <param name="a"></param> |
138 | ''' <param name="b"></param> |
139 | ''' <returns></returns> |
140 | Public Function Equals(Of T As IComparable)(a As T, b As T) As Boolean |
141 | Return a.CompareTo(b) = 0 |
142 | End Function |
143 | |
144 | ''' <summary> |
145 | ''' < |
146 | ''' </summary> |
147 | ''' <typeparam name="T"></typeparam> |
148 | ''' <param name="a"></param> |
149 | ''' <param name="b"></param> |
150 | ''' <returns></returns> |
151 | <Extension> Public Function LessThan(Of T As IComparable)(a As T, b As T) As Boolean |
152 | Return a.CompareTo(b) < 0 |
153 | End Function |
154 | |
155 | ''' <summary> |
156 | ''' a > b |
157 | ''' </summary> |
158 | ''' <typeparam name="T"></typeparam> |
159 | ''' <param name="a"></param> |
160 | ''' <param name="b"></param> |
161 | ''' <returns></returns> |
162 | <Extension> Public Function GreaterThan(Of T As IComparable)(a As T, b As T) As Boolean |
163 | Return a.CompareTo(b) > 0 |
164 | End Function |
165 | |
166 | ''' <summary> |
167 | ''' ``<paramref name="a"/> <= <paramref name="b"/>`` |
168 | ''' </summary> |
169 | ''' <typeparam name="T"></typeparam> |
170 | ''' <param name="a"></param> |
171 | ''' <param name="b"></param> |
172 | ''' <returns></returns> |
173 | <Extension> Public Function LessThanOrEquals(Of T As IComparable)(a As T, b As T) As Boolean |
174 | Return a.LessThan(b) OrElse Equals(a, b) |
175 | End Function |
176 | |
177 | ''' <summary> |
178 | ''' => |
179 | ''' </summary> |
180 | ''' <typeparam name="T"></typeparam> |
181 | ''' <param name="a"></param> |
182 | ''' <param name="b"></param> |
183 | ''' <returns></returns> |
184 | <Extension> Public Function GreaterThanOrEquals(Of T As IComparable)(a As T, b As T) As Boolean |
185 | Return a.GreaterThan(b) OrElse Equals(a, b) |
186 | End Function |
187 | |
188 | ''' <summary> |
189 | ''' <see cref="Random"/> get next integer in the specific range <paramref name="max"/> |
190 | ''' </summary> |
191 | ''' <param name="rnd"></param> |
192 | ''' <param name="max"></param> |
193 | ''' <returns></returns> |
194 | <Extension> Public Function NextInteger(rnd As Random, max As Integer) As int |
195 | Return New int(rnd.Next(max)) |
196 | End Function |
197 | |
198 | Public Function ToUInt32(value As Single) As UInteger |
199 | Dim bytes As Byte() = BitConverter.GetBytes(value) |
200 | Return BitConverter.ToUInt32(bytes, 0) |
201 | End Function |
202 | |
203 | Public Function ToUInt64(value As Double) As ULong |
204 | Dim bytes As Byte() = BitConverter.GetBytes(value) |
205 | Return BitConverter.ToUInt64(bytes, 0) |
206 | End Function |
207 | |
208 | Public Function Reverse(value As UInteger) As UInteger |
209 | Dim reversed As UInteger = (value And &HFF) << 24 Or (value And &HFF00) << 8 Or (value And &HFF0000) >> 8 Or (value And &HFF000000UI) >> 24 |
210 | Return reversed |
211 | End Function |
212 | |
213 | Public Function Reverse(value As Integer) As Integer |
214 | Dim uvalue As UInteger = CUInt(value) |
215 | Dim reversed As UInteger = Reverse(uvalue) |
216 | Return CInt(reversed) |
217 | End Function |
218 | End Module |
219 | |
220 | Public Class Precise : Inherits Value(Of Decimal) |
221 | |
222 | End Class |
223 | End Namespace |