-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandomizer.SpanParams.cs
More file actions
211 lines (180 loc) · 7.2 KB
/
Copy pathRandomizer.SpanParams.cs
File metadata and controls
211 lines (180 loc) · 7.2 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*!
* @author electricessence / https://github.com/electricessence/
* Licensing: MIT https://github.com/Open-NET-Libraries/Open.RandomizationExtensions/blob/master/LICENSE
*/
#if NET10_0_OR_GREATER
using System;
using System.Diagnostics.CodeAnalysis;
using System.Buffers;
using System.Collections.Generic;
namespace Open.RandomizationExtensions;
public static partial class Randomizer
{
// C# 13 params-span overloads for the exclusion family. Callers writing
// source.RandomSelectOneExcept('a', 'b') no longer allocate a params array per
// call, and the exclusions are checked by direct linear scan -- for the small
// exclusion counts these methods exist for, that beats building a set.
// Overload resolution prefers these over the params T[] forms for literal
// arguments; explicitly-typed arrays still bind the array overloads.
private static bool SpanContains<T>(ReadOnlySpan<T> values, T value)
{
var comparer = EqualityComparer<T>.Default;
foreach (var v in values)
{
if (comparer.Equals(v, value))
return true;
}
return false;
}
/// <inheritdoc cref="RandomSelectIndexExcept{T}(in ReadOnlySpan{T}, Random?, T, T[])"/>
public static int RandomSelectIndexExcept<T>(
this in ReadOnlySpan<T> source, Random? random, T excluding, params ReadOnlySpan<T> others)
{
if (source.Length == 0)
return -1;
var pool = ArrayPool<int>.Shared;
var indexes = pool.Rent(source.Length);
try
{
var comparer = EqualityComparer<T>.Default;
var indexCount = 0;
for (var i = 0; i < source.Length; ++i)
{
var value = source[i];
if (!comparer.Equals(excluding, value) && !SpanContains(others, value))
indexes[indexCount++] = i;
}
return indexCount == 0 ? -1 : indexes[(random ?? Default).Next(indexCount)];
}
finally
{
pool.Return(indexes);
}
}
/// <inheritdoc cref="RandomSelectIndexExcept{T}(in ReadOnlySpan{T}, T, T[])"/>
public static int RandomSelectIndexExcept<T>(
this in ReadOnlySpan<T> source, T excluding, params ReadOnlySpan<T> others)
=> RandomSelectIndexExcept(in source, null, excluding, others);
/// <inheritdoc cref="TryRandomSelectOneExcept{T}(in ReadOnlySpan{T}, out T, Random?, T, T[])"/>
public static bool TryRandomSelectOneExcept<T>(
this in ReadOnlySpan<T> source, [MaybeNullWhen(false)] out T value, Random? random, T excluding, params ReadOnlySpan<T> others)
{
var index = RandomSelectIndexExcept(in source, random, excluding, others);
if (index == -1)
{
value = default;
return false;
}
value = source[index];
return true;
}
/// <inheritdoc cref="TryRandomSelectOneExcept{T}(in ReadOnlySpan{T}, out T, T, T[])"/>
public static bool TryRandomSelectOneExcept<T>(
this in ReadOnlySpan<T> source, [MaybeNullWhen(false)] out T value, T excluding, params ReadOnlySpan<T> others)
=> TryRandomSelectOneExcept(in source, out value, null, excluding, others);
/// <inheritdoc cref="RandomSelectOneExcept{T}(in ReadOnlySpan{T}, Random?, T, T[])"/>
public static T RandomSelectOneExcept<T>(
this in ReadOnlySpan<T> source, Random? random, T excluding, params ReadOnlySpan<T> others)
=> source.Length == 0
? throw new InvalidOperationException("Source collection is empty.")
: TryRandomSelectOneExcept(in source, out var value, random, excluding, others)
? value
: throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.");
/// <inheritdoc cref="RandomSelectOneExcept{T}(in ReadOnlySpan{T}, T, T[])"/>
public static T RandomSelectOneExcept<T>(
this in ReadOnlySpan<T> source, T excluding, params ReadOnlySpan<T> others)
=> RandomSelectOneExcept(in source, null, excluding, others);
/// <inheritdoc cref="TryRandomSelectOneExcept{T}(IReadOnlyCollection{T}, out T, Random?, T, T[])"/>
public static bool TryRandomSelectOneExcept<T>(
this IReadOnlyCollection<T> source, [MaybeNullWhen(false)] out T value, Random? random, T excluding, params ReadOnlySpan<T> others)
{
if (source is null) throw new ArgumentNullException(nameof(source));
if (source.Count == 0)
{
value = default;
return false;
}
var pool = ArrayPool<int>.Shared;
var indexes = pool.Rent(source.Count);
try
{
var comparer = EqualityComparer<T>.Default;
var i = -1;
var indexCount = 0;
foreach (var v in source)
{
++i;
if (!comparer.Equals(excluding, v) && !SpanContains(others, v))
indexes[indexCount++] = i;
}
if (indexCount == 0)
{
value = default;
return false;
}
value = GetElementAt(source, indexes[(random ?? Default).Next(indexCount)]);
return true;
}
finally
{
pool.Return(indexes);
}
}
/// <inheritdoc cref="TryRandomSelectOneExcept{T}(IReadOnlyCollection{T}, out T, T, T[])"/>
public static bool TryRandomSelectOneExcept<T>(
this IReadOnlyCollection<T> source, [MaybeNullWhen(false)] out T value, T excluding, params ReadOnlySpan<T> others)
=> TryRandomSelectOneExcept(source, out value, null, excluding, others);
/// <inheritdoc cref="RandomSelectOneExcept{T}(IReadOnlyCollection{T}, Random?, T, T[])"/>
public static T RandomSelectOneExcept<T>(
this IReadOnlyCollection<T> source, Random? random, T excluding, params ReadOnlySpan<T> others)
=> source is null ? throw new ArgumentNullException(nameof(source))
: source.Count == 0
? throw new InvalidOperationException("Source collection is empty.")
: TryRandomSelectOneExcept(source, out var value, random, excluding, others)
? value
: throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.");
/// <inheritdoc cref="RandomSelectOneExcept{T}(IReadOnlyCollection{T}, T, T[])"/>
public static T RandomSelectOneExcept<T>(
this IReadOnlyCollection<T> source, T excluding, params ReadOnlySpan<T> others)
=> RandomSelectOneExcept(source, null, excluding, others);
/// <inheritdoc cref="NextExcluding(Random, int, int, int[])"/>
public static int NextExcluding(this Random source,
int range, int excluding, params ReadOnlySpan<int> others)
{
if (source is null) throw new ArgumentNullException(nameof(source));
if (range <= 0)
throw new ArgumentOutOfRangeException(nameof(range), range, "Must be a number greater than zero.");
if (others.Length == 0)
{
// Inline the single-exclusion skip-shift rather than re-dispatching:
// a call with no 'others' (e.g. NextExcluding(range, x)) binds THIS overload
// under C# 13's params-span preference, so delegating to "the two-argument
// form" would recurse right back here forever.
if (excluding >= range || excluding < 0)
return source.Next(range);
if (excluding == 0 && range == 1)
throw new ArgumentException("No value is available with a range of 1 and exclusion of 0.", nameof(range));
var single = source.Next(range - 1);
return single < excluding ? single : single + 1;
}
var pool = ArrayPool<int>.Shared;
var candidates = pool.Rent(range);
try
{
var count = 0;
for (var i = 0; i < range; ++i)
{
if (i != excluding && !SpanContains<int>(others, i))
candidates[count++] = i;
}
return count == 0
? throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.")
: candidates[source.Next(count)];
}
finally
{
pool.Return(candidates);
}
}
}
#endif