Skip to content

Commit 5e59ec5

Browse files
Revised use of copy to avoid errors with buffer lengths.
1 parent 1e211f5 commit 5e59ec5

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

source/Extensions.Subsets.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ public static IEnumerable<T[]> Subsets<T>(this IReadOnlyList<T> source, int coun
9090
{
9191
foreach (var s in SubsetsBuffered(source, count))
9292
{
93-
var a = new T[count];
94-
s.CopyTo(a, 0);
95-
yield return a;
93+
yield return s.AsCopy(count);
9694
}
9795
}
9896

source/Extensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,15 @@ public static void ForEach<T>(this T[,] source, Action<int, int, T> closure)
118118
}
119119
}
120120

121-
public static T[] AsCopy<T>(this T[] source)
121+
public static T[] AsCopy<T>(this T[] source, int? length = null)
122122
{
123123
if (source is null)
124124
throw new NullReferenceException();
125125
Contract.EndContractBlock();
126126

127-
var newArray = new T[source.Length];
128-
for (var i = 0; i < source.Length; i++)
127+
var newArray = new T[length ?? source.Length];
128+
var len = Math.Min(newArray.Length, source.Length);
129+
for (var i = 0; i < len; i++)
129130
newArray[i] = source[i];
130131
return newArray;
131132
}

source/Open.Collections.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PackageProjectUrl>https://github.com/Open-NET-Libraries/Open.Collections/</PackageProjectUrl>
1717
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Collections/</RepositoryUrl>
1818
<RepositoryType>git</RepositoryType>
19-
<Version>2.9.0</Version>
19+
<Version>2.9.1</Version>
2020
<PackageReleaseNotes></PackageReleaseNotes>
2121
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2222
<PublishRepositoryUrl>true</PublishRepositoryUrl>

0 commit comments

Comments
 (0)