2019-03-10 18:10:46 +08:00
|
|
|
|
#Region "Microsoft.VisualBasic::be1eb54371c961df549b5d8f7cc90a81, Microsoft.VisualBasic.Core\ComponentModel\System.Collections.Generic\PriorityQueue\PriorityQueue.vb"
|
2018-08-02 20:14:48 +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 PriorityQueue
|
|
|
|
|
|
'
|
|
|
|
|
|
' Constructor: (+2 Overloads) Sub New
|
|
|
|
|
|
'
|
2019-02-19 18:25:51 +08:00
|
|
|
|
' Function: count, empty, isHeap, pop, push
|
|
|
|
|
|
' top, ToString
|
2018-08-02 20:14:48 +08:00
|
|
|
|
'
|
2019-02-19 18:25:51 +08:00
|
|
|
|
' Sub: forEach, reduceKey
|
2018-08-02 20:14:48 +08:00
|
|
|
|
'
|
|
|
|
|
|
'
|
|
|
|
|
|
' /********************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
#End Region
|
|
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Imports Microsoft.VisualBasic.Linq
|
|
|
|
|
|
Imports Microsoft.VisualBasic.Serialization
|
|
|
|
|
|
Imports any = System.Object
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
Namespace ComponentModel.Collection
|
|
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
2019-02-19 18:25:51 +08:00
|
|
|
|
''' a min priority queue backed by a pairing heap
|
2018-08-02 20:14:48 +08:00
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <typeparam name="T"></typeparam>
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Public Class PriorityQueue(Of T)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Dim root As PairingHeap(Of T)
|
|
|
|
|
|
Dim lessThan As Func(Of T, T, Boolean)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
|
|
|
|
|
''' <summary>
|
2019-02-19 18:25:51 +08:00
|
|
|
|
''' <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><ECBAAF><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>Ƚϵı<CFB5><C4B1>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶȹ<C8B6>ϵ
|
|
|
|
|
|
''' <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DFBC><EFBFBD>ϵ<EFBFBD><CFB5>
|
|
|
|
|
|
'''
|
|
|
|
|
|
''' ```
|
|
|
|
|
|
''' priority = a < b
|
|
|
|
|
|
''' ```
|
2018-08-02 20:14:48 +08:00
|
|
|
|
''' </summary>
|
2019-02-19 18:25:51 +08:00
|
|
|
|
''' <param name="lessThan"></param>
|
|
|
|
|
|
Public Sub New(lessThan As Func(Of T, T, Boolean))
|
|
|
|
|
|
Me.lessThan = lessThan
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Sub
|
|
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Sub New(source As IEnumerable(Of T), lessThan As Func(Of T, T, Boolean))
|
|
|
|
|
|
Call Me.New(lessThan)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
For Each element As T In source.SafeQuery
|
|
|
|
|
|
Call Me.push(element)
|
|
|
|
|
|
Next
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Sub
|
|
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
'*
|
|
|
|
|
|
' * @method top
|
|
|
|
|
|
' * @return the top element (the min element as defined by lessThan)
|
|
|
|
|
|
'
|
2018-08-02 20:14:48 +08:00
|
|
|
|
''' <summary>
|
2019-02-19 18:25:51 +08:00
|
|
|
|
''' <20>൱<EFBFBD><E0B5B1>Stack<63><6B>Peek<65><6B><EFBFBD><EFBFBD>
|
2018-08-02 20:14:48 +08:00
|
|
|
|
''' </summary>
|
|
|
|
|
|
''' <returns></returns>
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Public Function top() As T
|
|
|
|
|
|
If Me.empty() Then
|
|
|
|
|
|
Return Nothing
|
|
|
|
|
|
End If
|
|
|
|
|
|
Return Me.root.elem
|
|
|
|
|
|
End Function
|
|
|
|
|
|
'*
|
|
|
|
|
|
' * @method push
|
|
|
|
|
|
' * put things on the heap
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
|
|
|
|
Public Function push(ParamArray args As T()) As PairingHeap(Of T)
|
|
|
|
|
|
Dim pairingNode As any = Nothing
|
|
|
|
|
|
Dim i As Integer = 0
|
|
|
|
|
|
Dim arg As T
|
|
|
|
|
|
|
|
|
|
|
|
While i > -1
|
|
|
|
|
|
arg = args(i - 1)
|
|
|
|
|
|
pairingNode = New PairingHeap(Of T)(arg)
|
|
|
|
|
|
|
|
|
|
|
|
If Me.empty Then
|
|
|
|
|
|
root = pairingNode
|
|
|
|
|
|
Else
|
|
|
|
|
|
root = root.merge(pairingNode, Me.lessThan)
|
|
|
|
|
|
End If
|
|
|
|
|
|
|
|
|
|
|
|
i += 1
|
|
|
|
|
|
End While
|
|
|
|
|
|
|
|
|
|
|
|
Return pairingNode
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
2019-02-19 18:25:51 +08:00
|
|
|
|
'*
|
|
|
|
|
|
' * @method empty
|
|
|
|
|
|
' * @return true if no more elements in queue
|
|
|
|
|
|
'
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Public Function empty() As Boolean
|
|
|
|
|
|
Return Me.root Is Nothing OrElse Me.root.elem Is Nothing
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
2019-02-19 18:25:51 +08:00
|
|
|
|
'*
|
|
|
|
|
|
' * @method isHeap check heap condition (for testing)
|
|
|
|
|
|
' * @return true if queue is in valid state
|
|
|
|
|
|
'
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Public Function isHeap() As Boolean
|
|
|
|
|
|
Return Me.root.isHeap(Me.lessThan)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
2019-02-19 18:25:51 +08:00
|
|
|
|
'*
|
|
|
|
|
|
' * @method forEach apply f to each element of the queue
|
|
|
|
|
|
' * @param f function to apply
|
|
|
|
|
|
'
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Public Sub forEach(f As any)
|
|
|
|
|
|
Me.root.forEach(f)
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
'*
|
|
|
|
|
|
' * @method pop remove and return the min element from the queue
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
|
|
|
|
Public Function pop() As T
|
|
|
|
|
|
If Me.empty() Then
|
|
|
|
|
|
Return Nothing
|
|
|
|
|
|
End If
|
|
|
|
|
|
Dim obj = Me.root.min()
|
|
|
|
|
|
Me.root = Me.root.removeMin(Me.lessThan)
|
|
|
|
|
|
Return obj
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
2019-02-19 18:25:51 +08:00
|
|
|
|
'*
|
|
|
|
|
|
' * @method reduceKey reduce the key value of the specified heap node
|
|
|
|
|
|
'
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Public Sub reduceKey(heapNode As PairingHeap(Of T), newKey As T, setHeapNode As Action(Of T, PairingHeap(Of T)))
|
|
|
|
|
|
Me.root = Me.root.decreaseKey(heapNode, newKey, setHeapNode, Me.lessThan)
|
|
|
|
|
|
End Sub
|
|
|
|
|
|
|
|
|
|
|
|
Public Overloads Function ToString(selector As IToString(Of T)) As String
|
|
|
|
|
|
Return Me.root.ToString(selector)
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
2019-02-19 18:25:51 +08:00
|
|
|
|
'*
|
|
|
|
|
|
' * @method count
|
|
|
|
|
|
' * @return number of elements in queue
|
|
|
|
|
|
'
|
2018-08-02 20:14:48 +08:00
|
|
|
|
|
2019-02-19 18:25:51 +08:00
|
|
|
|
Public Function count() As Double
|
|
|
|
|
|
Return Me.root.count()
|
2018-08-02 20:14:48 +08:00
|
|
|
|
End Function
|
|
|
|
|
|
End Class
|
|
|
|
|
|
End Namespace
|