2019-11-10 15:16:27 +08:00
|
|
|
|
#Region "Microsoft.VisualBasic::051df9d9d951aea29e194e13c8232320, Microsoft.VisualBasic.Core\Extensions\Collection\BucketOperators.vb"
|
2019-06-04 20:43:55 +08:00
|
|
|
|
|
|
|
|
|
|
' Author:
|
|
|
|
|
|
'
|
|
|
|
|
|
' asuka (amethyst.asuka@gcmodeller.org)
|
|
|
|
|
|
' xie (genetics@smrucc.org)
|
|
|
|
|
|
' xieguigang (xie.guigang@live.com)
|
|
|
|
|
|
'
|
|
|
|
|
|
' Copyright (c) 2018 GPL3 Licensed
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' GNU GENERAL PUBLIC LICENSE (GPL3)
|
|
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
' it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
' the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
' (at your option) any later version.
|
|
|
|
|
|
'
|
|
|
|
|
|
' This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
' but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
' GNU General Public License for more details.
|
|
|
|
|
|
'
|
|
|
|
|
|
' You should have received a copy of the GNU General Public License
|
|
|
|
|
|
' along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
' /********************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
' Summaries:
|
|
|
|
|
|
|
2019-11-10 15:16:27 +08:00
|
|
|
|
' Module BucketOperators
|
2019-06-04 20:43:55 +08:00
|
|
|
|
'
|
|
|
|
|
|
' Function: (+4 Overloads) Join, Split, SplitIterator
|
|
|
|
|
|
'
|
|
|
|
|
|
' /********************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#End Region
|
|
|
|
|
|
|
|
|
|
|
|
Imports System.Runtime.CompilerServices
|
2019-06-03 18:46:39 +08:00
|
|
|
|
Imports Microsoft.VisualBasic.Language
|
|
|
|
|
|
|
2019-10-12 22:38:14 +08:00
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' 进行集合分块切割或者合并等操作
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
Public Module BucketOperators
|
2019-06-03 18:46:39 +08:00
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Data partitioning function.
|
2019-10-12 22:38:14 +08:00
|
|
|
|
''' (将目标集合之中的数据按照<paramref name="partitionSize"></paramref>参数分配到子集合之中,
|
2019-06-03 18:46:39 +08:00
|
|
|
|
''' 这个函数之中不能够使用并行化Linq拓展,以保证元素之间的相互原有的顺序,
|
2019-10-12 22:38:14 +08:00
|
|
|
|
''' 每一个子集和之中的元素数量为<paramref name="partitionSize"/>)
|
2019-06-03 18:46:39 +08:00
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <typeparam name="T"></typeparam>
|
|
|
|
|
|
''' <param name="source"></param>
|
2019-10-12 22:38:14 +08:00
|
|
|
|
''' <param name="partitionSize">每一个子集合之中的元素的数目</param>
|
2019-06-03 18:46:39 +08:00
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
''' <remarks></remarks>
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2019-10-12 22:38:14 +08:00
|
|
|
|
<Extension> Public Function Split(Of T)(source As IEnumerable(Of T), partitionSize As Integer) As T()()
|
|
|
|
|
|
Return source.SplitIterator(partitionSize).ToArray
|
2019-06-03 18:46:39 +08:00
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Performance the partitioning operation on the input sequence.
|
2019-10-12 22:38:14 +08:00
|
|
|
|
''' (请注意,这个函数只适用于数量较少的序列。对所输入的序列进行分区操作,<paramref name="partitionSize"/>函数参数是每一个分区里面的元素的数量)
|
2019-06-03 18:46:39 +08:00
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <typeparam name="T"></typeparam>
|
|
|
|
|
|
''' <param name="source"></param>
|
2019-10-12 22:38:14 +08:00
|
|
|
|
''' <param name="partitionSize"></param>
|
2019-06-03 18:46:39 +08:00
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
<Extension>
|
2019-10-12 22:38:14 +08:00
|
|
|
|
Public Iterator Function SplitIterator(Of T)(source As IEnumerable(Of T), partitionSize As Integer) As IEnumerable(Of T())
|
|
|
|
|
|
Dim buffer As New List(Of T)(capacity:=partitionSize)
|
2019-06-03 18:46:39 +08:00
|
|
|
|
|
|
|
|
|
|
For Each item As T In source
|
2019-10-12 22:38:14 +08:00
|
|
|
|
buffer += item
|
2019-06-03 18:46:39 +08:00
|
|
|
|
|
2019-10-12 22:38:14 +08:00
|
|
|
|
If buffer >= partitionSize Then
|
2019-06-03 18:46:39 +08:00
|
|
|
|
Yield buffer.ToArray
|
|
|
|
|
|
|
|
|
|
|
|
buffer *= 0
|
|
|
|
|
|
End If
|
|
|
|
|
|
Next
|
|
|
|
|
|
|
|
|
|
|
|
If buffer > 0 Then
|
|
|
|
|
|
Yield buffer.ToArray
|
|
|
|
|
|
End If
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Merge two type specific collection.(函数会忽略掉空的集合,函数会构建一个新的集合,原有的集合不受影响)
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <typeparam name="T"></typeparam>
|
|
|
|
|
|
''' <param name="source"></param>
|
|
|
|
|
|
''' <param name="target"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
<Extension> Public Function Join(Of T)(source As IEnumerable(Of T), target As IEnumerable(Of T)) As List(Of T)
|
|
|
|
|
|
Dim srcList As List(Of T) = If(source Is Nothing, New List(Of T), source.AsList)
|
|
|
|
|
|
If Not target Is Nothing Then
|
|
|
|
|
|
Call srcList.AddRange(target)
|
|
|
|
|
|
End If
|
|
|
|
|
|
Return srcList
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
|
|
|
|
|
<Extension> Public Function Join(Of T)(source As IEnumerable(Of T), ParamArray data As T()) As List(Of T)
|
|
|
|
|
|
Return source.Join(target:=data)
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' Source list join a new <paramref name="data"/> element.
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <typeparam name="T"></typeparam>
|
|
|
|
|
|
''' <param name="source"></param>
|
|
|
|
|
|
''' <param name="data"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
<Extension> Public Function Join(Of T)(source As IEnumerable(Of T), data As T) As List(Of T)
|
|
|
|
|
|
Return source.Join({data})
|
|
|
|
|
|
End Function
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
|
|
|
|
|
''' ``X, ....``
|
|
|
|
|
|
'''
|
|
|
|
|
|
''' (这个函数是一个安全的函数,当<paramref name="collection"/>为空值的时候回忽略掉<paramref name="collection"/>,
|
|
|
|
|
|
''' 只返回包含有一个<paramref name="obj"/>元素的列表)
|
|
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <typeparam name="T"></typeparam>
|
|
|
|
|
|
''' <param name="obj"></param>
|
|
|
|
|
|
''' <param name="collection"></param>
|
|
|
|
|
|
''' <returns></returns>
|
|
|
|
|
|
<Extension> Public Function Join(Of T)(obj As T, collection As IEnumerable(Of T)) As List(Of T)
|
|
|
|
|
|
With New List(Of T) From {obj}
|
|
|
|
|
|
If Not collection Is Nothing Then
|
|
|
|
|
|
Call .AddRange(collection)
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
|
|
Return .ByRef
|
|
|
|
|
|
End With
|
|
|
|
|
|
End Function
|
|
|
|
|
|
End Module
|