1 #Region "Microsoft.VisualBasic::026c1c3ecc6669ac3a318139c538402b, Microsoft.VisualBasic.Core\ComponentModel\Ranges\RangeModel\IntRange.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 IntRange
35     
36     '         Properties: Length, Max, Min
37     
38     '         Constructor: (+3 OverloadsSub New
39     '         FunctionGetEnumerator, IEnumerable_GetEnumerator, (+3 OverloadsIsInside, (+2 OverloadsIsOverlapping, ScaleMapping
40     '                   ToString
41     
42     
43     ' /********************************************************************************/
44
45 #End Region
46
47 ' AForge Library
48 ' ' Copyright © Andrew Kirillov, 2006
49 ' andrew.kirillov@gmail.com
50 '
51
52 Imports Microsoft.VisualBasic.Language
53
54 Namespace ComponentModel.Ranges.Model
55
56     ''' <summary>
57     ''' Represents an <see cref="Integer"/> range with minimum and maximum values
58     ''' </summary>
59     Public Class IntRange : Inherits BaseClass
60         Implements IRanges(Of Integer)
61         Implements IEnumerable(Of Integer)
62
63         ''' <summary>
64         ''' Minimum value
65         ''' </summary>
66         Public Property Min() As Integer Implements IRanges(Of Integer).Min
67
68         ''' <summary>
69         ''' Maximum value
70         ''' </summary>
71         Public Property Max() As Integer Implements IRanges(Of Integer).Max
72
73         ''' <summary>
74         ''' Length of the range (deffirence between maximum and minimum values)
75         ''' </summary>
76         Public ReadOnly Property Length() As Integer
77             Get
78                 Return Max - Min
79             End Get
80         End Property
81
82         ''' <summary>
83         ''' Initializes a new instance of the <see cref="IntRange"/> class
84         ''' </summary>
85         '''
86         ''' <param name="min">Minimum value of the range</param>
87         ''' <param name="max">Maximum value of the range</param>
88         Public Sub New(min%, max%)
89             Me.Min = min
90             Me.Max = max
91         End Sub
92
93         ''' <summary>
94         ''' 这个构造函数之中会自动求出最大值和最小值
95         ''' </summary>
96         ''' <param name="source"></param>
97         Sub New(source As IEnumerable(Of Integer))
98             With source.ToArray
99                 Min = .Min
100                 Max = .Max
101             End With
102         End Sub
103
104         Sub New()
105         End Sub
106
107         ''' <summary>
108         ''' 
109         ''' </summary>
110         ''' <returns></returns>
111         ''' <remarks>
112         ''' <see cref="DoubleRange.ToString()"/>
113         ''' </remarks>
114         Public Overrides Function ToString() As String
115             Return $"[min={Min}, max={Max}]"
116         End Function
117
118         ''' <summary>
119         ''' Check if the specified value ``<paramref name="x"/>`` is inside this range
120         ''' </summary>
121         ''' <param name="x">Value to check</param>
122         ''' <returns><b>True</b> if the specified value is inside this range or
123         ''' <b>false</b> otherwise.</returns>
124         Public Function IsInside(x As IntegerAs Boolean Implements IRanges(Of Integer).IsInside
125             Return ((x >= Min) AndAlso (x <= Max))
126         End Function
127
128         ''' <summary>
129         ''' Check if the specified range is inside this range
130         ''' </summary>
131         '''
132         ''' <param name="range">Range to check</param>
133         '''
134         ''' <returns><b>True</b> if the specified range is inside this range or
135         ''' <b>false</b> otherwise.</returns>
136         '''         Public Function IsInside(range As IntRange) As Boolean
137             Return ((IsInside(range.Min)) AndAlso (IsInside(range.Max)))
138         End Function
139
140         ''' <summary>
141         ''' Check if the specified range overlaps with this range
142         ''' </summary>
143         '''
144         ''' <param name="range">Range to check for overlapping</param>
145         '''
146         ''' <returns><b>True</b> if the specified range overlaps with this range or
147         ''' <b>false</b> otherwise.</returns>
148         '''         Public Function IsOverlapping(range As IntRange) As Boolean
149             Return ((IsInside(range.Min)) OrElse (IsInside(range.Max)))
150         End Function
151
152         Public Function IsInside(range As IRanges(Of Integer)) As Boolean Implements IRanges(Of Integer).IsInside
153             Return ((IsInside(range.Min)) AndAlso (IsInside(range.Max)))
154         End Function
155
156         Public Function IsOverlapping(range As IRanges(Of Integer)) As Boolean Implements IRanges(Of Integer).IsOverlapping
157             Return ((IsInside(range.Min)) OrElse (IsInside(range.Max)))
158         End Function
159
160         ''' <summary>
161         ''' Transform a numeric value in this <see cref="IntRange"/> into 
162         ''' target numeric range: ``<paramref name="valueRange"/>``.
163         ''' (将当前的范围内的一个实数映射到另外的一个范围内的实数区间之中)
164         ''' </summary>
165         ''' <param name="x#"></param>
166         ''' <param name="valueRange"></param>
167         ''' <returns></returns>
168         Public Function ScaleMapping(x%, valueRange As IntRange) As Double
169             Dim percent# = (x - Min) / Length
170             Dim value# = percent * valueRange.Length + valueRange.Min
171             Return value
172         End Function
173
174         ''' <summary>
175         ''' 枚举出这个数值范围内的所有整数值,步长为1
176         ''' </summary>
177         ''' <returns></returns>
178         Public Iterator Function GetEnumerator() As IEnumerator(Of IntegerImplements IEnumerable(Of Integer).GetEnumerator
179             For i As Integer = Min To Max
180                 Yield i
181             Next
182         End Function
183
184         Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
185             Yield GetEnumerator()
186         End Function
187
188         Public Shared Widening Operator CType(exp$) As IntRange
189             Dim r As New IntRange
190             Call exp.Parser(r.Min, r.Max)
191             Return r
192         End Operator
193
194         ' 2017-10-6
195         ' 因为下面的这个隐式转换操作符会和Vector的Item属性产生冲突,所以在这里将这个操作符移除掉
196         'Public Shared Widening Operator CType(values%()) As IntRange
197         '    With values
198         '        Return New IntRange(.Min, .Max)
199         '    End With
200         'End Operator
201
202         Public Shared Widening Operator CType(list As List(Of Integer)) As IntRange
203             With list
204                 Return New IntRange(.Min, .Max)
205             End With
206         End Operator
207     End Class
208 End Namespace