-
-
Save bbarry/89e7ac85256e8fd5b592 to your computer and use it in GitHub Desktop.
Simple slice implementation for C#
This file contains hidden or 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
public static class SliceExtensions | |
{ | |
public static Slice<T> ToSlice<T>(this T[] array) | |
{ | |
return new Slice<T>(array); | |
} | |
public static Slice<T> ToSlice<T>(this T[] array, int offset, int length) | |
{ | |
return new Slice<T>(array, offset, length); | |
} | |
public static Slice<T> ToSlice<T>(this Slice<T> slice) | |
{ | |
return slice; | |
} | |
public static Slice<T> ToSlice<T>(this Slice<T> slice, int offset, int length) | |
{ | |
return new Slice<T>(slice, offset, length); | |
} | |
} |
This file contains hidden or 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
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class Slice<T> : IList<T>, IReadOnlyList<T> | |
{ | |
private readonly T[] _array; | |
private readonly int _offset; | |
public Slice(Slice<T> slice) | |
: this(slice, 0, slice.Count) | |
{ | |
} | |
public Slice(Slice<T> slice, int offset, int length) | |
{ | |
if (offset + length > slice.Count) | |
throw new InvalidOperationException(); | |
_offset = slice._offset + offset; | |
Count = length; | |
_array = slice._array; | |
} | |
public Slice(T[] array) | |
: this(array, 0, array.Length) | |
{ | |
} | |
public Slice(T[] array, int offset, int length) | |
{ | |
if (offset + length > array.Length) | |
throw new InvalidOperationException(); | |
_array = array; | |
_offset = offset; | |
Count = length; | |
} | |
public int IndexOf(T item) | |
{ | |
var comparer = Comparer<T>.Default; | |
for (var index = 0; index < Count; index++) | |
{ | |
if (comparer.Compare(array[index + _offset], item) == 0) | |
return index; | |
} | |
return -1; | |
} | |
public void Insert(int index, T item) | |
{ | |
throw new NotSupportedException(); | |
} | |
public void RemoveAt(int index) | |
{ | |
throw new NotSupportedException(); | |
} | |
public T this[int index] | |
{ | |
get | |
{ | |
if (index >= Count) | |
throw new IndexOutOfRangeException(); | |
return _array[_offset + index]; | |
} | |
set | |
{ | |
if (index >= Count) | |
throw new IndexOutOfRangeException(); | |
_array[_offset + index] = value; | |
} | |
} | |
public void Add(T item) | |
{ | |
throw new NotSupportedException(); | |
} | |
public void Clear() | |
{ | |
throw new NotSupportedException(); | |
} | |
public bool Contains(T item) | |
{ | |
return IndexOf(item) != -1; | |
} | |
public void CopyTo(T[] array, int arrayIndex) | |
{ | |
foreach (var thisItem in AsEnumerable()) | |
array[arrayIndex++] = thisItem; | |
} | |
public void CopyFrom(IEnumerable<T> source) | |
{ | |
var targetOffset = 0; | |
foreach (var item in source) | |
this[targetOffset++] = item; | |
} | |
public bool Remove(T item) | |
{ | |
throw new NotSupportedException(); | |
} | |
public int Count { get; } | |
public bool IsReadOnly => false; | |
private IEnumerable<T> AsEnumerable() | |
{ | |
for (int index = 0; index < Count; index++) | |
{ | |
yield return array[i + _offset]; | |
} | |
} | |
public IEnumerator<T> GetEnumerator() | |
{ | |
return AsEnumerable().GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
public T[] ToArray(int startIndex) | |
{ | |
if (startIndex >= Count) | |
throw new InvalidOperationException(); | |
return ToArray(startIndex, Count - startIndex); | |
} | |
public T[] ToArray(int startIndex, int length) | |
{ | |
if (startIndex + length > Count) | |
throw new InvalidOperationException(); | |
var temp = new T[length]; | |
Array.Copy(_array, _offset + startIndex, temp, 0, length); | |
return temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment