Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/util/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,25 @@ export function wrapChunkedCall2<T extends DataType>(fn: (c: Data<T>, _1: number
};
}

/**
* Turns an `indexOf` offset into a start index, using the same rules as
* `Array.prototype.indexOf`: fractions are truncated and a negative offset
* counts back from the end.
* @ignore
*/
export function toIndexOfStart(offset: number | undefined, length: number) {
const index = Math.trunc(Number(offset)) || 0;
return index < 0 ? Math.max(0, length + index) : index;
}

/** @ignore */
export function wrapChunkedIndexOf<T extends DataType>(indexOf: (c: Data<T>, e: T['TValue'], o?: number) => any) {
let _1: any;
function chunkedIndexOf(data: ReadonlyArray<Data<T>>, chunkIndex: number, fromIndex: number) {
let begin = fromIndex, index = 0, total = 0;
for (let i = -1; ++i < chunkIndex;) {
total += data[i].length;
}
for (let i = chunkIndex - 1, n = data.length; ++i < n;) {
const chunk = data[i];
if (~(index = indexOf(chunk, _1, begin))) {
Expand All @@ -156,9 +170,10 @@ export function wrapChunkedIndexOf<T extends DataType>(indexOf: (c: Data<T>, e:
return function (this: any, element: T['TValue'], offset?: number) {
_1 = element;
const data = this.data as ReadonlyArray<Data<T>>;
const result = typeof offset !== 'number'
const start = toIndexOfStart(offset, this.length);
const result = start === 0
? chunkedIndexOf(data, 0, 0)
: binarySearch(data, this._offsets, offset, chunkedIndexOf);
: binarySearch(data, this._offsets, start, chunkedIndexOf) ?? -1;
_1 = undefined;
return result;
};
Expand Down
3 changes: 2 additions & 1 deletion src/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
wrapChunkedCall1,
wrapChunkedCall2,
wrapChunkedIndexOf,
toIndexOfStart,
} from './util/chunk.js';

import { instance as getVisitor } from './visitor/get.js';
Expand Down Expand Up @@ -90,7 +91,7 @@ export class Vector<T extends DataType = any> {
this.isValid = (index: number) => isChunkedValid(unchunkedData, index);
this.get = (index: number) => get(unchunkedData, index);
this.set = (index: number, value: T) => set(unchunkedData, index, value);
this.indexOf = (index: number) => indexOf(unchunkedData, index);
this.indexOf = (element: T['TValue'], offset?: number) => indexOf(unchunkedData, element, toIndexOfStart(offset, unchunkedData.length));
this._offsets = [0, unchunkedData.length];
break;
}
Expand Down
12 changes: 6 additions & 6 deletions src/visitor/indexof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ export interface IndexOfVisitor extends Visitor {
export class IndexOfVisitor extends Visitor { }

/** @ignore */
function nullIndexOf(data: Data<Null>, searchElement?: null) {
// if you're looking for nulls and the vector isn't empty, we've got 'em!
return searchElement === null && data.length > 0 ? 0 : -1;
function nullIndexOf(data: Data<Null>, searchElement?: null, fromIndex = 0) {
// if you're looking for nulls and the search starts inside the vector, we've got 'em!
return searchElement === null && fromIndex < data.length ? fromIndex : -1;
}

/** @ignore */
function indexOfNull<T extends DataType>(data: Data<T>, fromIndex?: number): number {
function indexOfNull<T extends DataType>(data: Data<T>, fromIndex = 0): number {
const { nullBitmap } = data;
if (!nullBitmap || data.nullCount <= 0) {
return -1;
}
let i = 0;
for (const isValid of new BitIterator(nullBitmap, data.offset + (fromIndex || 0), data.length, nullBitmap, getBool)) {
let i = fromIndex;
for (const isValid of new BitIterator(nullBitmap, data.offset + i, data.length - i, nullBitmap, getBool)) {
if (!isValid) { return i; }
++i;
}
Expand Down
25 changes: 25 additions & 0 deletions test/unit/vector/vector-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,31 @@ describe(`toArray()`, () => {
});
});

describe(`indexOf() and includes() with an offset`, () => {
const values = [5, 7, 5, 7, 5];
const withNulls = [1, null, 2, null, 3];
const cases: [string, (number | null)[], Vector][] = [
['one chunk', values, vectorFromArray(values, new Int32)],
['two chunks', values, vectorFromArray([5, 7], new Int32).concat(vectorFromArray([5, 7, 5], new Int32))],
['one chunk with nulls', withNulls, vectorFromArray(withNulls, new Int32)],
['two chunks with nulls', withNulls, vectorFromArray([1, null], new Int32).concat(vectorFromArray([2, null, 3], new Int32))],
['Null type', [null, null, null], vectorFromArray([null, null, null])],
];

const offsets = [undefined, 0, 1, 1.5, 2, 3, 4, 5, 6, 99, -1, -2, -4, -99, Number.NaN, Infinity, -Infinity];

for (const [name, expected, vector] of cases) {
test(`${name}: same results as Array.prototype.indexOf`, () => {
for (const value of [...new Set(expected), 9]) {
for (const offset of offsets) {
expect(vector.indexOf(value, offset)).toBe(expected.indexOf(value, offset));
expect(vector.includes(value, offset)).toBe(expected.includes(value, offset));
}
}
});
}
});

// Creates some basic tests for the given vector.
// Verifies that:
// - `get` and the native iterator return the same data as `values`
Expand Down