X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Futils.ts;h=fc328a6c688e2d85f04a04be0e4874584ad5c57c;hb=557991e8ddb66a72a47d62606e142e8be86677f2;hp=753219c5e37d367b8d5d83790f76847445d36663;hpb=4f3c3d894171421375559b43ce469bd5ccb475da;p=poolifier.git diff --git a/src/utils.ts b/src/utils.ts index 753219c5..fc328a6c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,5 @@ +import type { WorkerChoiceStrategyOptions } from './pools/selection-strategies/selection-strategies-types' + /** * An intentional empty function. */ @@ -6,6 +8,27 @@ export const EMPTY_FUNCTION: () => void = Object.freeze(() => { }) /** - * An intentional empty object literal. + * Default worker choice strategy options. + */ +export const DEFAULT_WORKER_CHOICE_STRATEGY_OPTIONS: WorkerChoiceStrategyOptions = + { + medRunTime: false + } + +/** + * Compute the median of the given data set. + * + * @param dataSet - Data set. + * @returns The median of the given data set. */ -export const EMPTY_OBJECT_LITERAL = {} +export const median = (dataSet: number[]): number => { + if (Array.isArray(dataSet) && dataSet.length === 1) { + return dataSet[0] + } + const sortedDataSet = dataSet.slice().sort((a, b) => a - b) + const middleIndex = Math.floor(sortedDataSet.length / 2) + if (sortedDataSet.length % 2 === 0) { + return sortedDataSet[middleIndex / 2] + } + return (sortedDataSet[middleIndex - 1] + sortedDataSet[middleIndex]) / 2 +}