1 #Region "Microsoft.VisualBasic::05e59390398e54608cf7ea8de00ebadf, Microsoft.VisualBasic.Core\Language\Value\Numeric\int.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     '     Class int
35     
36     '         Constructor: (+2 OverloadsSub New
37     '         Function: (+2 Overloads) CompareTo, Equals, (+2 OverloadsToString
38     '         Operators: (+2 Overloads) -, (+2 Overloads) /, (+2 Overloads) +, (+4 Overloads) <, <<
39     '                    <=, (+4 Overloads) >, >=
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports System.Runtime.CompilerServices
47 Imports Microsoft.VisualBasic.ApplicationServices
48 Imports Microsoft.VisualBasic.Language.UnixBash.FileSystem
49
50 Namespace Language
51
52     ''' <summary>
53     ''' Alias of <see cref="Int32"/>
54     ''' </summary>
55     Public Class int : Inherits Value(Of Integer)
56         Implements IComparable
57         Implements IComparable(Of Integer)
58         Implements IEquatable(Of Integer)
59         Implements IFormattable
60
61         <MethodImpl(MethodImplOptions.AggressiveInlining)>
62         Sub New(Optional x% = Scan0)
63             Value = x
64         End Sub
65
66         Sub New()
67             Call Me.New(0)
68         End Sub
69
70         <MethodImpl(MethodImplOptions.AggressiveInlining)>
71         Public Overrides Function ToString() As String
72             Return Value
73         End Function
74
75         ''' <summary>
76         ''' Compare <see cref="Int"/> or <see cref="Int32"/>
77         ''' </summary>
78         ''' <param name="obj"></param>
79         ''' <returns></returns>
80         Public Function CompareTo(obj As ObjectAs Integer Implements IComparable.CompareTo
81             Dim type As Type = obj.GetType
82
83             If type.Equals(GetType(Integer)) Then
84                 Return Value.CompareTo(DirectCast(obj, Integer))
85             ElseIf type.Equals(GetType(int)) Then
86                 Return Value.CompareTo(DirectCast(obj, int).Value)
87             Else
88                 Throw New Exception($"Miss-match of type:  {GetType(int).FullName} --> {type.FullName}")
89             End If
90         End Function
91
92         ''' <summary>
93         ''' n &lt; value &lt;= n2
94         ''' 假若n 大于value,则返回最大值,上面的表达式肯定不成立
95         ''' </summary>
96         ''' <param name="n"></param>
97         ''' <param name="x"></param>
98         ''' <returns></returns>
99         Public Shared Operator <(n As Integer, x As int) As int
100             If n >= x.Value Then
101                 Return New int(Integer.MaxValue)
102             Else
103                 Return x
104             End If
105         End Operator
106
107         ''' <summary>
108         ''' ``x.value &lt; n``
109         ''' </summary>
110         ''' <param name="x"></param>
111         ''' <param name="n"></param>
112         ''' <returns></returns>
113         <MethodImpl(MethodImplOptions.AggressiveInlining)>
114         Public Shared Operator <(x As int, n As IntegerAs Boolean
115             Return x.Value < n
116         End Operator
117
118         <MethodImpl(MethodImplOptions.AggressiveInlining)>
119         Public Shared Operator <(n As Double, x As int) As Boolean
120             Return n < x.Value
121         End Operator
122
123         <MethodImpl(MethodImplOptions.AggressiveInlining)>
124         Public Shared Operator >(n As Double, x As int) As Boolean
125             Return n > x.Value
126         End Operator
127
128         ''' <summary>
129         ''' ``x.value > n``
130         ''' </summary>
131         ''' <param name="x"></param>
132         ''' <param name="n"></param>
133         ''' <returns></returns>
134         <MethodImpl(MethodImplOptions.AggressiveInlining)>
135         Public Shared Operator >(x As int, n As IntegerAs Boolean
136             Return x.Value > n
137         End Operator
138
139         <MethodImpl(MethodImplOptions.AggressiveInlining)>
140         Public Overloads Shared Operator <=(x As int, n As IntegerAs Boolean
141             Return x.Value <= n
142         End Operator
143
144         <MethodImpl(MethodImplOptions.AggressiveInlining)>
145         Public Overloads Shared Operator >=(x As int, n As IntegerAs Boolean
146             Return x.Value >= n
147         End Operator
148
149         <MethodImpl(MethodImplOptions.AggressiveInlining)>
150         Public Shared Operator >(n As Integer, x As int) As int
151             Return x
152         End Operator
153
154         <MethodImpl(MethodImplOptions.AggressiveInlining)>
155         Public Overloads Shared Operator -(x As int, n As IntegerAs int
156             x.Value -= n
157             Return x
158         End Operator
159
160         ''' <summary>
161         ''' 正常的减法四则运算
162         ''' </summary>
163         ''' <param name="x%"></param>
164         ''' <param name="n"></param>
165         ''' <returns></returns>
166         <MethodImpl(MethodImplOptions.AggressiveInlining)>
167         Public Overloads Shared Operator -(x%, n As int) As Integer
168             Return x - n.Value
169         End Operator
170
171         <MethodImpl(MethodImplOptions.AggressiveInlining)>
172         Public Shared Operator /(x As int, b As IntegerAs Double
173             Return x.Value / b
174         End Operator
175
176         <MethodImpl(MethodImplOptions.AggressiveInlining)>
177         Public Shared Operator /(x As Integer, b As int) As Double
178             Return x / b.Value
179         End Operator
180
181         ''' <summary>
182         ''' 必须要overloads这个方法,否则会出现无法将Value(Of Integer)转换为int的错误
183         ''' </summary>
184         ''' <param name="n"></param>
185         ''' <returns></returns>
186         <MethodImpl(MethodImplOptions.AggressiveInlining)>
187         Public Overloads Shared Widening Operator CType(n As IntegerAs int
188             Return New int(n)
189         End Operator
190
191         <MethodImpl(MethodImplOptions.AggressiveInlining)>
192         Public Overloads Shared Narrowing Operator CType(n As int) As Double
193             Return CDbl(n.Value)
194         End Operator
195
196         <MethodImpl(MethodImplOptions.AggressiveInlining)>
197         Public Overloads Shared Narrowing Operator CType(n As int) As Integer
198             Return n.Value
199         End Operator
200
201         ''' <summary>
202         ''' Auto increment value with step 1 and then returns the previous value.
203         ''' (自增1然后返回之前的值)
204         ''' </summary>
205         ''' <param name="x"></param>
206         ''' <returns></returns>
207         Public Overloads Shared Operator +(x As int) As Integer
208             Dim i As Integer = x.Value
209             x.Value += 1
210             Return i
211         End Operator
212
213         ''' <summary>
214         ''' 位移<paramref name="n"/>个单位然后返回位移之后的结果值
215         ''' 
216         ''' 对于<see cref="int"/>类型而言,其更加侧重于迭代器中的位移,所以这个加法运算是符合
217         ''' ```vbnet
218         ''' x += n
219         ''' ```
220         ''' 
221         ''' 但是对于<see cref="float"/>类型而言,其更加侧重于模型计算,所以其加法不符合上述的语法,
222         ''' 不会修改源变量的值,返回的是一个单纯的<see cref="Double"/>值类型
223         ''' </summary>
224         ''' <param name="x"></param>
225         ''' <param name="n%"></param>
226         ''' <returns></returns>
227         Public Overloads Shared Operator +(x As int, n%) As int
228             x.Value += n
229             Return x
230         End Operator
231
232         'Public Overloads Shared Operator =(x As int, a As IntegerAs int
233
234         'End Operator
235
236         Public Shared Operator >(source As IEnumerable, handle As int) As Boolean
237             Dim file As FileHandle = FileHandles.__getHandle(handle.Value)
238             Return IOHandler.DefaultHandle()(source, file.FileName, file.encoding)
239         End Operator
240
241         Public Shared Operator <(source As IEnumerable, handle As int) As Boolean
242             Throw New NotSupportedException
243         End Operator
244
245         ''' <summary>
246         ''' p的值增加x,然后返回之前的值
247         ''' </summary>
248         ''' <param name="p"></param>
249         ''' <param name="x"></param>
250         ''' <returns></returns>
251         Public Shared Operator <<(p As int, x%) As Integer
252             Dim i As Integer = p.Value
253             p.Value += x
254             Return i
255         End Operator
256
257         <MethodImpl(MethodImplOptions.AggressiveInlining)>
258         Public Function CompareTo(other As IntegerAs Integer Implements IComparable(Of Integer).CompareTo
259             Return Value.CompareTo(other)
260         End Function
261
262         <MethodImpl(MethodImplOptions.AggressiveInlining)>
263         Public Overloads Function Equals(other As IntegerAs Boolean Implements IEquatable(Of Integer).Equals
264             Return Value.Equals(other)
265         End Function
266
267         <MethodImpl(MethodImplOptions.AggressiveInlining)>
268         Public Overloads Function ToString(format As String, formatProvider As IFormatProvider) As String Implements IFormattable.ToString
269             Return Value.ToString(format, formatProvider)
270         End Function
271     End Class
272 End Namespace