Select.cs
3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/******************************************************************************
* Copyright (C) Leap Motion, Inc. 2011-2017. *
* Leap Motion proprietary and confidential. *
* *
* Use subject to the terms of the Leap Motion SDK Agreement available at *
* https://developer.leapmotion.com/sdk_agreement, or another agreement *
* between Leap Motion and you, your company or other organization. *
******************************************************************************/
using System;
namespace Leap.Unity.Query {
public struct SelectOp<SourceType, ResultType, SourceOp> : IQueryOp<ResultType>
where SourceOp : IQueryOp<SourceType> {
private SourceOp _source;
private Func<SourceType, ResultType> _mapping;
public SelectOp(SourceOp enumerator, Func<SourceType, ResultType> mapping) {
_source = enumerator;
_mapping = mapping;
}
public bool TryGetNext(out ResultType t) {
SourceType sourceObj;
if (_source.TryGetNext(out sourceObj)) {
t = _mapping(sourceObj);
return true;
} else {
t = default(ResultType);
return false;
}
}
public void Reset() {
_source.Reset();
}
}
public partial struct QueryWrapper<QueryType, QueryOp> where QueryOp : IQueryOp<QueryType> {
/// <summary>
/// Returns a new query operation representing the current query sequence mapped element-by-element
/// into a new query sequence by a mapping operation.
///
/// For example:
/// (1, 2, 3, 4).Query().Select(num => (num * 2).ToString())
/// Would result in:
/// ("2", "4", "6", "8")
/// </summary>
public QueryWrapper<NewType, SelectOp<QueryType, NewType, QueryOp>> Select<NewType>(Func<QueryType, NewType> mapping) {
return new QueryWrapper<NewType, SelectOp<QueryType, NewType, QueryOp>>(new SelectOp<QueryType, NewType, QueryOp>(_op, mapping));
}
/// <summary>
/// Returns a new query operation representing the current query sequence where each element is cast
/// to a new type. This method ONLY works for casting to types that are reference types. If you want
/// to cast to a specific primitive like float or int, you can use one of the methods defined in
/// the DirectQueryExtensions, like ToFloats or ToInts. For casting to a sequence of structs, you will
/// need to use an explicit Select statement like.
/// </summary>
public QueryWrapper<NewType, SelectOp<QueryType, NewType, QueryOp>> Cast<NewType>() where NewType : class {
return Select(obj => obj as NewType);
}
/// <summary>
/// Returns a new query operation representing the string representation of each of the elements
/// in the source sequence.
///
/// For example:
/// (1, 2, 3, 4).Query().ToStrings()
/// Would result in:
/// ("1", "2", "3", "4")
/// </summary>
public QueryWrapper<string, SelectOp<QueryType, string, QueryOp>> ToStrings() {
return Select(obj => obj.ToString());
}
}
}