diff --git a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/BufferSequence.cs b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/BufferSequence.cs index 0d080496df3..b2e05b00eb5 100644 --- a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/BufferSequence.cs +++ b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/BufferSequence.cs @@ -55,9 +55,9 @@ public bool TryGet(ref Position position, out Memory item, bool advance = return false; } - var sequence = position.GetItem(); - item = sequence.Memory.Slice(0, sequence._written); - if (advance) { position = Position.Create(sequence._next); } + var (buffer, index) = position.Get(); + item = buffer.Memory.Slice(index, buffer._written - index); + if (advance) { position = Position.Create(buffer._next); } return true; } @@ -69,9 +69,9 @@ public bool TryGet(ref Position position, out ReadOnlyMemory item, bool ad return false; } - var sequence = position.GetItem(); - item = sequence.WrittenMemory; - if (advance) { position = Position.Create(sequence._next); } + var (buffer, index) = position.Get(); + item = buffer.WrittenMemory.Slice(index); + if (advance) { position = Position.Create(buffer._next); } return true; } diff --git a/src/System.Buffers.Experimental/System/Buffers/Sequences/MemoryList.cs b/src/System.Buffers.Experimental/System/Buffers/Sequences/MemoryList.cs index c840379c775..eac6a3f2649 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Sequences/MemoryList.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Sequences/MemoryList.cs @@ -79,9 +79,9 @@ public bool TryGet(ref Position position, out Memory item, bool advance = return false; } - var sequence = position.GetItem(); - item = sequence._data; - if (advance) { position = Position.Create(sequence._next); } + var (list, index) = position.Get(); + item = list._data.Slice(index); + if (advance) { position = Position.Create(list._next); } return true; } diff --git a/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadOnlyBytes.cs b/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadOnlyBytes.cs index 9a6eac730b3..eb5c96bfde5 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadOnlyBytes.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadOnlyBytes.cs @@ -140,8 +140,8 @@ public ReadOnlyBytes Slice(Position position) switch (kind) { case Type.Array: - var array = position.GetItem(); - return new ReadOnlyBytes(array, position.Index, array.Length - position.Index); + var (array, index) = position.Get(); + return new ReadOnlyBytes(array, index, array.Length - index); case Type.MemoryList: return Slice(position, Position.Create((IMemoryList)_end, _endIndex)); default: throw new NotImplementedException(); @@ -154,12 +154,12 @@ public ReadOnlyBytes Slice(Position start, Position end) switch (kind) { case Type.Array: - var startArray = start.GetItem(); - return new ReadOnlyBytes(startArray, start.Index, end.Index - start.Index); + var (array, index) = start.Get(); + return new ReadOnlyBytes(array, index, (int)end - index); case Type.MemoryList: - var startList = start.GetItem>(); - var endList = end.GetItem>(); - return new ReadOnlyBytes(startList, start.Index, endList, end.Index); + var (startList, startIndex) = start.Get>(); + var (endList, endIndex) = end.Get>(); + return new ReadOnlyBytes(startList, startIndex, endList, endIndex); default: throw new NotImplementedException(); } @@ -281,8 +281,8 @@ public bool TryGet(ref Position position, out ReadOnlyMemory item, bool ad var array = _start as byte[]; if (array != null) { - var start = position.Index; - var length = _endIndex - position.Index; + var start = (int)position; + var length = _endIndex - (int)position; item = new ReadOnlyMemory(array, start, length); if (advance) position = default; return true; diff --git a/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadWriteBytes.cs b/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadWriteBytes.cs index f1dc601a533..33538ecda46 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadWriteBytes.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Sequences/ReadWriteBytes.cs @@ -136,8 +136,8 @@ public ReadWriteBytes Slice(Position position) switch (kind) { case Type.Array: - var array = position.GetItem(); - return new ReadWriteBytes(array, position.Index, array.Length - position.Index); + var (array, index) = position.Get(); + return new ReadWriteBytes(array, index, array.Length - index); case Type.MemoryList: return Slice(position, Position.Create((IMemoryList)_end, _endIndex)); default: throw new NotImplementedException(); @@ -150,12 +150,12 @@ public ReadWriteBytes Slice(Position start, Position end) switch (kind) { case Type.Array: - var startArray = start.GetItem(); - return new ReadWriteBytes(startArray, start.Index, end.Index - start.Index); + var (array, index) = start.Get(); + return new ReadWriteBytes(array, index, (int)end - index); case Type.MemoryList: - var startList = start.GetItem>(); - var endList = end.GetItem>(); - return new ReadWriteBytes(startList, start.Index, endList, end.Index); + var (startList, startIndex) = start.Get>(); + var (endList, endIndex) = end.Get>(); + return new ReadWriteBytes(startList, startIndex, endList, endIndex); default: throw new NotImplementedException(); } @@ -278,8 +278,8 @@ public bool TryGet(ref Position position, out Memory item, bool advance = var array = _start as byte[]; if (array != null) { - var start = _startIndex + position.Index; - var length = _endIndex - _startIndex - position.Index; + var start = _startIndex + (int)position; + var length = _endIndex - _startIndex - (int)position; item = new Memory(array, start, length); if (advance) position = default; return true; diff --git a/src/System.Buffers.Experimental/System/Buffers/Text/BufferReader.cs b/src/System.Buffers.Experimental/System/Buffers/Text/BufferReader.cs index 14fc0748087..a224a3a54fd 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Text/BufferReader.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Text/BufferReader.cs @@ -93,8 +93,8 @@ public bool TryReadBytes(out ReadOnlyBytes bytes, byte delimiter) return false; } - var startObj = range.Start.GetItem(); - var endObj = range.End.GetItem(); + var (startObj, startIndex) = range.Start.Get(); + var (endObj, endIndex) = range.End.Get(); // TODO: this is a hack. Once we move this to System.Memory, we should remove if (startObj == null || endObj == null) { @@ -302,14 +302,7 @@ public bool TryRead(out int value, bool littleEndian = false) ReadOnlySpan Unread => _currentSpan.Slice(_currentSpanIndex); - Position Position - { - get { - var result = _currentSegmentPosition; - result += _currentSpanIndex; - return result; - } - } + Position Position =>_currentSegmentPosition + _currentSpanIndex; Position? AdvanceToDelimiter(byte value) { @@ -358,10 +351,10 @@ Position Position if(index != -1 && unread.Length - index < value.Length) { Span temp = stackalloc byte[value.Length]; - int copied = Sequence.Copy(_bytes, _currentSegmentPosition + _currentSpanIndex + index, temp); + int copied = Sequence.Copy(_bytes, _currentSegmentPosition + (_currentSpanIndex + index), temp); if (copied < value.Length) return null; - if (temp.SequenceEqual(value)) return _currentSegmentPosition + _currentSpanIndex + index; + if (temp.SequenceEqual(value)) return _currentSegmentPosition + (_currentSpanIndex + index); else throw new NotImplementedException(); // need to check farther in this span } diff --git a/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs b/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs index 098ac06e866..a083f9e2cf2 100644 --- a/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs +++ b/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs @@ -16,20 +16,19 @@ public static Position Create(T item, int index = 0) where T : class public static implicit operator Position(int index) => new Position(index, null); - public static explicit operator int(Position position) => position.Index; - - public int Index => _index; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T GetItem() => _item == null ? default : (T)_item; + public static explicit operator int(Position position) => position._index; [MethodImpl(MethodImplOptions.AggressiveInlining)] - public (T item, int index) Get() => (GetItem(), (int)this); + public (T item, int index) Get() { + var item = _item == null ? default : (T) _item; + return (item, _index); + } - public static bool operator ==(Position left, Position right) => left.Index == right.Index && left._item == right._item; - public static bool operator !=(Position left, Position right) => left.Index != right.Index || left._item != right._item; + public static bool operator ==(Position left, Position right) => left._index == right._index && left._item == right._item; + public static bool operator !=(Position left, Position right) => left._index != right._index || left._item != right._item; - public static Position operator +(Position position, int offset) => new Position(position.Index + offset, position._item); + public static Position operator +(Position value, int index) + => new Position(value._index + index, value._item); [EditorBrowsable(EditorBrowsableState.Never)] public bool Equals(Position position) => this == position; @@ -40,16 +39,21 @@ public override bool Equals(object obj) => [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() => - Index.GetHashCode() ^ (_item == null ? 0 : _item.GetHashCode()); + _index.GetHashCode() ^ (_item == null ? 0 : _item.GetHashCode()); [EditorBrowsable(EditorBrowsableState.Never)] public override string ToString() => - this==default ? "(default)" : _item == null ? $"{Index}" : $"{Index}, {_item}"; + this==default ? "(default)" : _item == null ? $"{_index}" : $"{_index}, {_item}"; private Position(int index, object item) { _item = item; _index = index; } + private Position(int index) + { + _item = null; + _index = index; + } } } diff --git a/src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs b/src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs index 4f5fd755363..0f07f4668e0 100644 --- a/src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs +++ b/src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs @@ -111,7 +111,7 @@ public bool TryGet(ref Position position, out T item, bool advance = true) int index = (int)position; if (index < _count) { item = _array[index]; - if (advance) { position += 1; } + if (advance) { position +=1; } return true; } diff --git a/tests/System.Collections.Sequences.Tests/SampleCollections/LinkedContainer.cs b/tests/System.Collections.Sequences.Tests/SampleCollections/LinkedContainer.cs index a8c8bb407b8..c5f8d6bec11 100644 --- a/tests/System.Collections.Sequences.Tests/SampleCollections/LinkedContainer.cs +++ b/tests/System.Collections.Sequences.Tests/SampleCollections/LinkedContainer.cs @@ -36,8 +36,8 @@ public bool TryGet(ref Position position, out T item, bool advance = true) return false; } - var node = position.GetItem(); - if (node == null) { + var (node, index) = position.Get(); + if (node == null || index != 0) { item = default; position = default; return false;