11using System.Collections.Generic;
15 public static class SliceExtensions {
30 public static Slice<T> Slice<T>(
this IList<T> list,
int beginIdx = -1,
int endIdx = -1) {
31 if (beginIdx == -1 && endIdx == -1) {
32 return new Slice<T>(list, 0, list.Count);
34 else if (beginIdx == -1 && endIdx != -1) {
35 return new Slice<T>(list, 0, endIdx);
37 else if (endIdx == -1 && beginIdx != -1) {
38 return new Slice<T>(list, beginIdx, list.Count);
41 return new Slice<T>(list, beginIdx, endIdx);
45 public static Slice<T> FromIndex<T>(
this IList<T> list,
int fromIdx) {
46 return Slice(list, fromIdx);
51 public static T[] ToArray<T>(
this Slice<T> slice) {
52 var array =
new T[slice.Count];
53 for (
int i = 0; i < slice.Count; i++) {
63 private IList<T> _list;
65 private int _beginIdx;
68 private int _direction;
77 public Slice(IList<T> list,
int beginIdx = 0,
int endIdx = -1) {
80 if (endIdx == -1) endIdx = _list.Count;
82 _direction = beginIdx <= endIdx ? 1 : -1;
85 public T
this[
int index] {
87 if (index < 0 || index >
Count - 1) {
throw new IndexOutOfRangeException(); }
88 return _list[_beginIdx + index * _direction];
91 if (index < 0 || index >
Count - 1) {
throw new IndexOutOfRangeException(); }
92 _list[_beginIdx + index * _direction] = value;
98 return (_endIdx - _beginIdx) * _direction;
102 #region foreach and Query()
109 T[] array = ArrayPool<T>.Spawn(
Count);
110 for (
int i = 0; i <
Count; i++) {
This is a definition-friendly interface that new "indexable" struct definitions can implement to make...
A two-generic-argument variant of an enumerator that allows an IIndexableStruct to quickly define an ...
A Query object is a type of immutable ordered collection of elements that can be used to perform usef...
Slice(IList< T > list, int beginIdx=0, int endIdx=-1)
Creates a slice into the List with an inclusive beginIdx and an exclusive endIdx. A slice with identi...
IndexableStructEnumerator< T, Slice< T > > GetEnumerator()