Browse Source

2005-11-10 Zoltan Varga <[email protected]>

	* SortedList.cs Queue.cs Stack.cs: Implement TrimExcess methods.

svn path=/trunk/mcs/; revision=52887
Zoltan Varga 20 years ago
parent
commit
af48507239

+ 2 - 0
mcs/class/System/System.Collections.Generic/ChangeLog

@@ -1,5 +1,7 @@
 2005-11-10  Zoltan Varga  <[email protected]>
 
+	* SortedList.cs Queue.cs Stack.cs: Implement TrimExcess methods.
+	
 	* SortedList.cs: Fix build.
 
 	* Stack.cs SortedList.cs LinkedList.cs: Update to net 2.0 RTM.

+ 13 - 6
mcs/class/System/System.Collections.Generic/Queue.cs

@@ -45,16 +45,21 @@ namespace System.Collections.Generic
 		int tail;
 		int size;
 		int version;
+		int defaultCapacity;
+
+		private readonly static int INITIAL_SIZE = 16;
 		
 		public Queue ()
 		{
+			defaultCapacity = INITIAL_SIZE;
 		}
 		
 		public Queue (int count)
 		{
 			if (count < 0)
 				throw new ArgumentOutOfRangeException ("count");
-			
+
+			defaultCapacity = count;
 			data = new T [count];
 		}
 		
@@ -63,8 +68,9 @@ namespace System.Collections.Generic
 			if (collection == null)
 				throw new ArgumentNullException ("collection");
 			
-				foreach (T t in collection)
-					Enqueue (t);
+			foreach (T t in collection)
+				Enqueue (t);
+			defaultCapacity = size;
 		}
 		
 		public void Clear ()
@@ -186,10 +192,11 @@ namespace System.Collections.Generic
 			CopyTo (t, 0);
 			return t;
 		}
-		
-		public void TrimToSize ()
+
+		public void TrimExcess ()
 		{
-			SetCapacity (size);
+			if (data != null && (size < data.Length * 0.9))
+				Array.Resize <T> (ref data, size == 0 ? defaultCapacity : size);
 		}
 		
 		void SetCapacity (int new_size)

+ 2 - 10
mcs/class/System/System.Collections.Generic/SortedList.cs

@@ -492,10 +492,10 @@ namespace System.Collections.Generic {
 			return IndexOfValue (value) >= 0;
 		}
 
-		[MonoTODO]
 		public void TrimExcess ()
 		{
-			throw new NotImplementedException ();
+			if (inUse < table.Length * 0.9)
+				Capacity = inUse;
 		}
 
 		public bool TryGetValue (TKey key, out TValue value)
@@ -519,14 +519,6 @@ namespace System.Collections.Generic {
 		// Private methods
 		//
 
-		private void Resize (int n, bool copy)
-		{
-			KeyValuePair<TKey, TValue> [] table = this.table;
-			KeyValuePair<TKey, TValue> [] newTable = new KeyValuePair<TKey, TValue> [n];
-			if (copy) Array.Copy (table, 0, newTable, 0, n);
-			this.table = newTable;
-		}
-
 		private void EnsureCapacity (int n, int free)
 		{
 			KeyValuePair<TKey, TValue> [] table = this.table;

+ 11 - 6
mcs/class/System/System.Collections.Generic/Stack.cs

@@ -43,16 +43,21 @@ namespace System.Collections.Generic
 		T [] data;
 		int size;
 		int ver;
+		int defaultCapacity;
 		
+		private readonly static int INITIAL_SIZE = 16;
+
 		public Stack ()
 		{
+			defaultCapacity = INITIAL_SIZE;
 		}
 		
 		public Stack (int count)
 		{
 			if (count < 0)
 				throw new ArgumentOutOfRangeException ("count");
-			
+
+			defaultCapacity = count;			
 			data = new T [count];
 		}
 		
@@ -62,8 +67,7 @@ namespace System.Collections.Generic
 				throw new ArgumentNullException ("collection");
 			
 			ICollection <T> col = collection as ICollection <T>;
-			
-			
+						
 			if (col != null) {
 				size = col.Count;
 				data = new T [size];
@@ -72,6 +76,7 @@ namespace System.Collections.Generic
 				foreach (T t in collection)
 					Push (t);
 			}
+			defaultCapacity = size;
 		}
 		
 		public void Clear ()
@@ -120,7 +125,7 @@ namespace System.Collections.Generic
 		public void Push (T t)
 		{
 			if (size == 0 || size == data.Length)
-				Array.Resize <T> (ref data, size == 0 ? 10 : 2 * size);
+				Array.Resize <T> (ref data, size == 0 ? INITIAL_SIZE : 2 * size);
 			
 			ver ++;
 			
@@ -134,10 +139,10 @@ namespace System.Collections.Generic
 			return copy;
 		}
 
-		[MonoTODO]
 		public void TrimExcess ()
 		{
-			throw new NotImplementedException ();
+			if (data != null && (size < data.Length * 0.9))
+				Array.Resize <T> (ref data, size == 0 ? defaultCapacity : size);
 		}
 		
 		public int Count {