-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathCartesian.g.tt
168 lines (157 loc) · 7.1 KB
/
Cartesian.g.tt
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ output extension=".cs" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Globalization" #>
<#@ import namespace="System.Linq" #>
#region License and Terms
// MoreLINQ - Extensions to LINQ to Objects
// Copyright (c) 2010 Leopold Bushkin. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
#nullable enable // required for auto-generated sources (see below why)
// > Older code generation strategies may not be nullable aware. Setting the
// > project-level nullable context to "enable" could result in many
// > warnings that a user is unable to fix. To support this scenario any syntax
// > tree that is determined to be generated will have its nullable state
// > implicitly set to "disable", regardless of the overall project state.
//
// Source: https://github.com/dotnet/roslyn/blob/70e158ba6c2c99bd3c3fc0754af0dbf82a6d353d/docs/features/nullable-reference-types.md#generated-code
namespace MoreLinq
{
using System;
using System.Collections.Generic;
using Experimental;
static partial class MoreEnumerable
{<#
var overloads =
from args in new[]
{
new[]
{
new { Ordinal = "First" , Arity = "one" },
new { Ordinal = "Second" , Arity = "two" },
new { Ordinal = "Third" , Arity = "three" },
new { Ordinal = "Fourth" , Arity = "four" },
new { Ordinal = "Fifth" , Arity = "five" },
new { Ordinal = "Sixth" , Arity = "six" },
new { Ordinal = "Seventh", Arity = "seven" },
new { Ordinal = "Eighth" , Arity = "eight" },
}
}
select args.Select((a, i) => new
{
a.Ordinal,
OrdinalLower = a.Ordinal.ToLowerInvariant(),
a.Arity,
Count = i + 1,
Number = (i + 1).ToString(CultureInfo.InvariantCulture),
})
into args
select args.ToList() into args
from a in args.Skip(1)
select new
{
a.Arity,
Arguments = args.Take(a.Count).ToList(),
TypeParams = string.Join(", ", from aa in args.Take(a.Count) select $"T{aa.Number}")
};
foreach (var o in overloads)
{ #>
/// <summary>
/// Returns the Cartesian product of <#= o.Arity #> sequences by enumerating all
/// possible combinations of one item from each sequence, and applying
/// a user-defined projection to the items in a given combination.
/// </summary>
<# foreach (var arg in o.Arguments) { #>
/// <typeparam name="T<#= arg.Number #>">
/// The type of the elements of <paramref name="<#= arg.OrdinalLower #>"/>.</typeparam>
<# } #>
/// <typeparam name="TResult">
/// The type of the elements of the result sequence.</typeparam>
<# foreach (var arg in o.Arguments) {#>
/// <param name="<#= arg.OrdinalLower #>">The <#= arg.OrdinalLower #> sequence of elements.</param>
<# } #>
/// <param name="resultSelector">A projection function that combines
/// elements from all of the sequences.</param>
/// <returns>A sequence of elements returned by
/// <paramref name="resultSelector"/>.</returns>
/// <remarks>
/// <para>
/// The method returns items in the same order as a nested foreach
/// loop, but all sequences except for <paramref name="first"/> are
/// cached when iterated over. The cache is then re-used for any
/// subsequent iterations.</para>
/// <para>
/// This method uses deferred execution and stream its results.</para>
/// </remarks>
public static IEnumerable<TResult> Cartesian<<#= o.TypeParams #>, TResult>(
this <#
foreach (var arg in o.Arguments) { #>
IEnumerable<T<#= arg.Number #>> <#= arg.OrdinalLower #>,
<#
} #>
Func<<#= o.TypeParams #>, TResult> resultSelector)
{
<# foreach (var arg in o.Arguments) { #>
if (<#= arg.OrdinalLower #> == null) throw new ArgumentNullException(nameof(<#= arg.OrdinalLower #>));
<# } #>
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));
return _(); IEnumerable<TResult> _()
{
<# foreach (var arg in o.Arguments.Skip(1)) { #>
IEnumerable<T<#= arg.Number #>> <#= arg.OrdinalLower #>Memo;
<# } #>
<# foreach (var arg in o.Arguments.Skip(1)) { #>
using ((<#= arg.OrdinalLower #>Memo = <#= arg.OrdinalLower #>.Memoize()) as IDisposable)
<# } #>
{
foreach (var item1 in first)
<# foreach (var arg in o.Arguments.Skip(1)) { #>
foreach (var item<#= arg.Number #> in <#= arg.OrdinalLower #>Memo)
<# } #>
yield return resultSelector(<#= string.Join(", ", from x in o.Arguments select "item" + x.Number) #>);
}
}
}
/// <summary>
/// Returns the Cartesian product of <#= o.Arity #> sequences by enumerating tuples
/// of all possible combinations of one item from each sequence.
/// </summary>
<# foreach (var arg in o.Arguments) { #>
/// <typeparam name="T<#= arg.Number #>">The type of the elements of <paramref name="<#= arg.OrdinalLower #>"/>.</typeparam>
<# } #>
<# foreach (var arg in o.Arguments) {#>
/// <param name="<#= arg.OrdinalLower #>">The <#= arg.OrdinalLower #> sequence of elements.</param>
<# } #>
/// <returns>A sequence of tuples.</returns>
/// <remarks>
/// <para>
/// The method returns items in the same order as a nested foreach
/// loop, but all sequences except for <paramref name="first"/> are
/// cached when iterated over. The cache is then re-used for any
/// subsequent iterations.</para>
/// <para>
/// This method uses deferred execution and stream its results.</para>
/// </remarks>
public static IEnumerable<(<#= string.Join(", ", from a in o.Arguments select $"T{a.Number} {a.Ordinal}") #>)>
Cartesian<<#= o.TypeParams #>>(
this <#= string.Join($",{Environment.NewLine} ",
from arg in o.Arguments
select $"IEnumerable<T{arg.Number}> {arg.OrdinalLower}") #>)
{
return Cartesian(<#= string.Join(", ", from a in o.Arguments select a.OrdinalLower) #>, ValueTuple.Create);
}
<# } #>
}
}