|
| 1 | +// eslint-disable-next-line ava/use-test |
| 2 | +import ava from 'ava'; |
| 3 | + |
| 4 | +import * as spec from '@aureooms/js-in-situ-sort-spec'; |
| 5 | +import {min, copy} from '@aureooms/js-array'; |
| 6 | +import {increasing, decreasing} from '@aureooms/js-compare'; |
| 7 | +import { |
| 8 | + sort, |
| 9 | + sortTypedIncreasing, |
| 10 | + sortTypedDecreasing, |
| 11 | + sortTypedIncreasingOptimized, |
| 12 | + sortTypedDecreasingOptimized, |
| 13 | +} from '../../src'; |
| 14 | + |
| 15 | +const sortTyped = (compare, a, i, j) => { |
| 16 | + switch (compare) { |
| 17 | + case increasing: |
| 18 | + return sortTypedIncreasing(a, i, j); |
| 19 | + case decreasing: |
| 20 | + return sortTypedDecreasing(a, i, j); |
| 21 | + default: |
| 22 | + throw new Error( |
| 23 | + 'First argument `compare` should be one of {increasing, decreasing}.', |
| 24 | + ); |
| 25 | + } |
| 26 | +}; |
| 27 | + |
| 28 | +const sortTypedOptimized = (compare, a, i, j) => { |
| 29 | + const X = a.constructor; |
| 30 | + const N = j - i; |
| 31 | + const b = new X(N + 1); |
| 32 | + copy(a, i, j, b, 1); |
| 33 | + b[0] = min(compare, a, i, j); |
| 34 | + switch (compare) { |
| 35 | + case increasing: |
| 36 | + sortTypedIncreasingOptimized(b, 1, N + 1); |
| 37 | + return copy(b, 1, N + 1, a, i); |
| 38 | + case decreasing: |
| 39 | + sortTypedDecreasingOptimized(b, 1, N + 1); |
| 40 | + return copy(b, 1, N + 1, a, i); |
| 41 | + default: |
| 42 | + throw new Error( |
| 43 | + 'First argument `compare` should be one of {increasing, decreasing}.', |
| 44 | + ); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +spec.test(ava, [ |
| 49 | + ['sort', sort], |
| 50 | + ['sortTyped', sortTyped], |
| 51 | + ['sortTypedOptimized', sortTypedOptimized], |
| 52 | +]); |
0 commit comments