table.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. import { StringBuilder } from '../../mol-util';
  9. /** A collection of columns */
  10. type Table<Schema extends Table.Schema = any> = {
  11. readonly _rowCount: number,
  12. readonly _columns: ReadonlyArray<string>,
  13. readonly _schema: Schema
  14. } & Table.Columns<Schema>
  15. /** An immutable table */
  16. namespace Table {
  17. export type Schema = { [field: string]: Column.Schema }
  18. export type Columns<S extends Schema> = { [C in keyof S]: Column<S[C]['T']> }
  19. export type Row<S extends Schema> = { [C in keyof S]: S[C]['T'] }
  20. export type Arrays<S extends Schema> = { [C in keyof S]: ArrayLike<S[C]['T']> }
  21. export type PartialColumns<S extends Schema> = { [C in keyof S]?: Column<S[C]['T']> }
  22. export type PartialTable<S extends Table.Schema> = { readonly _rowCount: number, readonly _columns: ReadonlyArray<string> } & PartialColumns<S>
  23. export function is(t: any): t is Table<any> {
  24. return t && typeof t._rowCount === 'number' && !!t._columns && !!t._schema;
  25. }
  26. export function pickColumns<S extends Schema>(schema: S, table: PartialTable<S>, guard: Partial<Columns<S>> = {}): Table<S> {
  27. const ret = Object.create(null);
  28. const keys = Object.keys(schema);
  29. ret._rowCount = table._rowCount;
  30. ret._columns = keys;
  31. ret._schema = schema;
  32. for (const k of keys) {
  33. if (!!table[k]) ret[k] = table[k];
  34. else if (!!guard[k]) ret[k] = guard[k];
  35. else throw Error(`Cannot find column '${k}'.`);
  36. }
  37. return ret;
  38. }
  39. export function ofColumns<S extends Schema, R extends Table<S> = Table<S>>(schema: S, columns: Columns<S>): R {
  40. const _columns = Object.keys(columns);
  41. const _rowCount = columns[_columns[0]].rowCount;
  42. return { _rowCount, _columns, _schema: schema, ...(columns as any) };
  43. }
  44. export function ofPartialColumns<S extends Schema, R extends Table<S> = Table<S>>(schema: S, partialColumns: PartialColumns<S>, rowCount: number): R {
  45. const ret = Object.create(null);
  46. const columns = Object.keys(schema);
  47. ret._rowCount = rowCount;
  48. ret._columns = columns;
  49. ret._schema = schema;
  50. for (const k of columns) {
  51. if (k in partialColumns) ret[k] = partialColumns[k];
  52. else ret[k] = Column.Undefined(rowCount, schema[k]);
  53. }
  54. return ret;
  55. }
  56. export function ofUndefinedColumns<S extends Schema, R extends Table<S> = Table<S>>(schema: S, rowCount: number): R {
  57. const ret = Object.create(null);
  58. const columns = Object.keys(schema);
  59. ret._rowCount = rowCount;
  60. ret._columns = columns;
  61. ret._schema = schema;
  62. for (const k of columns) {
  63. ret[k] = Column.Undefined(rowCount, schema[k]);
  64. }
  65. return ret;
  66. }
  67. export function ofRows<S extends Schema, R extends Table<S> = Table<S>>(schema: S, rows: ArrayLike<Partial<Row<S>>>): R {
  68. const ret = Object.create(null);
  69. const rowCount = rows.length;
  70. const columns = Object.keys(schema);
  71. ret._rowCount = rowCount;
  72. ret._columns = columns;
  73. ret._schema = schema;
  74. for (const k of columns) {
  75. (ret as any)[k] = Column.ofLambda({
  76. rowCount,
  77. schema: schema[k],
  78. value: r => rows[r][k],
  79. valueKind: r => typeof rows[r][k] === 'undefined' ? Column.ValueKind.NotPresent : Column.ValueKind.Present
  80. });
  81. }
  82. return ret as R;
  83. }
  84. export function ofArrays<S extends Schema, R extends Table<S> = Table<S>>(schema: S, arrays: Partial<Arrays<S>>): R {
  85. const ret = Object.create(null);
  86. const columns = Object.keys(schema);
  87. ret._rowCount = 0;
  88. ret._columns = columns;
  89. ret._schema = schema;
  90. for (const k of columns) {
  91. if (typeof arrays[k] !== 'undefined') {
  92. (ret as any)[k] = Column.ofArray({ array: arrays[k]!, schema: schema[k] });
  93. ret._rowCount = arrays[k]?.length;
  94. } else {
  95. (ret as any)[k] = Column.Undefined(ret._rowCount, schema[k]);
  96. }
  97. }
  98. return ret as R;
  99. }
  100. export function view<S extends R, R extends Schema>(table: Table<S>, schema: R, view: ArrayLike<number>) {
  101. const ret = Object.create(null);
  102. const columns = Object.keys(schema);
  103. ret._rowCount = view.length;
  104. ret._columns = columns;
  105. ret._schema = schema;
  106. for (const k of columns) {
  107. (ret as any)[k] = Column.view(table[k], view);
  108. }
  109. return ret as Table<R>;
  110. }
  111. export function pick<S extends R, R extends Schema>(table: Table<S>, schema: R, test: (i: number) => boolean) {
  112. const _view: number[] = [];
  113. for (let i = 0, il = table._rowCount; i < il; ++i) {
  114. if (test(i)) _view.push(i);
  115. }
  116. return view(table, schema, _view);
  117. }
  118. export function window<S extends R, R extends Schema>(table: Table<S>, schema: R, start: number, end: number) {
  119. if (start === 0 && end === table._rowCount) return table;
  120. const ret = Object.create(null);
  121. const columns = Object.keys(schema);
  122. ret._rowCount = end - start;
  123. ret._columns = columns;
  124. ret._schema = schema;
  125. for (const k of columns) {
  126. (ret as any)[k] = Column.window(table[k], start, end);
  127. }
  128. return ret as Table<R>;
  129. }
  130. export function concat<S extends R, R extends Schema>(tables: Table<S>[], schema: R) {
  131. const ret = Object.create(null);
  132. const columns = Object.keys(schema);
  133. ret._rowCount = 0;
  134. for (const table of tables) {
  135. ret._rowCount += table._rowCount;
  136. }
  137. const arrays: any = {};
  138. for (const column of columns) {
  139. arrays[column] = new Array(ret._rowCount);
  140. }
  141. ret._columns = columns;
  142. ret._schema = schema;
  143. let offset = 0;
  144. for (const table of tables) {
  145. for (const k of columns) {
  146. Column.copyToArray(table[k], arrays[k], offset);
  147. }
  148. offset += table._rowCount;
  149. }
  150. for (const k of columns) {
  151. ret[k] = Column.ofArray({ array: arrays[k], schema: schema[k] });
  152. }
  153. return ret as Table<R>;
  154. }
  155. export function columnToArray<S extends Schema>(table: Table<S>, name: keyof S, array?: Column.ArrayCtor<any>) {
  156. (table as Columns<S>)[name] = Column.asArrayColumn((table as Columns<S>)[name], array);
  157. }
  158. /** Sort and return a new table */
  159. export function sort<T extends Table>(table: T, cmp: (i: number, j: number) => number) {
  160. const indices = new Int32Array(table._rowCount);
  161. for (let i = 0, _i = indices.length; i < _i; i++) indices[i] = i;
  162. sortArray(indices, (_, i, j) => cmp(i, j));
  163. let isIdentity = true;
  164. for (let i = 0, _i = indices.length; i < _i; i++) {
  165. if (indices[i] !== i) {
  166. isIdentity = false;
  167. break;
  168. }
  169. }
  170. if (isIdentity) return table;
  171. const ret = Object.create(null);
  172. ret._rowCount = table._rowCount;
  173. ret._columns = table._columns;
  174. ret._schema = table._schema;
  175. for (const c of table._columns) {
  176. ret[c] = Column.view((table as any)[c], indices, false);
  177. }
  178. return ret;
  179. }
  180. export function areEqual<T extends Table<any>>(a: T, b: T) {
  181. if (a._rowCount !== b._rowCount) return false;
  182. if (a._columns.length !== b._columns.length) return false;
  183. for (const c of a._columns) {
  184. if (!b[c]) return false;
  185. }
  186. for (const c of a._columns) {
  187. if (!Column.areEqual(a[c], b[c])) return false;
  188. }
  189. return true;
  190. }
  191. /** Allocate a new object with the given row values. */
  192. export function getRow<S extends Schema>(table: Table<S>, index: number) {
  193. const row: Row<S> = Object.create(null);
  194. const { _columns: cols } = table;
  195. for (let i = 0; i < cols.length; i++) {
  196. const c = cols[i] as keyof S;
  197. row[c] = table[c].value(index);
  198. }
  199. return row;
  200. }
  201. /** Pick the first row for which `test` evaluates to true */
  202. export function pickRow<S extends Schema>(table: Table<S>, test: (i: number) => boolean) {
  203. for (let i = 0, il = table._rowCount; i < il; ++i) {
  204. if (test(i)) return getRow(table, i);
  205. }
  206. }
  207. export function getRows<S extends Schema>(table: Table<S>) {
  208. const ret: Row<S>[] = [];
  209. const { _rowCount: c } = table;
  210. for (let i = 0; i < c; i++) {
  211. ret[i] = getRow(table, i);
  212. }
  213. return ret;
  214. }
  215. export function toArrays<S extends Schema>(table: Table<S>) {
  216. const arrays: { [k: string]: ArrayLike<any> } = {};
  217. const { _columns } = table;
  218. for (let i = 0; i < _columns.length; i++) {
  219. const c = _columns[i];
  220. arrays[c] = table[c].toArray();
  221. }
  222. return arrays as { [k in keyof S]: ArrayLike<S[k]['T']> };
  223. }
  224. export function formatToString<S extends Schema>(table: Table<S>) {
  225. const sb = StringBuilder.create();
  226. const { _columns: cols, _rowCount } = table;
  227. let headerLength = 1;
  228. StringBuilder.write(sb, '|');
  229. for (let i = 0; i < cols.length; i++) {
  230. StringBuilder.write(sb, cols[i]);
  231. StringBuilder.write(sb, '|');
  232. headerLength += cols[i].length + 1;
  233. }
  234. StringBuilder.newline(sb);
  235. StringBuilder.write(sb, new Array(headerLength + 1).join('-'));
  236. StringBuilder.newline(sb);
  237. for (let r = 0; r < _rowCount; r++) {
  238. StringBuilder.write(sb, '|');
  239. for (let i = 0; i < cols.length; i++) {
  240. const c = table[cols[i]];
  241. if (c.valueKind(r) === Column.ValueKind.Present) {
  242. StringBuilder.write(sb, c.value(r));
  243. StringBuilder.write(sb, '|');
  244. } else {
  245. StringBuilder.write(sb, '.|');
  246. }
  247. }
  248. StringBuilder.newline(sb);
  249. }
  250. return StringBuilder.getString(sb);
  251. }
  252. }
  253. export { Table };