Skip to content

Commit 81032e9

Browse files
committed
use a simple boolean instead of a count to determine if enumeration has values
1 parent 8e0469b commit 81032e9

4 files changed

Lines changed: 27 additions & 27 deletions

File tree

LinqStatistics/EnumerableStatsMinMax.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ public static Range<int> MinMax(this IEnumerable<int> source)
4040
// so that the first comparisons in the aggregate function work as expected
4141
var minMax = new Range<int>(int.MaxValue, int.MinValue, true);
4242

43-
long n = 0;
43+
bool any = false;
4444
var result = source.Aggregate<int, Range<int>>(minMax, (accumulator, value) =>
4545
{
4646
var min = Math.Min(accumulator.Min, value);
4747
var max = Math.Max(accumulator.Max, value);
48-
n++;
48+
any = true;
4949

5050
return new Range<int>(min, max);
5151
});
5252

53-
if (n > 0)
53+
if (any)
5454
return result;
5555

5656
throw new InvalidOperationException("source sequence contains no elements");
@@ -122,17 +122,17 @@ public static Range<long> MinMax(this IEnumerable<long> source)
122122
// so that the first comparisons in the aggregate function work as expected
123123
var minMax = new Range<long>(long.MaxValue, long.MinValue, true);
124124

125-
long n = 0;
125+
bool any = false;
126126
var result = source.Aggregate<long, Range<long>>(minMax, (accumulator, value) =>
127127
{
128128
var min = Math.Min(accumulator.Min, value);
129129
var max = Math.Max(accumulator.Max, value);
130-
n++;
130+
any = true;
131131

132132
return new Range<long>(min, max);
133133
});
134134

135-
if (n > 0)
135+
if (any)
136136
return result;
137137

138138
throw new InvalidOperationException("source sequence contains no elements");
@@ -204,17 +204,17 @@ public static Range<float> MinMax(this IEnumerable<float> source)
204204
// so that the first comparisons in the aggregate function work as expected
205205
var minMax = new Range<float>(float.MaxValue, float.MinValue, true);
206206

207-
long n = 0;
207+
bool any = false;
208208
var result = source.Aggregate<float, Range<float>>(minMax, (accumulator, value) =>
209209
{
210210
var min = Math.Min(accumulator.Min, value);
211211
var max = Math.Max(accumulator.Max, value);
212-
n++;
212+
any = true;
213213

214214
return new Range<float>(min, max);
215215
});
216216

217-
if (n > 0)
217+
if (any)
218218
return result;
219219

220220
throw new InvalidOperationException("source sequence contains no elements");
@@ -286,17 +286,17 @@ public static Range<double> MinMax(this IEnumerable<double> source)
286286
// so that the first comparisons in the aggregate function work as expected
287287
var minMax = new Range<double>(double.MaxValue, double.MinValue, true);
288288

289-
long n = 0;
289+
bool any = false;
290290
var result = source.Aggregate<double, Range<double>>(minMax, (accumulator, value) =>
291291
{
292292
var min = Math.Min(accumulator.Min, value);
293293
var max = Math.Max(accumulator.Max, value);
294-
n++;
294+
any = true;
295295

296296
return new Range<double>(min, max);
297297
});
298298

299-
if (n > 0)
299+
if (any)
300300
return result;
301301

302302
throw new InvalidOperationException("source sequence contains no elements");
@@ -368,17 +368,17 @@ public static Range<decimal> MinMax(this IEnumerable<decimal> source)
368368
// so that the first comparisons in the aggregate function work as expected
369369
var minMax = new Range<decimal>(decimal.MaxValue, decimal.MinValue, true);
370370

371-
long n = 0;
371+
bool any = false;
372372
var result = source.Aggregate<decimal, Range<decimal>>(minMax, (accumulator, value) =>
373373
{
374374
var min = Math.Min(accumulator.Min, value);
375375
var max = Math.Max(accumulator.Max, value);
376-
n++;
376+
any = true;
377377

378378
return new Range<decimal>(min, max);
379379
});
380380

381-
if (n > 0)
381+
if (any)
382382
return result;
383383

384384
throw new InvalidOperationException("source sequence contains no elements");

LinqStatistics/EnumerableStatsMinMax.tt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ namespace LinqStatistics
5252
// so that the first comparisons in the aggregate function work as expected
5353
var minMax = new Range<<#= type #>>(<#= type #>.MaxValue, <#= type #>.MinValue, true);
5454

55-
long n = 0;
55+
bool any = false;
5656
var result = source.Aggregate<<#= type #>, Range<<#= type #>>>(minMax, (accumulator, value) =>
5757
{
5858
var min = Math.Min(accumulator.Min, value);
5959
var max = Math.Max(accumulator.Max, value);
60-
n++;
60+
any = true;
6161

6262
return new Range<<#= type #>>(min, max);
6363
});
6464

65-
if (n > 0)
65+
if (any)
6666
return result;
6767

6868
throw new InvalidOperationException("source sequence contains no elements");

LinqStatistics/NaN/EnumerableStatsMinMax.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@ public static Range<float> MinMaxNaN(this IEnumerable<float> source)
4040
// so that the first comparisons in the aggregate function work as expected
4141
var minMax = new Range<float>(float.MaxValue, float.MinValue, true);
4242

43-
long n = 0;
43+
bool any = false;
4444
var result = source.Aggregate<float, Range<float>>(minMax, (accumulator, value) =>
4545
{
4646
var min = Math.Min(accumulator.Min, value);
4747
var max = Math.Max(accumulator.Max, value);
48-
n++;
48+
any = true;
4949

5050
return new Range<float>(min, max);
5151
});
5252

53-
if (n > 0)
53+
if (any)
5454
return result;
5555

5656
return new Range<float>(float.NaN, float.NaN);
@@ -122,17 +122,17 @@ public static Range<double> MinMaxNaN(this IEnumerable<double> source)
122122
// so that the first comparisons in the aggregate function work as expected
123123
var minMax = new Range<double>(double.MaxValue, double.MinValue, true);
124124

125-
long n = 0;
125+
bool any = false;
126126
var result = source.Aggregate<double, Range<double>>(minMax, (accumulator, value) =>
127127
{
128128
var min = Math.Min(accumulator.Min, value);
129129
var max = Math.Max(accumulator.Max, value);
130-
n++;
130+
any = true;
131131

132132
return new Range<double>(min, max);
133133
});
134134

135-
if (n > 0)
135+
if (any)
136136
return result;
137137

138138
return new Range<double>(double.NaN, double.NaN);

LinqStatistics/NaN/EnumerableStatsMinMax.tt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ namespace LinqStatistics.NaN
5252
// so that the first comparisons in the aggregate function work as expected
5353
var minMax = new Range<<#= type #>>(<#= type #>.MaxValue, <#= type #>.MinValue, true);
5454

55-
long n = 0;
55+
bool any = false;
5656
var result = source.Aggregate<<#= type #>, Range<<#= type #>>>(minMax, (accumulator, value) =>
5757
{
5858
var min = Math.Min(accumulator.Min, value);
5959
var max = Math.Max(accumulator.Max, value);
60-
n++;
60+
any = true;
6161

6262
return new Range<<#= type #>>(min, max);
6363
});
6464

65-
if (n > 0)
65+
if (any)
6666
return result;
6767

6868
return new Range<<#= type #>>(<#= type #>.NaN, <#= type #>.NaN);

0 commit comments

Comments
 (0)