10using System.Collections.Generic;
11using System.Collections;
15 public static class QueryConversionExtensions {
28 List<T> list = Pool<List<T>>.Spawn();
30 list.AddRange(enumerable);
34 Pool<List<T>>.Recycle(list);
42 List<T> list = Pool<List<T>>.Spawn();
44 while (enumerator.MoveNext()) {
45 list.Add(enumerator.Current);
50 Pool<List<T>>.Recycle(list);
58 var dst = ArrayPool<T>.Spawn(array.GetLength(0) * array.GetLength(1));
60 for (
int i = 0; i < array.GetLength(0); i++) {
61 for (
int j = 0; j < array.GetLength(1); j++) {
62 dst[dstIndex++] = array[i, j];
66 return new Query<T>(dst, array.GetLength(0) * array.GetLength(1));
94 private Validator _validator;
101 public Query(T[] array,
int count) {
103 throw new ArgumentNullException(
"array");
107 throw new ArgumentException(
"Count must be non-negative, but was " + count);
110 if (count > array.Length) {
111 throw new ArgumentException(
"Count was " + count +
" but the provided array only had a length of " + array.Length);
116 _validator = Validator.Spawn();
122 public Query(ICollection<T> collection) {
123 _array = ArrayPool<T>.Spawn(collection.Count);
124 _count = collection.Count;
125 collection.CopyTo(_array, 0);
127 _validator = Validator.Spawn();
134 other._validator.Validate();
136 _array = ArrayPool<T>.Spawn(other._count);
137 _count = other._count;
138 Array.Copy(other._array, _array, _count);
140 _validator = Validator.Spawn();
146 #region DIRECT IMPLEMENTED OPERATORS
158 _validator.Validate();
160 var dstArray = ArrayPool<K>.Spawn(_count);
163 for (
int i = 0; i < _count; i++) {
164 if (_array[i] is K) {
165 dstArray[dstCount++] = (K)_array[i];
170 return new Query<K>(dstArray, dstCount);
178 return this.Select(item => item as K);
188 _validator.Validate();
190 ArrayPool<T>.Recycle(_array);
191 Validator.Invalidate(_validator);
203 _validator.Validate();
208 Validator.Invalidate(_validator);
228 _validator.Validate();
240 private int _nextIndex;
252 object IEnumerator.Current {
254 if (_nextIndex == 0) {
255 throw new InvalidOperationException();
263 if (_nextIndex >= _count) {
267 Current = _array[_nextIndex++];
272 ArrayPool<T>.Recycle(_array);
275 public void Reset() {
throw new InvalidOperationException(); }
287 public T
this[
int index] {
298 private struct Validator {
299 private static int _nextId = 1;
302 private int _idValue;
304 public void Validate() {
306 throw new InvalidOperationException(
"This Query is not valid, you cannot construct a Query using the default constructor.");
309 if (_idRef ==
null || _idRef.value != _idValue) {
310 throw new InvalidOperationException(
"This Query has already been disposed. A Query can only be used once before it is automatically disposed.");
314 public static Validator Spawn() {
315 Id
id = Pool<Id>.Spawn();
316 id.value = _nextId++;
318 return new Validator() {
324 public static void Invalidate(Validator validator) {
325 validator._idRef.value = -1;
326 Pool<Id>.Recycle(validator._idRef);
Enumerator(T[] array, int count)
readonly T[] BackingArray
QuerySlice(T[] array, int count)
A Query object is a type of immutable ordered collection of elements that can be used to perform usef...
Query(Query< T > other)
Constructs a query that is an exact copy of another query.
Query(T[] array, int count)
Constructs a new query given a source array and a count. The query assumes ownership of the array,...
Query< K > OfType< K >()
Returns a new Query representing only the items of the current Query that are of a specific type.
Query(ICollection< T > collection)
Constructs a new query of the given collection.
void Deconstruct(out T[] array, out int count)
Deconstructs this Query into its base elements, the array and the count. The caller assumes ownership...
void Dispose()
Disposes of the resources that this query holds. The Query cannot be used after this method is called...
Query< K > Cast< K >()
Returns a new Query representing the current query sequence where each element is cast to a new type.
Enumerator GetEnumerator()
QuerySlice Deconstruct()
Deconstructs this Query into a simple QuerySlice construct. This is simply a utility overload of the ...