Skip to content

Commit b86ee6e

Browse files
Fix slicing out of order
1 parent b1fe5eb commit b86ee6e

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

tfjs-core/src/io/weights_loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ function search<T>(sortedArray: T[], compare: (t: T) => number): number {
399399
let max = sortedArray.length;
400400

401401
while (min <= max) {
402-
const middle = Math.floor((max - min) / 2);
402+
const middle = Math.floor((max - min) / 2) + min;
403403
const side = compare(sortedArray[middle]);
404404

405405
if (side === 0) {

tfjs-core/src/io/weights_loader_test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,12 @@ describe('CompositeArrayBuffer', () => {
597597
expectArraysEqual(new Uint8Array(composite.slice(7, 1000)),
598598
[7,8,9,10,11,12,13,14,15,16]);
599599
});
600+
601+
it(`${buffersType}: slices multiple ranges out of order`, () => {
602+
expectArraysEqual(new Uint8Array(composite.slice(13, 15)), [13, 14]);
603+
expectArraysEqual(new Uint8Array(composite.slice(0, 2)), [0, 1]);
604+
expectArraysEqual(new Uint8Array(composite.slice(9, 13)), [9, 10, 11, 12]);
605+
});
600606
}
601607

602608
it('can be passed an empty arraybuffer', () => {

0 commit comments

Comments
 (0)