Hi, can anyone review this patch that align the behavior of Keys and Values Enumerator of SortedList<TKey,TValue> to the behavior of .NET (I tested it on VS C# Express).
Changes on UnitTest are also provided. Code is contributed under MIT/X11 license. Best Regards Torello Querci
Index: mcs/class/System/Test/System.Collections.Generic/SortedListTest.cs =================================================================== --- mcs/class/System/Test/System.Collections.Generic/SortedListTest.cs (revisione 125931) +++ mcs/class/System/Test/System.Collections.Generic/SortedListTest.cs (copia locale) @@ -275,6 +275,54 @@ Assert.AreEqual ("B", values [1]); Assert.AreEqual ("C", values [2]); } + + [Test] + [ExpectedException (typeof (InvalidOperationException))] + public void EnumeratorKeysInvalid () { + IEnumerator<int> en=list.Keys.GetEnumerator (); + bool toDelete=true; + while (en.MoveNext ()) { + if (toDelete) { + list.RemoveAt (1); + toDelete = false; + } + } + + } + + [Test] + public void EnumeratorKeysReset () { + IEnumerator<int> en=list.Keys.GetEnumerator (); + en.MoveNext (); + en.Reset (); + while (en.MoveNext ()) { + } + + } + + [Test] + [ExpectedException (typeof (InvalidOperationException))] + public void EnumeratorValuesInvalid () { + IEnumerator<string> en=list.Values.GetEnumerator (); + bool toDelete=true; + while (en.MoveNext ()) { + if (toDelete) { + list.RemoveAt (1); + toDelete = false; + } + } + + } + + [Test] + public void EnumeratorValuesReset () { + IEnumerator<string> en=list.Values.GetEnumerator (); + en.MoveNext (); + en.Reset (); + while (en.MoveNext ()) { + } + + } } } Index: mcs/class/System/System.Collections.Generic/SortedList.cs =================================================================== --- mcs/class/System/System.Collections.Generic/SortedList.cs (revisione 125931) +++ mcs/class/System/System.Collections.Generic/SortedList.cs (copia locale) @@ -680,16 +680,16 @@ // - private sealed class Enumerator : ICloneable, IDictionaryEnumerator, IEnumerator { + private class Enumerator : ICloneable, IDictionaryEnumerator, IEnumerator { - private SortedList<TKey, TValue>host; - private int stamp; - private int pos; - private int size; - private EnumeratorMode mode; + protected SortedList<TKey, TValue>host; + protected int stamp; + protected int pos; + protected int size; + protected EnumeratorMode mode; - private object currentKey; - private object currentValue; + protected object currentKey; + protected object currentValue; bool invalid = false; @@ -878,8 +878,7 @@ public virtual IEnumerator<TKey> GetEnumerator () { - for (int i = 0; i < host.Count; ++i) - yield return host.KeyAt (i); + return new KeysEnumerator(host); } // @@ -926,6 +925,54 @@ } } + private sealed class KeysEnumerator : Enumerator, IEnumerator<TKey> { + + public KeysEnumerator (SortedList<TKey, TValue> host) : base(host) + { + } + + TKey IEnumerator<TKey>.Current { + get { + return (TKey)currentKey; + } + } + + object IEnumerator.Current { + get { + return currentKey; + } + } + + public void Dispose () { + currentKey=default (TKey); + host=null; + } + } + + private sealed class ValuesEnumerator : Enumerator, IEnumerator<TValue> { + + public ValuesEnumerator (SortedList<TKey, TValue> host) : base(host) + { + } + + TValue IEnumerator<TValue>.Current { + get { + return (TValue)currentValue; + } + } + + object IEnumerator.Current { + get { + return currentValue; + } + } + + public void Dispose () { + currentKey=default (TKey); + host=null; + } + } + private class ListValues : IList<TValue>, ICollection, IEnumerable { private SortedList<TKey, TValue>host; @@ -1003,8 +1050,7 @@ public virtual IEnumerator<TValue> GetEnumerator () { - for (int i = 0; i < host.Count; ++i) - yield return host.ValueAt (i); + return new ValuesEnumerator(host); } //
_______________________________________________ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list