Skip to content

Commit a5edddf

Browse files
author
Oren (electricessence)
committed
Resharper inspection fixes.
1 parent a257085 commit a5edddf

7 files changed

Lines changed: 90 additions & 34 deletions

File tree

IProcedureResult.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Open.Numeric
2+
{
3+
interface IProcedureResult<T>
4+
{
5+
int Count { get; }
6+
T Sum { get; }
7+
T Average { get; }
8+
}
9+
}

Numeric.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static bool IsNaN(this float value)
2525
public static bool IsDefault(this double value)
2626
{
2727
// ReSharper disable CompareOfFloatsByEqualityOperator
28-
return value.Equals(default(double));
28+
return value.Equals(default);
2929
// ReSharper restore CompareOfFloatsByEqualityOperator
3030
}
3131
#endregion

Open.Numeric.csproj

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,19 @@ Part of the "Open" set of libraries.</Description>
1616
<PackageProjectUrl>https://github.com/electricessence/Open.Numeric/</PackageProjectUrl>
1717
<RepositoryType>git</RepositoryType>
1818
<PackageTags>dotnet, dotnetcore, cs, numbers, double, float, precision, extensions</PackageTags>
19-
<Version>1.1.0</Version>
20-
<AssemblyVersion>1.1.0.0</AssemblyVersion>
21-
<FileVersion>1.1.0.0</FileVersion>
19+
<Version>1.2.0</Version>
20+
<AssemblyVersion>1.2.0.0</AssemblyVersion>
21+
<FileVersion>1.2.0.0</FileVersion>
22+
<PackageReleaseNotes>Added ProcedureResults class which can contain multiple values summed.</PackageReleaseNotes>
2223
</PropertyGroup>
2324

2425
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
2526
<Optimize>True</Optimize>
27+
<LangVersion>latest</LangVersion>
28+
</PropertyGroup>
29+
30+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
31+
<LangVersion>latest</LangVersion>
2632
</PropertyGroup>
2733

2834
<ItemGroup>
@@ -32,4 +38,8 @@ Part of the "Open" set of libraries.</Description>
3238
<None Remove="README.md" />
3339
</ItemGroup>
3440

41+
<ItemGroup>
42+
<PackageReference Include="System.Memory" Version="4.5.1" />
43+
</ItemGroup>
44+
3545
</Project>

Precision.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static int DecimalPlaces(this double source)
6666
if (source.IsNaN())
6767
return 0;
6868

69-
string valueString = source.ToString();
69+
var valueString = source.ToString();
7070
var index = valueString.IndexOf('.');
7171
return index == -1 ? 0 : valueString.Length - index - 1;
7272
}
@@ -179,9 +179,9 @@ public static double ToDouble(this float value, int precision)
179179
throw new ArgumentOutOfRangeException(nameof(precision), precision, "Must be bewteen 0 and 15.");
180180
Contract.EndContractBlock();
181181

182-
double result = value.ReturnZeroIfFinite();
182+
var result = value.ReturnZeroIfFinite();
183183
// ReSharper disable RedundantCast
184-
return result.IsZero() ? Math.Round((double)value, precision) : result;
184+
return result.IsZero() ? Math.Round(value, precision) : result;
185185
// ReSharper restore RedundantCast
186186
}
187187

@@ -190,7 +190,7 @@ public static double ToDouble(this float value, int precision)
190190
/// </summary>
191191
public static double ToDouble(this float value)
192192
{
193-
double result = value.ReturnZeroIfFinite();
193+
var result = value.ReturnZeroIfFinite();
194194
return result.IsZero() ? double.Parse(value.ToString(CultureInfo.InvariantCulture)) : result;
195195
}
196196

@@ -262,7 +262,7 @@ public static double SumUsingIntegers(this double source, double value)
262262
var v = (long)(source * x);
263263
var a = (long)(value * x);
264264
var result = v + a;
265-
return (double)result / x;
265+
return result / x;
266266
}
267267

268268
}

ProcedureResult.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
namespace Open.Numeric
66
{
7-
public struct ProcedureResult : IComparable<ProcedureResult>
7+
public struct ProcedureResult : IComparable<ProcedureResult>, IProcedureResult<double>
88
{
9-
public readonly int Count;
10-
public readonly double Sum;
11-
public readonly double Average;
9+
public int Count { get; }
10+
public double Sum { get; }
11+
public double Average { get; }
12+
1213
public ProcedureResult(double sum, int count)
1314
{
1415
Count = count;
1516
Sum = sum;
1617
Average = count == 0 ? double.NaN : sum / count;
1718
}
1819

19-
2020
public ProcedureResult Add(double value, int count = 1)
2121
{
2222
return new ProcedureResult(Sum + value, Count + count);

ProcedureResults.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace Open.Numeric
5+
{
6+
public class ProcedureResults : IProcedureResult<ReadOnlyMemory<double>>
7+
{
8+
public int Count { get; }
9+
public ReadOnlyMemory<double> Sum { get; }
10+
public ReadOnlyMemory<double> Average { get; }
11+
public ProcedureResults(ReadOnlySpan<double> sum, int count)
12+
{
13+
Count = count;
14+
var s = sum.ToArray();
15+
Sum = s;
16+
Average = s.Select(v => count == 0 ? double.NaN : v / count).ToArray();
17+
}
18+
19+
static double[] SumValues(ReadOnlySpan<double> a, ReadOnlySpan<double> b)
20+
{
21+
if (a.Length != a.Length)
22+
throw new ArgumentException("Length mismatch.");
23+
24+
var len = a.Length;
25+
var result = new double[len];
26+
for (var i = 0; i < len; i++)
27+
result[i] = a[i] + b[i];
28+
29+
return result;
30+
}
31+
32+
public ProcedureResults Add(ReadOnlySpan<double> values, int count = 1)
33+
=> new ProcedureResults(SumValues(Sum.Span, values), Count + count);
34+
35+
public static ProcedureResults operator +(ProcedureResults a, ProcedureResults b)
36+
=> new ProcedureResults(SumValues(a.Sum.Span, b.Sum.Span), a.Count + b.Count);
37+
38+
}
39+
}

Random.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*!
1+
/*!
22
* @author electricessence / https://github.com/electricessence/
33
* Licensing: MIT https://github.com/electricessence/Genetic-Algorithm-Platform/blob/master/LICENSE.md
44
*/
@@ -11,7 +11,7 @@ namespace Open.Numeric
1111
{
1212
public static class RandomUtilities
1313
{
14-
static Lazy<Random> R = new Lazy<Random>(() => new Random());
14+
static readonly Lazy<Random> R = new Lazy<Random>(() => new Random());
1515
public static Random Random
1616
{
1717
get
@@ -20,12 +20,11 @@ public static Random Random
2020
}
2121
}
2222

23-
2423
public static bool TryRandomPluck<T>(this LinkedList<T> source, out T value)
2524
{
2625
if (source.Count == 0)
2726
{
28-
value = default(T);
27+
value = default;
2928
return false;
3029
}
3130

@@ -40,7 +39,7 @@ public static bool TryRandomPluck<T>(this LinkedList<T> source, out T value)
4039

4140
public static T RandomPluck<T>(this LinkedList<T> source)
4241
{
43-
if (source.TryRandomPluck(out T value))
42+
if (source.TryRandomPluck(out var value))
4443
return value;
4544

4645
throw new InvalidOperationException("Source collection is empty.");
@@ -50,7 +49,7 @@ public static bool TryRandomPluck<T>(this IList<T> source, out T value)
5049
{
5150
if (source.Count == 0)
5251
{
53-
value = default(T);
52+
value = default;
5453
return false;
5554
}
5655

@@ -62,7 +61,7 @@ public static bool TryRandomPluck<T>(this IList<T> source, out T value)
6261

6362
public static T RandomPluck<T>(this IList<T> source)
6463
{
65-
if (source.TryRandomPluck(out T value))
64+
if (source.TryRandomPluck(out var value))
6665
return value;
6766

6867
throw new InvalidOperationException("Source collection is empty.");
@@ -76,20 +75,19 @@ public static bool TryRandomSelectOne<T>(
7675
var count = source.Count;
7776
if (count == 0)
7877
{
79-
value = default(T);
78+
value = default;
8079
return false;
8180
}
8281

83-
if (excluding == null || excluding.Count == 0)
84-
{
85-
value = source[R.Value.Next(count)];
86-
return true;
87-
}
82+
if (excluding != null && excluding.Count != 0)
83+
return source
84+
.Where(o => !excluding.Contains(o))
85+
.ToArray()
86+
.TryRandomSelectOne(out value);
87+
88+
value = source[R.Value.Next(count)];
89+
return true;
8890

89-
return source
90-
.Where(o => !excluding.Contains(o))
91-
.ToArray()
92-
.TryRandomSelectOne(out value);
9391
}
9492

9593
public static T RandomSelectOne<T>(
@@ -106,14 +104,14 @@ public static T RandomSelectOne<T>(
106104
}
107105

108106
public static bool TryRandomSelectOneExcept<T>(
109-
this IReadOnlyList<T> source,
107+
this IReadOnlyCollection<T> source,
110108
T excluding,
111109
out T value)
112110
{
113111
var count = source.Count;
114112
if (count == 0)
115113
{
116-
value = default(T);
114+
value = default;
117115
return false;
118116
}
119117

@@ -124,7 +122,7 @@ public static bool TryRandomSelectOneExcept<T>(
124122
}
125123

126124
public static T RandomSelectOneExcept<T>(
127-
this IReadOnlyList<T> source,
125+
this IReadOnlyCollection<T> source,
128126
T excluding)
129127
{
130128
if (source.Count == 0)

0 commit comments

Comments
 (0)