microsoft-visualbasic-runtime/ComponentModel/Ranges/RangeModel/IntRange.vb

212 lines
7.3 KiB
VB.net
Raw Normal View History

2019-11-10 15:16:27 +08:00
#Region "Microsoft.VisualBasic::a22ce0ce5b9821bc33f44903dc549653, Microsoft.VisualBasic.Core\ComponentModel\Ranges\RangeModel\IntRange.vb"
2018-08-02 20:14:48 +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:
' Class IntRange
'
' Properties: Length, Max, Min
'
' Constructor: (+3 Overloads) Sub New
' Function: GetEnumerator, IEnumerable_GetEnumerator, (+3 Overloads) IsInside, (+2 Overloads) IsOverlapping, ScaleMapping
' ToString
'
'
' /********************************************************************************/
#End Region
' AForge Library
'
' Copyright © Andrew Kirillov, 2006
' andrew.kirillov@gmail.com
'
Imports Microsoft.VisualBasic.Language
Namespace ComponentModel.Ranges.Model
''' <summary>
''' Represents an <see cref="Integer"/> range with minimum and maximum values
''' </summary>
2019-06-08 17:00:01 +08:00
Public Class IntRange
2020-03-08 08:11:08 +08:00
Implements IRangeModel(Of Integer)
2018-08-02 20:14:48 +08:00
Implements IEnumerable(Of Integer)
''' <summary>
''' Minimum value
''' </summary>
2020-03-08 08:11:08 +08:00
Public Property Min As Integer Implements IRangeModel(Of Integer).Min
2018-08-02 20:14:48 +08:00
''' <summary>
''' Maximum value
''' </summary>
2020-03-08 08:11:08 +08:00
Public Property Max As Integer Implements IRangeModel(Of Integer).Max
2018-08-02 20:14:48 +08:00
''' <summary>
''' Length of the range (deffirence between maximum and minimum values)
''' </summary>
2019-10-22 16:42:34 +08:00
Public ReadOnly Property Length As Integer
2018-08-02 20:14:48 +08:00
Get
Return Max - Min
End Get
End Property
''' <summary>
''' Initializes a new instance of the <see cref="IntRange"/> class
''' </summary>
'''
''' <param name="min">Minimum value of the range</param>
''' <param name="max">Maximum value of the range</param>
Public Sub New(min%, max%)
Me.Min = min
Me.Max = max
End Sub
''' <summary>
''' 这个构造函数之中会自动求出最大值和最小值
''' </summary>
''' <param name="source"></param>
Sub New(source As IEnumerable(Of Integer))
With source.ToArray
Min = .Min
Max = .Max
End With
End Sub
Sub New()
End Sub
''' <summary>
'''
''' </summary>
''' <returns></returns>
''' <remarks>
''' <see cref="DoubleRange.ToString()"/>
''' </remarks>
Public Overrides Function ToString() As String
Return $"[min={Min}, max={Max}]"
End Function
''' <summary>
''' Check if the specified value ``<paramref name="x"/>`` is inside this range
''' </summary>
''' <param name="x">Value to check</param>
''' <returns><b>True</b> if the specified value is inside this range or
''' <b>false</b> otherwise.</returns>
2020-03-08 08:11:08 +08:00
Public Function IsInside(x As Integer) As Boolean Implements IRangeModel(Of Integer).IsInside
2018-08-02 20:14:48 +08:00
Return ((x >= Min) AndAlso (x <= Max))
End Function
''' <summary>
''' Check if the specified range is inside this range
''' </summary>
'''
''' <param name="range">Range to check</param>
'''
''' <returns><b>True</b> if the specified range is inside this range or
''' <b>false</b> otherwise.</returns>
'''
Public Function IsInside(range As IntRange) As Boolean
Return ((IsInside(range.Min)) AndAlso (IsInside(range.Max)))
End Function
''' <summary>
''' Check if the specified range overlaps with this range
''' </summary>
'''
''' <param name="range">Range to check for overlapping</param>
'''
''' <returns><b>True</b> if the specified range overlaps with this range or
''' <b>false</b> otherwise.</returns>
'''
Public Function IsOverlapping(range As IntRange) As Boolean
Return ((IsInside(range.Min)) OrElse (IsInside(range.Max)))
End Function
2020-03-08 08:11:08 +08:00
Public Function IsInside(range As IRangeModel(Of Integer)) As Boolean Implements IRangeModel(Of Integer).IsInside
2018-08-02 20:14:48 +08:00
Return ((IsInside(range.Min)) AndAlso (IsInside(range.Max)))
End Function
2020-03-08 08:11:08 +08:00
Public Function IsOverlapping(range As IRangeModel(Of Integer)) As Boolean Implements IRangeModel(Of Integer).IsOverlapping
2018-08-02 20:14:48 +08:00
Return ((IsInside(range.Min)) OrElse (IsInside(range.Max)))
End Function
''' <summary>
''' Transform a numeric value in this <see cref="IntRange"/> into
''' target numeric range: ``<paramref name="valueRange"/>``.
''' (将当前的范围内的一个实数映射到另外的一个范围内的实数区间之中)
''' </summary>
''' <param name="x#"></param>
''' <param name="valueRange"></param>
''' <returns></returns>
Public Function ScaleMapping(x%, valueRange As IntRange) As Double
Dim percent# = (x - Min) / Length
Dim value# = percent * valueRange.Length + valueRange.Min
Return value
End Function
''' <summary>
''' 枚举出这个数值范围内的所有整数值步长为1
''' </summary>
''' <returns></returns>
Public Iterator Function GetEnumerator() As IEnumerator(Of Integer) Implements IEnumerable(Of Integer).GetEnumerator
For i As Integer = Min To Max
Yield i
Next
End Function
Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Yield GetEnumerator()
End Function
Public Shared Widening Operator CType(exp$) As IntRange
Dim r As New IntRange
Call exp.Parser(r.Min, r.Max)
Return r
End Operator
' 2017-10-6
' 因为下面的这个隐式转换操作符会和Vector的Item属性产生冲突所以在这里将这个操作符移除掉
'Public Shared Widening Operator CType(values%()) As IntRange
' With values
' Return New IntRange(.Min, .Max)
' End With
'End Operator
2018-10-20 20:29:57 +08:00
Public Shared Widening Operator CType(list As List(Of Integer)) As IntRange
With list
Return New IntRange(.Min, .Max)
End With
End Operator
2018-08-02 20:14:48 +08:00
End Class
End Namespace