/** * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal */ import Column from './column' import { sortArray } from '../util/sort' /** A collection of columns */ type Table = { readonly _rowCount: number, readonly _columns: ReadonlyArray, readonly _schema: Schema } & Table.Columns /** An immutable table */ namespace Table { export type Schema = { [field: string]: Column.Schema } export type Columns = { [C in keyof S]: Column } export type Row = { [C in keyof S]: S[C]['T'] } export type Arrays = { [C in keyof S]: ArrayLike } export type PartialTable = { readonly _rowCount: number, readonly _columns: ReadonlyArray } & { [C in keyof S]?: Column } export function is(t: any): t is Table { return t && typeof t._rowCount === 'number' && !!t._columns && !!t._schema; } export function pickColumns(schema: S, table: PartialTable, guard: Partial> = {}): Table { const ret = Object.create(null); const keys = Object.keys(schema); ret._rowCount = table._rowCount; ret._columns = keys; ret._schema = schema; for (const k of keys) { if (!!table[k]) ret[k] = table[k]; else if (!!guard[k]) ret[k] = guard[k]; else throw Error(`Cannot find column '${k}'.`); } return ret; } export function ofColumns = Table>(schema: S, columns: Columns): R { const _columns = Object.keys(columns); const _rowCount = columns[_columns[0]].rowCount; return { _rowCount, _columns, _schema: schema, ...(columns as any) }; } export function ofUndefinedColumns = Table>(schema: S, rowCount: number): R { const ret = Object.create(null); const columns = Object.keys(schema); ret._rowCount = rowCount; ret._columns = columns; ret._schema = schema; for (const k of columns) { ret[k] = Column.Undefined(rowCount, schema[k]) } return ret; } export function ofRows = Table>(schema: Schema, rows: ArrayLike>): R { const ret = Object.create(null); const rowCount = rows.length; const columns = Object.keys(schema); ret._rowCount = rowCount; ret._columns = columns; ret._schema = schema; for (const k of columns) { (ret as any)[k] = Column.ofLambda({ rowCount, schema: schema[k], value: r => rows[r][k], valueKind: r => typeof rows[r][k] === 'undefined' ? Column.ValueKind.NotPresent : Column.ValueKind.Present }) } return ret as R; } export function ofArrays = Table>(schema: Schema, arrays: Arrays): R { const ret = Object.create(null); const columns = Object.keys(schema); ret._rowCount = arrays[columns[0]].length; ret._columns = columns; ret._schema = schema; for (const k of columns) { (ret as any)[k] = Column.ofArray({ array: arrays[k], schema: schema[k] }) } return ret as R; } export function view(table: Table, schema: R, view: ArrayLike) { const ret = Object.create(null); const columns = Object.keys(schema); ret._rowCount = view.length; ret._columns = columns; ret._schema = schema; for (const k of columns) { (ret as any)[k] = Column.view(table[k], view); } return ret as Table; } export function columnToArray(table: Table, name: keyof S, array?: Column.ArrayCtor) { (table as Columns)[name] = Column.asArrayColumn((table as Columns)[name], array); } /** Sort and return a new table */ export function sort, S extends Schema>(table: T, cmp: (i: number, j: number) => number) { const indices = new Int32Array(table._rowCount); for (let i = 0, _i = indices.length; i < _i; i++) indices[i] = i; sortArray(indices, (_, i, j) => cmp(i, j)); let isIdentity = true; for (let i = 0, _i = indices.length; i < _i; i++) { if (indices[i] !== i) { isIdentity = false; break; } } if (isIdentity) return table; const ret = Object.create(null); ret._rowCount = table._rowCount; ret._columns = table._columns; ret._schema = table._schema; for (const c of table._columns) { ret[c] = Column.view((table as any)[c], indices, false); } return ret; } export function areEqual>(a: T, b: T) { if (a._rowCount !== b._rowCount) return false; if (a._columns.length !== b._columns.length) return false; for (const c of a._columns) { if (!b[c]) return false; } for (const c of a._columns) { if (!Column.areEqual(a[c], b[c])) return false; } return true; } /** Allocate a new object with the given row values. */ export function getRow(table: Table, index: number) { const row: Row = Object.create(null); const { _columns: cols } = table; for (let i = 0; i < cols.length; i++) { const c = cols[i]; row[c] = table[c].value(index); } return row; } export function getRows(table: Table) { const ret: Row[] = []; const { _rowCount: c } = table; for (let i = 0; i < c; i++) { ret[i] = getRow(table, i); } return ret; } } export default Table