|
| 1 | +import { isNumericLike, toPythonInteger, toPythonNumber } from './_operator.ts' |
| 2 | + |
| 3 | +type BisectSequence = unknown[] | string |
| 4 | + |
| 5 | +export function pythonBisect(sequence: unknown, item: unknown, lo?: unknown, hi?: unknown): number { |
| 6 | + return bisectCore(sequence, item, lo, hi, 'bisect', 'right') |
| 7 | +} |
| 8 | + |
| 9 | +export function pythonBisectLeft(sequence: unknown, item: unknown, lo?: unknown, hi?: unknown): number { |
| 10 | + return bisectCore(sequence, item, lo, hi, 'bisect_left', 'left') |
| 11 | +} |
| 12 | + |
| 13 | +export function pythonBisectRight(sequence: unknown, item: unknown, lo?: unknown, hi?: unknown): number { |
| 14 | + return bisectCore(sequence, item, lo, hi, 'bisect_right', 'right') |
| 15 | +} |
| 16 | + |
| 17 | +function bisectCore( |
| 18 | + sequence: unknown, |
| 19 | + item: unknown, |
| 20 | + lo: unknown, |
| 21 | + hi: unknown, |
| 22 | + functionName: string, |
| 23 | + side: 'left' | 'right', |
| 24 | +): number { |
| 25 | + const values = toBisectSequence(sequence, functionName) |
| 26 | + let left = normalizeLo(lo, functionName) |
| 27 | + let right = normalizeHi(hi, values.length, functionName) |
| 28 | + |
| 29 | + while (left < right) { |
| 30 | + const middle = Math.floor((left + right) / 2) |
| 31 | + const candidate = getSequenceItem(values, middle) |
| 32 | + |
| 33 | + if ( |
| 34 | + side === 'left' ? pythonLessThan(candidate, item, functionName) : !pythonLessThan(item, candidate, functionName) |
| 35 | + ) { |
| 36 | + left = middle + 1 |
| 37 | + } else { |
| 38 | + right = middle |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return left |
| 43 | +} |
| 44 | + |
| 45 | +function toBisectSequence(sequence: unknown, functionName: string): BisectSequence { |
| 46 | + if (Array.isArray(sequence) || typeof sequence === 'string') { |
| 47 | + return sequence |
| 48 | + } |
| 49 | + |
| 50 | + throw new TypeError(`${functionName}() expected an indexable sequence`) |
| 51 | +} |
| 52 | + |
| 53 | +function normalizeLo(value: unknown, functionName: string): number { |
| 54 | + if (value === undefined) { |
| 55 | + return 0 |
| 56 | + } |
| 57 | + |
| 58 | + const index = toSequenceIndex(value, functionName) |
| 59 | + if (index < 0) { |
| 60 | + throw new RangeError('lo must be non-negative') |
| 61 | + } |
| 62 | + |
| 63 | + return index |
| 64 | +} |
| 65 | + |
| 66 | +function normalizeHi(value: unknown, sequenceLength: number, functionName: string): number { |
| 67 | + if (value === undefined || value === null) { |
| 68 | + return sequenceLength |
| 69 | + } |
| 70 | + |
| 71 | + return toSequenceIndex(value, functionName) |
| 72 | +} |
| 73 | + |
| 74 | +function toSequenceIndex(value: unknown, functionName: string): number { |
| 75 | + const index = Number(toPythonInteger(value, functionName)) |
| 76 | + if (!Number.isSafeInteger(index)) { |
| 77 | + throw new RangeError(`${functionName}() index must fit within JS safe integer precision`) |
| 78 | + } |
| 79 | + |
| 80 | + return index |
| 81 | +} |
| 82 | + |
| 83 | +function getSequenceItem(sequence: BisectSequence, index: number): unknown { |
| 84 | + if (index < 0 || index >= sequence.length) { |
| 85 | + throw new Error(Array.isArray(sequence) ? 'list index out of range' : 'string index out of range') |
| 86 | + } |
| 87 | + |
| 88 | + return sequence[index] |
| 89 | +} |
| 90 | + |
| 91 | +function pythonLessThan(left: unknown, right: unknown, functionName: string): boolean { |
| 92 | + if (isNumericLike(left) && isNumericLike(right)) { |
| 93 | + const leftNumber = toPythonNumber(left, functionName) |
| 94 | + const rightNumber = toPythonNumber(right, functionName) |
| 95 | + if (Number.isNaN(leftNumber) || Number.isNaN(rightNumber)) { |
| 96 | + return false |
| 97 | + } |
| 98 | + |
| 99 | + return leftNumber < rightNumber |
| 100 | + } |
| 101 | + |
| 102 | + if (typeof left === 'string' && typeof right === 'string') { |
| 103 | + return left < right |
| 104 | + } |
| 105 | + |
| 106 | + throw new TypeError(`'<' not supported between instances of '${pythonTypeName(left)}' and '${pythonTypeName(right)}'`) |
| 107 | +} |
| 108 | + |
| 109 | +function pythonTypeName(value: unknown): string { |
| 110 | + if (typeof value === 'string') { |
| 111 | + return 'str' |
| 112 | + } |
| 113 | + |
| 114 | + if (typeof value === 'boolean') { |
| 115 | + return 'bool' |
| 116 | + } |
| 117 | + |
| 118 | + if (typeof value === 'bigint' || typeof value === 'number') { |
| 119 | + return 'int' |
| 120 | + } |
| 121 | + |
| 122 | + if (Array.isArray(value)) { |
| 123 | + return 'list' |
| 124 | + } |
| 125 | + |
| 126 | + if (value === null) { |
| 127 | + return 'NoneType' |
| 128 | + } |
| 129 | + |
| 130 | + if (value === undefined) { |
| 131 | + return 'undefined' |
| 132 | + } |
| 133 | + |
| 134 | + return typeof value |
| 135 | +} |
0 commit comments