Author: juraj
Date: 2007-06-20 14:00:23 -0400 (Wed, 20 Jun 2007)
New Revision: 80341
Modified:
trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog
trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs
trunk/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs
Log:
2007-06-20 Juraj Skripsky <[EMAIL PROTECTED]>
* Dictionary.cs: Optimize and add implementation notes. Use a "mini-heap"
to store the linked lists instead of the regular heap. Split arrays of
structs into separate arrays to make the GC's life simpler (see bug 81879
for details). Make class serialization compatible with MS.NET.
* EqualityComparer.cs: Rename IEquatableOfTEqualityComparer<T> to
GenericEqualityComparer<T> to fix binary serialization compatibility
with MS.
Modified: trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog 2007-06-20
17:25:25 UTC (rev 80340)
+++ trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog 2007-06-20
18:00:23 UTC (rev 80341)
@@ -1,3 +1,14 @@
+2007-06-20 Juraj Skripsky <[EMAIL PROTECTED]>
+
+ * Dictionary.cs: Optimize and add implementation notes. Use a "mini-heap"
+ to store the linked lists instead of the regular heap. Split arrays of
+ structs into separate arrays to make the GC's life simpler (see bug 81879
+ for details). Make class serialization compatible with MS.NET.
+
+ * EqualityComparer.cs: Rename IEquatableOfTEqualityComparer<T> to
+ GenericEqualityComparer<T> to fix binary serialization compatibility
+ with MS.
+
2006-04-16 Jonathan Chambers <[EMAIL PROTECTED]>
* List.cs: Clear entry in RemoveAt after removing item.
Modified: trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs
===================================================================
--- trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs
2007-06-20 17:25:25 UTC (rev 80340)
+++ trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs
2007-06-20 18:00:23 UTC (rev 80341)
@@ -6,10 +6,12 @@
// Marek Safar ([EMAIL PROTECTED]) (stubs)
// Ankit Jain ([EMAIL PROTECTED])
// David Waite ([EMAIL PROTECTED])
+// Juraj Skripsky ([EMAIL PROTECTED])
//
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
// Copyright (C) 2005 David Waite
+// Copyright (C) 2007 HotFeet GmbH (http://www.hotfeet.ch)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
@@ -41,7 +43,6 @@
using System.Runtime.InteropServices;
namespace System.Collections.Generic {
-
[ComVisible(true)]
[Serializable]
public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>,
@@ -52,80 +53,152 @@
ISerializable,
IDeserializationCallback
{
+ // The implementation of this class uses a hash table and
linked lists
+ // (see:
http://msdn2.microsoft.com/en-us/library/ms379571(VS.80).aspx).
+ //
+ // We use a kind of "mini-heap" instead of reference-based
linked lists:
+ // "keySlots" and "valueSlots" is the heap itself, it stores
the data
+ // "linkSlots" contains information about how the slots in the
heap
+ // are connected into linked lists
+ // "touchedSlots" and "emptySlot" manage the free space in the
heap
+
const int INITIAL_SIZE = 10;
const float DEFAULT_LOAD_FACTOR = (90f / 100);
+ const int NO_SLOT = -1;
+
+ private struct Link {
+ public int HashCode;
+ public int Next;
+ }
- private class Slot {
- // Can't use KeyValuePair here since the JIT won't
inline the Key/Value accessors
- public TKey Key;
- public TValue Value;
- public Slot Next;
+ // The hash table contains indices into the linkSlots array
+ int [] table;
+
+ // All (key,value) pairs are chained into linked lists. The
connection
+ // information is stored in "linkSlots" along with the key's
hash code
+ // (for performance reasons).
+ // TODO: get rid of the hash code in Link (this depends on a few
+ // JIT-compiler optimizations)
+ // Every link in "linkSlots" corresponds to the (key,value) pair
+ // in "keySlots"/"valueSlots" with the same index.
+ Link [] linkSlots;
+ TKey [] keySlots;
+ TValue [] valueSlots;
- public Slot (TKey Key, TValue Value, Slot Next)
- {
- this.Key = Key;
- this.Value = Value;
- this.Next = Next;
- }
- }
+ // The number of slots in "linkSlots" and
"keySlots"/"valueSlots" that
+ // are in use (i.e. filled with data) or have been used and
marked as
+ // "empty" later on.
+ int touchedSlots;
+
+ // The index of the first slot in the "empty slots chain".
+ // "Remove()" prepends the cleared slots to the empty chain.
+ // "Add()" fills the first slot in the empty slots chain with
the
+ // added item (or increases "touchedSlots" if the chain itself
is empty).
+ int emptySlot;
- Slot [] table;
- int used_slots;
- private int threshold;
+ // The number of (key,value) pairs in this dictionary.
+ int count;
+
+ // The number of (key,value) pairs the dictionary can hold
without
+ // resizing the hash table and the slots arrays.
+ int threshold;
IEqualityComparer<TKey> hcp;
SerializationInfo serialization_info;
- private uint generation;
+ // The number of changes made to this dictionary. Used by
enumerators
+ // to detect changes and invalidate themselves.
+ int generation;
public int Count {
- get { return used_slots; }
- /* FIXME: this should be 'private' not 'internal'. */
- internal set {
- used_slots = value;
- ++generation;
- }
+ get { return count; }
}
- // This is perf critical so inline calls etc.
public TValue this [TKey key] {
get {
if (key == null)
throw new ArgumentNullException ("key");
- uint size = (uint)table.Length;
- uint i = ((uint)hcp.GetHashCode (key) &
Int32.MaxValue) % size;
- Slot slot = table [i];
- while (slot != null) {
+
+ // get first item of linked list corresponding
to given key
+ int hashCode = hcp.GetHashCode (key);
+ int cur = table [(hashCode & int.MaxValue) %
table.Length] - 1;
+
+ // walk linked list until right slot is found
or end is reached
+ while (cur != NO_SLOT) {
// The ordering is important for
compatibility with MS and strange
// Object.Equals () implementations
- if (hcp.Equals (slot.Key, key))
- return slot.Value;
- slot = slot.Next;
+ if (linkSlots [cur].HashCode ==
hashCode && hcp.Equals (keySlots [cur], key))
+ return valueSlots [cur];
+ cur = linkSlots [cur].Next;
}
throw new KeyNotFoundException ();
}
set {
- int index;
- Slot prev = GetPrev (key, out index);
- Slot slot = prev == null ? table [index] :
prev.Next;
- if (slot == null) {
- if (Count++ >= threshold) {
+ if (key == null)
+ throw new ArgumentNullException ("key");
+
+ // get first item of linked list corresponding
to given key
+ int hashCode = hcp.GetHashCode (key);
+ int index = (hashCode & int.MaxValue) %
table.Length;
+ int cur = table [index] - 1;
+
+ // walk linked list until right slot (and its
predecessor) is
+ // found or end is reached
+ int prev = NO_SLOT;
+ if (cur != NO_SLOT) {
+ do {
+ // The ordering is important
for compatibility with MS and strange
+ // Object.Equals ()
implementations
+ if (linkSlots [cur].HashCode ==
hashCode && hcp.Equals (keySlots [cur], key))
+ break;
+ prev = cur;
+ cur = linkSlots [cur].Next;
+ } while (cur != NO_SLOT);
+ }
+
+ // is there no slot for the given key yet?
+ if (cur == NO_SLOT) {
+ // there is no existing slot for the
given key,
+ // allocate one and prepend it to its
corresponding linked
+ // list
+
+ if (++count > threshold) {
Resize ();
- index = DoHash (key);
+ index = (hashCode &
int.MaxValue) % table.Length;
}
- table [index] = new Slot (key, value,
table [index]);
+
+ // find an empty slot
+ cur = emptySlot;
+ if (cur == NO_SLOT)
+ cur = touchedSlots++;
+ else
+ emptySlot = linkSlots
[cur].Next;
+
+ // prepend the added item to its linked
list,
+ // update the hash table
+ linkSlots [cur].Next = table [index] -
1;
+ table [index] = cur + 1;
} else {
- ++generation;
- if (prev != null) {
- // move-to-front on update
- prev.Next = slot.Next;
- slot.Next = table [index];
- table [index] = slot;
+ // we already have a slot for the given
key,
+ // update the existing slot
+
+ // if the slot is not at the front of
its linked list,
+ // we move it there
+ if (prev != NO_SLOT) {
+ linkSlots [prev].Next =
linkSlots [cur].Next;
+ linkSlots [cur].Next = table
[index] - 1;
+ table [index] = cur + 1;
}
- slot.Key = key;
- slot.Value = value;
}
+
+ // store the hash code of the new item and
+ // the item's data itself
+ linkSlots [cur].HashCode = hashCode;
+ keySlots [cur] = key;
+ valueSlots [cur] = value;
+
+ generation++;
}
}
@@ -169,13 +242,6 @@
serialization_info = info;
}
- void SetThreshold ()
- {
- threshold = (int)(table.Length * DEFAULT_LOAD_FACTOR);
- if (threshold == 0 && table.Length > 0)
- threshold = 1;
- }
-
private void Init (int capacity, IEqualityComparer<TKey> hcp)
{
if (capacity < 0)
@@ -187,11 +253,25 @@
/* Modify capacity so 'capacity' elements can be added
without resizing */
capacity = (int)(capacity / DEFAULT_LOAD_FACTOR) + 1;
- table = new Slot [capacity];
- SetThreshold ();
+ InitArrays (capacity);
generation = 0;
}
+
+ private void InitArrays (int size) {
+ table = new int [size];
+ linkSlots = new Link [size];
+ emptySlot = NO_SLOT;
+
+ keySlots = new TKey [size];
+ valueSlots = new TValue [size];
+ touchedSlots = 0;
+
+ threshold = (int)(table.Length * DEFAULT_LOAD_FACTOR);
+ if (threshold == 0 && table.Length > 0)
+ threshold = 1;
+ }
+
void CopyTo (KeyValuePair<TKey, TValue> [] array, int index)
{
if (array == null)
@@ -204,89 +284,142 @@
if (array.Length - index < Count)
throw new ArgumentException ("Destination array
cannot hold the requested elements!");
- for (int i = 0; i < table.Length; ++i) {
- for (Slot slot = table [i]; slot != null; slot
= slot.Next)
- array [index++] = new
KeyValuePair<TKey, TValue> (slot.Key, slot.Value);
+ for (int i = 0; i < table.Length; i++) {
+ int cur = table [i] - 1;
+ while (cur != NO_SLOT) {
+ array [index++] = new
KeyValuePair<TKey, TValue> (keySlots [cur], valueSlots [cur]);
+ cur = linkSlots [cur].Next;
+ }
}
}
-
+
private void Resize ()
{
// From the SDK docs:
// Hashtable is automatically increased
// to the smallest prime number that is larger
// than twice the current number of Hashtable
buckets
- uint newSize = (uint) Hashtable.ToPrime ((table.Length
<< 1) | 1);
+ int newSize = Hashtable.ToPrime ((table.Length << 1) |
1);
- Slot nextslot = null;
- Slot [] oldTable = table;
+ // allocate new hash table and link slots array
+ int [] newTable = new int [newSize];
+ Link [] newLinkSlots = new Link [newSize];
- table = new Slot [newSize];
- SetThreshold ();
+ for (int i = 0; i < table.Length; i++) {
+ int cur = table [i] - 1;
+ while (cur != NO_SLOT) {
+ int hashCode = newLinkSlots
[cur].HashCode = hcp.GetHashCode(keySlots [cur]);
+ int index = (hashCode & int.MaxValue) %
newSize;
+ newLinkSlots [cur].Next = newTable
[index] - 1;
+ newTable [index] = cur + 1;
+ cur = linkSlots [cur].Next;
+ }
+ }
+ table = newTable;
+ linkSlots = newLinkSlots;
- int index;
- for (int i = 0; i < oldTable.Length; i++) {
- for (Slot slot = oldTable [i]; slot != null;
slot = nextslot) {
- nextslot = slot.Next;
+ // allocate new data slots, copy data
+ TKey [] newKeySlots = new TKey [newSize];
+ TValue [] newValueSlots = new TValue [newSize];
+ Array.Copy (keySlots, 0, newKeySlots, 0, touchedSlots);
+ Array.Copy (valueSlots, 0, newValueSlots, 0,
touchedSlots);
+ keySlots = newKeySlots;
+ valueSlots = newValueSlots;
- index = DoHash (slot.Key);
- slot.Next = table [index];
- table [index] = slot;
- }
- }
+ threshold = (int)(newSize * DEFAULT_LOAD_FACTOR);
}
-
+
public void Add (TKey key, TValue value)
{
- int index;
- Slot slot = GetSlot (key, out index);
- if (slot != null)
- throw new ArgumentException ("An element with
the same key already exists in the dictionary.");
- DoAdd (index, key, value);
- }
+ if (key == null)
+ throw new ArgumentNullException ("key");
- void DoAdd (int index, TKey key, TValue value)
- {
- if (Count++ >= threshold) {
+ // get first item of linked list corresponding to given
key
+ int hashCode = hcp.GetHashCode (key);
+ int index = (hashCode & int.MaxValue) % table.Length;
+ int cur = table [index] - 1;
+
+ // walk linked list until end is reached (throw an
exception if a
+ // existing slot is found having an equivalent key)
+ while (cur != NO_SLOT) {
+ // The ordering is important for compatibility
with MS and strange
+ // Object.Equals () implementations
+ if (linkSlots [cur].HashCode == hashCode &&
hcp.Equals (keySlots [cur], key))
+ throw new ArgumentException ("An
element with the same key already exists in the dictionary.");
+ cur = linkSlots [cur].Next;
+ }
+
+ if (++count > threshold) {
Resize ();
- index = DoHash (key);
+ index = (hashCode & int.MaxValue) %
table.Length;
}
- table [index] = new Slot (key, value, table [index]);
- }
+
+ // find an empty slot
+ cur = emptySlot;
+ if (cur == NO_SLOT)
+ cur = touchedSlots++;
+ else
+ emptySlot = linkSlots [cur].Next;
- private int DoHash (TKey key)
- {
- uint size = (uint)this.table.Length;
- uint h = (uint)hcp.GetHashCode (key) & Int32.MaxValue;
- int spot = (int) (h % size);
- return spot;
+ // store the hash code of the added item,
+ // prepend the added item to its linked list,
+ // update the hash table
+ linkSlots [cur].HashCode = hashCode;
+ linkSlots [cur].Next = table [index] - 1;
+ table [index] = cur + 1;
+
+ // store item's data
+ keySlots [cur] = key;
+ valueSlots [cur] = value;
+
+ generation++;
}
-
+
public IEqualityComparer<TKey> Comparer {
get { return hcp; }
}
public void Clear ()
{
- Count = 0;
- for (int i = 0; i < table.Length; i++)
- table [i] = null;
+ count = 0;
+ // clear the hash table
+ Array.Clear(table, 0, table.Length);
+
+ // empty the "empty slots chain"
+ emptySlot = NO_SLOT;
+
+ touchedSlots = 0;
+ generation++;
}
public bool ContainsKey (TKey key)
{
- int index;
- return GetSlot (key, out index) != null;
+ // get first item of linked list corresponding to given
key
+ int hashCode = hcp.GetHashCode (key);
+ int cur = table [(hashCode & int.MaxValue) %
table.Length] - 1;
+
+ // walk linked list until right slot is found or end is
reached
+ while (cur != NO_SLOT) {
+ // The ordering is important for compatibility
with MS and strange
+ // Object.Equals () implementations
+ if (linkSlots [cur].HashCode == hashCode &&
hcp.Equals (keySlots [cur], key))
+ return true;
+ cur = linkSlots [cur].Next;
+ }
+
+ return false;
}
public bool ContainsValue (TValue value)
{
IEqualityComparer<TValue> cmp =
EqualityComparer<TValue>.Default;
- for (int i = 0; i < table.Length; ++i) {
- for (Slot slot = table [i]; slot != null; slot
= slot.Next) {
- if (cmp.Equals (slot.Value, value))
+ for (int i = 0; i < table.Length; i++) {
+ int cur = table [i] - 1;
+ while (cur != NO_SLOT) {
+ if (cmp.Equals (valueSlots [cur],
value))
return true;
+ cur = linkSlots [cur].Next;
}
}
return false;
@@ -298,14 +431,15 @@
if (info == null)
throw new ArgumentNullException ("info");
- info.AddValue ("hcp", hcp);
+ info.AddValue ("Version", generation);
+ info.AddValue ("Comparer", hcp);
KeyValuePair<TKey, TValue> [] data = null;
- if (Count > 0) {
- data = new KeyValuePair<TKey,TValue> [Count];
+ if (count > 0) {
+ data = new KeyValuePair<TKey,TValue> [count];
CopyTo (data, 0);
}
- info.AddValue ("data", data);
- info.AddValue ("buckets_hint", table.Length);
+ info.AddValue ("HashSize", table.Length);
+ info.AddValue ("KeyValuePairs", data);
}
public virtual void OnDeserialization (object sender)
@@ -313,90 +447,98 @@
if (serialization_info == null)
return;
- hcp = (IEqualityComparer<TKey>)
serialization_info.GetValue ("hcp", typeof (IEqualityComparer<TKey>));
+ generation = serialization_info.GetInt32 ("Version");
+ hcp = (IEqualityComparer<TKey>)
serialization_info.GetValue ("Comparer", typeof (IEqualityComparer<TKey>));
+
+ int hashSize = serialization_info.GetInt32 ("HashSize");
KeyValuePair<TKey, TValue> [] data =
(KeyValuePair<TKey, TValue> [])
- serialization_info.GetValue ("data", typeof
(KeyValuePair<TKey, TValue> []));
+ serialization_info.GetValue ("KeyValuePairs",
typeof (KeyValuePair<TKey, TValue> []));
- int buckets = serialization_info.GetInt32
("buckets_hint");
- if (buckets < INITIAL_SIZE)
- buckets = INITIAL_SIZE;
+ if (hashSize < INITIAL_SIZE)
+ hashSize = INITIAL_SIZE;
+ InitArrays (hashSize);
+ count = 0;
- table = new Slot [buckets];
- SetThreshold ();
- Count = 0;
-
if (data != null) {
for (int i = 0; i < data.Length; ++i)
Add (data [i].Key, data [i].Value);
}
+ generation++;
serialization_info = null;
}
public bool Remove (TKey key)
{
- int index;
- Slot prev = GetPrev (key, out index);
- Slot slot = prev == null ? table [index] : prev.Next;
- if (slot == null)
+ if (key == null)
+ throw new ArgumentNullException ("key");
+
+ // get first item of linked list corresponding to given
key
+ int hashCode = hcp.GetHashCode (key);
+ int index = (hashCode & int.MaxValue) % table.Length;
+ int cur = table [index] - 1;
+
+ // if there is no linked list, return false
+ if (cur == NO_SLOT)
return false;
- --Count;
- if (prev == null)
- table [index] = slot.Next;
+
+ // walk linked list until right slot (and its
predecessor) is
+ // found or end is reached
+ int prev = NO_SLOT;
+ do {
+ // The ordering is important for compatibility
with MS and strange
+ // Object.Equals () implementations
+ if (linkSlots [cur].HashCode == hashCode &&
hcp.Equals (keySlots [cur], key))
+ break;
+ prev = cur;
+ cur = linkSlots [cur].Next;
+ } while (cur != NO_SLOT);
+
+ // if we reached the end of the chain, return false
+ if (cur == NO_SLOT)
+ return false;
+
+ count--;
+ // remove slot from linked list
+ // is slot at beginning of linked list?
+ if (prev == NO_SLOT)
+ table [index] = linkSlots [cur].Next + 1;
else
- prev.Next = slot.Next;
+ linkSlots [prev].Next = linkSlots [cur].Next;
+
+ // mark slot as empty and prepend it to "empty slots
chain"
+ linkSlots [cur].Next = emptySlot;
+ emptySlot = cur;
+
+ generation++;
return true;
}
- //
- // Return the predecessor of the slot containing 'key', and set
'index' to the
- // chain the key was found in.
- // If the key is not found, return null and set 'index' to the
chain that would've
- // contained the key.
- //
- private Slot GetPrev (TKey key, out int index)
+ public bool TryGetValue (TKey key, out TValue value)
{
- // This method is perf critical so inline DoHash () etc.
if (key == null)
throw new ArgumentNullException ("key");
- uint size = (uint)table.Length;
- // Use uint to help ABCREM
- uint i = ((uint)hcp.GetHashCode (key) & Int32.MaxValue)
% size;
- Slot slot = table [i];
- if (slot != null) {
- Slot prev = null;
- do {
- // The ordering is important for
compatibility with MS and strange
- // Object.Equals () implementations
- if (hcp.Equals (slot.Key, key))
- break;
- prev = slot;
- slot = slot.Next;
- } while (slot != null);
- index = (int)i;
- return prev;
+
+ // get first item of linked list corresponding to given
key
+ int hashCode = hcp.GetHashCode (key);
+ int cur = table [(hashCode & int.MaxValue) %
table.Length] - 1;
+
+ // walk linked list until right slot is found or end is
reached
+ while (cur != NO_SLOT) {
+ // The ordering is important for compatibility
with MS and strange
+ // Object.Equals () implementations
+ if (linkSlots [cur].HashCode == hashCode &&
hcp.Equals (keySlots [cur], key)) {
+ value = valueSlots [cur];
+ return true;
+ }
+ cur = linkSlots [cur].Next;
}
- else {
- index = (int)i;
- return null;
- }
- }
- private Slot GetSlot (TKey key, out int index)
- {
- Slot prev = GetPrev (key, out index);
- return prev == null ? table [index] : prev.Next;
+ // we did not find the slot
+ value = default (TValue);
+ return false;
}
- public bool TryGetValue (TKey key, out TValue value)
- {
- int index;
- Slot slot = GetSlot (key, out index);
- bool found = slot != null;
- value = found ? slot.Value : default (TValue);
- return found;
- }
-
ICollection<TKey> IDictionary<TKey, TValue>.Keys {
get { return Keys; }
}
@@ -517,12 +659,15 @@
// we want no exception for index==array.Length &&
Count == 0
if (index > array.Length)
throw new ArgumentException ("index larger than
largest valid index of array");
- if (array.Length - index < Count)
+ if (array.Length - index < count)
throw new ArgumentException ("Destination array
cannot hold the requested elements!");
- for (int i = 0; i < table.Length; ++i) {
- for (Slot slot = table [i]; slot != null; slot
= slot.Next)
- array.SetValue (new DictionaryEntry
(slot.Key, slot.Value), index++);
+ for (int i = 0; i < table.Length; i++) {
+ int cur = table [i] - 1;
+ while (cur != NO_SLOT) {
+ array.SetValue (new DictionaryEntry
(keySlots [cur], valueSlots [cur]), index++);
+ cur = linkSlots [cur].Next;
+ }
}
}
@@ -594,11 +739,10 @@
IDisposable, IDictionaryEnumerator, IEnumerator
{
Dictionary<TKey, TValue> dictionary;
- uint stamp;
+ int curTableItem;
+ int cur;
+ int stamp;
- Slot current;
- int next_index;
-
internal Enumerator (Dictionary<TKey, TValue>
dictionary)
{
this.dictionary = dictionary;
@@ -606,31 +750,51 @@
// The following stanza is identical to
IEnumerator.Reset (),
// but because of the definite assignment rule,
we cannot call it here.
- next_index = 0;
- current = null;
+ curTableItem = -1;
+ cur = NO_SLOT;
}
public bool MoveNext ()
{
VerifyState ();
+
+ do {
+ if (cur == NO_SLOT) {
+ //move to next item in table,
check if we reached the end
+ if (++curTableItem ==
dictionary.table.Length)
+ return false;
- // Pre-condition: current == null => this is
the first call to MoveNext ()
- if (current != null)
- current = current.Next;
-
- while (current == null && next_index <
dictionary.table.Length)
- current = dictionary.table
[next_index++];
-
- // Post-condition: current == null => this is
the last call to MoveNext()
- return current != null;
+ cur = dictionary.table
[curTableItem] - 1;
+ } else
+ cur = dictionary.linkSlots
[cur].Next;
+ } while (cur == NO_SLOT);
+
+ return true;
}
public KeyValuePair<TKey, TValue> Current {
get {
- Slot s = CurrentSlot ();
- return new KeyValuePair <TKey, TValue>
(s.Key, s.Value);
+ VerifyCurrent ();
+ return new KeyValuePair <TKey, TValue> (
+ dictionary.keySlots [cur],
+ dictionary.valueSlots [cur]
+ );
}
}
+
+ internal TKey CurrentKey {
+ get {
+ VerifyCurrent ();
+ return dictionary.keySlots [cur];
+ }
+ }
+
+ internal TValue CurrentValue {
+ get {
+ VerifyCurrent ();
+ return dictionary.valueSlots [cur];
+ }
+ }
object IEnumerator.Current {
get { return Current; }
@@ -638,23 +802,32 @@
void IEnumerator.Reset ()
{
- next_index = 0;
- current = null;
+ curTableItem = -1;
+ cur = NO_SLOT;
}
DictionaryEntry IDictionaryEnumerator.Entry {
get {
- Slot s = CurrentSlot ();
- return new DictionaryEntry (s.Key,
s.Value);
+ VerifyCurrent ();
+ return new DictionaryEntry (
+ dictionary.keySlots [cur],
+ dictionary.valueSlots [cur]
+ );
}
}
object IDictionaryEnumerator.Key {
- get { return Current.Key; }
+ get {
+ VerifyCurrent();
+ return dictionary.keySlots [cur];
+ }
}
object IDictionaryEnumerator.Value {
- get { return Current.Value; }
+ get {
+ VerifyCurrent();
+ return dictionary.valueSlots [cur];
+ }
}
void VerifyState ()
@@ -665,17 +838,15 @@
throw new InvalidOperationException
("out of sync");
}
- Slot CurrentSlot ()
+ void VerifyCurrent ()
{
VerifyState ();
- if (current == null)
+ if (cur == NO_SLOT)
throw new InvalidOperationException
("Current is not valid");
- return current;
}
public void Dispose ()
{
- current = null;
dictionary = null;
}
}
@@ -704,8 +875,13 @@
if (array.Length - index < dictionary.Count)
throw new ArgumentException
("Destination array cannot hold the requested elements!");
- foreach (TKey k in this)
- array [index++] = k;
+ for (int i = 0; i < dictionary.table.Length;
i++) {
+ int cur = dictionary.table [i] - 1;
+ while (cur != NO_SLOT) {
+ array [index++] =
dictionary.keySlots [cur];
+ cur = dictionary.linkSlots
[cur].Next;
+ }
+ }
}
public Enumerator GetEnumerator ()
@@ -784,11 +960,11 @@
}
public TKey Current {
- get { return
host_enumerator.Current.Key; }
+ get { return
host_enumerator.CurrentKey; }
}
object IEnumerator.Current {
- get { return
host_enumerator.Current.Key; }
+ get { return
host_enumerator.CurrentKey; }
}
void IEnumerator.Reset ()
@@ -822,8 +998,13 @@
if (array.Length - index < dictionary.Count)
throw new ArgumentException
("Destination array cannot hold the requested elements!");
- foreach (TValue k in this)
- array [index++] = k;
+ for (int i = 0; i < dictionary.table.Length;
i++) {
+ int cur = dictionary.table [i] - 1;
+ while (cur != NO_SLOT) {
+ array [index++] =
dictionary.valueSlots [cur];
+ cur = dictionary.linkSlots
[cur].Next;
+ }
+ }
}
public Enumerator GetEnumerator ()
@@ -902,11 +1083,11 @@
}
public TValue Current {
- get { return
host_enumerator.Current.Value; }
+ get { return
host_enumerator.CurrentValue; }
}
object IEnumerator.Current {
- get { return
host_enumerator.Current.Value; }
+ get { return
host_enumerator.CurrentValue; }
}
void IEnumerator.Reset ()
Modified: trunk/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs
===================================================================
--- trunk/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs
2007-06-20 17:25:25 UTC (rev 80340)
+++ trunk/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs
2007-06-20 18:00:23 UTC (rev 80341)
@@ -37,7 +37,7 @@
static EqualityComparer ()
{
if (typeof (IEquatable <T>).IsAssignableFrom (typeof
(T)))
- _default = (EqualityComparer <T>)
Activator.CreateInstance (typeof (IEquatableOfTEqualityComparer
<>).MakeGenericType (typeof (T)));
+ _default = (EqualityComparer <T>)
Activator.CreateInstance (typeof (GenericEqualityComparer <>).MakeGenericType
(typeof (T)));
else
_default = new DefaultComparer ();
}
@@ -83,7 +83,7 @@
}
[Serializable]
- class IEquatableOfTEqualityComparer <T> : EqualityComparer <T> where T
: IEquatable <T> {
+ class GenericEqualityComparer <T> : EqualityComparer <T> where T :
IEquatable <T> {
public override int GetHashCode (T obj)
{
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches