microsoft-visualbasic-runtime/ComponentModel/System.Collections.Generic/BucketDictionary.vb

257 lines
9.5 KiB
VB.net
Raw Normal View History

2019-09-06 19:20:43 +08:00
#Region "Microsoft.VisualBasic::fb63cde407f533b915185b636ca705d8, Microsoft.VisualBasic.Core\ComponentModel\System.Collections.Generic\BucketDictionary.vb"
2018-12-19 20:25:34 +08:00
2019-09-06 19:20:43 +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/>.
2019-09-06 19:08:36 +08:00
2019-09-06 19:20:43 +08:00
' /********************************************************************************/
2019-09-06 19:08:36 +08:00
2019-09-06 19:20:43 +08:00
' Summaries:
2019-09-06 19:08:36 +08:00
2019-09-06 19:20:43 +08:00
' Class BucketDictionary
'
' Properties: Count, Keys, Values
'
' Constructor: (+3 Overloads) Sub New
' Function: ContainsKey, GetEnumerator, IEnumerable_GetEnumerator, ToString, TryGetValue
'
' Module BucketDictionaryExtensions
'
' Function: (+3 Overloads) CreateBuckets
'
'
' /********************************************************************************/
2018-08-02 20:14:48 +08:00
#End Region
Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.Language
Imports Microsoft.VisualBasic.Linq
2019-09-06 19:08:36 +08:00
Imports Microsoft.VisualBasic.Serialization.JSON
2018-08-02 20:14:48 +08:00
Namespace ComponentModel.Collection
''' <summary>
''' An ultralarge size dictionary object.
''' (当你发现一个数据集合非常的大的时候一个字典会出现溢出则这个时候就需要这个超大容量的Bucket字典容器了)
''' </summary>
''' <typeparam name="K"></typeparam>
''' <typeparam name="V"></typeparam>
Public Class BucketDictionary(Of K, V) : Implements IReadOnlyDictionary(Of K, V)
Friend ReadOnly buckets As New List(Of Dictionary(Of K, V))
Friend minBucket As Dictionary(Of K, V)
2018-08-02 20:14:48 +08:00
''' <summary>
''' 每一个字典之中的最大的元素数目
''' </summary>
ReadOnly bucketSize As Integer
''' <summary>
''' 获取这个超大的字典集合之中的对象的数量总数
''' </summary>
''' <returns></returns>
Public ReadOnly Property Count As Integer Implements IReadOnlyCollection(Of KeyValuePair(Of K, V)).Count
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Get
Return buckets.Sum(Function(x) x.Count)
2018-08-02 20:14:48 +08:00
End Get
End Property
''' <summary>
''' 注意,不要直接使用这个方法来添加新的数据,使用<see cref="BucketDictionaryExtensions"/>的方法会更加高效
''' </summary>
''' <param name="key"></param>
''' <returns></returns>
Default Public Property Item(key As K) As V Implements IReadOnlyDictionary(Of K, V).Item
Get
For Each hash In buckets
2018-08-02 20:14:48 +08:00
If hash.ContainsKey(key) Then
Return hash(key)
End If
Next
Return Nothing
End Get
Set(value As V)
If buckets.Count = 0 Then
buckets.Add(New Dictionary(Of K, V) From {{key, value}})
minBucket = buckets(Scan0)
2018-08-02 20:14:48 +08:00
Else
For Each hash In buckets
2018-08-02 20:14:48 +08:00
If hash.ContainsKey(key) Then
hash(key) = value
Return
End If
Next
minBucket(key) = value
2018-08-02 20:14:48 +08:00
If minBucket.Count >= bucketSize Then
buckets.Add(New Dictionary(Of K, V))
minBucket = buckets.Last
2018-08-02 20:14:48 +08:00
End If
End If
End Set
End Property
Public ReadOnly Property Keys As IEnumerable(Of K) Implements IReadOnlyDictionary(Of K, V).Keys
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Get
Return buckets.Select(Function(x) x.Keys).IteratesALL
2018-08-02 20:14:48 +08:00
End Get
End Property
Public ReadOnly Property Values As IEnumerable(Of V) Implements IReadOnlyDictionary(Of K, V).Values
<MethodImpl(MethodImplOptions.AggressiveInlining)>
Get
Return buckets.Select(Function(x) x.Values).IteratesALL
2018-08-02 20:14:48 +08:00
End Get
End Property
Sub New(bucketSize As Integer)
Me.bucketSize = bucketSize
End Sub
Sub New()
Call Me.New(Short.MaxValue * 10)
End Sub
Sub New(buckets As IEnumerable(Of Dictionary(Of K, V)))
Me.buckets = buckets.AsList
Me.minBucket = Me.buckets _
.OrderBy(Function(table) table.Count) _
.FirstOrDefault
End Sub
2018-08-02 20:14:48 +08:00
Public Overrides Function ToString() As String
2019-09-06 19:08:36 +08:00
Return $"Tuple of [{GetType(K).Name}, {GetType(V).Name}] with {Count} records in {buckets.Count} buckets."
2018-08-02 20:14:48 +08:00
End Function
Public Function ContainsKey(key As K) As Boolean Implements IReadOnlyDictionary(Of K, V).ContainsKey
For Each hash In buckets
2018-08-02 20:14:48 +08:00
If hash.ContainsKey(key) Then
Return True
End If
Next
Return False
End Function
Public Iterator Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of K, V)) Implements IEnumerable(Of KeyValuePair(Of K, V)).GetEnumerator
For Each hash In buckets
2018-08-02 20:14:48 +08:00
For Each x In hash
Yield x
Next
Next
End Function
Public Function TryGetValue(key As K, ByRef value As V) As Boolean Implements IReadOnlyDictionary(Of K, V).TryGetValue
For Each hash In buckets
2018-08-02 20:14:48 +08:00
If hash.ContainsKey(key) Then
value = hash(key)
Return True
End If
Next
Return False
End Function
Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Yield GetEnumerator()
End Function
End Class
2019-09-04 20:38:12 +08:00
<HideModuleName>
2018-08-02 20:14:48 +08:00
Public Module BucketDictionaryExtensions
2019-09-06 19:08:36 +08:00
''' <summary>
'''
''' </summary>
''' <typeparam name="T"></typeparam>
''' <typeparam name="K"></typeparam>
''' <param name="source"></param>
''' <param name="getKey"></param>
''' <param name="size%"></param>
''' <param name="overridesDuplicates">
''' 当数据中存在重复的键名的时候,是将前面已有的数据覆盖掉还是抛出键名重复的错误?默认是不进行重写覆盖,而是键重复抛出错误。
''' </param>
''' <returns></returns>
<MethodImpl(MethodImplOptions.AggressiveInlining)>
2018-08-02 20:14:48 +08:00
<Extension>
2019-09-06 19:08:36 +08:00
Public Function CreateBuckets(Of T, K)(source As IEnumerable(Of T),
getKey As Func(Of T, K),
Optional size% = Short.MaxValue * 10,
Optional overridesDuplicates As Boolean = False) As BucketDictionary(Of K, T)
Return source.CreateBuckets(
getKey:=getKey,
getValue:=Function(o) o,
size:=size,
overridesDuplicates:=overridesDuplicates
)
End Function
<Extension>
Public Function CreateBuckets(Of T, K, V)(source As IEnumerable(Of T),
getKey As Func(Of T, K),
getValue As Func(Of T, V),
2019-09-06 19:08:36 +08:00
Optional size% = Short.MaxValue * 10,
Optional overridesDuplicates As Boolean = False) As BucketDictionary(Of K, V)
2018-08-02 20:14:48 +08:00
Dim table As New BucketDictionary(Of K, V)(size)
Dim bucket As New Dictionary(Of K, V)
For Each x As T In source
Dim key As K = getKey(x)
Dim value As V = getValue(x)
2019-09-06 19:08:36 +08:00
If overridesDuplicates Then
bucket(key) = value
ElseIf bucket.ContainsKey(key) Then
Throw New DuplicateNameException(key.GetJson)
Else
Call bucket.Add(key, value)
End If
2018-08-02 20:14:48 +08:00
If bucket.Count >= size Then
table.buckets.Add(bucket)
2018-08-02 20:14:48 +08:00
bucket = New Dictionary(Of K, V)
End If
Next
Call table.buckets.Add(bucket)
2018-08-02 20:14:48 +08:00
Return table
End Function
<MethodImpl(MethodImplOptions.AggressiveInlining)>
<Extension>
Public Function CreateBuckets(Of K, V)(source As IEnumerable(Of (K, V)), Optional size% = Short.MaxValue * 10) As BucketDictionary(Of K, V)
Return source.CreateBuckets(Function(t) t.Item1, Function(t) t.Item2, size:=size)
End Function
End Module
End Namespace