2019-07-17 22:01:31 +08:00
|
|
|
|
#Region "Microsoft.VisualBasic::3833795011a6ed46058df56330aca07e, ComponentModel\System.Collections.Generic\BucketDictionary.vb"
|
2018-12-19 20:25:34 +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:
|
|
|
|
|
|
|
|
|
|
|
|
' Class BucketDictionary
|
|
|
|
|
|
'
|
|
|
|
|
|
' Properties: Count, Keys, Values
|
|
|
|
|
|
'
|
|
|
|
|
|
' Constructor: (+3 Overloads) Sub New
|
|
|
|
|
|
' Function: ContainsKey, GetEnumerator, IEnumerable_GetEnumerator, ToString, TryGetValue
|
|
|
|
|
|
'
|
|
|
|
|
|
' Module BucketDictionaryExtensions
|
|
|
|
|
|
'
|
2019-06-04 20:43:55 +08:00
|
|
|
|
' Function: (+3 Overloads) CreateBuckets
|
2018-12-19 20:25:34 +08:00
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' /********************************************************************************/
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
#End Region
|
|
|
|
|
|
|
|
|
|
|
|
Imports System.Runtime.CompilerServices
|
|
|
|
|
|
Imports Microsoft.VisualBasic.Language
|
|
|
|
|
|
Imports Microsoft.VisualBasic.Linq
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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)
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
2019-06-03 18:46:39 +08:00
|
|
|
|
minBucket(key) = value
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
2019-06-03 18:46:39 +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
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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
|
2019-06-03 18:46:39 +08:00
|
|
|
|
Return buckets.Select(Function(x) x.Values).IteratesALL
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Get
|
|
|
|
|
|
End Property
|
|
|
|
|
|
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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-06-03 18:46:39 +08:00
|
|
|
|
Return $"Tuple of [{GetType(K).Name}, {GetType(V).Name}] with {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
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
Public Module BucketDictionaryExtensions
|
|
|
|
|
|
|
2019-06-03 18:46:39 +08:00
|
|
|
|
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
2018-08-02 20:14:48 +08:00
|
|
|
|
<Extension>
|
2019-06-03 18:46:39 +08:00
|
|
|
|
Public Function CreateBuckets(Of T, K)(source As IEnumerable(Of T), getKey As Func(Of T, K), Optional size% = Short.MaxValue * 10) As BucketDictionary(Of K, T)
|
|
|
|
|
|
Return source.CreateBuckets(getKey, Function(o) o, size:=size)
|
|
|
|
|
|
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),
|
|
|
|
|
|
Optional size% = Short.MaxValue * 10) 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)
|
|
|
|
|
|
|
|
|
|
|
|
Call bucket.Add(key, value)
|
|
|
|
|
|
|
|
|
|
|
|
If bucket.Count >= size Then
|
2019-06-03 18:46:39 +08:00
|
|
|
|
table.buckets.Add(bucket)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
bucket = New Dictionary(Of K, V)
|
|
|
|
|
|
End If
|
|
|
|
|
|
Next
|
|
|
|
|
|
|
2019-06-03 18:46:39 +08:00
|
|
|
|
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
|