wasSharp – Blame information for rev 51

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