1 |
#Region "Microsoft.VisualBasic::603f2180efea2a3c5e5f1a0bad2cc3ff, Microsoft.VisualBasic.Core\Extensions\Collection\Linq\Which.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
#End Region
|
44 |
|
45 |
Imports System.Runtime.CompilerServices
|
46 |
Imports Microsoft.VisualBasic.Language
|
47 |
Imports Microsoft.VisualBasic.Language.Default
|
48 |
Imports Microsoft.VisualBasic.Linq
|
49 |
|
50 |
Namespace Linq
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
Public NotInheritable Class Which
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
74 |
Public Shared Function Index(Of T)(source As IEnumerable(Of T), predicate As Func(Of T, Boolean)) As IEnumerable(Of Integer)
|
75 |
Return source _
|
76 |
.SeqIterator _
|
77 |
.Where(Function(i) predicate(i.value)) _
|
78 |
.Select(Function(o) o.i)
|
79 |
End Function
|
80 |
|
81 |
Public Shared Function GetMinIndex(values As List(Of Double)) As Integer
|
82 |
Dim min As Double = Double.MaxValue
|
83 |
Dim minIndex As Integer = 0
|
84 |
For i As Integer = 0 To values.Count - 1
|
85 |
If values(i) < min Then
|
86 |
min = values(i)
|
87 |
minIndex = i
|
88 |
End If
|
89 |
Next
|
90 |
|
91 |
Return minIndex
|
92 |
End Function
|
93 |
|
94 |
|
95 |
在这里不适用Module类型,要不然会和其他的Max拓展函数产生冲突的。。
|
96 |
|
97 |
Private Sub New()
|
98 |
End Sub
|
99 |
|
100 |
|
101 |
Determines the location, i.e., index of the (first) minimum or maximum of a numeric (or logical) vector.
|
102 |
|
103 |
|
104 |
<param name="x">
|
105 |
numeric (logical, integer or double) vector or an R object for which the internal coercion to
|
106 |
double works whose min or max is searched for.
|
107 |
</param>
|
108 |
|
109 |
|
110 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
111 |
Public Shared Function Max(Of T As IComparable)(x As IEnumerable(Of T)) As Integer
|
112 |
Return x.MaxIndex
|
113 |
End Function
|
114 |
|
115 |
|
116 |
Determines the location, i.e., index of the (first) minimum or maximum of a numeric (or logical) vector.
|
117 |
|
118 |
|
119 |
<param name="x">
|
120 |
numeric (logical, integer or double) vector or an R object for which the internal coercion to
|
121 |
double works whose min or max is searched for.
|
122 |
</param>
|
123 |
|
124 |
|
125 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
126 |
Public Shared Function Min(Of T As IComparable)(x As IEnumerable(Of T)) As Integer
|
127 |
Return x.MinIndex
|
128 |
End Function
|
129 |
|
130 |
|
131 |
Return all of the indices which is True
|
132 |
|
133 |
<param name="v"></param>
|
134 |
|
135 |
|
136 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
137 |
Public Shared Function IsTrue(v As IEnumerable(Of Boolean)) As Integer()
|
138 |
Return v _
|
139 |
.SeqIterator _
|
140 |
.Where(Function(b) True = +b) _
|
141 |
.Select(Function(i) CInt(i)) _
|
142 |
.ToArray
|
143 |
End Function
|
144 |
|
145 |
|
146 |
Syntax helper for <see cref="VectorShadows(Of T)(IEnumerable(Of T))"/>
|
147 |
|
148 |
<param name="list"></param>
|
149 |
|
150 |
|
151 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
152 |
Public Shared Function IsTrue(list As Object) As Integer()
|
153 |
Return IsTrue(DirectCast(list, IEnumerable).Cast(Of Object).Select(Function(o) CBool(o)))
|
154 |
End Function
|
155 |
|
156 |
|
157 |
Returns all of the indices which is False
|
158 |
|
159 |
<param name="v"></param>
|
160 |
|
161 |
|
162 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
163 |
Public Shared Function IsFalse(v As IEnumerable(Of Boolean)) As Integer()
|
164 |
Return v _
|
165 |
.SeqIterator _
|
166 |
.Where(Function(b) False = +b) _
|
167 |
.Select(Function(i) CInt(i)) _
|
168 |
.ToArray
|
169 |
End Function
|
170 |
|
171 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
172 |
Public Shared Function IsTrue([operator] As Func(Of Boolean())) As Integer()
|
173 |
Return IsTrue([operator]())
|
174 |
End Function
|
175 |
|
176 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
177 |
Public Shared Function IsFalse([operator] As Func(Of Boolean())) As Integer()
|
178 |
Return Which.IsFalse([operator]())
|
179 |
End Function
|
180 |
|
181 |
Public Shared Function Top(Of T As IComparable(Of T))(seq As IEnumerable(Of T), n As Integer) As Integer()
|
182 |
Return seq.SeqIterator _
|
183 |
.OrderByDescending(Function(x) x.value) _
|
184 |
.Take(n) _
|
185 |
.Ordinals _
|
186 |
.ToArray
|
187 |
End Function
|
188 |
|
189 |
|
190 |
查找出列表之中符合条件的所有的索引编号
|
191 |
|
192 |
|
193 |
<param name="array"></param>
|
194 |
<param name="condi"></param>
|
195 |
|
196 |
Public Shared Iterator Function [True](Of T)(array As IEnumerable(Of T), condi As Assert(Of T)) As IEnumerable(Of Integer)
|
197 |
Dim i%
|
198 |
|
199 |
For Each x As T In array
|
200 |
If True = condi(x) Then
|
201 |
Yield i
|
202 |
End If
|
203 |
|
204 |
i += 1
|
205 |
Next
|
206 |
End Function
|
207 |
|
208 |
|
209 |
枚举出所有大于目标的顶点编号
|
210 |
|
211 |
|
212 |
|
213 |
<param name="compareTo"></param>
|
214 |
|
215 |
<remarks>因为这个返回的是一个迭代器,所以可以和First结合产生FirstGreaterThan表达式</remarks>
|
216 |
|
217 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
218 |
Public Shared Function IsGreaterThan(Of T As IComparable)(source As IEnumerable(Of T), compareTo As T) As IEnumerable(Of Integer)
|
219 |
Return source _
|
220 |
.SeqIterator _
|
221 |
.Where(Function(o) Language.GreaterThan(o.value, compareTo)) _
|
222 |
.Select(Function(i) i.i)
|
223 |
End Function
|
224 |
|
225 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
226 |
Public Shared Function IsGreaterThan(Of T, C As IComparable)(source As IEnumerable(Of T), getValue As Func(Of T, C), compareTo As C) As IEnumerable(Of Integer)
|
227 |
Return Which.IsGreaterThan(source.Select(getValue), compareTo)
|
228 |
End Function
|
229 |
End Class
|
230 |
End Namespace
|