Sfoglia il codice sorgente

2002-06-25 Nick Drochak <[email protected]>

	* Queue.cs (CopyTo): Fix logic for copying the circular array.
	(Enqueue): Use actual length of array to determine when to grow
	(QueueEnumerator) Fixed Current to use array length, not capacity, and
	fixed off-by-one errror in MoveNext().

svn path=/trunk/mcs/; revision=5444
Nick Drochak 23 anni fa
parent
commit
5eea95dbf3

+ 6 - 0
mcs/class/corlib/System.Collections/ChangeLog

@@ -1,3 +1,9 @@
+2002-06-25   Nick Drochak  <[email protected]>
+
+	* Queue.cs (CopyTo): Fix logic for copying the circular array.
+	(Enqueue): Use actual length of array to determine when to grow
+	(QueueEnumerator) Fixed Current to use array length, not capacity, and
+	fixed off-by-one errror in MoveNext().
 
 Tue Jun 4 13:08:43 CEST 2002 Paolo Molaro <[email protected]>
 

+ 11 - 9
mcs/class/corlib/System.Collections/Queue.cs

@@ -75,13 +75,15 @@ namespace System.Collections {
 				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.Max (count, capacity - head));
-			if (count > capacity - head)
+				    Math.Min (count, length_from_head));
+			if (count >  length_from_head)
 				Array.Copy (contents, 0, array, 
-					    index + capacity - head,
-					    count - (capacity - head));
+					    index + length_from_head,
+					    count - length_from_head);
 		}
 
 		// from IEnumerable
@@ -155,9 +157,9 @@ namespace System.Collections {
 
 		public virtual void Enqueue (object obj) {
 			modCount++;
-			if (count == capacity) 
+			if (count == contents.Length) 
 				grow ();
-			contents[(head + count) % capacity] = obj;
+			contents[(head + count) % contents.Length] = obj;
 			count++;
 		}
 
@@ -190,7 +192,7 @@ namespace System.Collections {
 
 		private void grow () {
 			int newCapacity = (int) Math.Ceiling
-				(capacity * growFactor);
+				(contents.Length * growFactor);
 			object[] newContents = new object[newCapacity];
 			CopyTo (newContents, 0);
 			contents = newContents;
@@ -311,7 +313,7 @@ namespace System.Collections {
 					    || current < 0
 					    || current >= queue.count)
 						throw new InvalidOperationException ();
-					return queue.contents[(queue.head + current) % queue.capacity];
+					return queue.contents[(queue.head + current) % queue.contents.Length];
 				}
 			}
 
@@ -320,7 +322,7 @@ namespace System.Collections {
 					throw new InvalidOperationException ();
 				}
 
-				if (current >= queue.count) {
+				if (current >= queue.count - 1) {
 					return false;
 				} else {
 					current++;