diff --git a/src/util/chunk.ts b/src/util/chunk.ts index 36620627..f6912ddd 100644 --- a/src/util/chunk.ts +++ b/src/util/chunk.ts @@ -138,11 +138,25 @@ export function wrapChunkedCall2(fn: (c: Data, _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(indexOf: (c: Data, e: T['TValue'], o?: number) => any) { let _1: any; function chunkedIndexOf(data: ReadonlyArray>, 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))) { @@ -156,9 +170,10 @@ export function wrapChunkedIndexOf(indexOf: (c: Data, e: return function (this: any, element: T['TValue'], offset?: number) { _1 = element; const data = this.data as ReadonlyArray>; - 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; }; diff --git a/src/vector.ts b/src/vector.ts index a72f0068..98a77bcb 100644 --- a/src/vector.ts +++ b/src/vector.ts @@ -30,6 +30,7 @@ import { wrapChunkedCall1, wrapChunkedCall2, wrapChunkedIndexOf, + toIndexOfStart, } from './util/chunk.js'; import { instance as getVisitor } from './visitor/get.js'; @@ -90,7 +91,7 @@ export class Vector { 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; } diff --git a/src/visitor/indexof.ts b/src/visitor/indexof.ts index ea3869c8..811f0efa 100644 --- a/src/visitor/indexof.ts +++ b/src/visitor/indexof.ts @@ -102,19 +102,19 @@ export interface IndexOfVisitor extends Visitor { export class IndexOfVisitor extends Visitor { } /** @ignore */ -function nullIndexOf(data: Data, 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, 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(data: Data, fromIndex?: number): number { +function indexOfNull(data: Data, 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; } diff --git a/test/unit/vector/vector-tests.ts b/test/unit/vector/vector-tests.ts index d8199e57..8dc785c0 100644 --- a/test/unit/vector/vector-tests.ts +++ b/test/unit/vector/vector-tests.ts @@ -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`