Skip to content

Commit

Permalink
add a single-chunk column type
Browse files Browse the repository at this point in the history
  • Loading branch information
trxcllnt committed Jan 9, 2019
1 parent 6bcaad6 commit 380dbc7
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions js/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import { Field } from './schema';
import { Vector } from './vector';
import { DataType } from './type';
import { Chunked } from './vector/chunked';
import { Clonable, Sliceable, Applicative } from './vector';
import { Chunked, SearchContinuation } from './vector/chunked';

export interface Column<T extends DataType = any> {
typeId: T['TType'];
Expand All @@ -35,8 +35,12 @@ export class Column<T extends DataType = any>
Applicative<T, Column<T>> {

constructor(field: Field<T>, vectors: Vector<T>[] = [], offsets?: Uint32Array) {
super(field.type, Chunked.flatten(...vectors), offsets);
vectors = Chunked.flatten(...vectors);
super(field.type, vectors, offsets);
this._field = field;
if (vectors.length === 1 && !(this instanceof SingleChunkColumn)) {
return new SingleChunkColumn(field, vectors[0], this._chunkOffsets);
}
}

protected _field: Field<T>;
Expand Down Expand Up @@ -69,3 +73,28 @@ export class Column<T extends DataType = any>
return null;
}
}

class SingleChunkColumn<T extends DataType = any> extends Column<T> {
protected _chunk: Vector<T>;
constructor(field: Field<T>, vector: Vector<T>, offsets?: Uint32Array) {
super(field, [vector], offsets);
this._chunk = vector;
}
public search(index: number): [number, number] | null;
public search<N extends SearchContinuation<Chunked<T>>>(index: number, then?: N): ReturnType<N>;
public search<N extends SearchContinuation<Chunked<T>>>(index: number, then?: N) {
return then ? then(this, 0, index) : [0, index];
}
public isValid(index: number): boolean {
return this._chunk.isValid(index);
}
public get(index: number): T['TValue'] | null {
return this._chunk.get(index);
}
public set(index: number, value: T['TValue'] | null): void {
this._chunk.set(index, value);
}
public indexOf(element: T['TValue'], offset?: number): number {
return this._chunk.indexOf(element, offset);
}
}

0 comments on commit 380dbc7

Please sign in to comment.