// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-#if NET_2_0
-
using System;
using System.Collections;
using System.Globalization;
using System.Runtime.InteropServices;
+using System.Diagnostics;
namespace System.Collections.Generic
{
/// </summary>
[Serializable]
[ComVisible(false)]
+ [DebuggerDisplay ("Count={Count}")]
+ [DebuggerTypeProxy (typeof (CollectionDebuggerView<,>))]
public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>,
IDictionary,
ICollection,
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 {
Array.Copy (table, newTable, inUse);
this.table = newTable;
}
-#if NET_1_0
- else if (current > defaultCapacity && value < current) {
- KeyValuePair<TKey, TValue> [] newTable = new KeyValuePair<TKey, TValue> [defaultCapacity];
- Array.Copy (table, newTable, inUse);
- this.table = newTable;
- }
-#endif
else if (value > inUse) {
KeyValuePair<TKey, TValue> [] newTable = new KeyValuePair<TKey, TValue> [value];
Array.Copy (table, newTable, inUse);
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++;
}
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));
}
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)
}
}
+ 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;
int right = len-1;
while (left <= right) {
- int guess = (left + right) >> 1;
+ int guess = left + ((right - left) >> 1);
- int cmp = comparer.Compare (key, table[guess].Key);
+ int cmp = Compare (table[guess].Key, key);
if (cmp == 0) return guess;
- if (cmp > 0) left = guess+1;
+ if (cmp < 0) left = guess+1;
else right = guess-1;
}
}
[Serializable]
- public struct KeyEnumerator : IEnumerator <TKey>, IDisposable {
+ struct KeyEnumerator : IEnumerator <TKey>, IDisposable {
const int NOT_STARTED = -2;
// this MUST be -1, because we depend on it in move next.
}
[Serializable]
- public struct ValueEnumerator : IEnumerator <TValue>, IDisposable {
+ struct ValueEnumerator : IEnumerator <TValue>, IDisposable {
const int NOT_STARTED = -2;
// this MUST be -1, because we depend on it in move next.
} // SortedList
} // System.Collections.Generic
-
-#endif