1 #Region "Microsoft.VisualBasic::db823fc685023f06205b5c43d4ba9f77, Microsoft.VisualBasic.Core\ComponentModel\Ranges\RangeList.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 RangeList
35     
36     '         Constructor: (+1 OverloadsSub New
37     
38     '         Function: [Select], GetEnumerator, IEnumerable_GetEnumerator, (+2 OverloadsSelectValue
39     
40     '         Sub: Add
41     
42     
43     ' /********************************************************************************/
44
45 #End Region
46
47 Imports System.Runtime.CompilerServices
48 Imports Microsoft.VisualBasic.ComponentModel.Ranges.Model
49 Imports Microsoft.VisualBasic.Language
50 Imports Microsoft.VisualBasic.Serialization.JSON
51
52 Namespace ComponentModel.Ranges
53
54     Public Class RangeList(Of T As IComparable, V) : Implements IEnumerable(Of RangeTagValue(Of T, V))
55
56         Dim buffer As New List(Of RangeTagValue(Of T, V))
57
58         Public ReadOnly Iterator Property Values As IEnumerable(Of V)
59             Get
60                 For Each x As RangeTagValue(Of T, V) In Me
61                     Yield x.Value
62                 Next
63             End Get
64         End Property
65
66         Public ReadOnly Iterator Property Keys As IEnumerable(Of Range(Of T))
67             Get
68                 For Each x As RangeTagValue(Of T, V) In Me
69                     Yield x
70                 Next
71             End Get
72         End Property
73
74         Sub New(Optional capacity% = 128)
75             If capacity > 0 Then
76                 buffer = New List(Of RangeTagValue(Of T, V))(capacity)
77             Else
78                 ' do nothing, internal used only
79             End If
80         End Sub
81
82         <MethodImpl(MethodImplOptions.AggressiveInlining)>
83         Public Sub Add(range As RangeTagValue(Of T, V))
84             Call buffer.Add(range)
85         End Sub
86
87         Public Function [Select](x As T) As RangeTagValue(Of T, V)
88             Dim LQuery = LinqAPI.DefaultFirst(Of RangeTagValue(Of T, V)) _
89  _
90                 () <= From r As RangeTagValue(Of T, V)
91                       In Me.AsQueryable
92                       Where r.IsInside(x)
93                       Select r
94
95             Return LQuery
96         End Function
97
98         Public Function SelectValue(x As T, Optional [throw] As Boolean = TrueOptional ByRef success As Boolean = FalseAs V
99             Dim n As RangeTagValue(Of T, V) = [Select](x)
100
101             If n Is Nothing Then
102                 If [throw] Then
103                     Throw New DataException($"{x.GetJson} is not in any ranges!")
104                 Else
105                     Return Nothing
106                 End If
107             Else
108                 success = True
109                 Return n.Value
110             End If
111         End Function
112
113         Public Function SelectValue(x As T, [default] As Func(Of T, V)) As V
114             Dim success As Boolean = False
115             Dim v As V = SelectValue(x, [throw]:=False, success:=success)
116
117             If success Then
118                 Return v
119             Else
120                 Return [default](x)
121             End If
122         End Function
123
124         Public Iterator Function GetEnumerator() As IEnumerator(Of RangeTagValue(Of T, V)) Implements IEnumerable(Of RangeTagValue(Of T, V)).GetEnumerator
125             For Each x In buffer
126                 Yield x
127             Next
128         End Function
129
130         Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
131             Yield GetEnumerator()
132         End Function
133
134         <MethodImpl(MethodImplOptions.AggressiveInlining)>
135         Public Shared Widening Operator CType(buffer As RangeTagValue(Of T, V)()) As RangeList(Of T, V)
136             Return New RangeList(Of T, V)(-1) With {
137                 .buffer = buffer.AsList
138             }
139         End Operator
140     End Class
141 End Namespace