Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Make it easier to iterate through an ArraySegment (#8559)
Browse files Browse the repository at this point in the history
* Make it easier to iterate through an ArraySegment. See Make it easier to iterate through an ArraySegment
  • Loading branch information
AlexRadch authored and jkotas committed Dec 9, 2016
1 parent bc8a092 commit 2813ef4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
8 changes: 8 additions & 0 deletions src/mscorlib/model.xml
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,21 @@
<Member Name="get_Array" />
<Member Name="get_Count" />
<Member Name="get_Offset" />
<Member Name="GetEnumerator" />
<Member Name="GetHashCode" />
<Member Name="op_Equality(System.ArraySegment&lt;T&gt;,System.ArraySegment&lt;T&gt;)" />
<Member Name="op_Inequality(System.ArraySegment&lt;T&gt;,System.ArraySegment&lt;T&gt;)" />
<Member MemberType="Property" Name="Array" />
<Member MemberType="Property" Name="Count" />
<Member MemberType="Property" Name="Offset" />
</Type>
<Type Name="System.ArraySegment&lt;T&gt;+Enumerator">
<Member Name="Dispose" />
<Member Name="get_Current" />
<Member Name="MoveNext" />
<Member MemberType="Property" Name="Current" />
<Member Name="System.Collections.IEnumerator.Reset" />
</Type>
<Type Name="System.ArrayTypeMismatchException">
<Member Name="#ctor" />
<Member Name="#ctor(System.String)" />
Expand Down
71 changes: 32 additions & 39 deletions src/mscorlib/src/System/ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ namespace System
[Serializable]
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)
{
if (array == null)
Expand Down Expand Up @@ -67,7 +67,7 @@ public T[] Array
Contract.Assert( (null == _array && 0 == _offset && 0 == _count)
|| (null != _array && _offset >= 0 && _count >= 0 && _offset + _count <= _array.Length),
"ArraySegment is invalid");

return _array;
}
}
Expand Down Expand Up @@ -109,7 +109,16 @@ public int Count
return _count;
}
}


public Enumerator GetEnumerator()
{
if (_array == null)
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
Contract.EndContractBlock();

return new Enumerator(this);
}

public override int GetHashCode()
{
if (_array == null)
Expand Down Expand Up @@ -184,7 +193,7 @@ int IList<T>.IndexOf(T item)

int index = System.Array.IndexOf<T>(_array, item, _offset, _count);

Contract.Assert(index == -1 ||
Contract.Assert(index == -1 ||
(index >= _offset && index < _offset + _count));

return index >= 0 ? index - _offset : -1;
Expand Down Expand Up @@ -269,46 +278,34 @@ bool ICollection<T>.Remove(T item)
#endregion

#region IEnumerable<T>
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
if (_array == null)
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
Contract.EndContractBlock();

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

#region IEnumerable
IEnumerator IEnumerable.GetEnumerator()
{
if (_array == null)
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
Contract.EndContractBlock();

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

[Serializable]
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)
{
Contract.Requires(arraySegment.Array != null);
Contract.Requires(arraySegment.Offset >= 0);
Contract.Requires(arraySegment.Count >= 0);
Contract.Requires(arraySegment.Offset + arraySegment.Count <= arraySegment.Array.Length);

_array = arraySegment._array;
_start = arraySegment._offset;
_end = _start + arraySegment._count;
_current = _start - 1;
_array = arraySegment.Array;
_start = arraySegment.Offset;
_end = arraySegment.Offset + arraySegment.Count;
_current = arraySegment.Offset - 1;
}

public bool MoveNext()
Expand All @@ -325,19 +322,15 @@ public T Current
{
get
{
if (_current < _start) ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumNotStarted();
if (_current >= _end) ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumEnded();
if (_current < _start)
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumNotStarted();
if (_current >= _end)
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumEnded();
return _array[_current];
}
}

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

void IEnumerator.Reset()
{
Expand Down

0 comments on commit 2813ef4

Please sign in to comment.