Skip to content

Commit

Permalink
Some uses of collection expressions, where they make some kind of sense.
Browse files Browse the repository at this point in the history
  • Loading branch information
colgreen committed Aug 5, 2024
1 parent d00abae commit b290d5b
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 46 deletions.
11 changes: 5 additions & 6 deletions Redzen.Tests/IO/MemoryStreamFuzzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public class MemoryStreamFuzzer
readonly MemoryStream _strmA;
readonly MemoryBlockStream _strmB;
readonly IRandomSource _rng;
readonly DiscreteDistribution<double> _opDistribution = new(new double[]
{
readonly DiscreteDistribution<double> _opDistribution = new(
[
0.688, // Write
0.05, // Write byte
0.05, // Change read/write head position.
Expand All @@ -21,7 +21,7 @@ public class MemoryStreamFuzzer
0.002, // Trim
0.01, // Read byte
0.1, // Read
});
]);

#region Constructors

Expand All @@ -36,8 +36,7 @@ public MemoryStreamFuzzer(MemoryStream strmA, MemoryBlockStream strmB, int seed)
_strmB = strmB;
_rng = RandomDefaults.CreateRandomSource((ulong)seed);
_opDistribution = new DiscreteDistribution<double>(
new double[]
{
[
0.688, // Write
0.05, // Write byte
0.05, // Change read/write head position.
Expand All @@ -46,7 +45,7 @@ public MemoryStreamFuzzer(MemoryStream strmA, MemoryBlockStream strmB, int seed)
0.002, // Trim
0.01, // Read byte
0.1, // Read
});
]);
}

#endregion
Expand Down
12 changes: 6 additions & 6 deletions Redzen.Tests/MathSpanDoubleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,31 @@ public void MeanSquaredDelta()
public void MedianOfSorted()
{
// Empty array.
var arr = Array.Empty<double>();
double[] arr = [];
Assert.Throws<ArgumentException>(() => MathSpan.MedianOfSorted<double>(arr));

// Single element.
arr = new double[] { 5 };
arr = [5];
double actual = MathSpan.MedianOfSorted<double>(arr);
Assert.Equal(5, actual);

// Two elements.
arr = new double[] { 2, 4 };
arr = [2, 4];
actual = MathSpan.MedianOfSorted<double>(arr);
Assert.Equal(3.0, actual);

// Three elements.
arr = new double[] { 1, 2, 3 };
arr = [1, 2, 3];
actual = MathSpan.MedianOfSorted<double>(arr);
Assert.Equal(2, actual);

// Five elements.
arr = new double[] { 1, 2, 3, 4, 5 };
arr = [1, 2, 3, 4, 5];
actual = MathSpan.MedianOfSorted<double>(arr);
Assert.Equal(3, actual);

// Six elements.
arr = new double[] { 1, 2, 3, 4, 5, 6 };
arr = [1, 2, 3, 4, 5, 6];
actual = MathSpan.MedianOfSorted<double>(arr);
Assert.Equal(3.5, actual);
}
Expand Down
12 changes: 6 additions & 6 deletions Redzen.Tests/MathSpanInt32Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,31 @@ public void MinMax()
public void MedianOfSorted()
{
// Empty array.
var arr = Array.Empty<int>();
int[] arr = [];
Assert.Throws<ArgumentException>(() => MathSpan.MedianOfSortedIntegers<int>(arr));

// Single element.
arr = new int[] { 5 };
arr = [5];
double actual = MathSpan.MedianOfSortedIntegers<int>(arr);
actual.Should().Be(5.0);

// Two elements.
arr = new int[] { 2, 4 };
arr = [2, 4];
actual = MathSpan.MedianOfSortedIntegers<int>(arr);
actual.Should().Be(3.0);

// Three elements.
arr = new int[] { 1, 2, 3 };
arr = [1, 2, 3];
actual = MathSpan.MedianOfSortedIntegers<int>(arr);
actual.Should().Be(2.0);

// Five elements.
arr = new int[] { 1, 2, 3, 4, 5 };
arr = [1, 2, 3, 4, 5];
actual = MathSpan.MedianOfSortedIntegers<int>(arr);
actual.Should().Be(3.0);

// Six elements.
arr = new int[] { 1, 2, 3, 4, 5, 6 };
arr = [1, 2, 3, 4, 5, 6];
actual = MathSpan.MedianOfSortedIntegers<int>(arr);
actual.Should().Be(3.5);
}
Expand Down
12 changes: 6 additions & 6 deletions Redzen.Tests/MathSpanSingleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,31 @@ public void MeanSquaredDelta()
public void MedianOfSorted()
{
// Empty array.
var arr = Array.Empty<float>();
float[] arr = [];
((Action)(() => MathSpan.MedianOfSorted<float>(arr))).Should().Throw<ArgumentException>();

// Single element.
arr = new float[] { 5 };
arr = [5];
double actual = MathSpan.MedianOfSorted<float>(arr);
actual.Should().Be(5.0);

// Two elements.
arr = new float[] { 2, 4 };
arr = [2, 4];
actual = MathSpan.MedianOfSorted<float>(arr);
actual.Should().Be(3.0);

// Three elements.
arr = new float[] { 1, 2, 3 };
arr = [1, 2, 3];
actual = MathSpan.MedianOfSorted<float>(arr);
actual.Should().Be(2.0);

// Five elements.
arr = new float[] { 1, 2, 3, 4, 5 };
arr = [1, 2, 3, 4, 5];
actual = MathSpan.MedianOfSorted<float>(arr);
actual.Should().Be(3.0);

// Six elements.
arr = new float[] { 1, 2, 3, 4, 5, 6 };
arr = [1, 2, 3, 4, 5, 6];
actual = MathSpan.MedianOfSorted<float>(arr);
actual.Should().Be(3.5);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class DiscreteDistributionTestsDouble
public void Sample()
{
var dist = new DiscreteDistribution<double>(
new double[]
{
[
0.688,
0.05,
0.05,
Expand All @@ -21,7 +20,7 @@ public void Sample()
0.002,
0.01,
0.1,
});
]);

var sampler = new DiscreteDistributionSampler<double>(dist, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ public class DiscreteDistributionTestsHalf
public void Sample()
{
var dist = new DiscreteDistribution<Half>(
new Half[]
{
[
(Half)0.688f,
(Half)0.05f,
(Half)0.05f,
Expand All @@ -19,7 +18,7 @@ public void Sample()
(Half)0.002f,
(Half)0.01f,
(Half)0.1f,
});
]);

var sampler = new DiscreteDistributionSampler<Half>(dist, 0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ public class DiscreteDistributionTestsSingle
public void Sample()
{
var dist = new DiscreteDistribution<float>(
new float[]
{
[
0.688f,
0.05f,
0.05f,
Expand All @@ -21,7 +20,7 @@ public void Sample()
0.002f,
0.01f,
0.1f,
});
]);

var sampler = new DiscreteDistributionSampler<float>(dist, 0);

Expand Down
6 changes: 3 additions & 3 deletions Redzen.Tests/Sorting/IntroSortKVWTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class IntroSortKVWTests
[Fact]
public void Sort_ShortArray()
{
int[] keys = new int[] { 5, 8, 2, 16, 32, 12, 7};
int[] v = new int[] { 45, 42, 48, 24, 8, 28, 43};
int[] w = new int[] { 0, 1, 2, 3, 4, 5, 6};
int[] keys = [5, 8, 2, 16, 32, 12, 7];
int[] v = [45, 42, 48, 24, 8, 28, 43];
int[] w = [0, 1, 2, 3, 4, 5, 6];
IntroSort.Sort<int,int,int>(keys, v, w);

keys.Should().BeEquivalentTo(new int[] { 2, 5, 7, 8, 12, 16, 32 });
Expand Down
4 changes: 2 additions & 2 deletions Redzen.Tests/Sorting/TimSortKVTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class TimSortKVTests
[Fact]
public void Sort_ShortArray()
{
int[] keys = new int[] { 5, 8, 2, 16, 32, 12, 7 };
int[] vals = new int[] { 0, 1, 2, 3, 4, 5, 6 };
int[] keys = [5, 8, 2, 16, 32, 12, 7];
int[] vals = [0, 1, 2, 3, 4, 5, 6];

TimSort.Sort<int, int>(keys, vals);

Expand Down
6 changes: 3 additions & 3 deletions Redzen.Tests/Sorting/TimSortKVWTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ public class TimSortKVWTests
[Fact]
public void Sort_ShortArray()
{
int[] keys = new int[] { 5, 8, 2, 16, 32, 12, 7 };
int[] vals = new int[] { 0, 1, 2, 3, 4, 5, 6 };
int[] wals = new int[] { 6, 5, 4, 3, 2, 1, 0 };
int[] keys = [5, 8, 2, 16, 32, 12, 7];
int[] vals = [0, 1, 2, 3, 4, 5, 6];
int[] wals = [6, 5, 4, 3, 2, 1, 0];

TimSort.Sort<int,int,int>(keys, vals, wals);

Expand Down
2 changes: 1 addition & 1 deletion Redzen.Tests/Sorting/TimSortTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class TimSortTests
[Fact]
public void Sort_ShortArray()
{
int[] keys = new int[] { 5, 8, 2, 16, 32, 12, 7 };
int[] keys = [5, 8, 2, 16, 32, 12, 7];
TimSort.Sort<int>(keys);
keys.Should().BeEquivalentTo(new int[] { 2, 5, 7, 8, 12, 16, 32 });
}
Expand Down
2 changes: 1 addition & 1 deletion Redzen/Numerics/Distributions/DiscreteDistribution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class DiscreteDistribution<T>
/// <summary>
/// A singleton instance that represents the special case of a discrete distribution with a single possible outcome with probability 1.
/// </summary>
public static readonly DiscreteDistribution<T> SingleOutcome = new(new T[] { T.One });
public static readonly DiscreteDistribution<T> SingleOutcome = new([T.One]);

readonly T[] _probArr;
readonly int[] _labelArr;
Expand Down
6 changes: 3 additions & 3 deletions Redzen/PrimeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Redzen;
/// </summary>
public static class PrimeUtils
{
static readonly int[] __primes = new int[]
{
static readonly int[] __primes =
[
1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,
53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127,
131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211,
Expand All @@ -17,7 +17,7 @@ public static class PrimeUtils
409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,
503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607,
613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709
};
];

/// <summary>
/// Returns the smallest prime greater than or equal to the given integer value.
Expand Down

0 comments on commit b290d5b

Please sign in to comment.