Skip to content

Commit a9dd289

Browse files
authored
cleaner selection get methods (#199)
Split the new qid-getting method back off <!-- ELLIPSIS_HIDDEN --> ---- > [!IMPORTANT] > Refactor `get()` method in `DataSelection` and `SortedDataSelection` by splitting logic into `get()`, `getQid()`, and `getBase()` for improved clarity. > > - **Refactoring**: > - Split `get()` method in `DataSelection` and `SortedDataSelection` into `get()`, `getQid()`, and `getBase()` for clarity. > - `get()` now calls `getBase()` with `returnQid` set to `false`. > - `getQid()` added to return a tuple of `[tile index, row index]` by calling `getBase()` with `returnQid` set to `true`. > - `getBase()` contains the core logic for retrieving elements based on index. > > <sup>This description was created by </sup>[<img alt="Ellipsis" src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=nomic-ai%2Fdeepscatter&utm_source=github&utm_medium=referral)<sup> for 8489ba4. You can [customize](https://app.ellipsis.dev/nomic-ai/settings/summaries) this summary. It will automatically update as commits are pushed.</sup> <!-- ELLIPSIS_HIDDEN -->
1 parent 96728d5 commit a9dd289

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

src/selection.ts

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -798,22 +798,33 @@ export class DataSelection {
798798
return this.deeptable.highest_known_ix;
799799
}
800800

801+
/**
802+
*
803+
* Returns the nth element in the selection. This is a bit tricky because
804+
* the selection is stored as a list of tiles, each of which has a list of
805+
* matches. So we have to iterate through the tiles until we find the one
806+
* that contains the nth match, then iterate through the matches in that
807+
* tile until we find the nth match.
808+
*
809+
* @param i the index of the row to get. If less than zero, will return the element counting from the end of the selection.
810+
*/
811+
get(i: number | undefined = undefined): StructRowProxy {
812+
const result = this.getBase(i, false) as StructRowProxy;
813+
if (result === undefined) {
814+
throw new Error(`Index ${i} out of bounds`);
815+
}
816+
return result;
817+
}
801818

802-
get(i: number | undefined, returnQid: false): StructRowProxy | undefined;
803-
get(i: number | undefined, returnQid: true): [number, number] | undefined;
819+
getQid(i: number | undefined = undefined): [number, number] {
820+
const result = this.getBase(i, true) as [number, number];
821+
if (result === undefined) {
822+
throw new Error(`Index ${i} out of bounds`);
823+
}
824+
return result;
825+
}
804826

805-
/**
806-
*
807-
* Returns the nth element in the selection. This is a bit tricky because
808-
* the selection is stored as a list of tiles, each of which has a list of
809-
* matches. So we have to iterate through the tiles until we find the one
810-
* that contains the nth match, then iterate through the matches in that
811-
* tile until we find the nth match.
812-
*
813-
* @param i the index of the row to get. If less than zero, will return
814-
* @param returnQid if true, returns a tuple of [tile index, row index] instead of the row data
815-
*/
816-
get(i: number | undefined = undefined, returnQid: boolean = false): StructRowProxy | [number, number] | undefined {
827+
protected getBase(i: number | undefined = undefined, returnQid: boolean = false): StructRowProxy | [number, number] | undefined {
817828
if (i === undefined) {
818829
i = this.cursor;
819830
}
@@ -847,10 +858,11 @@ export class DataSelection {
847858
if (offset >= cached.length) {
848859
throw new Error(`unable to locate point ${i}`);
849860
}
850-
const tix = cached[offset];
851-
852-
return relevantTile.tile.record_batch.get(tix) || undefined;
853-
861+
const rix = cached[offset];
862+
if (returnQid) {
863+
return [relevantTile.tile.tix, rix]
864+
}
865+
return relevantTile.tile.record_batch.get(rix) || undefined;
854866
}
855867

856868
// Iterate over the points in raw order.
@@ -1112,15 +1124,28 @@ export class SortedDataSelection extends DataSelection {
11121124
};
11131125
}
11141126

1115-
get(i: number | undefined, returnQid: false): StructRowProxy | undefined;
1116-
get(i: number | undefined, returnQid: true): [number, number] | undefined;
11171127

1128+
get(i: number | undefined = undefined): StructRowProxy {
1129+
const result = this.getBase(i, false) as StructRowProxy;
1130+
if (result === undefined) {
1131+
throw new Error(`Index ${i} out of bounds`);
1132+
}
1133+
return result;
1134+
}
11181135

1136+
getQid(i: number | undefined = undefined): [number, number] {
1137+
const result = this.getBase(i, true) as [number, number];
1138+
if (result === undefined) {
1139+
throw new Error(`Index ${i} out of bounds`);
1140+
}
1141+
return result;
1142+
}
1143+
11191144
/**
11201145
* Returns the k-th element in the sorted selection.
11211146
* This implementation uses Quickselect with a pivot selected from actual data.
11221147
*/
1123-
get(k: number | undefined = undefined, returnQid: boolean = false): StructRowProxy | [number, number] | undefined {
1148+
protected getBase(k: number | undefined = undefined, returnQid: boolean = false): StructRowProxy | [number, number] | undefined {
11241149
if (k >= this.selectionSize || k < -this.selectionSize) {
11251150
return undefined;
11261151
}

0 commit comments

Comments
 (0)