Skip to content

Commit

Permalink
better mimic js array in selection getters (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmschmidt authored Oct 26, 2024
1 parent 71b7bee commit 9aedf13
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
25 changes: 20 additions & 5 deletions src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,14 +805,17 @@ export class DataSelection {
* that contains the nth match, then iterate through the matches in that
* tile until we find the nth match.
*
* @param i the index of the row to get
* @param i the index of the row to get. If less than zero, will return
*/
get(i: number | undefined = undefined): StructRowProxy | undefined {
if (i === undefined) {
i = this.cursor;
}
if (i >= this.selectionSize) {
throw new Error('Selection out of bounds');
if (i >= this.selectionSize || i < -this.selectionSize) {
return undefined;
}
if (i < 0) {
i = this.selectionSize + i;
}
let currentOffset = 0;
let relevantTile: Tile | undefined = undefined;
Expand Down Expand Up @@ -1103,6 +1106,15 @@ export class SortedDataSelection extends DataSelection {
* This implementation uses Quickselect with a pivot selected from actual data.
*/
get(k: number): StructRowProxy | undefined {
if (k >= this.selectionSize || k < -this.selectionSize) {
return undefined;
}

if (k < 0) {
k = this.selectionSize + k;
}

// Implement Quickselect over the combined data
const actualK = this.order === 'ascending' ? k : this.selectionSize - k - 1;
// Implement Quickselect over the combined data
const result = quickSelect(actualK, this.tiles, this.key, true);
Expand Down Expand Up @@ -1236,7 +1248,10 @@ export class TileSorter
// Finally add to heap
for (const sortInfo of sortInfos) {
// Only add if there are indices and we haven't reached the end of the values
if (sortInfo.indices.length > 0 && sortInfo.pointer < sortInfo.values.length) {
if (
sortInfo.indices.length > 0 &&
sortInfo.pointer < sortInfo.values.length
) {
this.valueHeap.insert(sortInfo);
}
}
Expand All @@ -1257,7 +1272,7 @@ export class TileSorter
const rawSortInfo = tile.sorts[this.sortKey];
// Skip tiles with empty selections
if (rawSortInfo.indices.length <= 0) {
continue
continue;
}
const sortInfo: SortInfoWithPointer = {
...rawSortInfo,
Expand Down
15 changes: 11 additions & 4 deletions tests/dataset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ test('Test composition of selections', async () => {

await selectNothing.applyToAllLoadedTiles();
assert.is(selectNothing.selectionSize, 0);
assert.is(selectNothing.get(), undefined);
});

test('Test sorting of selections', async () => {
Expand All @@ -121,6 +122,12 @@ test('Test sorting of selections', async () => {
const mid = sorted.get(Math.floor(sorted.selectionSize / 2));
assert.ok(mid.random > 0.45);
assert.ok(mid.random < 0.55);

// Check negative offsets
const end = sorted.get(-1);
const end2 = sorted.get(8191);
assert.ok(end.random > 0.99);
assert.ok(end.ix === end2.ix);
});

test('Test iterated sorting of selections', async () => {
Expand Down Expand Up @@ -169,14 +176,14 @@ test('Test iterated sorting of selections', async () => {
const second = sorted.iterator(5);

const numbers = [0, 1, 2, 3, 5, 6, 7, 8, 9, 10];
const firstVals = numbers.map(d => first.next().value[sortKey])
const firstVals = numbers.map((d) => first.next().value[sortKey]);

for (let i = 0; i < 5; i++) {
assert.ok(firstVals[5 + i] === second.next().value[sortKey]);
}
});

test ('Iterated sorting of empty selection', async() => {
test('Iterated sorting of empty selection', async () => {
const dataset = createIntegerDataset();
await dataset.root_tile.preprocessRootTileInfo();
const emptySelection = new DataSelection(dataset, {
Expand Down Expand Up @@ -207,7 +214,7 @@ test ('Iterated sorting of empty selection', async() => {
thrown = true;
}
assert.ok(thrown);
})
});

test('Edge cases for iterated sorting of selections', async () => {
const dataset = createIntegerDataset();
Expand Down

0 comments on commit 9aedf13

Please sign in to comment.