1 | #Region "Microsoft.VisualBasic::9dbc70c8a7df8057d1d1449a5c94baf7, Microsoft.VisualBasic.Core\ComponentModel\System.Collections.Generic\PriorityQueue\PriorityQueue.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class PriorityQueue |
35 | ' |
36 | ' Properties: Count |
37 | ' |
38 | ' Constructor: (+2 Overloads) Sub New |
39 | ' |
40 | ' Function: Contains, Dequeue, GetEnumerator, IEnumerable_GetEnumerator, Peek |
41 | ' ToString |
42 | ' |
43 | ' Sub: Add, Clear, Enqueue, Remove, Sort |
44 | ' |
45 | ' |
46 | ' /********************************************************************************/ |
47 | |
48 | #End Region |
49 | |
50 | '! |
51 | '@file PriorityQueue.cs |
52 | '@author Woong Gyu La a.k.a Chris. <juhgiyo@gmail.com> |
53 | ' <http://github.com/juhgiyo/epForceDirectedGraph.cs> |
54 | '@date September 27, 2013 |
55 | '@brief PriorityQueue Interface |
56 | '@version 2.0 |
57 | ' '@section LICENSE |
58 | ' 'The MIT License (MIT) |
59 | ' 'Copyright (c) 2013 Woong Gyu La <juhgiyo@gmail.com> |
60 | ' 'Permission is hereby granted, free of charge, to any person obtaining a copy |
61 | 'of this software and associated documentation files (the "Software"), to deal |
62 | 'in the Software without restriction, including without limitation the rights |
63 | 'to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
64 | 'copies of the Software, and to permit persons to whom the Software is |
65 | 'furnished to do so, subject to the following conditions: |
66 | ' 'The above copyright notice and this permission notice shall be included in |
67 | 'all copies or substantial portions of the Software. |
68 | ' 'THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
69 | 'IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
70 | 'FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
71 | 'AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
72 | 'LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
73 | 'OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
74 | 'THE SOFTWARE. |
75 | ' '@section DESCRIPTION |
76 | ' 'An Interface for the PriorityQueue Class. |
77 | ' ' |
78 | |
79 | Imports System.Runtime.CompilerServices |
80 | Imports Microsoft.VisualBasic.Serialization.JSON |
81 | |
82 | Namespace ComponentModel.Collection |
83 | |
84 | ''' <summary> |
85 | ''' An Interface for the PriorityQueue Class. |
86 | ''' </summary> |
87 | ''' <typeparam name="T"></typeparam> |
88 | Public Class PriorityQueue(Of T As IComparable) |
89 | Implements IEnumerable(Of T) |
90 | |
91 | Protected list As New List(Of T) |
92 | |
93 | Public Overridable ReadOnly Property Count() As Integer |
94 | Get |
95 | Return list.Count |
96 | End Get |
97 | End Property |
98 | |
99 | Sub New() |
100 | End Sub |
101 | |
102 | Sub New(source As IEnumerable(Of T)) |
103 | list = source.ToList |
104 | list.Sort() |
105 | End Sub |
106 | |
107 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
108 | Public Overridable Sub Enqueue(queueItem As T) |
109 | Call list.Add(queueItem) |
110 | Call list.Sort() |
111 | End Sub |
112 | |
113 | ''' <summary> |
114 | ''' Add without sort |
115 | ''' </summary> |
116 | ''' <param name="x"></param> |
117 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
118 | Public Sub Add(x As T) |
119 | Call list.Add(x) |
120 | End Sub |
121 | |
122 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
123 | Public Sub Sort() |
124 | Call list.Sort() |
125 | End Sub |
126 | |
127 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
128 | Public Overridable Sub Clear() |
129 | Call list.Clear() |
130 | End Sub |
131 | |
132 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
133 | Public Overridable Sub Remove(o As T) |
134 | Call list.Remove(o) |
135 | End Sub |
136 | |
137 | ''' <summary> |
138 | ''' Poll |
139 | ''' </summary> |
140 | ''' <returns></returns> |
141 | Public Overridable Function Dequeue() As T |
142 | Dim frontItem As T = list(0) |
143 | list.RemoveAt(0) |
144 | Return frontItem |
145 | End Function |
146 | |
147 | Public Overridable Function Peek() As T |
148 | Dim frontItem As T = list(0) |
149 | Return frontItem |
150 | End Function |
151 | |
152 | Public Overrides Function ToString() As String |
153 | Return $"Queue {list.Count} items, 1st_item:={Peek.GetJson}" |
154 | End Function |
155 | |
156 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
157 | Public Overridable Function Contains(queueItem As T) As Boolean |
158 | Return list.Contains(queueItem) |
159 | End Function |
160 | |
161 | Public Overridable Iterator Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator |
162 | For Each x As T In list |
163 | Yield x |
164 | Next |
165 | End Function |
166 | |
167 | Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator |
168 | Yield GetEnumerator() |
169 | End Function |
170 | End Class |
171 | End Namespace |