Skip to content

Commit 83cb5a0

Browse files
author
Oren (electricessence)
committed
Integrated TemporaryArray struct.
1 parent 49ba697 commit 83cb5a0

File tree

2 files changed

+75
-86
lines changed

2 files changed

+75
-86
lines changed

Open.RandomizationExtensions.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
66
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
77
<Description>
@@ -14,10 +14,11 @@ Part of the "Open" set of libraries.</Description>
1414
<RepositoryUrl>https://github.com/electricessence/Open.RandomizationExtensions</RepositoryUrl>
1515
<RepositoryType>Git</RepositoryType>
1616
<PackageTags>random select</PackageTags>
17+
<Version>1.0.1</Version>
1718
</PropertyGroup>
1819

1920
<ItemGroup>
20-
<PackageReference Include="System.Memory" Version="4.5.3" />
21+
<PackageReference Include="Open.MemoryExtensions" Version="2.1.0" />
2122
</ItemGroup>
2223

2324
</Project>

Random.cs

Lines changed: 72 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensing: MIT https://github.com/electricessence/Genetic-Algorithm-Platform/blob/master/LICENSE.md
44
*/
55

6+
using Open.Memory;
67
using System;
78
using System.Buffers;
89
using System.Collections.Generic;
@@ -138,29 +139,29 @@ public static int RandomSelectIndex<T>(this in ReadOnlySpan<T> source, IEnumerab
138139
return -1;
139140

140141
HashSet<T> setCreated = null;
141-
var exclusionSet = exclusion == null ? default
142-
: exclusion as ISet<T> ?? (setCreated = new HashSet<T>(exclusion));
143-
144-
if (exclusionSet == null || exclusionSet.Count == 0)
145-
return R.Value.Next(source.Length);
146-
147-
var count = source.Length;
148-
var pool = count > 1048576 ? null : ArrayPool<int>.Shared;
149-
var indexes = pool?.Rent(count) ?? new int[count];
150-
151142
try
152143
{
153-
var indexCount = 0;
154-
for (var i = 0; i < count; ++i)
144+
var exclusionSet = exclusion == null ? default
145+
: exclusion as ISet<T> ?? (setCreated = new HashSet<T>(exclusion));
146+
147+
if (exclusionSet == null || exclusionSet.Count == 0)
148+
return R.Value.Next(source.Length);
149+
150+
var count = source.Length;
151+
using (var indexesTemp = ArrayPool<int>.Shared.RentDisposable(count))
155152
{
156-
if (!exclusionSet.Contains(source[i]))
157-
indexes[indexCount++] = i;
153+
var indexes = indexesTemp.Array;
154+
var indexCount = 0;
155+
for (var i = 0; i < count; ++i)
156+
{
157+
if (!exclusionSet.Contains(source[i]))
158+
indexes[indexCount++] = i;
159+
}
160+
return indexCount == 0 ? -1 : indexes[R.Value.Next(indexCount)];
158161
}
159-
return indexCount == 0 ? -1 : indexes[R.Value.Next(indexCount)];
160162
}
161163
finally
162164
{
163-
pool?.Return(indexes);
164165
setCreated?.Clear();
165166
}
166167
}
@@ -190,31 +191,30 @@ public static int RandomSelectIndex<T>(this ICollection<T> source, IEnumerable<T
190191
return -1;
191192

192193
HashSet<T> setCreated = null;
193-
var exclusionSet = exclusion == null ? default
194+
try
195+
{
196+
var exclusionSet = exclusion == null ? default
194197
: exclusion as ISet<T> ?? (setCreated = new HashSet<T>(exclusion));
195198

196-
if (exclusionSet == null || exclusionSet.Count == 0)
197-
return R.Value.Next(source.Count);
199+
if (exclusionSet == null || exclusionSet.Count == 0)
200+
return R.Value.Next(source.Count);
198201

199-
var count = source.Count;
200-
var pool = count > 1048576 ? null : ArrayPool<int>.Shared;
201-
var indexes = pool?.Rent(count) ?? new int[count];
202-
203-
try
204-
{
205-
var i = -1;
206-
var indexCount = 0;
207-
foreach (var value in source)
202+
using (var indexesTemp = ArrayPool<int>.Shared.RentDisposable(source.Count))
208203
{
209-
++i;
210-
if (!exclusionSet.Contains(value))
211-
indexes[indexCount++] = i;
204+
var indexes = indexesTemp.Array;
205+
var i = -1;
206+
var indexCount = 0;
207+
foreach (var value in source)
208+
{
209+
++i;
210+
if (!exclusionSet.Contains(value))
211+
indexes[indexCount++] = i;
212+
}
213+
return indexCount == 0 ? -1 : indexes[R.Value.Next(indexCount)];
212214
}
213-
return indexCount == 0 ? -1 : indexes[R.Value.Next(indexCount)];
214215
}
215216
finally
216217
{
217-
pool?.Return(indexes);
218218
setCreated?.Clear();
219219
}
220220
}
@@ -247,23 +247,17 @@ public static int RandomSelectIndexExcept<T>(this in ReadOnlySpan<T> source, T e
247247
}
248248

249249
var count = source.Length;
250-
var pool = count > 1048576 ? null : ArrayPool<int>.Shared;
251-
var indexes = pool?.Rent(count) ?? new int[count];
252-
253-
try
250+
using (var indexesTemp = ArrayPool<int>.Shared.RentDisposable(count))
254251
{
252+
var indexes = indexesTemp.Array;
255253
var indexCount = 0;
256-
for (var i = 0; i < source.Length; i++)
254+
for (var i = 0; i < count; i++)
257255
{
258256
if (!exclusion.Equals(source[i]))
259257
indexes[indexCount++] = i;
260258
}
261259
return indexCount == 0 ? -1 : indexes[R.Value.Next(indexCount)];
262260
}
263-
finally
264-
{
265-
pool?.Return(indexes);
266-
}
267261
}
268262

269263
/// <summary>
@@ -305,12 +299,9 @@ public static int RandomSelectIndexExcept<T>(this ICollection<T> source, T exclu
305299
}
306300
}
307301

308-
var count = source.Count;
309-
var pool = count > 1048576 ? null : ArrayPool<int>.Shared;
310-
var indexes = pool?.Rent(count) ?? new int[count];
311-
312-
try
302+
using (var indexesTemp = ArrayPool<int>.Shared.RentDisposable(source.Count))
313303
{
304+
var indexes = indexesTemp.Array;
314305
var i = -1;
315306
var indexCount = 0;
316307
foreach (var value in source)
@@ -321,10 +312,6 @@ public static int RandomSelectIndexExcept<T>(this ICollection<T> source, T exclu
321312
}
322313
return indexCount == 0 ? -1 : indexes[R.Value.Next(indexCount)];
323314
}
324-
finally
325-
{
326-
pool?.Return(indexes);
327-
}
328315
}
329316

330317
/// <summary>
@@ -563,31 +550,32 @@ public static ushort NextRandomIntegerExcluding(
563550
throw new ArgumentOutOfRangeException(nameof(range), range, "Must be a number greater than zero.");
564551

565552
HashSet<ushort> setCreated = null;
566-
var exclusionSet = exclusion == null ? null
553+
try
554+
{
555+
var exclusionSet = exclusion == null ? null
567556
: exclusion as ISet<ushort> ?? (setCreated = new HashSet<ushort>(exclusion));
568557

569-
if (exclusionSet == null || exclusionSet.Count == 0)
570-
return (ushort)R.Value.Next(range);
558+
if (exclusionSet == null || exclusionSet.Count == 0)
559+
return (ushort)R.Value.Next(range);
571560

572-
var pool = ArrayPool<ushort>.Shared;
573-
var indexes = pool?.Rent(range) ?? new ushort[range];
574-
575-
try
576-
{
577-
var indexCount = 0;
578-
for (ushort i = 0; i < range; ++i)
561+
using (var indexesTemp = ArrayPool<ushort>.Shared.RentDisposable(range))
579562
{
580-
if (!exclusionSet.Contains(i))
581-
indexes[indexCount++] = i;
563+
var indexes = indexesTemp.Array;
564+
565+
var indexCount = 0;
566+
for (ushort i = 0; i < range; ++i)
567+
{
568+
if (!exclusionSet.Contains(i))
569+
indexes[indexCount++] = i;
570+
}
571+
if (indexCount == 0)
572+
throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.");
573+
574+
return indexes[R.Value.Next(indexCount)];
582575
}
583-
if(indexCount==0)
584-
throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.");
585-
586-
return indexes[R.Value.Next(indexCount)];
587576
}
588577
finally
589578
{
590-
pool.Return(indexes);
591579
setCreated?.Clear();
592580
}
593581
}
@@ -606,30 +594,30 @@ public static int NextRandomIntegerExcluding(
606594
throw new ArgumentOutOfRangeException(nameof(range), range, "Must be a number greater than zero.");
607595

608596
HashSet<int> setCreated = null;
609-
var exclusionSet = exclusion == null ? null
597+
try
598+
{
599+
var exclusionSet = exclusion == null ? null
610600
: exclusion as ISet<int> ?? (setCreated = new HashSet<int>(exclusion));
611601

612-
if (exclusionSet == null || exclusionSet.Count == 0)
613-
return R.Value.Next(range);
602+
if (exclusionSet == null || exclusionSet.Count == 0)
603+
return R.Value.Next(range);
614604

615-
var pool = range > 1048576 ? null : ArrayPool<int>.Shared;
616-
var indexes = pool?.Rent(range) ?? new int[range];
617-
618-
try
619-
{
620-
var indexCount = 0;
621-
for (var i = 0; i < range; ++i)
605+
using (var indexesTemp = ArrayPool<int>.Shared.RentDisposable(range))
622606
{
623-
if (!exclusionSet.Contains(i))
624-
indexes[indexCount++] = i;
607+
var indexes = indexesTemp.Array;
608+
var indexCount = 0;
609+
for (var i = 0; i < range; ++i)
610+
{
611+
if (!exclusionSet.Contains(i))
612+
indexes[indexCount++] = i;
613+
}
614+
if (indexCount == 0)
615+
throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.");
616+
return indexes[R.Value.Next(indexCount)];
625617
}
626-
if(indexCount==0)
627-
throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.");
628-
return indexes[R.Value.Next(indexCount)];
629618
}
630619
finally
631620
{
632-
pool?.Return(indexes);
633621
setCreated?.Clear();
634622
}
635623
}

0 commit comments

Comments
 (0)