Skip to content

Commit 5a035d6

Browse files
author
Oren (electricessence)
committed
Updates.
1 parent 6b9ea4e commit 5a035d6

2 files changed

Lines changed: 45 additions & 29 deletions

File tree

Extensions.cs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Collections.Concurrent;
1010
using System.Collections.Generic;
1111
using System.Diagnostics;
12+
using System.Diagnostics.CodeAnalysis;
1213
using System.Diagnostics.Contracts;
1314
using System.Linq;
1415
using System.Threading.Tasks;
@@ -20,6 +21,7 @@ public static class Extensions
2021
/// <summary>
2122
/// Debug utility for asserting if a collection is equal.
2223
/// </summary>
24+
[SuppressMessage("ReSharper", "HeuristicUnreachableCode")]
2325
public static void AssertEquality<TKey, TValue>(this IDictionary<TKey, TValue> target, IDictionary<TKey, TValue> copy)
2426
where TValue : IComparable
2527
{
@@ -61,12 +63,10 @@ public static void AssertEquality<TKey, TValue>(this IDictionary<TKey, TValue> t
6163
{
6264
var a = target[key];
6365
var b = copy[key];
64-
if (!a.IsNearEqual(b, 0.001))
65-
{
66-
Debugger.Break();
67-
Debug.Fail("Copied value is not equal!");
68-
return;
69-
}
66+
if (a.IsNearEqual(b, 0.001)) continue;
67+
Debugger.Break();
68+
Debug.Fail("Copied value is not equal!");
69+
return;
7070
}
7171
}
7272
}
@@ -75,7 +75,8 @@ public static void AssertEquality<TKey, TValue>(this IDictionary<TKey, TValue> t
7575
/// <summary>
7676
/// Creates a single dictionary containing the sum of the values grouped by cacheKey.
7777
/// </summary>
78-
/// <param name="autoPrecision">True is keepgoing accurate but less performant. False uses default double precision math.</param>
78+
/// <param name="values">The source enumerable.</param>
79+
/// <param name="autoPrecision">True is more accurate but less performant. False uses default double precision math.</param>
7980
public static IDictionary<TKey, double> SumValues<TKey>(this IEnumerable<IDictionary<TKey, double>> values, bool autoPrecision = true)
8081
where TKey : IComparable
8182
{
@@ -96,7 +97,9 @@ public static IDictionary<TKey, double> SumValues<TKey>(this IEnumerable<IDictio
9697
/// <summary>
9798
/// Creates a single sorted dictionary containing the sum of the values grouped by cacheKey.
9899
/// </summary>
99-
/// <param name="autoPrecision">True is keepgoing accurate but less performant. False uses default double precision math.</param>
100+
/// <param name="values">The source values.</param>
101+
/// <param name="autoPrecision">True is more accurate but less performant. False uses default double precision math.</param>
102+
/// <param name="allowParallel">Enables parallel processing of source enumerable.</param>
100103
public static SortedDictionary<TKey, double> SumValuesOrdered<TKey>(this IEnumerable<IDictionary<TKey, double>> values, bool autoPrecision = true, bool allowParallel = false)
101104
where TKey : IComparable
102105
{
@@ -118,7 +121,8 @@ public static SortedDictionary<TKey, double> SumValuesOrdered<TKey>(this IEnumer
118121
/// <summary>
119122
/// Creates a single sorted dictionary containing the sum of the values grouped by cacheKey.
120123
/// </summary>
121-
/// <param name="autoPrecision">True is keepgoing accurate but less performant. False uses default double precision math.</param>
124+
/// <param name="values">The source enumerable</param>
125+
/// <param name="autoPrecision">True is more accurate but less performant. False uses default double precision math.</param>
122126
public static SortedDictionary<TKey, double> SumValuesOrdered<TKey>(this ParallelQuery<IDictionary<TKey, double>> values, bool autoPrecision = true)
123127
where TKey : IComparable
124128
{
@@ -140,6 +144,7 @@ public static SortedDictionary<TKey, double> SumValuesOrdered<TKey>(this Paralle
140144
/// <summary>
141145
/// Returns how the set of values has changed.
142146
/// </summary>
147+
/// <param name="values">The source enumerable</param>
143148
public static IDictionary<TKey, double> Deltas<TKey>(this IEnumerable<KeyValuePair<TKey, double>> values)
144149
{
145150
if (values == null) throw new NullReferenceException();
@@ -161,6 +166,7 @@ public static IDictionary<TKey, double> Deltas<TKey>(this IEnumerable<KeyValuePa
161166
/// <summary>
162167
/// Is the effective inverse of Deltas. Renders the values as they are based on their changes.
163168
/// </summary>
169+
/// <param name="values">The source enumerable</param>
164170
public static IEnumerable<KeyValuePair<TKey, double>> DeltaCurve<TKey>(this IEnumerable<KeyValuePair<TKey, double>> values)
165171
{
166172
if (values == null) throw new NullReferenceException();
@@ -178,6 +184,7 @@ public static IEnumerable<KeyValuePair<TKey, double>> DeltaCurve<TKey>(this IEnu
178184
/// <summary>
179185
/// Returns how the set of values has changed.
180186
/// </summary>
187+
/// <param name="values">The source enumerable</param>
181188
public static IEnumerable<IDictionary<TKey, double>> Deltas<TKey>(this IEnumerable<IEnumerable<KeyValuePair<TKey, double>>> values)
182189
{
183190
if (values == null) throw new NullReferenceException();
@@ -189,6 +196,7 @@ public static IEnumerable<IDictionary<TKey, double>> Deltas<TKey>(this IEnumerab
189196
/// <summary>
190197
/// Returns how the set of values has changed.
191198
/// </summary>
199+
/// <param name="values">The source enumerable</param>
192200
public static ParallelQuery<IDictionary<TKey, double>> Deltas<TKey>(this ParallelQuery<IDictionary<TKey, double>> values)
193201
{
194202
if (values == null) throw new NullReferenceException();
@@ -200,21 +208,28 @@ public static ParallelQuery<IDictionary<TKey, double>> Deltas<TKey>(this Paralle
200208
/// <summary>
201209
/// Accurately adds the values from a set of curves and returns one curve.
202210
/// </summary>
203-
public static IEnumerable<KeyValuePair<TKey, double>> SumCurves<TKey>(this IEnumerable<IDictionary<TKey, double>> values, bool autoPrecision = false)
211+
/// <param name="values">The source enumerable</param>
212+
public static IEnumerable<KeyValuePair<TKey, double>> SumCurves<TKey>(this IEnumerable<IDictionary<TKey, double>> values)
204213
where TKey : IComparable
205214
{
206215
if (values == null) throw new NullReferenceException();
207216
Contract.EndContractBlock();
208217

209218
// Optimize to avoiding unnecessary processing...
210-
var one = values.Take(2).ToArray();
219+
var v = values.Memoize();
220+
var one = v.Take(2).ToArray();
211221

212-
if (one.Length == 0)
213-
return new SortedDictionary<TKey, double>();
214-
if (one.Length == 1)
215-
return new SortedDictionary<TKey, double>(one.Single());
222+
switch (one.Length)
223+
{
224+
case 0:
225+
v.Dispose();
226+
return new SortedDictionary<TKey, double>();
227+
case 1:
228+
v.Dispose();
229+
return new SortedDictionary<TKey, double>(one.Single());
230+
}
216231

217-
return values
232+
return v
218233
.Deltas()
219234
.SumValuesOrdered()
220235
.DeltaCurve();
@@ -223,6 +238,7 @@ public static IEnumerable<KeyValuePair<TKey, double>> SumCurves<TKey>(this IEnum
223238
/// <summary>
224239
/// Accurately adds the values from a set of curves and returns one curve.
225240
/// </summary>
241+
// ReSharper disable once UnusedParameter.Global
226242
public static IEnumerable<KeyValuePair<TKey, double>> SumCurves<TKey>(this ParallelQuery<IDictionary<TKey, double>> values, bool autoPrecision = false)
227243
where TKey : IComparable
228244
{
@@ -340,7 +356,7 @@ public static void AddValue<TKey>(this ConcurrentDictionary<TKey, double> target
340356

341357
/// <summary>
342358
/// Adds a value to the colleciton or replaces the existing value with the sum of the two.
343-
/// Uses a keepgoing accurate and less performant method instead of double precision math.
359+
/// Uses a more accurate and less performant method instead of double precision math.
344360
/// </summary>
345361
public static void AddValueAccurate<TKey>(this ConcurrentDictionary<TKey, double> target, TKey key, double value)
346362
{
@@ -457,7 +473,7 @@ public static void AddValues<TKey>(this IDictionary<TKey, double> target, IDicti
457473

458474
/// <summary>
459475
/// Adds values to the colleciton or replaces the existing values with the sum of the two.
460-
/// Uses a keepgoing accurate and less performant method instead of double precision math.
476+
/// Uses a more accurate and less performant method instead of double precision math.
461477
/// </summary>
462478
public static void AddValuesAccurateSelective<TKey>(this ConcurrentDictionary<TKey, double> target, IDictionary<TKey, double> add, bool allowParallel = false)
463479
{
@@ -472,7 +488,7 @@ public static void AddValuesAccurateSelective<TKey>(this ConcurrentDictionary<TK
472488

473489
/// <summary>
474490
/// Adds values to the colleciton or replaces the existing values with the sum of the two.
475-
/// Uses a keepgoing accurate and less performant method instead of double precision math.
491+
/// Uses a more accurate and less performant method instead of double precision math.
476492
/// </summary>
477493
public static void AddValuesAccurate<TKey>(this ConcurrentDictionary<TKey, double> target, IDictionary<TKey, double> add)
478494
{
@@ -482,7 +498,7 @@ public static void AddValuesAccurate<TKey>(this ConcurrentDictionary<TKey, doubl
482498
throw new ArgumentNullException(nameof(add));
483499
Contract.EndContractBlock();
484500

485-
AddValuesAccurateSelective(target, add, false);
501+
AddValuesAccurateSelective(target, add);
486502
}
487503

488504
#endregion
@@ -543,7 +559,7 @@ public static void AddValue<TKey>(this IDictionary<TKey, uint> target, TKey key,
543559

544560
/// <summary>
545561
/// Adds a value to the colleciton or replaces the existing value with the sum of the two.
546-
/// Uses a keepgoing accurate and less performant method instead of double precision math.
562+
/// Uses a more accurate and less performant method instead of double precision math.
547563
/// </summary>
548564
public static void AddValueAccurateSynchronized<TKey>(this IDictionary<TKey, double> target, TKey key, double value)
549565
{
@@ -638,7 +654,7 @@ public static void AddValueSynchronized<TKey>(this IDictionary<TKey, double> tar
638654

639655
/// <summary>
640656
/// Adds values to the colleciton or replaces the existing values with the sum of the two.
641-
/// Uses a keepgoing accurate and less performant method instead of double precision math.
657+
/// Uses a more accurate and less performant method instead of double precision math.
642658
/// </summary>
643659
public static void AddValueAccurateSynchronized<TKey>(this IDictionary<TKey, double> target, IDictionary<TKey, double> add)
644660
{

Open.Collections.Numeric.csproj

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ Part of the "Open" set of libraries.</Description>
1515
<RepositoryUrl>https://github.com/electricessence/Open.Collections.Numeric/</RepositoryUrl>
1616
<RepositoryType>git</RepositoryType>
1717
<PackageTags>dotnet, dotnet-core, dotnetcore, cs, collections, numeric, double, extensions, threadsafe, thread-safe</PackageTags>
18-
<Version>1.1.0</Version>
19-
<PackageReleaseNotes>Updated to .NET Standard for compatability.</PackageReleaseNotes>
20-
<AssemblyVersion>1.1.0.0</AssemblyVersion>
21-
<FileVersion>1.1.0.0</FileVersion>
18+
<Version>1.2.1</Version>
19+
<PackageReleaseNotes></PackageReleaseNotes>
20+
<AssemblyVersion>1.2.1.0</AssemblyVersion>
21+
<FileVersion>1.2.1.0</FileVersion>
2222
</PropertyGroup>
2323

2424
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
@@ -37,9 +37,9 @@ Part of the "Open" set of libraries.</Description>
3737
</ItemGroup>
3838

3939
<ItemGroup>
40-
<PackageReference Include="Open.Collections" Version="2.3.0" />
41-
<PackageReference Include="Open.Numeric" Version="1.2.0" />
42-
<PackageReference Include="Open.Threading" Version="1.4.0" />
40+
<PackageReference Include="Open.Collections" Version="2.4.4" />
41+
<PackageReference Include="Open.Numeric" Version="1.2.1" />
42+
<PackageReference Include="Open.Threading" Version="1.4.2" />
4343
</ItemGroup>
4444

4545
</Project>

0 commit comments

Comments
 (0)