Tanoda
QueryValueGenerators.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 System;
10using System.Collections.Generic;
11
12namespace Leap.Unity.Query {
13
14 public static class Values {
15
19 public static Query<T> Single<T>(T value) {
20 var array = ArrayPool<T>.Spawn(1);
21 array[0] = value;
22 return new Query<T>(array, 1);
23 }
24
29 public static Query<T> Repeat<T>(T value, int times) {
30 var array = ArrayPool<T>.Spawn(times);
31 for (int i = 0; i < times; i++) {
32 array[i] = value;
33 }
34 return new Query<T>(array, times);
35 }
36
40 public static Query<T> Empty<T>() {
41 var array = ArrayPool<T>.Spawn(0);
42 return new Query<T>(array, 0);
43 }
44
57 public static Query<int> Range(int from, int to, int step = 1, bool endIsExclusive = true) {
58 if (step <= 0) {
59 throw new ArgumentException("Step must be positive and non-zero.");
60 }
61
62 List<int> values = Pool<List<int>>.Spawn();
63 try {
64 int value = from;
65 int sign = Utils.Sign(to - from);
66
67 if (sign != 0) {
68 while (Utils.Sign(to - value) == sign) {
69 values.Add(value);
70 value += step * sign;
71 }
72 }
73
74 if (!endIsExclusive && value == to) {
75 values.Add(to);
76 }
77
78 return new Query<int>(values);
79 } finally {
80 values.Clear();
81 Pool<List<int>>.Recycle(values);
82 }
83 }
84 }
85}