Skip to content

Commit 7161101

Browse files
Reduced dependencies and cleanup.
1 parent d8db79f commit 7161101

File tree

4 files changed

+111
-65
lines changed

4 files changed

+111
-65
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,4 @@ ASALocalRun/
332332

333333
# Local History for Visual Studio
334334
.localhistory/
335+
/nuget.config

DeferredHashSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public DeferredHashSet(IEnumerable<T> source)
2222
if (base.Contains(item))
2323
return true;
2424

25-
while(Source.MoveNext())
25+
while (Source.MoveNext())
2626
{
2727
var i = Source.Current;
2828
Add(i);

Open.RandomizationExtensions.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<RepositoryUrl>https://github.com/electricessence/Open.RandomizationExtensions</RepositoryUrl>
1919
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2020
<RepositoryType>git</RepositoryType>
21-
<Version>2.5.0</Version>
21+
<Version>2.5.1</Version>
2222
<PackageReleaseNotes></PackageReleaseNotes>
2323
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2424
<PublishRepositoryUrl>true</PublishRepositoryUrl>
@@ -31,6 +31,11 @@
3131
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
3232
</ItemGroup>
3333

34+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
35+
<PackageReference Include="System.Buffers" Version="4.5.1" />
36+
<PackageReference Include="System.Memory" Version="4.5.4" />
37+
</ItemGroup>
38+
3439
<ItemGroup>
3540
<None Remove=".gitattributes" />
3641
<None Remove=".gitignore" />
@@ -40,8 +45,4 @@
4045
</None>
4146
</ItemGroup>
4247

43-
<ItemGroup>
44-
<PackageReference Include="Open.MemoryExtensions" Version="2.8.0" />
45-
</ItemGroup>
46-
4748
</Project>

Randomizer.cs

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

6-
using Open.Memory;
76
using System;
87
using System.Buffers;
98
using System.Collections.Generic;
@@ -165,16 +164,23 @@ public static int RandomSelectIndex<T>(this in ReadOnlySpan<T> source, Random? r
165164
return RandomSelectIndexExcept(in source, random, exclusionSet.Single());
166165

167166
var count = source.Length;
168-
using var temp = ArrayPool<int>.Shared.RentDisposable(count);
169-
var indexes = temp.Array;
170-
var indexCount = 0;
171-
for (var i = 0; i < count; ++i)
167+
var pool = ArrayPool<int>.Shared;
168+
var indexes = pool.Rent(count);
169+
try
172170
{
173-
if (!exclusionSet.Contains(source[i]))
174-
indexes[indexCount++] = i;
171+
var indexCount = 0;
172+
for (var i = 0; i < count; ++i)
173+
{
174+
if (!exclusionSet.Contains(source[i]))
175+
indexes[indexCount++] = i;
176+
}
177+
return indexCount == 0 ? -1
178+
: indexes[(random ?? R.Value).Next(indexCount)];
179+
}
180+
finally
181+
{
182+
pool.Return(indexes);
175183
}
176-
return indexCount == 0 ? -1
177-
: indexes[(random ?? R.Value).Next(indexCount)];
178184
}
179185
finally
180186
{
@@ -211,18 +217,26 @@ public static int RandomSelectIndexExcept<T>(this in ReadOnlySpan<T> source, Ran
211217
if (others.Length != 0)
212218
return RandomSelectIndex(in source, random, Combined(excluding, others));
213219

214-
using var temp = ArrayPool<int>.Shared.RentDisposable(others.Length);
215-
var indexes = temp.Array;
216-
var i = -1;
217-
var indexCount = 0;
218-
foreach (var value in source)
220+
var pool = ArrayPool<int>.Shared;
221+
var indexes = pool.Rent(others.Length);
222+
try
223+
{
224+
var i = -1;
225+
var indexCount = 0;
226+
foreach (var value in source)
227+
{
228+
++i;
229+
bool equals = excluding is null ? value is null : excluding.Equals(value);
230+
if (!equals)
231+
indexes[indexCount++] = i;
232+
}
233+
return indexCount == 0 ? -1 : indexes[R.Value.Next(indexCount)];
234+
}
235+
finally
219236
{
220-
++i;
221-
bool equals = excluding is null ? value is null : excluding.Equals(value);
222-
if (!equals)
223-
indexes[indexCount++] = i;
237+
pool.Return(indexes);
224238
}
225-
return indexCount == 0 ? -1 : indexes[R.Value.Next(indexCount)];
239+
226240
}
227241

228242
/// <summary>
@@ -302,17 +316,25 @@ static int RandomSelectIndex<T>(Random? random, int count, IEnumerable<T> source
302316
if (exclusionSet.Count == 1)
303317
return RandomSelectIndexExcept(random, count, source, exclusionSet.Single());
304318

305-
using var indexesTemp = ArrayPool<int>.Shared.RentDisposable(count);
306-
var indexes = indexesTemp.Array;
307-
var i = -1;
308-
var indexCount = 0;
309-
foreach (var value in source)
319+
var pool = ArrayPool<int>.Shared;
320+
var indexes = pool.Rent(count);
321+
try
310322
{
311-
++i;
312-
if (!exclusionSet.Contains(value))
313-
indexes[indexCount++] = i;
323+
324+
var i = -1;
325+
var indexCount = 0;
326+
foreach (var value in source)
327+
{
328+
++i;
329+
if (!exclusionSet.Contains(value))
330+
indexes[indexCount++] = i;
331+
}
332+
return indexCount == 0 ? -1 : indexes[(random ?? R.Value).Next(indexCount)];
333+
}
334+
finally
335+
{
336+
pool.Return(indexes);
314337
}
315-
return indexCount == 0 ? -1 : indexes[(random ?? R.Value).Next(indexCount)];
316338
}
317339
finally
318340
{
@@ -328,18 +350,26 @@ static int RandomSelectIndexExcept<T>(Random? random, int count, IEnumerable<T>
328350
if (others.Length != 0)
329351
RandomSelectIndex(random, count, source, Combined(excluding, others));
330352

331-
using var indexesTemp = ArrayPool<int>.Shared.RentDisposable(count);
332-
var indexes = indexesTemp.Array;
333-
var i = -1;
334-
var indexCount = 0;
335-
foreach (var value in source)
353+
var pool = ArrayPool<int>.Shared;
354+
var indexes = pool.Rent(count);
355+
try
356+
{
357+
var i = -1;
358+
var indexCount = 0;
359+
foreach (var value in source)
360+
{
361+
++i;
362+
bool equals = excluding is null ? value is null : excluding.Equals(value);
363+
if (!equals)
364+
indexes[indexCount++] = i;
365+
}
366+
return indexCount == 0 ? -1 : indexes[R.Value.Next(indexCount)];
367+
}
368+
finally
336369
{
337-
++i;
338-
bool equals = excluding is null ? value is null : excluding.Equals(value);
339-
if (!equals)
340-
indexes[indexCount++] = i;
370+
pool.Return(indexes);
341371
}
342-
return indexCount == 0 ? -1 : indexes[R.Value.Next(indexCount)];
372+
343373
}
344374

345375
/// <summary>
@@ -629,7 +659,7 @@ public static bool TryRandomSelectOneExcept<T>(
629659
return false;
630660
}
631661

632-
value = GetElementAt(source,index);
662+
value = GetElementAt(source, index);
633663

634664
return true;
635665
}
@@ -778,19 +808,25 @@ public static ushort NextExcluding(this Random source,
778808
if (exclusionSet == null || exclusionSet.Count == 0)
779809
return (ushort)source.Next(range);
780810

781-
using var indexesTemp = ArrayPool<ushort>.Shared.RentDisposable(range);
782-
var indexes = indexesTemp.Array;
783-
784-
var indexCount = 0;
785-
for (ushort i = 0; i < range; ++i)
811+
var pool = ArrayPool<ushort>.Shared;
812+
var indexes = pool.Rent(range);
813+
try
786814
{
787-
if (!exclusionSet.Contains(i))
788-
indexes[indexCount++] = i;
815+
var indexCount = 0;
816+
for (ushort i = 0; i < range; ++i)
817+
{
818+
if (!exclusionSet.Contains(i))
819+
indexes[indexCount++] = i;
820+
}
821+
if (indexCount == 0)
822+
throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.");
823+
824+
return indexes[source.Next(indexCount)];
825+
}
826+
finally
827+
{
828+
pool.Return(indexes);
789829
}
790-
if (indexCount == 0)
791-
throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.");
792-
793-
return indexes[source.Next(indexCount)];
794830
}
795831
finally
796832
{
@@ -820,17 +856,25 @@ public static int NextExcluding(this Random source,
820856
if (exclusionSet == null || exclusionSet.Count == 0)
821857
return source.Next(range);
822858

823-
using var indexesTemp = ArrayPool<int>.Shared.RentDisposable(range);
824-
var indexes = indexesTemp.Array;
825-
var indexCount = 0;
826-
for (var i = 0; i < range; ++i)
859+
var pool = ArrayPool<int>.Shared;
860+
var indexes = pool.Rent(range);
861+
try
827862
{
828-
if (!exclusionSet.Contains(i))
829-
indexes[indexCount++] = i;
863+
var indexCount = 0;
864+
for (var i = 0; i < range; ++i)
865+
{
866+
if (!exclusionSet.Contains(i))
867+
indexes[indexCount++] = i;
868+
}
869+
if (indexCount == 0)
870+
throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.");
871+
return indexes[source.Next(indexCount)];
830872
}
831-
if (indexCount == 0)
832-
throw new InvalidOperationException("Exclusion set invalidates the source. No possible value can be selected.");
833-
return indexes[source.Next(indexCount)];
873+
finally
874+
{
875+
pool.Return(indexes);
876+
}
877+
834878
}
835879
finally
836880
{

0 commit comments

Comments
 (0)