Tanoda
Slice.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using Leap.Unity.Query;
10using System;
11using System.Collections.Generic;
12
13namespace Leap.Unity {
14
15 public static class SliceExtensions {
16
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);
33 }
34 else if (beginIdx == -1 && endIdx != -1) {
35 return new Slice<T>(list, 0, endIdx);
36 }
37 else if (endIdx == -1 && beginIdx != -1) {
38 return new Slice<T>(list, beginIdx, list.Count);
39 }
40 else {
41 return new Slice<T>(list, beginIdx, endIdx);
42 }
43 }
44
45 public static Slice<T> FromIndex<T>(this IList<T> list, int fromIdx) {
46 return Slice(list, fromIdx);
47 }
48
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++) {
54 array[i] = slice[i];
55 }
56 return array;
57 }
58
59 }
60
61 public struct Slice<T> : IIndexableStruct<T, Slice<T>> {
62
63 private IList<T> _list;
64
65 private int _beginIdx;
66 private int _endIdx;
67
68 private int _direction;
69
77 public Slice(IList<T> list, int beginIdx = 0, int endIdx = -1) {
78 _list = list;
79 _beginIdx = beginIdx;
80 if (endIdx == -1) endIdx = _list.Count;
81 _endIdx = endIdx;
82 _direction = beginIdx <= endIdx ? 1 : -1;
83 }
84
85 public T this[int index] {
86 get {
87 if (index < 0 || index > Count - 1) { throw new IndexOutOfRangeException(); }
88 return _list[_beginIdx + index * _direction];
89 }
90 set {
91 if (index < 0 || index > Count - 1) { throw new IndexOutOfRangeException(); }
92 _list[_beginIdx + index * _direction] = value;
93 }
94 }
95
96 public int Count {
97 get {
98 return (_endIdx - _beginIdx) * _direction;
99 }
100 }
101
102 #region foreach and Query()
103
106 }
107
108 public Query<T> Query() {
109 T[] array = ArrayPool<T>.Spawn(Count);
110 for (int i = 0; i < Count; i++) {
111 array[i] = this[i];
112 }
113 return new Query<T>(array, Count);
114 }
115
116 #endregion
117
118 }
119
120}
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...
Definition: Query.cs:90
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...
Definition: Slice.cs:77
IndexableStructEnumerator< T, Slice< T > > GetEnumerator()
Definition: Slice.cs:104
Query< T > Query()
Definition: Slice.cs:108