This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Make it easier to iterate through an ArraySegment #8559
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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; | ||
} | ||
} | ||
|
@@ -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) | ||
|
@@ -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; | ||
|
@@ -269,54 +278,42 @@ 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 int _current; | ||
private readonly T[] _array; | ||
private readonly int _offset; | ||
private readonly int _endIndex; // cache Offset + Count, since it's a little slow | ||
private int _index; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep the original names the start with underscore - it matches the coding conventions. |
||
|
||
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; | ||
_offset = arraySegment.Offset; | ||
_endIndex = arraySegment.Offset + arraySegment.Count; | ||
_index = arraySegment.Offset - 1; | ||
} | ||
|
||
public bool MoveNext() | ||
{ | ||
if (_current < _end) | ||
if (_index < _endIndex) | ||
{ | ||
_current++; | ||
return (_current < _end); | ||
_index++; | ||
return (_index < _endIndex); | ||
} | ||
return false; | ||
} | ||
|
@@ -325,23 +322,19 @@ public T Current | |
{ | ||
get | ||
{ | ||
if (_current < _start) ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumNotStarted(); | ||
if (_current >= _end) ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumEnded(); | ||
return _array[_current]; | ||
if (_index < _offset) | ||
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumNotStarted(); | ||
if (_index >= _endIndex) | ||
ThrowHelper.ThrowInvalidOperationException_InvalidOperation_EnumEnded(); | ||
return _array[_index]; | ||
} | ||
} | ||
|
||
object IEnumerator.Current | ||
{ | ||
get | ||
{ | ||
return Current; | ||
} | ||
} | ||
object IEnumerator.Current => Current; | ||
|
||
void IEnumerator.Reset() | ||
{ | ||
_current = _start - 1; | ||
_index = _offset - 1; | ||
} | ||
|
||
public void Dispose() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think that the original names
_start
,_end
,_current
were better.