microsoft-visualbasic-runtime/Language/Value/Numeric/i32.vb

311 lines
11 KiB
VB.net
Raw Normal View History

2020-04-04 21:15:06 +08:00
#Region "Microsoft.VisualBasic::2379efaff63a9544f94cebaa2e1d3c8b, Microsoft.VisualBasic.Core\Language\Value\Numeric\i32.vb"
2019-01-18 22:48:45 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
' /********************************************************************************/
' Summaries:
2019-09-06 19:20:43 +08:00
' Class i32
2019-01-18 22:48:45 +08:00
'
' Properties: Hex, Oct
'
' Constructor: (+2 Overloads) Sub New
' Function: (+2 Overloads) CompareTo, Equals, (+2 Overloads) ToString
2019-05-06 18:27:46 +08:00
' Operators: (+3 Overloads) -, (+2 Overloads) /, (+2 Overloads) +, (+3 Overloads) <, <<
' <=, (+3 Overloads) >, >=, (+2 Overloads) IsFalse, (+2 Overloads) IsTrue
2019-01-18 22:48:45 +08:00
'
'
' /********************************************************************************/
2018-08-02 20:14:48 +08:00
#End Region
Imports System.Runtime.CompilerServices
Namespace Language
''' <summary>
''' Alias of <see cref="Int32"/>
''' </summary>
2019-09-04 20:38:12 +08:00
Public Class i32 : Inherits Value(Of Integer)
2018-08-02 20:14:48 +08:00
Implements IComparable
Implements IComparable(Of Integer)
Implements IEquatable(Of Integer)
Implements IFormattable
2019-01-18 22:40:02 +08:00
''' <summary>
''' 转换为16进制
''' </summary>
''' <returns></returns>
Public ReadOnly Property Hex As String
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Get
Return Convert.ToString(Value, 16).ToUpper
End Get
End Property
''' <summary>
''' 转换为8进制
''' </summary>
''' <returns></returns>
Public ReadOnly Property Oct As String
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Get
Return Convert.ToString(Value, 8)
End Get
End Property
2018-08-02 20:14:48 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Sub New(Optional x% = Scan0)
Value = x
End Sub
Sub New()
Call Me.New(0)
End Sub
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Overrides Function ToString() As String
Return Value
End Function
''' <summary>
2019-09-04 20:38:12 +08:00
''' Compare <see cref="i32"/> or <see cref="Int32"/>
2018-08-02 20:14:48 +08:00
''' </summary>
''' <param name="obj"></param>
''' <returns></returns>
Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
Dim type As Type = obj.GetType
If type.Equals(GetType(Integer)) Then
Return Value.CompareTo(DirectCast(obj, Integer))
2019-09-04 20:38:12 +08:00
ElseIf type.Equals(GetType(i32)) Then
Return Value.CompareTo(DirectCast(obj, i32).Value)
2018-08-02 20:14:48 +08:00
Else
2019-09-04 20:38:12 +08:00
Throw New Exception($"Miss-match of type: {GetType(i32).FullName} -> {type.FullName}")
2018-08-02 20:14:48 +08:00
End If
End Function
2018-12-31 20:40:33 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Shared Operator IsTrue(n As i32) As Boolean
2018-12-31 20:40:33 +08:00
Return Not n Is Nothing AndAlso n.Value <> 0
End Operator
2019-09-04 20:38:12 +08:00
Public Shared Operator IsFalse(n As i32) As Boolean
2018-12-31 20:40:33 +08:00
If n Then
Return False
Else
Return True
End If
End Operator
2018-08-02 20:14:48 +08:00
''' <summary>
''' n &lt; value &lt;= n2
''' 假若n 大于value则返回最大值上面的表达式肯定不成立
''' </summary>
''' <param name="n"></param>
''' <param name="x"></param>
''' <returns></returns>
2019-09-04 20:38:12 +08:00
Public Shared Operator <(n As Integer, x As i32) As i32
2018-08-02 20:14:48 +08:00
If n >= x.Value Then
2019-09-04 20:38:12 +08:00
Return New i32(Integer.MaxValue)
2018-08-02 20:14:48 +08:00
Else
Return x
End If
End Operator
''' <summary>
''' ``x.value &lt; n``
''' </summary>
''' <param name="x"></param>
''' <param name="n"></param>
''' <returns></returns>
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Shared Operator <(x As i32, n As Integer) As Boolean
2018-08-02 20:14:48 +08:00
Return x.Value < n
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Shared Operator <(n As Double, x As i32) As Boolean
2018-08-02 20:14:48 +08:00
Return n < x.Value
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Shared Operator >(n As Double, x As i32) As Boolean
2018-08-02 20:14:48 +08:00
Return n > x.Value
End Operator
''' <summary>
''' ``x.value > n``
''' </summary>
''' <param name="x"></param>
''' <param name="n"></param>
''' <returns></returns>
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Shared Operator >(x As i32, n As Integer) As Boolean
2018-08-02 20:14:48 +08:00
Return x.Value > n
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Overloads Shared Operator <=(x As i32, n As Integer) As Boolean
2018-08-02 20:14:48 +08:00
Return x.Value <= n
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Overloads Shared Operator >=(x As i32, n As Integer) As Boolean
2018-08-02 20:14:48 +08:00
Return x.Value >= n
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Shared Operator >(n As Integer, x As i32) As i32
2018-08-02 20:14:48 +08:00
Return x
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Overloads Shared Operator -(x As i32, n As Integer) As i32
2018-08-02 20:14:48 +08:00
x.Value -= n
Return x
End Operator
''' <summary>
''' 正常的减法四则运算
''' </summary>
''' <param name="x%"></param>
''' <param name="n"></param>
''' <returns></returns>
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Overloads Shared Operator -(x%, n As i32) As Integer
2018-08-02 20:14:48 +08:00
Return x - n.Value
End Operator
2020-03-29 19:33:45 +08:00
''' <summary>
''' value / b
''' </summary>
''' <param name="x"></param>
''' <param name="b"></param>
''' <returns></returns>
2018-08-02 20:14:48 +08:00
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Shared Operator /(x As i32, b As Integer) As Double
2018-08-02 20:14:48 +08:00
Return x.Value / b
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Shared Operator /(x As Integer, b As i32) As Double
2018-08-02 20:14:48 +08:00
Return x / b.Value
End Operator
''' <summary>
''' 必须要overloads这个方法否则会出现无法将Value(Of Integer)转换为int的错误
''' </summary>
''' <param name="n"></param>
''' <returns></returns>
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Overloads Shared Widening Operator CType(n As Integer) As i32
Return New i32(n)
2018-08-02 20:14:48 +08:00
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Overloads Shared Narrowing Operator CType(n As i32) As Double
2018-08-02 20:14:48 +08:00
Return CDbl(n.Value)
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2019-09-04 20:38:12 +08:00
Public Overloads Shared Narrowing Operator CType(n As i32) As Integer
2018-08-02 20:14:48 +08:00
Return n.Value
End Operator
2018-12-31 20:40:33 +08:00
''' <summary>
'''
''' </summary>
''' <param name="x"></param>
''' <returns></returns>
2019-09-04 20:38:12 +08:00
Public Shared Operator -(x As i32) As Integer
2018-12-31 20:40:33 +08:00
Dim i As Integer = x.Value
x.Value -= 1
Return -i
End Operator
2018-08-02 20:14:48 +08:00
''' <summary>
''' Auto increment value with step 1 and then returns the previous value.
''' (自增1然后返回之前的值)
''' </summary>
''' <param name="x"></param>
''' <returns></returns>
2019-09-04 20:38:12 +08:00
Public Overloads Shared Operator +(x As i32) As Integer
2018-08-02 20:14:48 +08:00
Dim i As Integer = x.Value
x.Value += 1
Return i
End Operator
''' <summary>
''' 位移<paramref name="n"/>个单位然后返回位移之后的结果值
'''
2019-09-04 20:38:12 +08:00
''' + 对于<see cref="i32"/>类型而言,其更加侧重于迭代器中的位移,所以这个加法运算是符合
2019-01-18 22:40:02 +08:00
''' ```vbnet
''' x += n
''' ```
2019-09-04 20:38:12 +08:00
''' + 但是对于<see cref="f64"/>类型而言,其更加侧重于模型计算,所以其加法不符合上述的语法,
2018-08-02 20:14:48 +08:00
''' 不会修改源变量的值,返回的是一个单纯的<see cref="Double"/>值类型
''' </summary>
''' <param name="x"></param>
''' <param name="n%"></param>
''' <returns></returns>
2019-09-04 20:38:12 +08:00
Public Overloads Shared Operator +(x As i32, n%) As i32
2018-08-02 20:14:48 +08:00
x.Value += n
Return x
End Operator
''' <summary>
''' p的值增加x然后返回之前的值
''' </summary>
''' <param name="p"></param>
''' <param name="x"></param>
''' <returns></returns>
2019-09-04 20:38:12 +08:00
Public Shared Operator <<(p As i32, x%) As Integer
2018-08-02 20:14:48 +08:00
Dim i As Integer = p.Value
p.Value += x
Return i
End Operator
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Function CompareTo(other As Integer) As Integer Implements IComparable(Of Integer).CompareTo
Return Value.CompareTo(other)
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Overloads Function Equals(other As Integer) As Boolean Implements IEquatable(Of Integer).Equals
Return Value.Equals(other)
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Public Overloads Function ToString(format As String, formatProvider As IFormatProvider) As String Implements IFormattable.ToString
Return Value.ToString(format, formatProvider)
End Function
End Class
End Namespace