Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Make it easier to iterate through an ArraySegment (#2331)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexRadch authored and jkotas committed Dec 12, 2016
1 parent 2c75b67 commit c38bab3
Showing 1 changed file with 24 additions and 33 deletions.
57 changes: 24 additions & 33 deletions src/System.Private.CoreLib/src/System/ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ namespace System

public struct ArraySegment<T> : IList<T>, IReadOnlyList<T>
{
private T[] _array;
private int _offset;
private int _count;
private readonly T[] _array;
private readonly int _offset;
private readonly int _count;

public ArraySegment(T[] array)
{
Expand Down Expand Up @@ -110,6 +110,15 @@ public int Count
}
}

public Enumerator GetEnumerator()
{
if (_array == null)
throw new InvalidOperationException(SR.InvalidOperation_NullArray);
Contract.EndContractBlock();

return new Enumerator(this);
}

public override int GetHashCode()
{
if (_array == null)
Expand Down Expand Up @@ -266,35 +275,21 @@ bool ICollection<T>.Remove(T item)
#endregion

#region IEnumerable<T>
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
if (_array == null)
throw new InvalidOperationException(SR.InvalidOperation_NullArray);
Contract.EndContractBlock();

return new ArraySegmentEnumerator(this);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
#endregion

#region IEnumerable
IEnumerator IEnumerable.GetEnumerator()
{
if (_array == null)
throw new InvalidOperationException(SR.InvalidOperation_NullArray);
Contract.EndContractBlock();

return new ArraySegmentEnumerator(this);
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion

private sealed class ArraySegmentEnumerator : IEnumerator<T>
public struct Enumerator : IEnumerator<T>
{
private T[] _array;
private int _start;
private int _end;
private readonly T[] _array;
private readonly int _start;
private readonly int _end; // cache Offset + Count, since it's a little slow
private int _current;

internal ArraySegmentEnumerator(ArraySegment<T> arraySegment)
internal Enumerator(ArraySegment<T> arraySegment)
{
Debug.Assert(arraySegment.Array != null);
Debug.Assert(arraySegment.Offset >= 0);
Expand All @@ -321,19 +316,15 @@ public T Current
{
get
{
if (_current < _start) throw new InvalidOperationException(SR.InvalidOperation_EnumNotStarted);
if (_current >= _end) throw new InvalidOperationException(SR.InvalidOperation_EnumEnded);
if (_current < _start)
throw new InvalidOperationException(SR.InvalidOperation_EnumNotStarted);
if (_current >= _end)
throw new InvalidOperationException(SR.InvalidOperation_EnumEnded);
return _array[_current];
}
}

object IEnumerator.Current
{
get
{
return Current;
}
}
object IEnumerator.Current => Current;

void IEnumerator.Reset()
{
Expand Down

0 comments on commit c38bab3

Please sign in to comment.