column.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**
  2. * Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  6. */
  7. import * as ColumnHelpers from './column-helpers'
  8. import { Tensor as Tensors } from '../../mol-math/linear-algebra'
  9. import { Tokens } from '../../mol-io/reader/common/text/tokenizer';
  10. import { parseInt as fastParseInt, parseFloat as fastParseFloat } from '../../mol-io/reader/common/text/number-parser';
  11. interface Column<T> {
  12. readonly schema: Column.Schema,
  13. readonly __array: ArrayLike<any> | undefined,
  14. readonly isDefined: boolean,
  15. readonly rowCount: number,
  16. value(row: number): T,
  17. valueKind(row: number): Column.ValueKind,
  18. toArray(params?: Column.ToArrayParams<T>): ArrayLike<T>,
  19. areValuesEqual(rowA: number, rowB: number): boolean
  20. }
  21. namespace Column {
  22. export type ArrayCtor<T> = { new(size: number): ArrayLike<T> }
  23. export type Schema<T = any> = Schema.Str | Schema.Int | Schema.Float | Schema.Coordinate | Schema.Aliased<T> | Schema.Tensor | Schema.List<number|string>
  24. export namespace Schema {
  25. // T also serves as a default value for undefined columns
  26. type Base<T extends string> = { valueType: T }
  27. export type Str = { '@type': 'str', T: string } & Base<'str'>
  28. export type Int = { '@type': 'int', T: number } & Base<'int'>
  29. export type Float = { '@type': 'float', T: number } & Base<'float'>
  30. export type Coordinate = { '@type': 'coord', T: number } & Base<'float'>
  31. export type Tensor = { '@type': 'tensor', T: Tensors.Data, space: Tensors.Space, baseType: Int | Float } & Base<'tensor'>
  32. export type Aliased<T> = { '@type': 'aliased', T: T } & Base<'str' | 'int'>
  33. export type List<T extends number|string> = { '@type': 'list', T: T[], separator: string, itemParse: (x: string) => T } & Base<'list'>
  34. export const str: Str = { '@type': 'str', T: '', valueType: 'str' };
  35. export const int: Int = { '@type': 'int', T: 0, valueType: 'int' };
  36. export const coord: Coordinate = { '@type': 'coord', T: 0, valueType: 'float' };
  37. export const float: Float = { '@type': 'float', T: 0, valueType: 'float' };
  38. export function Str(defaultValue = ''): Str { return { '@type': 'str', T: defaultValue, valueType: 'str' } };
  39. export function Int(defaultValue = 0): Int { return { '@type': 'int', T: defaultValue, valueType: 'int' } };
  40. export function Float(defaultValue = 0): Float { return { '@type': 'float', T: defaultValue, valueType: 'float' } };
  41. export function Tensor(space: Tensors.Space, baseType: Int | Float = float): Tensor { return { '@type': 'tensor', T: space.create(), space, valueType: 'tensor', baseType }; }
  42. export function Vector(dim: number, baseType: Int | Float = float): Tensor { return Tensor(Tensors.Vector(dim, baseType['@type'] === 'int' ? Int32Array : Float64Array), baseType); }
  43. export function Matrix(rows: number, cols: number, baseType: Int | Float = float): Tensor { return Tensor(Tensors.ColumnMajorMatrix(rows, cols, baseType['@type'] === 'int' ? Int32Array : Float64Array), baseType); }
  44. export function Aliased<T>(t: Str | Int, defaultValue?: T): Aliased<T> {
  45. if (typeof defaultValue !== 'undefined') return { ...t, T: defaultValue } as any as Aliased<T>;
  46. return t as any as Aliased<T>;
  47. }
  48. export function List<T extends number|string>(separator: string, itemParse: (x: string) => T, defaultValue: T[] = []): List<T> {
  49. return { '@type': 'list', T: defaultValue, separator, itemParse, valueType: 'list' }
  50. }
  51. }
  52. export interface ToArrayParams<T> {
  53. array?: ArrayCtor<T>,
  54. start?: number,
  55. /** Last row (exclusive) */
  56. end?: number
  57. }
  58. export interface LambdaSpec<T extends Schema> {
  59. value: (row: number) => T['T'],
  60. rowCount: number,
  61. schema: T,
  62. valueKind?: (row: number) => ValueKind,
  63. areValuesEqual?: (rowA: number, rowB: number) => boolean
  64. }
  65. export interface ArraySpec<T extends Schema> {
  66. array: ArrayLike<T['T']>,
  67. schema: T,
  68. valueKind?: (row: number) => ValueKind
  69. }
  70. export interface MapSpec<S extends Schema, T extends Schema> {
  71. f: (v: S['T']) => T['T'],
  72. schema: T,
  73. valueKind?: (row: number) => ValueKind,
  74. }
  75. export function is(v: any): v is Column<any> {
  76. return !!v && !!(v as Column<any>).schema && !!(v as Column<any>).value;
  77. }
  78. export const enum ValueKind { Present = 0, NotPresent = 1, Unknown = 2 }
  79. export function Undefined<T extends Schema>(rowCount: number, schema: T): Column<T['T']> {
  80. return constColumn(schema['T'], rowCount, schema, ValueKind.NotPresent);
  81. }
  82. export function ofConst<T extends Schema>(v: T['T'], rowCount: number, type: T): Column<T['T']> {
  83. return constColumn(v, rowCount, type, ValueKind.Present);
  84. }
  85. export function ofLambda<T extends Schema>(spec: LambdaSpec<T>): Column<T['T']> {
  86. return lambdaColumn(spec);
  87. }
  88. /** values [min, max] (i.e. include both values) */
  89. export function range(min: number, max: number): Column<number> {
  90. return ofLambda({
  91. value: i => i + min,
  92. rowCount: Math.max(max - min + 1, 0),
  93. schema: Schema.int
  94. });
  95. }
  96. export function ofArray<T extends Column.Schema>(spec: Column.ArraySpec<T>): Column<T['T']> {
  97. return arrayColumn(spec);
  98. }
  99. export function ofIntArray(array: ArrayLike<number>) {
  100. return arrayColumn({ array, schema: Schema.int });
  101. }
  102. export function ofFloatArray(array: ArrayLike<number>) {
  103. return arrayColumn({ array, schema: Schema.float });
  104. }
  105. export function ofStringArray(array: ArrayLike<string>) {
  106. return arrayColumn({ array, schema: Schema.str });
  107. }
  108. export function ofIntTokens(tokens: Tokens) {
  109. const { count, data, indices } = tokens
  110. return lambdaColumn({
  111. value: (row: number) => fastParseInt(data, indices[2 * row], indices[2 * row + 1]) || 0,
  112. rowCount: count,
  113. schema: Schema.int,
  114. });
  115. }
  116. export function ofFloatTokens(tokens: Tokens) {
  117. const { count, data, indices } = tokens
  118. return lambdaColumn({
  119. value: (row: number) => fastParseFloat(data, indices[2 * row], indices[2 * row + 1]) || 0,
  120. rowCount: count,
  121. schema: Schema.float,
  122. });
  123. }
  124. export function ofStringTokens(tokens: Tokens) {
  125. const { count, data, indices } = tokens
  126. return lambdaColumn({
  127. value: (row: number) => {
  128. const ret = data.substring(indices[2 * row], indices[2 * row + 1]);
  129. if (ret === '.' || ret === '?') return '';
  130. return ret;
  131. },
  132. rowCount: count,
  133. schema: Schema.str,
  134. });
  135. }
  136. export function window<T>(column: Column<T>, start: number, end: number) {
  137. return windowColumn(column, start, end);
  138. }
  139. export function view<T>(column: Column<T>, indices: ArrayLike<number>, checkIndentity = true) {
  140. return columnView(column, indices, checkIndentity);
  141. }
  142. /** A map of the 1st occurence of each value. */
  143. export function createFirstIndexMap<T>(column: Column<T>) {
  144. return createFirstIndexMapOfColumn(column);
  145. }
  146. export function createIndexer<T, R extends number = number>(column: Column<T>) {
  147. return createIndexerOfColumn(column) as ((e: T) => R);
  148. }
  149. export function mapToArray<T, S>(column: Column<T>, f: (v: T) => S, ctor?: ArrayCtor<S>): ArrayLike<S> {
  150. return mapToArrayImpl<T, S>(column, f, ctor || Array);
  151. }
  152. export function areEqual<T>(a: Column<T>, b: Column<T>) {
  153. return areColumnsEqual(a, b);
  154. }
  155. export function indicesOf<T>(c: Column<T>, test: (e: T) => boolean) {
  156. return columnIndicesOf(c, test);
  157. }
  158. /** Makes the column backed by an array. Useful for columns that are accessed often. */
  159. export function asArrayColumn<T>(c: Column<T>, array?: ArrayCtor<T>): Column<T> {
  160. if (c.__array) return c;
  161. if (!c.isDefined) return Undefined(c.rowCount, c.schema) as any as Column<T>;
  162. return arrayColumn({ array: c.toArray({ array }), schema: c.schema, valueKind: c.valueKind });
  163. }
  164. export function copyToArray<T extends number>(c: Column<T>, array: { [k: number]: T, length: number }, offset = 0) {
  165. if (!c.isDefined) return;
  166. const cArray = c.__array
  167. if (cArray) {
  168. for (let i = 0, _i = cArray.length; i < _i; i++) array[offset + i] = cArray[i];
  169. } else {
  170. for (let i = 0, _i = c.rowCount; i < _i; i++) array[offset + i] = c.value(i);
  171. }
  172. }
  173. }
  174. export default Column;
  175. function createFirstIndexMapOfColumn<T>(c: Column<T>): Map<T, number> {
  176. const map = new Map<T, number>();
  177. for (let i = 0, _i = c.rowCount; i < _i; i++) {
  178. const v = c.value(i);
  179. if (!map.has(v)) map.set(c.value(i), i);
  180. }
  181. return map;
  182. }
  183. function createIndexerOfColumn<T>(c: Column<T>): (value: T) => number {
  184. const map = new Map<T, number>();
  185. for (let i = 0, _i = c.rowCount; i < _i; i++) {
  186. const v = c.value(i);
  187. if (!map.has(v)) map.set(c.value(i), i);
  188. }
  189. return v => map.has(v) ? map.get(v)! : -1;
  190. }
  191. function constColumn<T extends Column.Schema>(v: T['T'], rowCount: number, schema: T, valueKind: Column.ValueKind): Column<T['T']> {
  192. const value: Column<T['T']>['value'] = row => v;
  193. return {
  194. schema: schema,
  195. __array: void 0,
  196. isDefined: valueKind === Column.ValueKind.Present,
  197. rowCount,
  198. value,
  199. valueKind: row => valueKind,
  200. toArray: params => {
  201. const { array } = ColumnHelpers.createArray(rowCount, params);
  202. for (let i = 0, _i = array.length; i < _i; i++) array[i] = v;
  203. return array;
  204. },
  205. areValuesEqual: (rowA, rowB) => true
  206. }
  207. }
  208. function lambdaColumn<T extends Column.Schema>({ value, valueKind, areValuesEqual, rowCount, schema }: Column.LambdaSpec<T>): Column<T['T']> {
  209. return {
  210. schema: schema,
  211. __array: void 0,
  212. isDefined: true,
  213. rowCount,
  214. value,
  215. valueKind: valueKind ? valueKind : row => Column.ValueKind.Present,
  216. toArray: params => {
  217. const { array, start } = ColumnHelpers.createArray(rowCount, params);
  218. for (let i = 0, _i = array.length; i < _i; i++) array[i] = value(i + start);
  219. return array;
  220. },
  221. areValuesEqual: areValuesEqual ? areValuesEqual : (rowA, rowB) => value(rowA) === value(rowB)
  222. }
  223. }
  224. function arrayColumn<T extends Column.Schema>({ array, schema, valueKind }: Column.ArraySpec<T>): Column<T['T']> {
  225. const rowCount = array.length;
  226. const value: Column<T['T']>['value'] = schema.valueType === 'str'
  227. ? row => { const v = array[row]; return typeof v === 'string' ? v : '' + v; }
  228. : row => array[row];
  229. const isTyped = ColumnHelpers.isTypedArray(array);
  230. return {
  231. schema: schema,
  232. __array: array,
  233. isDefined: true,
  234. rowCount,
  235. value,
  236. valueKind: valueKind ? valueKind : row => Column.ValueKind.Present,
  237. toArray: schema.valueType === 'str'
  238. ? params => {
  239. const { start, end } = ColumnHelpers.getArrayBounds(rowCount, params);
  240. const ret = new (params && typeof params.array !== 'undefined' ? params.array : (array as any).constructor)(end - start) as any;
  241. for (let i = 0, _i = end - start; i < _i; i++) {
  242. const v = array[start + i];
  243. ret[i] = typeof v === 'string' ? v : '' + v;
  244. }
  245. return ret;
  246. }
  247. : isTyped
  248. ? params => ColumnHelpers.typedArrayWindow(array, params) as any as ReadonlyArray<T>
  249. : params => {
  250. const { start, end } = ColumnHelpers.getArrayBounds(rowCount, params);
  251. if (start === 0 && end === array.length) return array as ReadonlyArray<T['T']>;
  252. const ret = new (params && typeof params.array !== 'undefined' ? params.array : (array as any).constructor)(end - start) as any;
  253. for (let i = 0, _i = end - start; i < _i; i++) ret[i] = array[start + i];
  254. return ret;
  255. },
  256. areValuesEqual: (rowA, rowB) => array[rowA] === array[rowB]
  257. }
  258. }
  259. function windowColumn<T>(column: Column<T>, start: number, end: number) {
  260. if (!column.isDefined) return Column.Undefined(end - start, column.schema);
  261. if (start === 0 && end === column.rowCount) return column;
  262. if (!!column.__array && ColumnHelpers.isTypedArray(column.__array)) return windowTyped(column, start, end);
  263. return windowFull(column, start, end);
  264. }
  265. function windowTyped<T>(c: Column<T>, start: number, end: number): Column<T> {
  266. const array = ColumnHelpers.typedArrayWindow(c.__array, { start, end });
  267. return arrayColumn({ array, schema: c.schema, valueKind: c.valueKind }) as any;
  268. }
  269. function windowFull<T>(c: Column<T>, start: number, end: number): Column<T> {
  270. const v = c.value, vk = c.valueKind, ave = c.areValuesEqual;
  271. const value: Column<T>['value'] = start === 0 ? v : row => v(row + start);
  272. const rowCount = end - start;
  273. return {
  274. schema: c.schema,
  275. __array: void 0,
  276. isDefined: c.isDefined,
  277. rowCount,
  278. value,
  279. valueKind: start === 0 ? vk : row => vk(row + start),
  280. toArray: params => {
  281. const { array } = ColumnHelpers.createArray(rowCount, params);
  282. for (let i = 0, _i = array.length; i < _i; i++) array[i] = v(i + start);
  283. return array;
  284. },
  285. areValuesEqual: start === 0 ? ave : (rowA, rowB) => ave(rowA + start, rowB + start)
  286. };
  287. }
  288. function isIdentity(map: ArrayLike<number>, rowCount: number) {
  289. if (map.length !== rowCount) return false;
  290. for (let i = 0, _i = map.length; i < _i; i++) {
  291. if (map[i] !== i) return false;
  292. }
  293. return true;
  294. }
  295. function columnView<T>(c: Column<T>, map: ArrayLike<number>, checkIdentity: boolean): Column<T> {
  296. if (!c.isDefined) return c;
  297. if (checkIdentity && isIdentity(map, c.rowCount)) return c;
  298. if (!!c.__array) return arrayView(c, map);
  299. return viewFull(c, map);
  300. }
  301. function arrayView<T>(c: Column<T>, map: ArrayLike<number>): Column<T> {
  302. const array = c.__array!;
  303. const ret = new (array as any).constructor(map.length);
  304. for (let i = 0, _i = map.length; i < _i; i++) ret[i] = array[map[i]];
  305. return arrayColumn({ array: ret, schema: c.schema, valueKind: c.valueKind });
  306. }
  307. function viewFull<T>(c: Column<T>, map: ArrayLike<number>): Column<T> {
  308. const v = c.value, vk = c.valueKind, ave = c.areValuesEqual;
  309. const value: Column<T>['value'] = row => v(map[row]);
  310. const rowCount = map.length;
  311. return {
  312. schema: c.schema,
  313. __array: void 0,
  314. isDefined: c.isDefined,
  315. rowCount,
  316. value,
  317. valueKind: row => vk(map[row]),
  318. toArray: params => {
  319. const { array } = ColumnHelpers.createArray(rowCount, params);
  320. for (let i = 0, _i = array.length; i < _i; i++) array[i] = v(map[i]);
  321. return array;
  322. },
  323. areValuesEqual: (rowA, rowB) => ave(map[rowA], map[rowB])
  324. };
  325. }
  326. function mapToArrayImpl<T, S>(c: Column<T>, f: (v: T) => S, ctor: Column.ArrayCtor<S>): ArrayLike<S> {
  327. const ret = new ctor(c.rowCount) as any;
  328. for (let i = 0, _i = c.rowCount; i < _i; i++) ret[i] = f(c.value(i));
  329. return ret;
  330. }
  331. function areColumnsEqual(a: Column<any>, b: Column<any>) {
  332. if (a === b) return true;
  333. if (a.rowCount !== b.rowCount || a.isDefined !== b.isDefined || a.schema.valueType !== b.schema.valueType) return false;
  334. if (!!a.__array && !!b.__array) return areArraysEqual(a, b);
  335. return areValuesEqual(a, b);
  336. }
  337. function areArraysEqual(a: Column<any>, b: Column<any>) {
  338. const xs = a.__array!, ys = b.__array!;
  339. for (let i = 0, _i = a.rowCount; i < _i; i++) {
  340. if (xs[i] !== ys[i]) return false;
  341. }
  342. return true;
  343. }
  344. function areValuesEqual(a: Column<any>, b: Column<any>) {
  345. const va = a.value, vb = b.value;
  346. for (let i = 0, _i = a.rowCount; i < _i; i++) {
  347. if (va(i) !== vb(i)) return false;
  348. }
  349. return true;
  350. }
  351. function columnIndicesOf<T>(c: Column<T>, test: (e: T) => boolean) {
  352. const ret = [], v = c.value;
  353. for (let i = 0, _i = c.rowCount; i < _i; i++) {
  354. if (test(v(i))) ret[ret.length] = i;
  355. }
  356. return ret;
  357. }