| 1 |
#Region "Microsoft.VisualBasic::27a6e726bdd07af572a40c4625b66bd9, Microsoft.VisualBasic.Core\Extensions\Collection\Linq\Iterator.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 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#End Region
|
| 59 |
|
| 60 |
Imports System.Runtime.CompilerServices
|
| 61 |
Imports Microsoft.VisualBasic.ComponentModel
|
| 62 |
Imports Microsoft.VisualBasic.Emit.Marshal
|
| 63 |
Imports Microsoft.VisualBasic.Language
|
| 64 |
Imports Microsoft.VisualBasic.Language.Default
|
| 65 |
Imports Microsoft.VisualBasic.Serialization.JSON
|
| 66 |
|
| 67 |
Namespace Linq
|
| 68 |
|
| 69 |
Public Module IteratorExtensions
|
| 70 |
|
| 71 |
<Extension>
|
| 72 |
Public Iterator Function SeqIterator(source As IEnumerable, Optional offset% = 0) As IEnumerable(Of SeqValue(Of Object))
|
| 73 |
Dim i As Integer = offset
|
| 74 |
|
| 75 |
For Each o As Object In source
|
| 76 |
Yield New SeqValue(Of Object)(i, o)
|
| 77 |
i += 1
|
| 78 |
Next
|
| 79 |
End Function
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
<Extension>
|
| 89 |
Public Iterator Function SeqIterator(Of T)(source As IEnumerable(Of T), Optional offset% = 0) As IEnumerable(Of SeqValue(Of T))
|
| 90 |
If Not source Is Nothing Then
|
| 91 |
Dim idx% = offset
|
| 92 |
|
| 93 |
For Each x As T In source
|
| 94 |
Yield New SeqValue(Of T)(idx, x)
|
| 95 |
idx += 1
|
| 96 |
Next
|
| 97 |
End If
|
| 98 |
End Function
|
| 99 |
|
| 100 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 101 |
<Extension>
|
| 102 |
Public Function SeqTuple(Of T1, T2)(tuple As (a As IEnumerable(Of T1), b As IEnumerable(Of T2)), Optional offset% = 0) As IEnumerable(Of SeqValue(Of (a As T1, b As T2)))
|
| 103 |
Return (tuple.a.ToArray, tuple.b.ToArray).SeqTuple(offset)
|
| 104 |
End Function
|
| 105 |
|
| 106 |
<Extension>
|
| 107 |
Public Iterator Function SeqTuple(Of T1, T2)(tuple As (x As T1(), y As T2()), Optional offset% = 0) As IEnumerable(Of SeqValue(Of (a As T1, b As T2)))
|
| 108 |
Dim value As (T1, T2)
|
| 109 |
Dim length% = Math.Max(tuple.x.Length, tuple.y.Length)
|
| 110 |
|
| 111 |
For i As Integer = 0 To length - 1
|
| 112 |
value = (
|
| 113 |
tuple.x.ElementAtOrDefault(i),
|
| 114 |
tuple.y.ElementAtOrDefault(i)
|
| 115 |
)
|
| 116 |
Yield New SeqValue(Of (T1, T2))(i + offset, value)
|
| 117 |
Next
|
| 118 |
End Function
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
<Extension>
|
| 127 |
Public Function [Next](Of T)(source As IEnumerator(Of T)) As T
|
| 128 |
If source.MoveNext() Then
|
| 129 |
Return source.Current
|
| 130 |
Else
|
| 131 |
Return Nothing
|
| 132 |
End If
|
| 133 |
End Function
|
| 134 |
|
| 135 |
<Extension>
|
| 136 |
Public Function Previous(Of T)(source As IEnumerator(Of T)) As T
|
| 137 |
Throw New NotImplementedException
|
| 138 |
End Function
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 148 |
<Extension>
|
| 149 |
Public Function ValueArray(Of T)(source As IEnumerable(Of Value(Of T).IValueOf)) As T()
|
| 150 |
Return source.Select(Function(o) o.Value).ToArray
|
| 151 |
End Function
|
| 152 |
|
| 153 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 154 |
<Extension>
|
| 155 |
Public Function Indices(Of T)(source As IEnumerable(Of SeqValue(Of T))) As Integer()
|
| 156 |
Return source.Ordinals.ToArray
|
| 157 |
End Function
|
| 158 |
|
| 159 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 160 |
<Extension>
|
| 161 |
Public Function Ordinals(Of T)(source As IEnumerable(Of SeqValue(Of T))) As IEnumerable(Of Integer)
|
| 162 |
Return source.Select(Function(o) o.i)
|
| 163 |
End Function
|
| 164 |
|
| 165 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 166 |
<Extension>
|
| 167 |
Public Function Indices(Of T)(source As IEnumerable(Of T), assert As Func(Of T, Boolean)) As Integer()
|
| 168 |
Return source _
|
| 169 |
.SeqIterator _
|
| 170 |
.Where(Function(x) True = assert(x.value)) _
|
| 171 |
.Indices
|
| 172 |
End Function
|
| 173 |
End Module
|
| 174 |
|
| 175 |
|
| 176 |
Value <typeparamref name="T"/> with sequence index <see cref="i"/>.
|
| 177 |
|
| 178 |
|
| 179 |
Public Structure SeqValue(Of T) : Implements IAddressOf
|
| 180 |
Implements IComparable(Of Integer)
|
| 181 |
Implements IComparable
|
| 182 |
Implements Value(Of T).IValueOf
|
| 183 |
Implements IsEmpty
|
| 184 |
|
| 185 |
|
| 186 |
The position of this object value in the original sequence.
|
| 187 |
|
| 188 |
|
| 189 |
Public Property i As Integer Implements IAddressOf.Address
|
| 190 |
|
| 191 |
The Object data
|
| 192 |
|
| 193 |
|
| 194 |
Public Property value As T Implements Value(Of T).IValueOf.Value
|
| 195 |
|
| 196 |
Public ReadOnly Property IsEmpty As Boolean Implements IsEmpty.IsEmpty
|
| 197 |
Get
|
| 198 |
Return i = 0 AndAlso value Is Nothing
|
| 199 |
End Get
|
| 200 |
End Property
|
| 201 |
|
| 202 |
Sub New(i%, x As T)
|
| 203 |
Me.i = i
|
| 204 |
value = x
|
| 205 |
End Sub
|
| 206 |
|
| 207 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 208 |
Public Overrides Function ToString() As String
|
| 209 |
Return Me.value.GetJson(False)
|
| 210 |
End Function
|
| 211 |
|
| 212 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 213 |
Public Shared Narrowing Operator CType(x As SeqValue(Of T)) As T
|
| 214 |
Return x.value
|
| 215 |
End Operator
|
| 216 |
|
| 217 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 218 |
Public Shared Narrowing Operator CType(x As SeqValue(Of T)) As Integer
|
| 219 |
Return x.i
|
| 220 |
End Operator
|
| 221 |
|
| 222 |
Public Shared Operator +(list As System.Collections.Generic.List(Of T), x As SeqValue(Of T)) As System.Collections.Generic.List(Of T)
|
| 223 |
Call list.Add(x.value)
|
| 224 |
Return list
|
| 225 |
End Operator
|
| 226 |
|
| 227 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 228 |
Public Shared Operator Mod(i As SeqValue(Of T), n%) As Integer
|
| 229 |
Return i.i Mod n
|
| 230 |
End Operator
|
| 231 |
|
| 232 |
Public Shared Operator <>(v As SeqValue(Of T), i%) As Boolean
|
| 233 |
Return v.i <> i
|
| 234 |
End Operator
|
| 235 |
|
| 236 |
Public Shared Operator =(v As SeqValue(Of T), i%) As Boolean
|
| 237 |
Return v.i = i
|
| 238 |
End Operator
|
| 239 |
|
| 240 |
|
| 241 |
Get value from <see cref="value"/> property.
|
| 242 |
|
| 243 |
<param name="x"></param>
|
| 244 |
|
| 245 |
<remarks>
|
| 246 |
Syntax helper for the <see cref="Pointer(Of T)"/>:
|
| 247 |
|
| 248 |
```vbnet
|
| 249 |
Dim p As Pointer(Of T) = T()
|
| 250 |
Dim x As T = ++p
|
| 251 |
```
|
| 252 |
</remarks>
|
| 253 |
|
| 254 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 255 |
Public Shared Operator +(x As SeqValue(Of T)) As T
|
| 256 |
Return x.value
|
| 257 |
End Operator
|
| 258 |
|
| 259 |
|
| 260 |
Get value from <see cref="value"/> property.
|
| 261 |
|
| 262 |
<param name="x"></param>
|
| 263 |
|
| 264 |
<remarks>
|
| 265 |
Syntax helper for the <see cref="Pointer(Of T)"/>:
|
| 266 |
|
| 267 |
```vbnet
|
| 268 |
Dim p As Pointer(Of T) = T()
|
| 269 |
Dim x As T = --p
|
| 270 |
```
|
| 271 |
</remarks>
|
| 272 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 273 |
Public Shared Operator -(x As SeqValue(Of T)) As T
|
| 274 |
Return x.value
|
| 275 |
End Operator
|
| 276 |
|
| 277 |
|
| 278 |
Compares the index value <see cref="i"/>.
|
| 279 |
|
| 280 |
<param name="other"></param>
|
| 281 |
|
| 282 |
|
| 283 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 284 |
Public Function CompareTo(other As Integer) As Integer Implements IComparable(Of Integer).CompareTo
|
| 285 |
Return i.CompareTo(other)
|
| 286 |
End Function
|
| 287 |
|
| 288 |
Public Function CompareTo(obj As Object) As Integer Implements IComparable.CompareTo
|
| 289 |
If obj Is Nothing Then
|
| 290 |
Return 1
|
| 291 |
End If
|
| 292 |
|
| 293 |
If Not obj.GetType Is Me.GetType Then
|
| 294 |
Return 10
|
| 295 |
End If
|
| 296 |
|
| 297 |
Return i.CompareTo(DirectCast(obj, SeqValue(Of T)).i)
|
| 298 |
End Function
|
| 299 |
|
| 300 |
Private Sub Assign(address As Integer) Implements IAddress(Of Integer).Assign
|
| 301 |
i = address
|
| 302 |
End Sub
|
| 303 |
End Structure
|
| 304 |
|
| 305 |
|
| 306 |
Exposes the enumerator, which supports a simple iteration over a collection of
|
| 307 |
a specified type.To browse the .NET Framework source code for this type, see
|
| 308 |
the Reference Source.
|
| 309 |
(使用这个的原因是系统自带的<see cref="IEnumerable(Of T)"/>在Xml序列化之中的支持不太好)
|
| 310 |
|
| 311 |
<typeparam name="T">The type of objects to enumerate.This type parameter is covariant. That is, you
|
| 312 |
can use either the type you specified or any type that is more derived. For more
|
| 313 |
information about covariance and contravariance, see Covariance and Contravariance
|
| 314 |
in Generics.</typeparam>
|
| 315 |
Public Interface IIterator(Of T)
|
| 316 |
|
| 317 |
|
| 318 |
Returns an enumerator that iterates through the collection.
|
| 319 |
|
| 320 |
<returns>An enumerator that can be used to iterate through the collection.</returns>
|
| 321 |
Function GetEnumerator() As IEnumerator(Of T)
|
| 322 |
|
| 323 |
|
| 324 |
Returns an enumerator that iterates through a collection.
|
| 325 |
|
| 326 |
<returns>An System.Collections.IEnumerator object that can be used to iterate through
|
| 327 |
the collection.</returns>
|
| 328 |
Function IGetEnumerator() As IEnumerator
|
| 329 |
End Interface
|
| 330 |
End Namespace
|