@@ -326,6 +326,14 @@ public struct Histogram<Count: FixedWidthInteger & Codable>: Codable {
326
326
* in the histogram are either larger than or equivalent to. Returns 0 if no recorded values exist.
327
327
*/
328
328
public func valueAtPercentile( _ percentile: Double ) -> UInt64 {
329
+ guard percentile != 0.0 else {
330
+ return self . min
331
+ }
332
+
333
+ guard percentile < 100.0 else {
334
+ return maxRecorded
335
+ }
336
+
329
337
// Truncate to 0..100%, and remove 1 ulp to avoid roundoff overruns into next bucket when we
330
338
// subsequently round up to the nearest integer:
331
339
let requestedPercentile = Swift . min ( Swift . max ( percentile. nextDown, 0.0 ) , 100.0 )
@@ -342,9 +350,8 @@ public struct Histogram<Count: FixedWidthInteger & Codable>: Codable {
342
350
totalToCurrentIndex += UInt64 ( counts [ i] )
343
351
if totalToCurrentIndex >= countAtPercentile {
344
352
let valueAtIndex = valueFromIndex ( i)
345
- return ( percentile == 0.0 ) ?
346
- lowestEquivalentForValue ( valueAtIndex) :
347
- highestEquivalentForValue ( valueAtIndex)
353
+ let returnValue = highestEquivalentForValue ( valueAtIndex)
354
+ return Swift . max ( Swift . min ( returnValue, maxRecorded) , self . min)
348
355
}
349
356
}
350
357
return 0
@@ -435,7 +442,7 @@ public struct Histogram<Count: FixedWidthInteger & Codable>: Codable {
435
442
}
436
443
437
444
/**
438
- * Get the lowest recorded value level in the histogram.
445
+ * Get the lowest recorded value in the histogram.
439
446
*
440
447
* If the histogram has no recorded values, the value returned is undefined.
441
448
*
@@ -446,12 +453,25 @@ public struct Histogram<Count: FixedWidthInteger & Codable>: Codable {
446
453
}
447
454
448
455
/**
449
- * Get the highest recorded value level in the histogram.
456
+ * Get the highest recorded value in the histogram.
450
457
*
451
458
* If the histogram has no recorded values, the value returned is undefined.
452
459
*
453
460
* - Returns: The maximum value recorded in the histogram.
454
461
*/
462
+ public var maxRecorded : UInt64 {
463
+ maxValue
464
+ }
465
+
466
+ /**
467
+ * Get the highest recorded value level in the histogram.
468
+ *
469
+ * If the histogram has no recorded values, the value returned is undefined.
470
+ *
471
+ * NB this can be larger than the maximum recorded value due to precision.
472
+ *
473
+ * - Returns: The maximum value level recorded in the histogram.
474
+ */
455
475
public var max : UInt64 {
456
476
maxValue == 0 ? 0 : highestEquivalentForValue ( maxValue)
457
477
}
@@ -460,6 +480,7 @@ public struct Histogram<Count: FixedWidthInteger & Codable>: Codable {
460
480
* Get the lowest recorded non-zero value level in the histogram.
461
481
*
462
482
* If the histogram has no recorded values, the value returned is undefined.
483
+ * NB this can be smaller than the minimum recorded value due to precision.
463
484
*
464
485
* - Returns: The lowest recorded non-zero value level in the histogram.
465
486
*/
@@ -1347,3 +1368,5 @@ extension Histogram: TextOutputStreamable {
1347
1368
outputPercentileDistribution ( to: & to, outputValueUnitScalingRatio: 1.0 )
1348
1369
}
1349
1370
}
1371
+
1372
+ // swiftlint:enable file_length type_body_length line_length identifier_name
0 commit comments