Exemplo n.º 1
0
        public ReadWriteBytes Slice(Position from)
        {
            var(segment, index) = from.Get <IMemoryList <byte> >();
            if (segment == null)
            {
                return(Slice(First.Length - index));
            }
            var headIndex    = _all.VirtualIndex + _all.Memory.Length - _first.Length;
            var newHeadIndex = segment.VirtualIndex;
            var diff         = newHeadIndex - headIndex;

            // TODO: this could be optimized to avoid the Slice
            return(new ReadWriteBytes(segment, Length - diff).Slice(index));
        }
Exemplo n.º 2
0
        public bool TryGet(ref Position position, out Memory <byte> item, bool advance = true)
        {
            if (position == default)
            {
                item = default;
                return(false);
            }

            var(list, index) = position.Get <BufferList>();
            item             = list._data.Slice(index);
            if (advance)
            {
                position = new Position(list._next, 0);
            }
            return(true);
        }
Exemplo n.º 3
0
        public ReadWriteBytes Slice(Position position)
        {
            var kind = Kind;

            switch (kind)
            {
            case Type.Array:
                var(array, index) = position.Get <byte[]>();
                return(new ReadWriteBytes(array, index, array.Length - index));

            case Type.MemoryList:
                return(Slice(position, new Position((IMemoryList <byte>)_end, _endIndex)));

            default: throw new NotImplementedException();
            }
        }
Exemplo n.º 4
0
        public bool TryGet(ref Position position, out Memory <byte> item, bool advance = true)
        {
            if (position == default)
            {
                item = default;
                return(false);
            }

            var array = _start as byte[];

            if (array != null)
            {
                var start  = _startIndex + (int)position;
                var length = _endIndex - _startIndex - (int)position;
                item = new Memory <byte>(array, start, length);
                if (advance)
                {
                    position = default;
                }
                return(true);
            }

            if (Kind == Type.MemoryList)
            {
                var(node, index) = position.Get <IMemoryList <byte> >();
                item             = node.Memory.Slice(index);
                if (ReferenceEquals(node, _end))
                {
                    item = item.Slice(0, _endIndex - index);
                    if (advance)
                    {
                        position = default;
                    }
                }
                else
                {
                    if (advance)
                    {
                        position = new Position(node.Next, 0);
                    }
                }
                return(true);
            }

            throw new NotImplementedException();
        }
Exemplo n.º 5
0
        public ReadWriteBytes Slice(Position start, Position end)
        {
            var kind = Kind;

            switch (kind)
            {
            case Type.Array:
                var(array, index) = start.Get <byte[]>();
                return(new ReadWriteBytes(array, index, (int)end - index));

            case Type.MemoryList:
                var(startList, startIndex) = start.Get <IMemoryList <byte> >();
                var(endList, endIndex)     = end.Get <IMemoryList <byte> >();
                return(new ReadWriteBytes(startList, startIndex, endList, endIndex));

            default:
                throw new NotImplementedException();
            }
        }
Exemplo n.º 6
0
        public ReadOnlyBytes Slice(Position from, Position to)
        {
            var(fromSegment, fromIndex) = from.Get <IMemoryList <byte> >();
            var(toSegment, toIndex)     = to.Get <IMemoryList <byte> >();

            if (fromSegment == null)
            {
                return(Slice(fromIndex - _totalLengthOrVirtualIndex, toIndex - fromIndex));
            }

            var headIndex      = _all.VirtualIndex + _all.Memory.Length - _first.Length;
            var newHeadIndex   = fromSegment.VirtualIndex + fromIndex;
            var newEndIndex    = toSegment.VirtualIndex + toIndex;
            var slicedOffFront = newHeadIndex - headIndex;
            var length         = newEndIndex - newHeadIndex;
            // TODO: this could be optimized to avoid the Slice
            var slice = new ReadOnlyBytes(fromSegment, length + fromIndex);

            slice = slice.Slice(fromIndex);
            return(slice);
        }
Exemplo n.º 7
0
        public bool TryGet(ref Position position, out ReadOnlyMemory <byte> item, bool advance = true)
        {
            if (position.IsEnd)
            {
                item = default;
                return(false);
            }

            var array = _start as byte[];

            if (array != null)
            {
                var start  = _startIndex + position.Index;
                var length = _endIndex - _startIndex - position.Index;
                item = new ReadOnlyMemory <byte>(array, start, length);
                if (advance)
                {
                    position = Position.End;
                }
                return(true);
            }

            if (Kind == Type.MemoryList)
            {
                if (position == default)
                {
                    var first = _start as IMemoryList <byte>;
                    item = first.Memory.Slice(_startIndex);
                    if (advance)
                    {
                        position = Position.Create(first.Next);
                    }
                    if (ReferenceEquals(_end, _start))
                    {
                        item = item.Slice(0, (int)Length);
                        if (advance)
                        {
                            position = Position.End;
                        }
                    }
                    return(true);
                }

                var(node, index) = position.Get <IMemoryList <byte> >();
                item             = node.Memory.Slice(index);
                if (ReferenceEquals(node, _end))
                {
                    item = item.Slice(0, _endIndex - index);
                    if (advance)
                    {
                        position = Position.End;
                    }
                }
                else
                {
                    if (advance)
                    {
                        position = Position.Create(node.Next);
                    }
                }
                return(true);
            }

            throw new NotImplementedException();
        }
Exemplo n.º 8
0
        public bool TryGet(ref Position position, out ReadOnlyMemory <byte> value, bool advance = true)
        {
            if (position == default)
            {
                value = _first;
                if (advance)
                {
                    position.SetItem(Rest);
                }
                return(!_first.IsEmpty || _all != null);
            }
            if (position.IsEnd)
            {
                value = default;
                return(false);
            }

            var(segment, index) = position.Get <IMemoryList <byte> >();

            if (segment == null)
            {
                if (_all == null) // single segment ROB
                {
                    value = _first.Slice(index - (int)_totalLengthOrVirtualIndex);
                    if (advance)
                    {
                        position = Position.End;
                    }
                    return(true);
                }
                else
                {
                    value = default;
                    return(false);
                }
            }

            var memory = segment.Memory;

            // We need to slice off the last segment based on length of this ROB.
            // This ROB is a potentially shorted view over a longer segment list.
            var virtualIndex  = segment.VirtualIndex;
            var lengthOfFirst = virtualIndex - VirtualIndex;
            var lengthOfEnd   = lengthOfFirst + memory.Length;

            if (lengthOfEnd > Length)
            {
                if (advance)
                {
                    position = Position.End;
                }
                value = memory.Slice(0, (int)(Length - lengthOfFirst));
                if (index < value.Length)
                {
                    if (index > 0)
                    {
                        value = value.Slice(index);
                    }
                    return(true);
                }
                else
                {
                    value = ReadOnlyMemory <byte> .Empty;
                    return(false);
                }
            }
            else
            {
                value = memory.Slice(index);
                if (advance)
                {
                    position = Position.Create(0, segment.Rest);
                }
                return(true);
            }
        }
Exemplo n.º 9
0
        public bool TryGet(ref Position position, out ReadOnlyMemory <byte> item, bool advance = true)
        {
            if (position == default)
            {
                item = default;
                return(false);
            }

            var array = _start as byte[];

            if (array != null)
            {
                var start  = (int)position;
                var length = _endIndex - (int)position;
                if (length == 0)
                {
                    item = ReadOnlyMemory <byte> .Empty;
                }
                else
                {
                    item = new ReadOnlyMemory <byte>(array, start, length);
                }
                if (advance)
                {
                    position = default;
                }
                return(true);
            }

            if (Kind == Type.MemoryList)
            {
                var(node, index) = position.Get <IBufferList>();
                item             = node.Memory.Slice(index);
                if (ReferenceEquals(node, _end))
                {
                    item = item.Slice(0, _endIndex - index);
                    if (advance)
                    {
                        position = default;
                    }
                }
                else
                {
                    if (advance)
                    {
                        position = new Position(node.Next, 0);
                    }
                }
                return(true);
            }

            var om = _start as OwnedMemory <byte>;

            if (om != null)
            {
                var start  = (int)position;
                var length = _endIndex - (int)position;
                item = om.Memory.Slice(start, length);
                if (advance)
                {
                    position = default;
                }
                return(true);
            }

            throw new NotImplementedException();
        }