wasSharp – Blame information for rev 12
?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; |
||
78 | default: |
||
79 | CurrentNode = Store.First; |
||
80 | break; |
||
81 | } |
||
82 | |||
83 | return value; |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | |||
88 | public IEnumerable<T> Items |
||
89 | { |
||
90 | get |
||
91 | { |
||
92 | lock (SyncRoot) |
||
93 | { |
||
94 | if (CurrentNode == null) |
||
95 | yield break; |
||
96 | |||
97 | var node = CurrentNode; |
||
98 | do |
||
99 | { |
||
100 | yield return node.Value; |
||
101 | node = node.Next; |
||
102 | } while (node != null); |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | public void Enqueue(IEnumerable<T> items) |
||
108 | { |
||
109 | lock (SyncRoot) |
||
110 | { |
||
111 | foreach (var i in items) |
||
112 | Store.AddLast(i); |
||
113 | |||
114 | if (CurrentNode == null) |
||
115 | CurrentNode = Store.First; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | public void Enqueue(T item) |
||
120 | { |
||
121 | lock (SyncRoot) |
||
122 | { |
||
123 | Store.AddLast(item); |
||
124 | |||
125 | if (CurrentNode == null) |
||
126 | CurrentNode = Store.First; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | public T Dequeue() |
||
131 | { |
||
132 | lock (SyncRoot) |
||
133 | { |
||
134 | return GetNext; |
||
135 | } |
||
136 | } |
||
137 | |||
138 | public IEnumerable<T> Dequeue(int count = 1) |
||
139 | { |
||
140 | if (count <= 0) |
||
141 | yield break; |
||
142 | |||
143 | lock (SyncRoot) |
||
144 | { |
||
145 | if (CurrentNode == null) |
||
146 | yield break; |
||
147 | |||
148 | do |
||
149 | { |
||
150 | yield return GetNext; |
||
151 | } while (--count != 0); |
||
152 | } |
||
153 | } |
||
154 | |||
155 | public void Clear() |
||
156 | { |
||
157 | lock (SyncRoot) |
||
158 | { |
||
159 | Store.Clear(); |
||
160 | |||
161 | CurrentNode = null; |
||
162 | } |
||
163 | } |
||
164 | |||
165 | public bool Contains(T item) |
||
166 | { |
||
167 | lock (SyncRoot) |
||
168 | { |
||
169 | return Store.Contains(item); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | public void CopyTo(T[] array, int arrayIndex) |
||
174 | { |
||
175 | lock (SyncRoot) |
||
176 | { |
||
177 | Store.CopyTo(array, arrayIndex); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | public bool Remove(T item) |
||
182 | { |
||
183 | lock (SyncRoot) |
||
184 | { |
||
185 | var node = Store.Find(item); |
||
186 | if (node == null) |
||
187 | return false; |
||
188 | if (CurrentNode.Equals(node)) |
||
189 | { |
||
190 | switch (node.Next != null) |
||
191 | { |
||
192 | case true: |
||
193 | CurrentNode = node.Next; |
||
194 | break; |
||
195 | default: |
||
196 | CurrentNode = Store.First; |
||
197 | break; |
||
198 | } |
||
199 | } |
||
200 | Store.Remove(node); |
||
201 | return true; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | public void RemoveAll(IEnumerable<T> items) |
||
206 | { |
||
207 | var itemSet = new HashSet<T>(items); |
||
208 | lock (SyncRoot) |
||
209 | { |
||
210 | var node = CurrentNode; |
||
211 | do |
||
212 | { |
||
213 | var next = node.Next; |
||
214 | if (itemSet.Contains(node.Value)) |
||
215 | { |
||
216 | switch (next != null) |
||
217 | { |
||
218 | case true: |
||
219 | CurrentNode = next; |
||
220 | break; |
||
221 | default: |
||
222 | CurrentNode = Store.First; |
||
223 | break; |
||
224 | } |
||
225 | Store.Remove(node); |
||
226 | } |
||
227 | node = next; |
||
228 | } while (node != null); |
||
229 | } |
||
230 | } |
||
231 | } |
||
232 | } |