Skip to content

Commit 27431a9

Browse files
💥 progress!: Allow BigInt/BigUint arrays and simplify.
BREAKING CHANGE: found/pos only take search output as argument.
1 parent abfc999 commit 27431a9

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

src/defaults.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
export const increasing = (a, b) => a - b;
2-
export const decreasing = (a, b) => b - a;
1+
export const increasing = (a, b) => Number(a - b);
2+
export const decreasing = (a, b) => Number(b - a);
33

44
export const DELTA = [increasing, decreasing];
55

@@ -19,6 +19,6 @@ export const ARRAY = [
1919
Uint32Array,
2020
Float32Array,
2121
Float64Array,
22-
// BigInt64Array,
23-
// BigUint64Array,
22+
BigInt64Array,
23+
BigUint64Array,
2424
];

src/fixtures.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,13 @@ export const arrayMaxValue = (ctor) => {
7373
throw new Error(`unknown array constructor ${ctor}`);
7474
}
7575
};
76+
77+
export const arrayValue = (ctor, v) => {
78+
switch (ctor) {
79+
case BigInt64Array:
80+
case BigUint64Array:
81+
return BigInt(v);
82+
default:
83+
return v;
84+
}
85+
};

src/macro.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {copy} from '@array-like/copy';
2-
import {entropy} from './fixtures.js';
2+
import {arrayValue, entropy} from './fixtures.js';
33
import {increasing, decreasing} from './defaults.js';
44

55
const macro = (
@@ -9,13 +9,12 @@ const macro = (
99
const {randint} = entropy(seed);
1010
// SETUP REF ARRAY
1111

12-
let _max = max;
13-
++_max;
14-
let _min = min;
15-
--_min;
12+
const _max = arrayValue(array, max + 1);
13+
const _min = arrayValue(array, min - 1);
1614
// eslint-disable-next-line new-cap
1715
const ref = new array(length);
18-
for (let j = 0; j < length; ++j) ref[j] = randint(min, _max);
16+
for (let j = 0; j < length; ++j)
17+
ref[j] = arrayValue(array, randint(min, max + 1));
1918
Array.prototype.sort.call(ref, delta);
2019

2120
// SETUP TEST ARRAY
@@ -28,25 +27,25 @@ const macro = (
2827
// CHECK > OUTER BOUND
2928
let v = delta(-1, 0) < 0 ? _max : _min;
3029
let r = search(delta, a, 0, length, v);
31-
t.false(found(delta, a, 0, length, r), `not found ${v}`);
32-
t.is(pos(delta, a, 0, length, r), length, `where === ${length}`);
30+
t.false(found(r), `not found ${v}`);
31+
t.is(pos(r), length, `where === ${length}`);
3332

3433
// CHECK BODY
3534
for (let i = length - 1; i >= 0; --i) {
3635
r = search(delta, a, 0, length, a[i]);
37-
t.true(found(delta, a, 0, length, r), `found a[${i}]`);
38-
t.deepEqual(a[pos(delta, a, 0, length, r)], a[i], `val === ${a[i]}`);
36+
t.true(found(r), `found a[${i}]`);
37+
t.deepEqual(a[pos(r)], a[i], `val === ${a[i]}`);
3938
}
4039

4140
// CHECK < OUTER BOUND
4241
v = delta(-1, 0) > 0 ? _max : _min;
4342
r = search(delta, a, 0, length, v);
44-
t.false(found(delta, a, 0, length, r), 'not found -1');
45-
t.is(pos(delta, a, 0, length, r), 0, 'where === 0');
43+
t.false(found(r), 'not found -1');
44+
t.is(pos(r), 0, 'where === 0');
4645
} else {
47-
const r = search(delta, a, 0, length, -1);
48-
t.false(found(delta, a, 0, length, r), 'not found -1');
49-
t.is(pos(delta, a, 0, length, r), 0, 'where === 0');
46+
const r = search(delta, a, 0, length, _min);
47+
t.false(found(r), 'not found -1');
48+
t.is(pos(r), 0, 'where === 0');
5049
}
5150

5251
// CHECK NOT MODIFIED

src/units.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const units = function* ({
1313
delta: deltas = DELTA,
1414
min: mins = MIN,
1515
max: maxs = MAX,
16-
pos = (_delta, _a, _i, _j, r) => _pos(r),
17-
found = (_delta, _a, _i, _j, r) => _found(r),
16+
pos = _pos,
17+
found = _found,
1818
}) {
1919
for (const [array, min] of product([arrays, mins])) {
2020
if (min < arrayMinValue(array)) continue;

0 commit comments

Comments
 (0)