wasSharp – Blame information for rev 27
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
10 | office | 1 | /////////////////////////////////////////////////////////////////////////// |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
||
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
||
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
||
5 | /////////////////////////////////////////////////////////////////////////// |
||
6 | |||
7 | using System.Collections.Generic; |
||
8 | |||
9 | namespace wasSharp.Collections.Generic |
||
10 | { |
||
11 | /////////////////////////////////////////////////////////////////////////// |
||
12 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
||
13 | /////////////////////////////////////////////////////////////////////////// |
||
14 | /// <summary> |
||
15 | /// A circular queue implementation based on linked lists. |
||
16 | /// </summary> |
||
17 | /// <typeparam name="T">the type of value to store</typeparam> |
||
18 | public class CircularQueue<T> |
||
19 | { |
||
20 | private readonly LinkedList<T> Store = new LinkedList<T>(); |
||
21 | |||
22 | private readonly object SyncRoot = new object(); |
||
12 | office | 23 | private LinkedListNode<T> CurrentNode; |
10 | office | 24 | |
25 | public CircularQueue() |
||
26 | { |
||
27 | } |
||
28 | |||
29 | public CircularQueue(IEnumerable<T> items) |
||
30 | { |
||
31 | Enqueue(items); |
||
32 | } |
||
33 | |||
34 | public CircularQueue(CircularQueue<T> queue) |
||
35 | { |
||
36 | lock (SyncRoot) |
||
37 | { |
||
38 | lock (queue.SyncRoot) |
||
39 | { |
||
40 | foreach (var item in queue.Items) |
||
41 | { |
||
42 | Store.AddLast(item); |
||
43 | } |
||
44 | |||
45 | if (CurrentNode == null) |
||
46 | CurrentNode = Store.First; |
||
47 | } |
||
48 | } |
||
49 | } |
||
50 | |||
51 | public int Count |
||
52 | { |
||
53 | get |
||
54 | { |
||
55 | lock (SyncRoot) |
||
56 | { |
||
57 | return Store.Count; |
||
58 | } |
||
59 | } |
||
60 | } |
||
61 | |||
62 | private T GetNext |
||
63 | { |
||
64 | get |
||
65 | { |
||
66 | lock (SyncRoot) |
||
67 | { |
||
68 | if (CurrentNode == null) |
||
69 | return default(T); |
||
70 | |||
71 | var value = CurrentNode.Value; |
||
72 | |||
73 | switch (CurrentNode.Next != null) |
||
74 | { |
||
75 | case true: |
||
76 | CurrentNode = CurrentNode.Next; |
||
77 | break; |
||
27 | office | 78 | |
10 | office | 79 | default: |
80 | CurrentNode = Store.First; |
||
81 | break; |
||
82 | } |
||
83 | |||
84 | return value; |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 | |||
89 | public IEnumerable<T> Items |
||
90 | { |
||
91 | get |
||
92 | { |
||
93 | lock (SyncRoot) |
||
94 | { |
||
95 | if (CurrentNode == null) |
||
96 | yield break; |
||
97 | |||
98 | var node = CurrentNode; |
||
99 | do |
||
100 | { |
||
101 | yield return node.Value; |
||
102 | node = node.Next; |
||
103 | } while (node != null); |
||
104 | } |
||
105 | } |
||
106 | } |
||
107 | |||
108 | public void Enqueue(IEnumerable<T> items) |
||
109 | { |
||
110 | lock (SyncRoot) |
||
111 | { |
||
112 | foreach (var i in items) |
||
113 | Store.AddLast(i); |
||
114 | |||
115 | if (CurrentNode == null) |
||
116 | CurrentNode = Store.First; |
||
117 | } |
||
118 | } |
||
119 | |||
120 | public void Enqueue(T item) |
||
121 | { |
||
122 | lock (SyncRoot) |
||
123 | { |
||
124 | Store.AddLast(item); |
||
125 | |||
126 | if (CurrentNode == null) |
||
127 | CurrentNode = Store.First; |
||
128 | } |
||
129 | } |
||
130 | |||
131 | public T Dequeue() |
||
132 | { |
||
133 | lock (SyncRoot) |
||
134 | { |
||
135 | return GetNext; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | public IEnumerable<T> Dequeue(int count = 1) |
||
140 | { |
||
141 | if (count <= 0) |
||
142 | yield break; |
||
143 | |||
144 | lock (SyncRoot) |
||
145 | { |
||
146 | if (CurrentNode == null) |
||
147 | yield break; |
||
148 | |||
149 | do |
||
150 | { |
||
151 | yield return GetNext; |
||
152 | } while (--count != 0); |
||
153 | } |
||
154 | } |
||
155 | |||
156 | public void Clear() |
||
157 | { |
||
158 | lock (SyncRoot) |
||
159 | { |
||
160 | Store.Clear(); |
||
161 | |||
162 | CurrentNode = null; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | public bool Contains(T item) |
||
167 | { |
||
168 | lock (SyncRoot) |
||
169 | { |
||
170 | return Store.Contains(item); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | public void CopyTo(T[] array, int arrayIndex) |
||
175 | { |
||
176 | lock (SyncRoot) |
||
177 | { |
||
178 | Store.CopyTo(array, arrayIndex); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | public bool Remove(T item) |
||
183 | { |
||
184 | lock (SyncRoot) |
||
185 | { |
||
186 | var node = Store.Find(item); |
||
187 | if (node == null) |
||
188 | return false; |
||
189 | if (CurrentNode.Equals(node)) |
||
190 | { |
||
191 | switch (node.Next != null) |
||
192 | { |
||
193 | case true: |
||
194 | CurrentNode = node.Next; |
||
195 | break; |
||
27 | office | 196 | |
10 | office | 197 | default: |
198 | CurrentNode = Store.First; |
||
199 | break; |
||
200 | } |
||
201 | } |
||
202 | Store.Remove(node); |
||
203 | return true; |
||
204 | } |
||
205 | } |
||
206 | |||
207 | public void RemoveAll(IEnumerable<T> items) |
||
208 | { |
||
209 | var itemSet = new HashSet<T>(items); |
||
210 | lock (SyncRoot) |
||
211 | { |
||
212 | var node = CurrentNode; |
||
213 | do |
||
214 | { |
||
215 | var next = node.Next; |
||
216 | if (itemSet.Contains(node.Value)) |
||
217 | { |
||
218 | switch (next != null) |
||
219 | { |
||
220 | case true: |
||
221 | CurrentNode = next; |
||
222 | break; |
||
27 | office | 223 | |
10 | office | 224 | default: |
225 | CurrentNode = Store.First; |
||
226 | break; |
||
227 | } |
||
228 | Store.Remove(node); |
||
229 | } |
||
230 | node = next; |
||
231 | } while (node != null); |
||
232 | } |
||
233 | } |
||
234 | } |
||
27 | office | 235 | } |