| 1 |
#Region "Microsoft.VisualBasic::4c096a1f4ef79bbe866c4e7bdc9d1319, Microsoft.VisualBasic.Core\ComponentModel\Algorithm\base\SlideWindow\Extensions.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 |
#End Region
|
| 42 |
|
| 43 |
Imports System.Runtime.CompilerServices
|
| 44 |
Imports Microsoft.VisualBasic.Language
|
| 45 |
Imports Microsoft.VisualBasic.Linq
|
| 46 |
|
| 47 |
Namespace ComponentModel.Algorithm.base
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
Public Module SlideWindowExtensions
|
| 53 |
|
| 54 |
|
| 55 |
(创建一个滑窗集合)
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
<Extension> Public Function CreateSlideWindows(Of T)(data As IEnumerable(Of T), winSize%,
|
| 65 |
Optional offset% = 1,
|
| 66 |
Optional extTails As Boolean = False) As SlideWindow(Of T)()
|
| 67 |
Return data.SlideWindows(winSize, offset, extTails).ToArray
|
| 68 |
End Function
|
| 69 |
|
| 70 |
|
| 71 |
(创建一个滑窗集合)
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
<Extension> Public Iterator Function SlideWindows(Of T)(data As IEnumerable(Of T), winSize%, Optional offset% = 1, Optional extTails As Boolean = False) As IEnumerable(Of SlideWindow(Of T))
|
| 81 |
Dim tmp As New List(Of T)(data)
|
| 82 |
Dim n% = tmp.Count
|
| 83 |
|
| 84 |
If n = 0 Then
|
| 85 |
|
| 86 |
Return
|
| 87 |
ElseIf winSize >= n Then
|
| 88 |
Yield New SlideWindow(Of T)() With {
|
| 89 |
.Left = 0,
|
| 90 |
.Items = tmp.ToArray
|
| 91 |
}
|
| 92 |
|
| 93 |
Return
|
| 94 |
End If
|
| 95 |
|
| 96 |
If offset < 1 Then
|
| 97 |
Call VBDebugger.Warning($"The offset parameter '{offset}' is not correct, set its value to 1 as default!")
|
| 98 |
offset = 1
|
| 99 |
End If
|
| 100 |
|
| 101 |
Dim p As int = 0
|
| 102 |
|
| 103 |
n = n - winSize - 1
|
| 104 |
|
| 105 |
For i As Integer = 0 To n Step offset
|
| 106 |
Dim buf As T() = tmp.Take(winSize).ToArray
|
| 107 |
|
| 108 |
Yield New SlideWindow(Of T)() With {
|
| 109 |
.Items = buf,
|
| 110 |
.Left = i,
|
| 111 |
.Index = ++p
|
| 112 |
}
|
| 113 |
|
| 114 |
tmp.RemoveRange(0, offset)
|
| 115 |
Next
|
| 116 |
|
| 117 |
If Not tmp.IsNullOrEmpty Then
|
| 118 |
Dim left = n + 1
|
| 119 |
|
| 120 |
If extTails Then
|
| 121 |
|
| 122 |
|
| 123 |
For Each x In __extendTails(tmp, winSize, left, CInt(p))
|
| 124 |
Yield x
|
| 125 |
Next
|
| 126 |
Else
|
| 127 |
Yield New SlideWindow(Of T)() With {
|
| 128 |
.Left = left,
|
| 129 |
.Items = tmp.ToArray,
|
| 130 |
.Index = p
|
| 131 |
}
|
| 132 |
End If
|
| 133 |
End If
|
| 134 |
End Function
|
| 135 |
|
| 136 |
Private Iterator Function __extendTails(Of T)(tempList As List(Of T), winSize%, left%, p As int) As IEnumerable(Of SlideWindow(Of T))
|
| 137 |
Dim array As T() = tempList.ToArray
|
| 138 |
|
| 139 |
For i As Integer = 0 To winSize - 1
|
| 140 |
Yield New SlideWindow(Of T) With {
|
| 141 |
.Left = left,
|
| 142 |
.Index = ++p,
|
| 143 |
.Items = array
|
| 144 |
}
|
| 145 |
|
| 146 |
left += 1
|
| 147 |
Next
|
| 148 |
End Function
|
| 149 |
|
| 150 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 151 |
<Extension>
|
| 152 |
Public Function [Select](Of T, TOut)(windows As IEnumerable(Of SlideWindow(Of T)), projection As Func(Of T, T, TOut)) As IEnumerable(Of TOut)
|
| 153 |
Return windows.Select(selector:=Function(win) projection(win(0), win(1)))
|
| 154 |
End Function
|
| 155 |
|
| 156 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 157 |
<Extension>
|
| 158 |
Public Function [Select](Of T, TOut)(windows As IEnumerable(Of SlideWindow(Of T)), projection As Func(Of T, T, T, TOut)) As IEnumerable(Of TOut)
|
| 159 |
Return windows.Select(selector:=Function(win) projection(win(0), win(1), win(2)))
|
| 160 |
End Function
|
| 161 |
|
| 162 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 163 |
<Extension>
|
| 164 |
Public Function [Select](Of T, TOut)(windows As IEnumerable(Of SlideWindow(Of T)), projection As Func(Of T, T, T, T, TOut)) As IEnumerable(Of TOut)
|
| 165 |
Return windows.Select(selector:=Function(win) projection(win(0), win(1), win(2), win(3)))
|
| 166 |
End Function
|
| 167 |
|
| 168 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 169 |
<Extension>
|
| 170 |
Public Function [Select](Of T, TOut)(windows As IEnumerable(Of SlideWindow(Of T)), projection As Func(Of T, T, T, T, T, TOut)) As IEnumerable(Of TOut)
|
| 171 |
Return windows.Select(selector:=Function(win) projection(win(0), win(1), win(2), win(3), win(4)))
|
| 172 |
End Function
|
| 173 |
|
| 174 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
| 175 |
<Extension>
|
| 176 |
Public Function [Select](Of T, TOut)(windows As IEnumerable(Of SlideWindow(Of T)), projection As Func(Of T, T, T, T, T, T, TOut)) As IEnumerable(Of TOut)
|
| 177 |
Return windows.Select(selector:=Function(win)
|
| 178 |
Dim i As int = Scan0
|
| 179 |
Return projection(win(++i), win(++i), win(++i), win(++i), win(++i), win(++i))
|
| 180 |
End Function)
|
| 181 |
End Function
|
| 182 |
End Module
|
| 183 |
End Namespace
|