99using System . Collections . Concurrent ;
1010using System . Collections . Generic ;
1111using System . Diagnostics ;
12+ using System . Diagnostics . CodeAnalysis ;
1213using System . Diagnostics . Contracts ;
1314using System . Linq ;
1415using 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 {
0 commit comments