From 78b452b7acf985747d3d2813dedea3d267b08bae Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Thu, 16 Jul 2015 09:26:10 +0100 Subject: Remove the struct-based iterator for RepeatedField. We don't use it in the runtime or generated code anywhere now, so the extra small performance boost isn't as critical, and it has some undesirable consequences. The tests have needed to change as iterator block enumerators don't throw when we might expect them to. --- .../Collections/RepeatedFieldTest.cs | 6 --- .../ProtocolBuffers/Collections/RepeatedField.cs | 63 +++------------------- 2 files changed, 6 insertions(+), 63 deletions(-) diff --git a/csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs b/csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs index 6eff8683..25be7731 100644 --- a/csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs +++ b/csharp/src/ProtocolBuffers.Test/Collections/RepeatedFieldTest.cs @@ -241,18 +241,12 @@ namespace Google.Protobuf.Collections var list = new RepeatedField { "first", "second" }; using (var enumerator = list.GetEnumerator()) { - Assert.Throws(() => enumerator.Current.GetHashCode()); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual("first", enumerator.Current); Assert.IsTrue(enumerator.MoveNext()); Assert.AreEqual("second", enumerator.Current); Assert.IsFalse(enumerator.MoveNext()); - Assert.Throws(() => enumerator.Current.GetHashCode()); Assert.IsFalse(enumerator.MoveNext()); - enumerator.Reset(); - Assert.Throws(() => enumerator.Current.GetHashCode()); - Assert.IsTrue(enumerator.MoveNext()); - Assert.AreEqual("first", enumerator.Current); } } diff --git a/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs b/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs index a09c7954..8375ae0b 100644 --- a/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs +++ b/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs @@ -288,14 +288,12 @@ namespace Google.Protobuf.Collections } } - public RepeatedField.Enumerator GetEnumerator() + public IEnumerator GetEnumerator() { - return new Enumerator(this); - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); + for (int i = 0; i < count; i++) + { + yield return array[i]; + } } public override bool Equals(object obj) @@ -467,55 +465,6 @@ namespace Google.Protobuf.Collections } Remove((T)value); } - #endregion - - public struct Enumerator : IEnumerator - { - private int index; - private readonly RepeatedField field; - - public Enumerator(RepeatedField field) - { - this.field = field; - this.index = -1; - } - - public bool MoveNext() - { - if (index + 1 >= field.Count) - { - index = field.Count; - return false; - } - index++; - return true; - } - - public void Reset() - { - index = -1; - } - - public T Current - { - get - { - if (index == -1 || index >= field.count) - { - throw new InvalidOperationException(); - } - return field.array[index]; - } - } - - object IEnumerator.Current - { - get { return Current; } - } - - public void Dispose() - { - } - } + #endregion } } -- cgit v1.2.3