Skip to content

Commit 8e0469b

Browse files
committed
eliminate Any redundant enumeration
1 parent 4183ea2 commit 8e0469b

4 files changed

Lines changed: 78 additions & 42 deletions

File tree

LinqStatistics/EnumerableStatsMinMax.cs

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,24 @@ public static Range<int> MinMax(this IEnumerable<int> source)
3636
if (source == null)
3737
throw new ArgumentNullException("source");
3838

39-
if (!source.Any())
40-
throw new InvalidOperationException("source sequence contains no elements");
41-
4239
// initialize minimum to max possible value and maximum to minimum possible value
4340
// so that the first comparisons in the aggregate function work as expected
4441
var minMax = new Range<int>(int.MaxValue, int.MinValue, true);
4542

46-
return source.Aggregate<int, Range<int>>(minMax, (accumulator, value) =>
43+
long n = 0;
44+
var result = source.Aggregate<int, Range<int>>(minMax, (accumulator, value) =>
4745
{
4846
var min = Math.Min(accumulator.Min, value);
4947
var max = Math.Max(accumulator.Max, value);
50-
48+
n++;
49+
5150
return new Range<int>(min, max);
5251
});
52+
53+
if (n > 0)
54+
return result;
55+
56+
throw new InvalidOperationException("source sequence contains no elements");
5357
}
5458

5559
/// <summary>
@@ -114,20 +118,24 @@ public static Range<long> MinMax(this IEnumerable<long> source)
114118
if (source == null)
115119
throw new ArgumentNullException("source");
116120

117-
if (!source.Any())
118-
throw new InvalidOperationException("source sequence contains no elements");
119-
120121
// initialize minimum to max possible value and maximum to minimum possible value
121122
// so that the first comparisons in the aggregate function work as expected
122123
var minMax = new Range<long>(long.MaxValue, long.MinValue, true);
123124

124-
return source.Aggregate<long, Range<long>>(minMax, (accumulator, value) =>
125+
long n = 0;
126+
var result = source.Aggregate<long, Range<long>>(minMax, (accumulator, value) =>
125127
{
126128
var min = Math.Min(accumulator.Min, value);
127129
var max = Math.Max(accumulator.Max, value);
128-
130+
n++;
131+
129132
return new Range<long>(min, max);
130133
});
134+
135+
if (n > 0)
136+
return result;
137+
138+
throw new InvalidOperationException("source sequence contains no elements");
131139
}
132140

133141
/// <summary>
@@ -192,20 +200,24 @@ public static Range<float> MinMax(this IEnumerable<float> source)
192200
if (source == null)
193201
throw new ArgumentNullException("source");
194202

195-
if (!source.Any())
196-
throw new InvalidOperationException("source sequence contains no elements");
197-
198203
// initialize minimum to max possible value and maximum to minimum possible value
199204
// so that the first comparisons in the aggregate function work as expected
200205
var minMax = new Range<float>(float.MaxValue, float.MinValue, true);
201206

202-
return source.Aggregate<float, Range<float>>(minMax, (accumulator, value) =>
207+
long n = 0;
208+
var result = source.Aggregate<float, Range<float>>(minMax, (accumulator, value) =>
203209
{
204210
var min = Math.Min(accumulator.Min, value);
205211
var max = Math.Max(accumulator.Max, value);
206-
212+
n++;
213+
207214
return new Range<float>(min, max);
208215
});
216+
217+
if (n > 0)
218+
return result;
219+
220+
throw new InvalidOperationException("source sequence contains no elements");
209221
}
210222

211223
/// <summary>
@@ -270,20 +282,24 @@ public static Range<double> MinMax(this IEnumerable<double> source)
270282
if (source == null)
271283
throw new ArgumentNullException("source");
272284

273-
if (!source.Any())
274-
throw new InvalidOperationException("source sequence contains no elements");
275-
276285
// initialize minimum to max possible value and maximum to minimum possible value
277286
// so that the first comparisons in the aggregate function work as expected
278287
var minMax = new Range<double>(double.MaxValue, double.MinValue, true);
279288

280-
return source.Aggregate<double, Range<double>>(minMax, (accumulator, value) =>
289+
long n = 0;
290+
var result = source.Aggregate<double, Range<double>>(minMax, (accumulator, value) =>
281291
{
282292
var min = Math.Min(accumulator.Min, value);
283293
var max = Math.Max(accumulator.Max, value);
284-
294+
n++;
295+
285296
return new Range<double>(min, max);
286297
});
298+
299+
if (n > 0)
300+
return result;
301+
302+
throw new InvalidOperationException("source sequence contains no elements");
287303
}
288304

289305
/// <summary>
@@ -348,20 +364,24 @@ public static Range<decimal> MinMax(this IEnumerable<decimal> source)
348364
if (source == null)
349365
throw new ArgumentNullException("source");
350366

351-
if (!source.Any())
352-
throw new InvalidOperationException("source sequence contains no elements");
353-
354367
// initialize minimum to max possible value and maximum to minimum possible value
355368
// so that the first comparisons in the aggregate function work as expected
356369
var minMax = new Range<decimal>(decimal.MaxValue, decimal.MinValue, true);
357370

358-
return source.Aggregate<decimal, Range<decimal>>(minMax, (accumulator, value) =>
371+
long n = 0;
372+
var result = source.Aggregate<decimal, Range<decimal>>(minMax, (accumulator, value) =>
359373
{
360374
var min = Math.Min(accumulator.Min, value);
361375
var max = Math.Max(accumulator.Max, value);
362-
376+
n++;
377+
363378
return new Range<decimal>(min, max);
364379
});
380+
381+
if (n > 0)
382+
return result;
383+
384+
throw new InvalidOperationException("source sequence contains no elements");
365385
}
366386

367387
/// <summary>

LinqStatistics/EnumerableStatsMinMax.tt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,24 @@ namespace LinqStatistics
4848
if (source == null)
4949
throw new ArgumentNullException("source");
5050

51-
if (!source.Any())
52-
throw new InvalidOperationException("source sequence contains no elements");
53-
5451
// initialize minimum to max possible value and maximum to minimum possible value
5552
// so that the first comparisons in the aggregate function work as expected
5653
var minMax = new Range<<#= type #>>(<#= type #>.MaxValue, <#= type #>.MinValue, true);
5754

58-
return source.Aggregate<<#= type #>, Range<<#= type #>>>(minMax, (accumulator, value) =>
55+
long n = 0;
56+
var result = source.Aggregate<<#= type #>, Range<<#= type #>>>(minMax, (accumulator, value) =>
5957
{
6058
var min = Math.Min(accumulator.Min, value);
6159
var max = Math.Max(accumulator.Max, value);
62-
60+
n++;
61+
6362
return new Range<<#= type #>>(min, max);
6463
});
64+
65+
if (n > 0)
66+
return result;
67+
68+
throw new InvalidOperationException("source sequence contains no elements");
6569
}
6670

6771
/// <summary>

LinqStatistics/NaN/EnumerableStatsMinMax.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,24 @@ public static Range<float> MinMaxNaN(this IEnumerable<float> source)
3636
if (source == null)
3737
throw new ArgumentNullException("source");
3838

39-
if (!source.Any())
40-
return new Range<float>(float.NaN, float.NaN);
41-
4239
// initialize minimum to max possible value and maximum to minimum possible value
4340
// so that the first comparisons in the aggregate function work as expected
4441
var minMax = new Range<float>(float.MaxValue, float.MinValue, true);
4542

46-
return source.Aggregate<float, Range<float>>(minMax, (accumulator, value) =>
43+
long n = 0;
44+
var result = source.Aggregate<float, Range<float>>(minMax, (accumulator, value) =>
4745
{
4846
var min = Math.Min(accumulator.Min, value);
4947
var max = Math.Max(accumulator.Max, value);
48+
n++;
5049

5150
return new Range<float>(min, max);
5251
});
52+
53+
if (n > 0)
54+
return result;
55+
56+
return new Range<float>(float.NaN, float.NaN);
5357
}
5458

5559
/// <summary>
@@ -114,20 +118,24 @@ public static Range<double> MinMaxNaN(this IEnumerable<double> source)
114118
if (source == null)
115119
throw new ArgumentNullException("source");
116120

117-
if (!source.Any())
118-
return new Range<double>(double.NaN, double.NaN);
119-
120121
// initialize minimum to max possible value and maximum to minimum possible value
121122
// so that the first comparisons in the aggregate function work as expected
122123
var minMax = new Range<double>(double.MaxValue, double.MinValue, true);
123124

124-
return source.Aggregate<double, Range<double>>(minMax, (accumulator, value) =>
125+
long n = 0;
126+
var result = source.Aggregate<double, Range<double>>(minMax, (accumulator, value) =>
125127
{
126128
var min = Math.Min(accumulator.Min, value);
127129
var max = Math.Max(accumulator.Max, value);
130+
n++;
128131

129132
return new Range<double>(min, max);
130133
});
134+
135+
if (n > 0)
136+
return result;
137+
138+
return new Range<double>(double.NaN, double.NaN);
131139
}
132140

133141
/// <summary>

LinqStatistics/NaN/EnumerableStatsMinMax.tt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,24 @@ namespace LinqStatistics.NaN
4848
if (source == null)
4949
throw new ArgumentNullException("source");
5050

51-
if (!source.Any())
52-
return new Range<<#= type #>>(<#= type #>.NaN, <#= type #>.NaN);
53-
5451
// initialize minimum to max possible value and maximum to minimum possible value
5552
// so that the first comparisons in the aggregate function work as expected
5653
var minMax = new Range<<#= type #>>(<#= type #>.MaxValue, <#= type #>.MinValue, true);
5754

58-
return source.Aggregate<<#= type #>, Range<<#= type #>>>(minMax, (accumulator, value) =>
55+
long n = 0;
56+
var result = source.Aggregate<<#= type #>, Range<<#= type #>>>(minMax, (accumulator, value) =>
5957
{
6058
var min = Math.Min(accumulator.Min, value);
6159
var max = Math.Max(accumulator.Max, value);
60+
n++;
6261

6362
return new Range<<#= type #>>(min, max);
6463
});
64+
65+
if (n > 0)
66+
return result;
67+
68+
return new Range<<#= type #>>(<#= type #>.NaN, <#= type #>.NaN);
6569
}
6670

6771
/// <summary>

0 commit comments

Comments
 (0)