瀏覽代碼

Replace some hashtables with dictionaries

Marek Safar 13 年之前
父節點
當前提交
bb05074a9f

+ 5 - 5
mcs/class/corlib/System.Globalization/CompareInfo.cs

@@ -32,7 +32,7 @@
 //
 
 using System.Reflection;
-using System.Collections;
+using System.Collections.Generic;
 using System.Runtime.Serialization;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
@@ -124,7 +124,7 @@ namespace System.Globalization
 		SimpleCollator collator;
 
 		// Maps culture IDs to SimpleCollator objects
-		private static Hashtable collators;
+		private static Dictionary<int, SimpleCollator> collators;
 
 		[NonSerialized]
 		// Protects access to 'collators'
@@ -137,9 +137,9 @@ namespace System.Globalization
 			if (UseManagedCollation) {
 				lock (monitor) {
 					if (collators == null)
-						collators = new Hashtable ();
-					collator = (SimpleCollator)collators [ci.LCID];
-					if (collator == null) {
+						collators = new Dictionary<int, SimpleCollator> ();
+
+					if (!collators.TryGetValue (ci.LCID, out collator)) {
 						collator = new SimpleCollator (ci);
 						collators [ci.LCID] = collator;
 					}

+ 11 - 13
mcs/class/corlib/System.Globalization/CultureInfo.cs

@@ -30,7 +30,7 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System.Collections;
+using System.Collections.Generic;
 using System.Threading;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
@@ -685,13 +685,14 @@ namespace System.Globalization
 		// current locale so we can initialize the object without
 		// doing any member initialization
 		private CultureInfo () { constructed = true; }
-		static Hashtable shared_by_number, shared_by_name;
+		static Dictionary<int, CultureInfo> shared_by_number;
+		static Dictionary<string, CultureInfo> shared_by_name;
 		
 		static void insert_into_shared_tables (CultureInfo c)
 		{
 			if (shared_by_number == null){
-				shared_by_number = new Hashtable ();
-				shared_by_name = new Hashtable ();
+				shared_by_number = new Dictionary<int, CultureInfo> ();
+				shared_by_name = new Dictionary<string, CultureInfo> ();
 			}
 			shared_by_number [c.cultureID] = c;
 			shared_by_name [c.m_name] = c;
@@ -702,12 +703,11 @@ namespace System.Globalization
 			CultureInfo c;
 			
 			lock (shared_table_lock){
-				if (shared_by_number != null){
-					c = shared_by_number [culture] as CultureInfo;
-
-					if (c != null)
-						return (CultureInfo) c;
+				if (shared_by_number != null) {
+					if (shared_by_number.TryGetValue (culture, out c))
+						return c;
 				}
+
 				c = new CultureInfo (culture, false, true);
 				insert_into_shared_tables (c);
 				return c;
@@ -722,10 +722,8 @@ namespace System.Globalization
 			CultureInfo c;
 			lock (shared_table_lock){
 				if (shared_by_name != null){
-					c = shared_by_name [name] as CultureInfo;
-
-					if (c != null)
-						return (CultureInfo) c;
+					if (shared_by_name.TryGetValue (name, out c))
+						return c;
 				}
 				c = new CultureInfo (name, false, true);
 				insert_into_shared_tables (c);

+ 5 - 5
mcs/class/corlib/System.Runtime.Serialization/SerializationInfo.cs

@@ -34,15 +34,15 @@
 //
 
 using System;
-using System.Collections;
+using System.Collections.Generic;
 
 namespace System.Runtime.Serialization
 {
 	[System.Runtime.InteropServices.ComVisibleAttribute (true)]
 	public sealed class SerializationInfo
 	{
-		Hashtable serialized = new Hashtable ();
-		ArrayList values = new ArrayList ();
+		Dictionary<string, SerializationEntry> serialized = new Dictionary<string, SerializationEntry> ();
+		List<SerializationEntry> values = new List<SerializationEntry> ();
 
 		string assemblyName; // the assembly being serialized
 		string fullTypeName; // the type being serialized.
@@ -181,7 +181,7 @@ namespace System.Runtime.Serialization
 			if (!serialized.ContainsKey (name))
 				throw new SerializationException ("No element named " + name + " could be found.");
 						
-			SerializationEntry entry = (SerializationEntry) serialized [name];
+			SerializationEntry entry = serialized [name];
 
 			if (entry.Value != null && !type.IsInstanceOfType (entry.Value))
 				return converter.Convert (entry.Value, type);
@@ -191,7 +191,7 @@ namespace System.Runtime.Serialization
 
 		internal bool HasKey (string name)
 		{
-			return serialized [name] != null;
+			return serialized.ContainsKey (name);
 		}
 		
 		public void SetType (Type type)

+ 1 - 2
mcs/class/corlib/System.Runtime.Serialization/SerializationInfoEnumerator.cs

@@ -31,7 +31,6 @@
 
 using System;
 using System.Collections;
-using System.Runtime.Serialization;
 
 namespace System.Runtime.Serialization
 {
@@ -41,7 +40,7 @@ namespace System.Runtime.Serialization
 		IEnumerator enumerator;
 
 		// Constructor
-		internal SerializationInfoEnumerator (ArrayList list)
+		internal SerializationInfoEnumerator (IEnumerable list)
 		{
 			this.enumerator = list.GetEnumerator ();
 		}

+ 11 - 12
mcs/class/corlib/System.Threading/Thread.cs

@@ -36,7 +36,7 @@ using System.Globalization;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
 using System.IO;
-using System.Collections;
+using System.Collections.Generic;
 using System.Reflection;
 using System.Security;
 using System.Runtime.ConstrainedExecution;
@@ -323,13 +323,13 @@ namespace System.Threading {
 		}
 
 		// Stores a hash keyed by strings of LocalDataStoreSlot objects
-		static Hashtable datastorehash;
+		static Dictionary<string, LocalDataStoreSlot> datastorehash;
 		private static object datastore_lock = new object ();
 		
 		private static void InitDataStoreHash () {
 			lock (datastore_lock) {
 				if (datastorehash == null) {
-					datastorehash = Hashtable.Synchronized(new Hashtable());
+					datastorehash = new Dictionary<string, LocalDataStoreSlot> ();
 				}
 			}
 		}
@@ -338,14 +338,14 @@ namespace System.Threading {
 			lock (datastore_lock) {
 				if (datastorehash == null)
 					InitDataStoreHash ();
-				LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash [name];
-				if (slot != null) {
+
+				if (datastorehash.ContainsKey (name)) {
 					// This exception isnt documented (of
 					// course) but .net throws it
 					throw new ArgumentException("Named data slot already added");
 				}
 			
-				slot = AllocateDataSlot ();
+				var slot = AllocateDataSlot ();
 
 				datastorehash.Add (name, slot);
 
@@ -357,10 +357,9 @@ namespace System.Threading {
 			lock (datastore_lock) {
 				if (datastorehash == null)
 					InitDataStoreHash ();
-				LocalDataStoreSlot slot = (LocalDataStoreSlot)datastorehash [name];
 
-				if (slot != null) {
-					datastorehash.Remove (slot);
+				if (datastorehash.ContainsKey (name)) {
+					datastorehash.Remove (name);
 				}
 			}
 		}
@@ -401,13 +400,13 @@ namespace System.Threading {
 			lock (datastore_lock) {
 				if (datastorehash == null)
 					InitDataStoreHash ();
-				LocalDataStoreSlot slot=(LocalDataStoreSlot)datastorehash[name];
 
-				if(slot==null) {
+				LocalDataStoreSlot slot;
+				if (!datastorehash.TryGetValue (name, out slot)) {
 					slot=AllocateNamedDataSlot(name);
 				}
 			
-				return(slot);
+				return slot;
 			}
 		}
 		

+ 14 - 16
mcs/class/corlib/System/AppDomain.cs

@@ -33,7 +33,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-using System.Collections;
 using System.Globalization;
 using System.IO;
 using System.Reflection;
@@ -78,13 +77,13 @@ namespace System {
 		static string _process_guid;
 
 		[ThreadStatic]
-		static Hashtable type_resolve_in_progress;
+		static Dictionary<string, object> type_resolve_in_progress;
 
 		[ThreadStatic]
-		static Hashtable assembly_resolve_in_progress;
+		static Dictionary<string, object> assembly_resolve_in_progress;
 
 		[ThreadStatic]
-		static Hashtable assembly_resolve_in_progress_refonly;
+		static Dictionary<string, object> assembly_resolve_in_progress_refonly;
 		// CAS
 		private Evidence _evidence;
 		private PermissionSet _granted;
@@ -1236,25 +1235,25 @@ namespace System {
 				return null;
 			
 			/* Prevent infinite recursion */
-			Hashtable ht;
+			Dictionary<string, object> ht;
 			if (refonly) {
 				ht = assembly_resolve_in_progress_refonly;
 				if (ht == null) {
-					ht = new Hashtable ();
+					ht = new Dictionary<string, object> ();
 					assembly_resolve_in_progress_refonly = ht;
 				}
 			} else {
 				ht = assembly_resolve_in_progress;
 				if (ht == null) {
-					ht = new Hashtable ();
+					ht = new Dictionary<string, object> ();
 					assembly_resolve_in_progress = ht;
 				}
 			}
 
-			string s = (string) ht [name];
-			if (s != null)
+			if (ht.ContainsKey (name))
 				return null;
-			ht [name] = name;
+
+			ht [name] = null;
 			try {
 				Delegate[] invocation_list = del.GetInvocationList ();
 
@@ -1286,16 +1285,15 @@ namespace System {
 				name = (string) name_or_tb;
 
 			/* Prevent infinite recursion */
-			Hashtable ht = type_resolve_in_progress;
+			var ht = type_resolve_in_progress;
 			if (ht == null) {
-				ht = new Hashtable ();
-				type_resolve_in_progress = ht;
+				type_resolve_in_progress = ht = new Dictionary<string, object> ();
 			}
 
-			if (ht.Contains (name))
+			if (ht.ContainsKey (name))
 				return null;
-			else
-				ht [name] = name;
+
+			ht [name] = null;
 
 			try {
 				foreach (Delegate d in TypeResolve.GetInvocationList ()) {

+ 16 - 22
mcs/class/corlib/System/Enum.cs

@@ -34,6 +34,7 @@
 //
 
 using System.Collections;
+using System.Collections.Generic;
 using System.Globalization;
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
@@ -45,10 +46,10 @@ namespace System
 		internal Type utype;
 		internal Array values;
 		internal string[] names;
-		internal Hashtable name_hash;
+		internal Dictionary<string, int> name_hash;
 		[ThreadStatic]
-		static Hashtable cache;
-		static Hashtable global_cache = new Hashtable ();
+		static Dictionary<Type, MonoEnumInfo> cache;
+		static readonly Dictionary<Type, MonoEnumInfo> global_cache = new Dictionary<Type, MonoEnumInfo> ();
 		static object global_cache_monitor = new object ();
 		
 		[MethodImplAttribute (MethodImplOptions.InternalCall)]
@@ -146,14 +147,6 @@ namespace System
 			}
 		}
 
-		static Hashtable Cache {
-			get {
-				if (cache == null) {
-					cache = new Hashtable ();
-				}
-				return cache;
-			}
-		}
 		private MonoEnumInfo (MonoEnumInfo other)
 		{
 			utype = other.utype;
@@ -165,16 +158,17 @@ namespace System
 		internal static void GetInfo (Type enumType, out MonoEnumInfo info)
 		{
 			/* First check the thread-local cache without locking */
-			if (Cache.ContainsKey (enumType)) {
-				info = (MonoEnumInfo) cache [enumType];
+			if (cache != null && cache.TryGetValue (enumType, out info)) {
 				return;
 			}
+
 			/* Threads could die, so keep a global cache too */
 			lock (global_cache_monitor) {
-				if (global_cache.ContainsKey (enumType)) {
-					object boxedInfo = global_cache [enumType];
-					cache [enumType] = boxedInfo;
-					info = (MonoEnumInfo)boxedInfo;
+				if (global_cache.TryGetValue (enumType, out info)) {
+					if (cache == null)
+						cache = new Dictionary<Type, MonoEnumInfo> ();
+
+					cache [enumType] = info;
 					return;
 				}
 			}
@@ -185,7 +179,7 @@ namespace System
 			SortEnums (et, info.values, info.names);
 			
 			if (info.names.Length > 50) {
-				info.name_hash = new Hashtable (info.names.Length);
+				info.name_hash = new Dictionary<string, int> (info.names.Length);
 				for (int i = 0; i <  info.names.Length; ++i)
 					info.name_hash [info.names [i]] = i;
 			}
@@ -459,14 +453,14 @@ namespace System
 			return Parse (enumType, value, false);
 		}
 
-		private static int FindName (Hashtable name_hash, string [] names, string name,  bool ignoreCase)
+		private static int FindName (IDictionary<string, int> name_hash, string [] names, string name,  bool ignoreCase)
 		{
 			if (!ignoreCase) {
 				/* For enums with many values, use a hash table */
 				if (name_hash != null) {
-					object val = name_hash [name];
-					if (val != null)
-						return (int)val;
+					int val;
+					if (name_hash.TryGetValue (name, out val))
+						return val;
 				} else {
 					for (int i = 0; i < names.Length; ++i) {
 						if (name == names [i])

+ 2 - 1
mcs/class/corlib/System/Exception.cs

@@ -29,6 +29,7 @@
 //
 
 using System.Collections;
+using System.Collections.Generic;
 using System.Diagnostics;
 using System.Reflection;
 using System.Text;
@@ -269,7 +270,7 @@ namespace System
 			get {
 				if (_data == null) {
 					// default to empty dictionary
-					_data = (IDictionary) new Hashtable ();
+					_data = new Dictionary<object, object> ();
 				}
 				return _data;
 			}

+ 5 - 6
mcs/class/corlib/System/MonoCustomAttrs.cs

@@ -186,10 +186,9 @@ namespace System
 				}
 			}
 
-			int initialSize = res.Length < 16 ? res.Length : 16;
-
-			Hashtable attributeInfos = new Hashtable (initialSize);
-			ArrayList a = new ArrayList (initialSize);
+			var initialSize = Math.Max (res.Length, 16);
+			var attributeInfos = new Dictionary<Type, AttributeInfo> (initialSize);
+			var a = new List<object> (initialSize);
 			ICustomAttributeProvider btype = obj;
 
 			int inheritanceLevel = 0;
@@ -211,8 +210,8 @@ namespace System
 						}
 					}
 
-					AttributeInfo firstAttribute = (AttributeInfo) attributeInfos[attrType];
-					if (firstAttribute != null)
+					AttributeInfo firstAttribute;
+					if (attributeInfos.TryGetValue (attrType, out firstAttribute))
 					{
 						usage = firstAttribute.Usage;
 					}

+ 4 - 4
mcs/class/corlib/System/TimeZone.cs

@@ -40,7 +40,7 @@
 //
 //    Rewrite ToUniversalTime to use a similar setup to that
 //
-using System.Collections;
+using System.Collections.Generic;
 using System.Globalization;
 using System.Runtime.CompilerServices;
 using System.Runtime.Serialization;
@@ -241,7 +241,7 @@ namespace System
 		private string m_daylightName;
 
 		// A yearwise cache of DaylightTime.
-		private Hashtable m_CachedDaylightChanges = new Hashtable (1);
+		private Dictionary<int, DaylightTime> m_CachedDaylightChanges = new Dictionary<int, DaylightTime> (1);
 
 		// the offset when daylightsaving is not on (in ticks)
 		private long m_ticksOffset;
@@ -337,8 +337,8 @@ namespace System
 				return this_year_dlt;
 			
 			lock (m_CachedDaylightChanges) {
-				DaylightTime dlt = (DaylightTime) m_CachedDaylightChanges [year];
-				if (dlt == null) {
+				DaylightTime dlt;
+				if (!m_CachedDaylightChanges.TryGetValue (year, out dlt)) {
 					Int64[] data;
 					string[] names;