forked from dotnet/corefxlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request dotnet#852 from KrzysztofCwalina/Sequences
Added SpanSequence
- Loading branch information
Showing
16 changed files
with
317 additions
and
150 deletions.
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
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
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
107 changes: 107 additions & 0 deletions
107
src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
|
||
namespace System.Collections.Sequences | ||
{ | ||
// a List<T> like type designed to be embeded in other types | ||
public struct ResizableArray<T> | ||
{ | ||
public T[] _array; | ||
public int _count; | ||
|
||
public ResizableArray(int capacity) | ||
{ | ||
_array = new T[capacity]; | ||
_count = 0; | ||
} | ||
|
||
public ResizableArray(T[] array, int count = 0) | ||
{ | ||
_array = array; | ||
_count = count; | ||
} | ||
|
||
public void Add(T item) | ||
{ | ||
if (_array.Length == _count) { | ||
Resize(); | ||
} | ||
_array[_count++] = item; | ||
} | ||
|
||
public T[] Resize(int newSize = -1) | ||
{ | ||
var oldArray = _array; | ||
if (newSize == -1) { | ||
if(_array == null || _array.Length == 0) { | ||
newSize = 4; | ||
} | ||
else { | ||
newSize = _array.Length << 1; | ||
} | ||
} | ||
|
||
var newArray = new T[newSize]; | ||
_array.CopyTo(newArray, 0); | ||
_array = newArray; | ||
return oldArray; | ||
} | ||
|
||
public T[] Resize(T[] newArray) | ||
{ | ||
if (newArray.Length < _count) throw new Exception(String.Format("{0} {1}", newArray.Length, _count)); | ||
var oldArray = _array; | ||
Array.Copy(_array, 0, newArray, 0, _count); | ||
_array = newArray; | ||
return oldArray; | ||
} | ||
|
||
public int Count => _count; | ||
|
||
public int Capacity => _array.Length; | ||
|
||
public T this[int index] { | ||
get { | ||
if (index > _count - 1) throw new IndexOutOfRangeException(); | ||
return _array[index]; | ||
} | ||
set { | ||
if (index > _count - 1) throw new IndexOutOfRangeException(); | ||
_array[index] = value; | ||
} | ||
} | ||
|
||
public T GetAt(ref Position position, bool advance = false) | ||
{ | ||
if( _count == 0) { | ||
position = Position.Invalid; | ||
return default(T); | ||
} | ||
|
||
if (position.Equals(Position.BeforeFirst)) { | ||
position = Position.Invalid; | ||
if (advance) { | ||
position = Position.First; | ||
} | ||
return default(T); | ||
} | ||
|
||
if (position.IntegerPosition < _count) { | ||
var item = _array[position.IntegerPosition]; | ||
if (advance) { | ||
position.IntegerPosition++; | ||
if (position.IntegerPosition == _count) position = Position.AfterLast; | ||
} | ||
return item; | ||
} | ||
|
||
position = Position.Invalid; | ||
return default(T); | ||
} | ||
|
||
public ArraySegment<T> Full => new ArraySegment<T>(_array, 0, _count); | ||
public ArraySegment<T> Free => new ArraySegment<T>(_array, _count, _array.Length - _count); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Sequences; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System | ||
{ | ||
public interface ISpanSequence<T> | ||
{ | ||
SpanSequenceEnumerator<T> GetEnumerator(); | ||
Span<T> GetAt(ref Position position, bool advance = false); | ||
} | ||
|
||
public struct SpanSequenceEnumerator<T> | ||
{ | ||
Position _position; | ||
ISpanSequence<T> _sequence; | ||
|
||
public SpanSequenceEnumerator(ISpanSequence<T> sequence) | ||
{ | ||
_sequence = sequence; | ||
_position = Position.BeforeFirst; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public bool MoveNext() | ||
{ | ||
var result = _sequence.GetAt(ref _position, advance:true); | ||
return _position.IsValid; | ||
} | ||
|
||
public Span<T> Current { | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
get { | ||
return _sequence.GetAt(ref _position); | ||
} | ||
} | ||
} | ||
} | ||
|
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
Oops, something went wrong.