Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Queue.cs « System.Collections « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e416eac87ed82aecf374b081232a3c76db97ee3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//
// System.Collections.Queue
//
// Author:
//    Ricardo Fernández Pascual
//
// (C) 2001 Ricardo Fernández Pascual
//

using System;
using System.Collections;

namespace System.Collections {

	[Serializable]
	public class Queue : ICollection, IEnumerable, ICloneable {

		private object[] contents;
		private int head = 0;   // points to the first used slot
		private int count = 0;
		private int capacity = 16;
		private float growFactor = 2.0F;
		private int modCount = 0;

		public Queue () {
			contents = new object[capacity];
		}

		public Queue (ICollection collection) {
			capacity = collection.Count;
			contents = new object[capacity];
			count = capacity;
			collection.CopyTo (contents, 0);
		}

		public Queue (int initialCapacity) {
			capacity = initialCapacity;
			contents = new object[capacity];
		}

		public Queue (int initialCapacity, float growFactor) {
			capacity = initialCapacity;
			contents = new object[capacity];
			// LAMESPEC: The spec says nothing, but I think this 
			// should throw an exception if growFactor <= 1.0
			this.growFactor = growFactor;
		}
		
		// from ICollection

		public virtual int Count {
			get { return count; }
		}

		public virtual bool IsSynchronized {
			get { return false; }
		}

		public virtual object SyncRoot {
			get { return this; }
		}

		public virtual void CopyTo (Array array, int index) {
			if (array == null) {
				throw new ArgumentNullException ();
			}

			if (index < 0) {
				throw new ArgumentOutOfRangeException ();
			}

			if (array.Rank > 1 
			    || index >= array.Length 
			    || count > array.Length - index) {
				throw new ArgumentException ();
			}
			
			int contents_length = contents.Length;
			int length_from_head = contents_length - head;
			// copy the contents of the circular array
			Array.Copy (contents, head, array, index,
				    Math.Min (count, length_from_head));
			if (count >  length_from_head)
				Array.Copy (contents, 0, array, 
					    index + length_from_head,
					    count - length_from_head);
		}

		// from IEnumerable
		
		public virtual IEnumerator GetEnumerator () {
			return new QueueEnumerator (this);
		}

		// from ICloneable
		
		public virtual object Clone () {
			Queue newQueue;
			
			newQueue = new Queue (); // FIXME: improve this...
			
			newQueue.contents = new object[this.contents.Length];
			Array.Copy (this.contents, 0, newQueue.contents, 0,
				    this.contents.Length);
			newQueue.head = this.head;
			newQueue.count = this.count;
			newQueue.capacity = this.capacity;
			newQueue.growFactor = this.growFactor;

			return newQueue;
		}

		// FIXME: should override Equals?

		// from Queue spec

/*
		public virtual bool IsReadOnly {
			get { return false; }
		}
*/

		public virtual void Clear () {
			modCount++;
			head = 0;
			count = 0;
			// FIXME: Should allocate a new contents array? 
			//        Should null the current array?
		}

		public virtual bool Contains (object obj) {
			int tail = head + count;
			if (obj == null) {
				for (int i = head; i < tail; i++) {
					if (contents[i % capacity] == null) 
						return true;
				}
			} else {
				for (int i = head; i < tail; i++) {
					if (obj.Equals (contents[i % capacity]))
						return true;
				}
			}
			return false;
		}
		
		public virtual object Dequeue ()
		{
			modCount++;
			if (count < 1)
				throw new InvalidOperationException ();
			object result = contents[head];
			head = (head + 1) % capacity;
			count--;
			return result;
		}

		public virtual void Enqueue (object obj) {
			modCount++;
			if (count == contents.Length) 
				grow ();
			contents[(head + count) % contents.Length] = obj;
			count++;
		}

		public virtual object Peek () {
			if (count < 1)
				throw new InvalidOperationException ();
			return contents[head];
		}

		public static Queue Synchronized (Queue queue) {
			if (queue == null) {
				throw new ArgumentNullException ();
			}
			return new SyncQueue (queue);
		}

		public virtual object[] ToArray () {
			object[] ret = new object[count];
			CopyTo (ret, 0);
			return ret;
		}

		public virtual void TrimToSize() {
			object[] trimmed = new object [count];
			CopyTo (trimmed, 0);
			contents = trimmed;
		}

		// private methods

		private void grow () {
			int newCapacity = (int) Math.Ceiling
				(contents.Length * growFactor);
			object[] newContents = new object[newCapacity];
			CopyTo (newContents, 0);
			contents = newContents;
			head = 0;
		}

		// private classes

		private class SyncQueue : Queue {
			Queue queue;
			
			internal SyncQueue (Queue queue) {
				this.queue = queue;
			}
			
			public override int Count {
				get { 
					lock (queue) {
						return queue.count; 
					}
				}
			}

			public override bool IsSynchronized {
				get { 
					lock (queue) {
						return queue.IsSynchronized;
					}
				}
			}

			public override object SyncRoot {
				get { 
					return queue.SyncRoot; 
				}
			}

			public override void CopyTo (Array array, int index) {
				lock (queue) {
					queue.CopyTo (array, index);
				}
			}
			
			public override IEnumerator GetEnumerator () {
				lock (queue) {
					return queue.GetEnumerator ();
				}
			}
			
			public override object Clone () {
				lock (queue) {
					return queue.Clone ();
				}
			}
			
/*
			public override bool IsReadOnly {
				get { 
					lock (queue) {
						return queue.IsReadOnly;
					}
				}
			}
*/

			public override void Clear () {
				lock (queue) {
					queue.Clear ();
				}
			}

			public override bool Contains (object obj) {
				lock (queue) {
					return queue.Contains (obj);
				}
			}
		
			public override object Dequeue () {
				lock (queue) {
					return queue.Dequeue ();
				}
			}
			
			public override void Enqueue (object obj) {
				lock (queue) {
					queue.Enqueue (obj);
				}
			}

			public override object Peek () {
				lock (queue) {
					return queue.Peek ();
				}
			}

			public override object[] ToArray () {
				lock (queue) {
					return queue.ToArray ();
				}
			}
		}

		[Serializable]
		private class QueueEnumerator : IEnumerator {
			Queue queue;
			private int modCount;
			private int current;

			internal QueueEnumerator (Queue q) {
				queue = q;
				modCount = q.modCount;
				current = -1;  // one element before the head
			}

			public virtual object Current {
				get {
					if (modCount != queue.modCount 
					    || current < 0
					    || current >= queue.count)
						throw new InvalidOperationException ();
					return queue.contents[(queue.head + current) % queue.contents.Length];
				}
			}

			public virtual bool MoveNext () {
				if (modCount != queue.modCount) {
					throw new InvalidOperationException ();
				}

				if (current >= queue.count - 1) {
					return false;
				} else {
					current++;
					return true;
				}
			}

			public virtual void Reset () {
				if (modCount != queue.modCount) {
					throw new InvalidOperationException();
				}
				current = -1;
			}
		}
	}
}