( error: number, sampleCount: number, current: Option.Option<number>, consumed: number, quantile: number, rest: ReadonlyArray<number> )
| 352 | |
| 353 | /** @internal */ |
| 354 | const resolveQuantile = ( |
| 355 | error: number, |
| 356 | sampleCount: number, |
| 357 | current: Option.Option<number>, |
| 358 | consumed: number, |
| 359 | quantile: number, |
| 360 | rest: ReadonlyArray<number> |
| 361 | ): ResolvedQuantile => { |
| 362 | let error_1 = error |
| 363 | let sampleCount_1 = sampleCount |
| 364 | let current_1 = current |
| 365 | let consumed_1 = consumed |
| 366 | let quantile_1 = quantile |
| 367 | let rest_1 = rest |
| 368 | let error_2 = error |
| 369 | let sampleCount_2 = sampleCount |
| 370 | let current_2 = current |
| 371 | let consumed_2 = consumed |
| 372 | let quantile_2 = quantile |
| 373 | let rest_2 = rest |
| 374 | // eslint-disable-next-line no-constant-condition |
| 375 | while (1) { |
| 376 | // If the remaining list of samples is empty, there is nothing more to resolve |
| 377 | if (!Arr.isNonEmptyReadonlyArray(rest_1)) { |
| 378 | return { |
| 379 | quantile: quantile_1, |
| 380 | value: Option.none(), |
| 381 | consumed: consumed_1, |
| 382 | rest: [] |
| 383 | } |
| 384 | } |
| 385 | // If the quantile is the 100% quantile, we can take the maximum of all the |
| 386 | // remaining values as the result |
| 387 | if (quantile_1 === 1) { |
| 388 | return { |
| 389 | quantile: quantile_1, |
| 390 | value: Option.some(Arr.lastNonEmpty(rest_1)), |
| 391 | consumed: consumed_1 + rest_1.length, |
| 392 | rest: [] |
| 393 | } |
| 394 | } |
| 395 | // Split into two chunks - the first chunk contains all elements of the same |
| 396 | // value as the chunk head |
| 397 | const headValue = Arr.headNonEmpty(rest_1) // Get head value since rest_1 is non-empty |
| 398 | const sameHead = Arr.span(rest_1, (n) => n === headValue) |
| 399 | // How many elements do we want to accept for this quantile |
| 400 | const desired = quantile_1 * sampleCount_1 |
| 401 | // The error margin |
| 402 | const allowedError = (error_1 / 2) * desired |
| 403 | // Taking into account the elements consumed from the samples so far and the |
| 404 | // number of same elements at the beginning of the chunk, calculate the number |
| 405 | // of elements we would have if we selected the current head as result |
| 406 | const candConsumed = consumed_1 + sameHead[0].length |
| 407 | const candError = Math.abs(candConsumed - desired) |
| 408 | // If we haven't got enough elements yet, recurse |
| 409 | if (candConsumed < desired - allowedError) { |
| 410 | error_2 = error_1 |
| 411 | sampleCount_2 = sampleCount_1 |
no test coverage detected