Переглянути джерело

More refactoring, fixed jest

David Sehnal 7 роки тому
батько
коміт
40a35dea8e

+ 1 - 0
package.json

@@ -24,6 +24,7 @@
     "transform": {
       "\\.ts$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
     },
+    "moduleDirectories": ["node_modules", "build/node_modules"], 
     "testRegex": "\\.spec\\.ts$"
   },
   "author": "",

+ 2 - 1
src/mol-base/collections/integer.ts

@@ -9,6 +9,7 @@ import OrderedSet from './integer/ordered-set'
 import Segmentation from './integer/segmentation'
 import SortedArray from './integer/sorted-array'
 import Tuple from './integer/tuple'
+import LinkedIndex from './integer/linked-index'
 import Iterator from './iterator'
 
-export { Interval, OrderedSet, Segmentation, SortedArray, Tuple, Iterator }
+export { Interval, OrderedSet, Segmentation, SortedArray, Tuple, LinkedIndex, Iterator }

+ 0 - 0
src/mol-base/collections/_spec/linked-index.spec.ts → src/mol-base/collections/integer/_spec/linked-index.spec.ts


+ 0 - 0
src/mol-base/collections/linked-index.ts → src/mol-base/collections/integer/linked-index.ts


+ 4 - 114
src/mol-base/collections/table.ts

@@ -4,118 +4,8 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column from './column'
-import { sortArray } from './sort'
+import Table from './table/table'
+import Column from './table/column'
+import * as ColumnHelpers from './table/column-helpers'
 
-type Table<Schema extends Table.Schema> = { readonly _rowCount: number, readonly _columns: ReadonlyArray<string> } & Table.Columns<Schema>
-
-/** An immutable table */
-namespace Table {
-    export type Schema = { [field: string]: Column.Type }
-    export type Columns<S extends Schema> = { [C in keyof S]: Column<S[C]['T']> }
-    export type Row<S extends Schema> = { [C in keyof S]: S[C]['T'] }
-    export type Arrays<S extends Schema> = { [C in keyof S]: ArrayLike<S[C]['T']> }
-    export type PartialTable<S extends Table.Schema> = { readonly _rowCount: number, readonly _columns: ReadonlyArray<string> } & { [C in keyof S]?: Column<S[C]['T']> }
-
-    export function pickColumns<S extends Schema>(schema: S, table: PartialTable<S>, guard: Partial<Columns<S>> = {}): Table<S> {
-        const ret = Object.create(null);
-        const keys = Object.keys(schema);
-        ret._rowCount = table._rowCount;
-        ret._columns = keys;
-        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<S extends Schema, R extends Table<S> = Table<S>>(columns: Columns<S>): R {
-        const _columns = Object.keys(columns);
-        const _rowCount = columns[_columns[0]].rowCount;
-        return { _rowCount, _columns, ...(columns as any) };
-    }
-
-    export function ofRows<S extends Schema, R extends Table<S> = Table<S>>(schema: Schema, rows: ArrayLike<Row<S>>): R {
-        const ret = Object.create(null);
-        const rowCount = rows.length;
-        const columns = Object.keys(schema);
-        ret._rowCount = rowCount;
-        ret._columns = columns;
-        for (const k of columns) {
-            (ret as any)[k] = Column.ofLambda({
-                rowCount,
-                type: 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<S extends Schema, R extends Table<S> = Table<S>>(schema: Schema, arrays: Arrays<S>): R {
-        const ret = Object.create(null);
-        const columns = Object.keys(schema);
-        ret._rowCount = arrays[columns[0]].length;
-        ret._columns = columns;
-        for (const k of columns) {
-            (ret as any)[k] = Column.ofArray({ array: arrays[k], type: schema[k] })
-        }
-        return ret as R;
-    }
-
-    export function view<S extends Schema, R extends Schema>(table: Table<S>, schema: R, view: ArrayLike<number>) {
-        const ret = Object.create(null);
-        const columns = Object.keys(schema);
-        ret._rowCount = view.length;
-        ret._columns = columns;
-        for (const k of columns) {
-            (ret as any)[k] = Column.view(table[k], view);
-        }
-        return ret as Table<R>;
-    }
-
-    export function columnToArray<S extends Schema>(table: Table<S>, name: keyof S, array?: Column.ArrayCtor<any>) {
-        table[name] = Column.asArrayColumn(table[name], array);
-    }
-
-    /** Sort and return a new table */
-    export function sort<T extends Table<S>, 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;
-        for (const c of table._columns) {
-            ret[c] = Column.view((table as any)[c], indices, false);
-        }
-        return ret;
-    }
-
-    export function areEqual<T extends Table<Schema>>(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;
-    }
-}
-
-export default Table
+export { Table, Column, ColumnHelpers }

+ 2 - 1
src/mol-base/collections/_spec/table.spec.ts → src/mol-base/collections/table/_spec/table.spec.ts

@@ -4,7 +4,8 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column, { ColumnHelpers } from '../column'
+import * as ColumnHelpers from '../column-helpers'
+import Column from '../column'
 import Table from '../table'
 
 describe('column', () => {

+ 40 - 0
src/mol-base/collections/table/column-helpers.ts

@@ -0,0 +1,40 @@
+/**
+ * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import Column from './column'
+
+export function getArrayBounds(rowCount: number, params?: Column.ToArrayParams<any>) {
+    const start = params && typeof params.start !== 'undefined' ? Math.max(Math.min(params.start, rowCount - 1), 0) : 0;
+    const end = params && typeof params.end !== 'undefined' ? Math.min(params.end, rowCount) : rowCount;
+    return { start, end };
+}
+
+export function createArray(rowCount: number, params?: Column.ToArrayParams<any>) {
+    const c = params && typeof params.array !== 'undefined' ? params.array : Array;
+    const { start, end } = getArrayBounds(rowCount, params);
+    return { array: new c(end - start) as any[], start, end };
+}
+
+export function fillArrayValues(value: (row: number) => any, target: any[], start: number) {
+    for (let i = 0, _e = target.length; i < _e; i++) target[i] = value(start + i);
+    return target;
+}
+
+export function createAndFillArray(rowCount: number, value: (row: number) => any, params?: Column.ToArrayParams<any>) {
+    const { array, start } = createArray(rowCount, params);
+    return fillArrayValues(value, array, start);
+}
+
+export function isTypedArray(data: any): boolean {
+    return !!data.buffer && typeof data.byteLength === 'number' && typeof data.BYTES_PER_ELEMENT === 'number';
+}
+
+export function typedArrayWindow(data: any, params?: Column.ToArrayParams<any>): ReadonlyArray<number> {
+    const { constructor, buffer, length, byteOffset, BYTES_PER_ELEMENT } = data;
+    const { start, end } = getArrayBounds(length, params);
+    if (start === 0 && end === length) return data;
+    return new constructor(buffer, byteOffset + BYTES_PER_ELEMENT * start, Math.min(length, end - start));
+}

+ 2 - 35
src/mol-base/collections/column.ts → src/mol-base/collections/table/column.ts

@@ -4,6 +4,8 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
+import * as ColumnHelpers from './column-helpers'
+
 interface Column<T> {
     readonly '@type': Column.Type,
     readonly '@array': ArrayLike<any> | undefined,
@@ -304,39 +306,4 @@ function areValuesEqual(a: Column<any>, b: Column<any>) {
         if (va(i) !== vb(i)) return false;
     }
     return true;
-}
-
-export namespace ColumnHelpers {
-    export function getArrayBounds(rowCount: number, params?: Column.ToArrayParams<any>) {
-        const start = params && typeof params.start !== 'undefined' ? Math.max(Math.min(params.start, rowCount - 1), 0) : 0;
-        const end = params && typeof params.end !== 'undefined' ? Math.min(params.end, rowCount) : rowCount;
-        return { start, end };
-    }
-
-    export function createArray(rowCount: number, params?: Column.ToArrayParams<any>) {
-        const c = params && typeof params.array !== 'undefined' ? params.array : Array;
-        const { start, end } = getArrayBounds(rowCount, params);
-        return { array: new c(end - start) as any[], start, end };
-    }
-
-    export function fillArrayValues(value: (row: number) => any, target: any[], start: number) {
-        for (let i = 0, _e = target.length; i < _e; i++) target[i] = value(start + i);
-        return target;
-    }
-
-    export function createAndFillArray(rowCount: number, value: (row: number) => any, params?: Column.ToArrayParams<any>) {
-        const { array, start } = createArray(rowCount, params);
-        return fillArrayValues(value, array, start);
-    }
-
-    export function isTypedArray(data: any): boolean {
-        return !!data.buffer && typeof data.byteLength === 'number' && typeof data.BYTES_PER_ELEMENT === 'number';
-    }
-
-    export function typedArrayWindow(data: any, params?: Column.ToArrayParams<any>): ReadonlyArray<number> {
-        const { constructor, buffer, length, byteOffset, BYTES_PER_ELEMENT } = data;
-        const { start, end } = getArrayBounds(length, params);
-        if (start === 0 && end === length) return data;
-        return new constructor(buffer, byteOffset + BYTES_PER_ELEMENT * start, Math.min(length, end - start));
-    }
 }

+ 121 - 0
src/mol-base/collections/table/table.ts

@@ -0,0 +1,121 @@
+/**
+ * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import Column from './column'
+import { sortArray } from '../sort'
+
+type Table<Schema extends Table.Schema> = { readonly _rowCount: number, readonly _columns: ReadonlyArray<string> } & Table.Columns<Schema>
+
+/** An immutable table */
+namespace Table {
+    export type Schema = { [field: string]: Column.Type }
+    export type Columns<S extends Schema> = { [C in keyof S]: Column<S[C]['T']> }
+    export type Row<S extends Schema> = { [C in keyof S]: S[C]['T'] }
+    export type Arrays<S extends Schema> = { [C in keyof S]: ArrayLike<S[C]['T']> }
+    export type PartialTable<S extends Table.Schema> = { readonly _rowCount: number, readonly _columns: ReadonlyArray<string> } & { [C in keyof S]?: Column<S[C]['T']> }
+
+    export function pickColumns<S extends Schema>(schema: S, table: PartialTable<S>, guard: Partial<Columns<S>> = {}): Table<S> {
+        const ret = Object.create(null);
+        const keys = Object.keys(schema);
+        ret._rowCount = table._rowCount;
+        ret._columns = keys;
+        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<S extends Schema, R extends Table<S> = Table<S>>(columns: Columns<S>): R {
+        const _columns = Object.keys(columns);
+        const _rowCount = columns[_columns[0]].rowCount;
+        return { _rowCount, _columns, ...(columns as any) };
+    }
+
+    export function ofRows<S extends Schema, R extends Table<S> = Table<S>>(schema: Schema, rows: ArrayLike<Row<S>>): R {
+        const ret = Object.create(null);
+        const rowCount = rows.length;
+        const columns = Object.keys(schema);
+        ret._rowCount = rowCount;
+        ret._columns = columns;
+        for (const k of columns) {
+            (ret as any)[k] = Column.ofLambda({
+                rowCount,
+                type: 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<S extends Schema, R extends Table<S> = Table<S>>(schema: Schema, arrays: Arrays<S>): R {
+        const ret = Object.create(null);
+        const columns = Object.keys(schema);
+        ret._rowCount = arrays[columns[0]].length;
+        ret._columns = columns;
+        for (const k of columns) {
+            (ret as any)[k] = Column.ofArray({ array: arrays[k], type: schema[k] })
+        }
+        return ret as R;
+    }
+
+    export function view<S extends Schema, R extends Schema>(table: Table<S>, schema: R, view: ArrayLike<number>) {
+        const ret = Object.create(null);
+        const columns = Object.keys(schema);
+        ret._rowCount = view.length;
+        ret._columns = columns;
+        for (const k of columns) {
+            (ret as any)[k] = Column.view(table[k], view);
+        }
+        return ret as Table<R>;
+    }
+
+    export function columnToArray<S extends Schema>(table: Table<S>, name: keyof S, array?: Column.ArrayCtor<any>) {
+        table[name] = Column.asArrayColumn(table[name], array);
+    }
+
+    /** Sort and return a new table */
+    export function sort<T extends Table<S>, 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;
+        for (const c of table._columns) {
+            ret[c] = Column.view((table as any)[c], indices, false);
+        }
+        return ret;
+    }
+
+    export function areEqual<T extends Table<Schema>>(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;
+    }
+}
+
+export default Table

+ 1 - 2
src/mol-data/structure/model/builders/mmcif.ts

@@ -6,8 +6,7 @@
 
 import { mmCIF } from '../data-format'
 import Model from '../model'
-import Column from 'mol-base/collections/column'
-import Table from 'mol-base/collections/table'
+import { Column, Table } from 'mol-base/collections/table'
 import { Interval, Segmentation } from 'mol-base/collections/integer'
 import { newUUID } from 'mol-base/utils/uuid'
 import * as Hierarchy from '../properties/hierarchy'

+ 1 - 1
src/mol-data/structure/model/properties/conformation.ts

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column from 'mol-base/collections/column'
+import { Column } from 'mol-base/collections/table'
 import UUID from 'mol-base/utils/uuid'
 
 interface Conformation {

+ 1 - 2
src/mol-data/structure/model/properties/hierarchy.ts

@@ -4,8 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column from 'mol-base/collections/column'
-import Table from 'mol-base/collections/table'
+import { Column, Table } from 'mol-base/collections/table'
 import { Segmentation } from 'mol-base/collections/integer'
 import { Schema as mmCIF } from 'mol-io/reader/cif/schema/mmcif'
 

+ 1 - 1
src/mol-data/structure/model/utils/hierarchy-keys.ts

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column from 'mol-base/collections/column'
+import { Column } from 'mol-base/collections/table'
 import { Data, Segments, Keys } from '../properties/hierarchy'
 import { Interval, Segmentation } from 'mol-base/collections/integer'
 

+ 1 - 1
src/mol-io/reader/_spec/column.spec.ts

@@ -7,7 +7,7 @@
 
 import FixedColumn from '../common/text/column/fixed'
 import TokenColumn from '../common/text/column/token'
-import Column, { ColumnHelpers } from 'mol-base/collections/column'
+import { Column, ColumnHelpers } from 'mol-base/collections/table'
 
 const lines = [
     '1.123 abc',

+ 1 - 1
src/mol-io/reader/cif/binary/field.ts

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column, { ColumnHelpers } from 'mol-base/collections/column'
+import { Column, ColumnHelpers } from 'mol-base/collections/table'
 import * as Data from '../data-model'
 import { EncodedColumn } from './encoding'
 import decode from './decoder'

+ 1 - 1
src/mol-io/reader/cif/data-model.ts

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column from 'mol-base/collections/column'
+import { Column } from 'mol-base/collections/table'
 
 export interface File {
     readonly name?: string,

+ 1 - 2
src/mol-io/reader/cif/schema.ts

@@ -5,8 +5,7 @@
  */
 
 import * as Data from './data-model'
-import Column, { ColumnHelpers } from 'mol-base/collections/column'
-import Table from 'mol-base/collections/table'
+import { Table, Column, ColumnHelpers } from 'mol-base/collections/table'
 
 export function toTypedFrame<Schema extends FrameSchema, Frame extends TypedFrame<Schema> = TypedFrame<Schema>>(schema: Schema, frame: Data.Frame): Frame {
     return createTypedFrame(schema, frame) as Frame;

+ 1 - 1
src/mol-io/reader/cif/text/field.ts

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column, { ColumnHelpers } from 'mol-base/collections/column'
+import { Column, ColumnHelpers } from 'mol-base/collections/table'
 import * as TokenColumn from '../../common/text/column/token'
 import { Tokens } from '../../common/text/tokenizer'
 import * as Data from '../data-model'

+ 1 - 1
src/mol-io/reader/common/text/column/fixed.ts

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column, { ColumnHelpers } from 'mol-base/collections/column'
+import { Column, ColumnHelpers } from 'mol-base/collections/table'
 import { trimStr, Tokens } from '../tokenizer'
 import { parseIntSkipLeadingWhitespace, parseFloatSkipLeadingWhitespace } from '../number-parser'
 

+ 1 - 1
src/mol-io/reader/common/text/column/token.ts

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column, { ColumnHelpers } from 'mol-base/collections/column'
+import { Column, ColumnHelpers } from 'mol-base/collections/table'
 import { Tokens } from '../tokenizer'
 import { parseInt as fastParseInt, parseFloat as fastParseFloat } from '../number-parser'
 

+ 1 - 1
src/mol-io/reader/gro/parser.ts

@@ -5,9 +5,9 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
+import { Column } from 'mol-base/collections/table'
 import Tokenizer from '../common/text/tokenizer'
 import FixedColumn from '../common/text/column/fixed'
-import Column from 'mol-base/collections/column'
 import * as Schema from './schema'
 import Result from '../result'
 import Computation from 'mol-base/computation'

+ 1 - 1
src/mol-io/reader/gro/schema.d.ts

@@ -5,7 +5,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Column from 'mol-base/collections/column'
+import { Column } from 'mol-base/collections/table'
 
 export interface Header {
     title: string,

+ 1 - 1
src/mol-io/reader/mol2/schema.d.ts

@@ -4,7 +4,7 @@
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import Column from 'mol-base/collections/column'
+import { Column } from 'mol-base/collections/table'
 
 // Full format http://chemyang.ccnu.edu.cn/ccb/server/AIMMS/mol2.pdf
 // there are many records but for now ignore (pass over) all but the following

+ 1 - 1
src/perf-tests/column.ts

@@ -1,5 +1,5 @@
 import * as B from 'benchmark'
-import C from 'mol-base/collections/column'
+import { Column as C } from 'mol-base/collections/table'
 
 export namespace Column {
     function createData(n: number) {