table.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import Column from './column'
  7. import { sortArray } from '../util/sort'
  8. /** A collection of columns */
  9. type Table<Schema extends Table.Schema> = {
  10. readonly _rowCount: number,
  11. readonly _columns: ReadonlyArray<string>,
  12. readonly _schema: Schema
  13. } & Table.Columns<Schema>
  14. /** An immutable table */
  15. namespace Table {
  16. export type Schema = { [field: string]: Column.Schema }
  17. export type Columns<S extends Schema> = { [C in keyof S]: Column<S[C]['T']> }
  18. export type Row<S extends Schema> = { [C in keyof S]: S[C]['T'] }
  19. export type Arrays<S extends Schema> = { [C in keyof S]: ArrayLike<S[C]['T']> }
  20. export type PartialTable<S extends Table.Schema> = { readonly _rowCount: number, readonly _columns: ReadonlyArray<string> } & { [C in keyof S]?: Column<S[C]['T']> }
  21. export function is(t: any): t is Table<any> {
  22. return t && typeof t._rowCount === 'number' && !!t._columns && !!t._schema;
  23. }
  24. export function pickColumns<S extends Schema>(schema: S, table: PartialTable<S>, guard: Partial<Columns<S>> = {}): Table<S> {
  25. const ret = Object.create(null);
  26. const keys = Object.keys(schema);
  27. ret._rowCount = table._rowCount;
  28. ret._columns = keys;
  29. ret._schema = schema;
  30. for (const k of keys) {
  31. if (!!table[k]) ret[k] = table[k];
  32. else if (!!guard[k]) ret[k] = guard[k];
  33. else throw Error(`Cannot find column '${k}'.`);
  34. }
  35. return ret;
  36. }
  37. export function ofColumns<S extends Schema, R extends Table<S> = Table<S>>(schema: S, columns: Columns<S>): R {
  38. const _columns = Object.keys(columns);
  39. const _rowCount = columns[_columns[0]].rowCount;
  40. return { _rowCount, _columns, _schema: schema, ...(columns as any) };
  41. }
  42. export function ofRows<S extends Schema, R extends Table<S> = Table<S>>(schema: Schema, rows: ArrayLike<Row<S>>): R {
  43. const ret = Object.create(null);
  44. const rowCount = rows.length;
  45. const columns = Object.keys(schema);
  46. ret._rowCount = rowCount;
  47. ret._columns = columns;
  48. ret._schema = schema;
  49. for (const k of columns) {
  50. (ret as any)[k] = Column.ofLambda({
  51. rowCount,
  52. schema: schema[k],
  53. value: r => rows[r][k],
  54. valueKind: r => typeof rows[r][k] === 'undefined' ? Column.ValueKind.NotPresent : Column.ValueKind.Present
  55. })
  56. }
  57. return ret as R;
  58. }
  59. export function ofArrays<S extends Schema, R extends Table<S> = Table<S>>(schema: Schema, arrays: Arrays<S>): R {
  60. const ret = Object.create(null);
  61. const columns = Object.keys(schema);
  62. ret._rowCount = arrays[columns[0]].length;
  63. ret._columns = columns;
  64. ret._schema = schema;
  65. for (const k of columns) {
  66. (ret as any)[k] = Column.ofArray({ array: arrays[k], schema: schema[k] })
  67. }
  68. return ret as R;
  69. }
  70. export function view<S extends R, R extends Schema>(table: Table<S>, schema: R, view: ArrayLike<number>) {
  71. const ret = Object.create(null);
  72. const columns = Object.keys(schema);
  73. ret._rowCount = view.length;
  74. ret._columns = columns;
  75. ret._schema = schema;
  76. for (const k of columns) {
  77. (ret as any)[k] = Column.view(table[k], view);
  78. }
  79. return ret as Table<R>;
  80. }
  81. export function columnToArray<S extends Schema>(table: Table<S>, name: keyof S, array?: Column.ArrayCtor<any>) {
  82. table[name] = Column.asArrayColumn(table[name], array);
  83. }
  84. /** Sort and return a new table */
  85. export function sort<T extends Table<S>, S extends Schema>(table: T, cmp: (i: number, j: number) => number) {
  86. const indices = new Int32Array(table._rowCount);
  87. for (let i = 0, _i = indices.length; i < _i; i++) indices[i] = i;
  88. sortArray(indices, (_, i, j) => cmp(i, j));
  89. let isIdentity = true;
  90. for (let i = 0, _i = indices.length; i < _i; i++) {
  91. if (indices[i] !== i) {
  92. isIdentity = false;
  93. break;
  94. }
  95. }
  96. if (isIdentity) return table;
  97. const ret = Object.create(null);
  98. ret._rowCount = table._rowCount;
  99. ret._columns = table._columns;
  100. ret._schema = table._schema;
  101. for (const c of table._columns) {
  102. ret[c] = Column.view((table as any)[c], indices, false);
  103. }
  104. return ret;
  105. }
  106. export function areEqual<T extends Table<Schema>>(a: T, b: T) {
  107. if (a._rowCount !== b._rowCount) return false;
  108. if (a._columns.length !== b._columns.length) return false;
  109. for (const c of a._columns) {
  110. if (!b[c]) return false;
  111. }
  112. for (const c of a._columns) {
  113. if (!Column.areEqual(a[c], b[c])) return false;
  114. }
  115. return true;
  116. }
  117. }
  118. export default Table