System/PCL: Implement HttpWebRequest.SupportsCookieContainer, WebRequest.CreateHttp...
[mono.git] / mcs / class / System / System.Collections.Generic / SortedList.cs
index 9ffa2505ac9826c9152255c9cbca3006b4111199..5ee7bfb94eebc42654b120cef9e0bae72ee06b1b 100644 (file)
@@ -169,10 +169,10 @@ namespace System.Collections.Generic
 
                object IDictionary.this [object key] {
                        get {
-                               if (!(key is TKey))
-                                       return null;
-                               else
-                                       return this [(TKey)key];
+                               TValue obj;
+                               if (key is TKey && TryGetValue ((TKey)key, out obj))
+                                       return obj;
+                               return null;
                        }
 
                        set {
@@ -305,16 +305,12 @@ namespace System.Collections.Generic
 
                void ICollection<KeyValuePair<TKey, TValue>>.Clear () 
                {
-                       defaultCapacity = INITIAL_SIZE;
-                       this.table = new KeyValuePair<TKey, TValue> [defaultCapacity];
-                       inUse = 0;
-                       modificationCount++;
+                       Clear ();
                }
 
                public void Clear () 
                {
-                       defaultCapacity = INITIAL_SIZE;
-                       this.table = new KeyValuePair<TKey, TValue> [defaultCapacity];
+                       Array.Clear (table, 0, table.Length);
                        inUse = 0;
                        modificationCount++;
                }
@@ -468,12 +464,7 @@ namespace System.Collections.Generic
                        if (key == null)
                                throw new ArgumentNullException ("key");
 
-                       int indx = 0;
-                       try {
-                               indx = Find (key);
-                       } catch (Exception) {
-                               throw new InvalidOperationException();
-                       }
+                       int indx = Find (key);
 
                        return (indx | (indx >> 31));
                }
@@ -563,13 +554,7 @@ namespace System.Collections.Generic
 
                        KeyValuePair<TKey, TValue> [] table = this.table;
 
-                       int freeIndx = -1;
-
-                       try {
-                               freeIndx = Find (key);
-                       } catch (Exception) {
-                               throw new InvalidOperationException();
-                       }
+                       int freeIndx = Find (key);
 
                        if (freeIndx >= 0) {
                                if (!overwrite)
@@ -624,6 +609,15 @@ namespace System.Collections.Generic
                        }
                }
 
+               private int Compare (TKey a, TKey b)
+               {
+                       try {
+                               return comparer.Compare (a, b);
+                       } catch (Exception ex) {
+                               throw new InvalidOperationException ("Failed to compare two elements.", ex);
+                       }
+               }
+
                private int Find (TKey key)
                {
                        KeyValuePair<TKey, TValue> [] table = this.table;
@@ -635,9 +629,9 @@ namespace System.Collections.Generic
                        int right = len-1;
 
                        while (left <= right) {
-                               int guess = (left + right) >> 1;
+                               int guess = left + ((right - left) >> 1);
 
-                               int cmp = comparer.Compare (table[guess].Key, key);
+                               int cmp = Compare (table[guess].Key, key);
                                if (cmp == 0) return guess;
 
                                if (cmp <  0) left = guess+1;