1 | #Region "Microsoft.VisualBasic::a01ebd5b55b9a07c10fd4c8ee2c3af7c, Microsoft.VisualBasic.Core\ComponentModel\DataStructures\CapacityQueue.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 CapacityQueue |
35 | ' |
36 | ' Properties: Capacity |
37 | ' |
38 | ' Constructor: (+1 Overloads) Sub New |
39 | ' |
40 | ' Function: Enqueue, GetEnumerator, IEnumerable_GetEnumerator, ToString |
41 | ' |
42 | ' Sub: Clear |
43 | ' |
44 | ' Operators: + |
45 | ' |
46 | ' |
47 | ' /********************************************************************************/ |
48 | |
49 | #End Region |
50 | |
51 | Imports Microsoft.VisualBasic.Serialization.JSON |
52 | |
53 | Namespace ComponentModel.Collection |
54 | |
55 | Public Class CapacityQueue(Of T) : Inherits Language.BaseClass |
56 | Implements IEnumerable(Of T) |
57 | |
58 | ReadOnly _queue As Queue(Of T) |
59 | |
60 | Public ReadOnly Property Capacity As Integer |
61 | |
62 | ''' <summary> |
63 | ''' |
64 | ''' </summary> |
65 | ''' <param name="capacity">The initial number of elements that the System.Collections.Generic.Queue`1 can |
66 | ''' contain.</param> |
67 | Sub New(capacity As Integer) |
68 | _queue = New Queue(Of T)(capacity) |
69 | End Sub |
70 | |
71 | Public Function Enqueue(x As T) As T |
72 | Dim o As T |
73 | |
74 | Call _queue.Enqueue(x) |
75 | |
76 | If _queue.Count = Capacity - 1 Then |
77 | o = _queue.Dequeue() |
78 | Else |
79 | o = _queue.Peek |
80 | End If |
81 | |
82 | Return o |
83 | End Function |
84 | |
85 | Public Sub Clear() |
86 | Call _queue.Clear() |
87 | End Sub |
88 | |
89 | Public Overrides Function ToString() As String |
90 | Return GetType(T()).GetObjectJson(Me.ToArray) |
91 | End Function |
92 | |
93 | Public Overloads Shared Operator +(q As CapacityQueue(Of T), x As T) As CapacityQueue(Of T) |
94 | Call q.Enqueue(x) |
95 | Return q |
96 | End Operator |
97 | |
98 | Public Iterator Function GetEnumerator() As IEnumerator(Of T) Implements IEnumerable(Of T).GetEnumerator |
99 | For Each x As T In _queue |
100 | Yield x |
101 | Next |
102 | End Function |
103 | |
104 | Private Iterator Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator |
105 | Yield GetEnumerator() |
106 | End Function |
107 | End Class |
108 | End Namespace |