Browse Source

Simplified some modules

David Sehnal 7 years ago
parent
commit
19d9439955

+ 1 - 0
src/mol-base/collections/_spec/iterators.spec.ts

@@ -27,4 +27,5 @@ describe('basic iterators', () => {
     check('array', Iterator.Array([1, 2, 3]), [1, 2, 3]);
     check('range', Iterator.Range(0, 3), [0, 1, 2, 3]);
     check('map', Iterator.map(Iterator.Range(0, 1), x => x + 1), [1, 2]);
+    check('filter', Iterator.filter(Iterator.Range(0, 3), x => x >= 2), [2, 3]);
 });

+ 14 - 0
src/mol-base/collections/integer.ts

@@ -0,0 +1,14 @@
+/**
+ * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import Interval from './integer/interval'
+import OrderedSet from './integer/ordered-set'
+import Segmentation from './integer/segmentation'
+import SortedArray from './integer/sorted-array'
+import Tuple from './integer/tuple'
+import Iterator from './iterator'
+
+export { Interval, OrderedSet, Segmentation, SortedArray, Tuple, Iterator }

+ 28 - 5
src/mol-base/collections/iterator.ts

@@ -21,7 +21,7 @@ class ArrayIteratorImpl<T> implements Iterator<T> {
     private length: number = 0;
     private lastValue: T;
 
-    hasNext: boolean;
+    hasNext: boolean = false;
 
     move() {
         ++this.index;
@@ -41,9 +41,8 @@ class ArrayIteratorImpl<T> implements Iterator<T> {
 }
 
 class RangeIteratorImpl implements Iterator<number> {
-    private value: number;
-
-    hasNext: boolean;
+    private value: number = 0;
+    hasNext: boolean = false;
 
     move() {
         ++this.value;
@@ -64,7 +63,7 @@ class ValueIterator<T> implements Iterator<T> {
 }
 
 class MapIteratorImpl<T, R> implements Iterator<R> {
-    hasNext: boolean;
+    hasNext: boolean = false;
 
     move() {
         const v = this.f(this.base.move());
@@ -77,12 +76,36 @@ class MapIteratorImpl<T, R> implements Iterator<R> {
     }
 }
 
+class FilterIteratorImpl<T> implements Iterator<T> {
+    private next: T;
+    hasNext: boolean;
+
+    move() {
+        const ret = this.next;
+        this.hasNext = this.findNext();
+        return ret;
+    }
+
+    private findNext() {
+        while (this.base.hasNext) {
+            this.next = this.base.move();
+            if (this.p(this.next)) return true;
+        }
+        return false;
+    }
+
+    constructor(private base: Iterator<T>, private p: (v: T) => boolean) {
+        this.hasNext = this.findNext();
+    }
+}
+
 namespace Iterator {
     export const Empty: Iterator<any> = new RangeIteratorImpl(0, -1);
     export function Array<T>(xs: ArrayLike<T>): Iterator<T> { return new ArrayIteratorImpl<T>(xs); }
     export function Value<T>(value: T): Iterator<T> { return new ValueIterator(value); }
     export function Range(min: number, max: number): Iterator<number> { return new RangeIteratorImpl(min, max); }
     export function map<T, R>(base: Iterator<T>, f: (v: T) => R): Iterator<R> { return new MapIteratorImpl(base, f); }
+    export function filter<T>(base: Iterator<T>, p: (v: T) => boolean): Iterator<T> { return new FilterIteratorImpl(base, p); }
 }
 
 export default Iterator

+ 2 - 2
src/mol-data/_spec/atom-set.spec.ts

@@ -4,8 +4,8 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import OrderedSet from '../../mol-base/collections/integer/ordered-set'
-import AtomSet from '../structure/atom-set'
+import { OrderedSet } from '../../mol-base/collections/integer'
+import AtomSet from '../structure/atom/set'
 import Atom from '../structure/atom'
 
 describe('atom set', () => {

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

@@ -9,8 +9,7 @@ import { Frame as mmCIF } from '../../../mol-io/reader/cif/schema/mmcif'
 import Model from '../../model'
 import Column from '../../../mol-base/collections/column'
 import Table from '../../../mol-base/collections/table'
-import Interval from '../../../mol-base/collections/integer/interval'
-import Segmentation from '../../../mol-base/collections/integer/segmentation'
+import { Interval, Segmentation } from '../../../mol-base/collections/integer'
 import { newUUID } from '../../../mol-base/utils/uuid'
 import * as Hierarchy from '../properties/hierarchy'
 import Conformation from '../properties/conformation'

+ 0 - 1
src/mol-data/model/properties/computed.ts

@@ -8,7 +8,6 @@
 // secondary structure is also a computed property
 
 // import { SecondaryStructureType } from '../constants'
-// import Segmentation from '../../../mol-base/collections/integer/segmentation'
 
 
 // interface SecondaryStructure {

+ 3 - 3
src/mol-data/model/properties/hierarchy.ts

@@ -6,7 +6,7 @@
 
 import Column from '../../../mol-base/collections/column'
 import Table from '../../../mol-base/collections/table'
-import IntervalSegmentation from '../../../mol-base/collections/integer/segmentation'
+import { Segmentation } from '../../../mol-base/collections/integer'
 import { Schema as mmCIF } from '../../../mol-io/reader/cif/schema/mmcif'
 
 const _esCache = Object.create(null);
@@ -59,8 +59,8 @@ export interface Data {
 }
 
 export interface Segments {
-    residueSegments: IntervalSegmentation,
-    chainSegments: IntervalSegmentation
+    residueSegments: Segmentation,
+    chainSegments: Segmentation
 }
 
 export interface Keys {

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

@@ -6,8 +6,7 @@
 
 import Column from '../../../mol-base/collections/column'
 import { Data, Segments, Keys } from '../properties/hierarchy'
-import Segmentation from '../../../mol-base/collections/integer/segmentation'
-import Interval from '../../../mol-base/collections/integer/interval'
+import { Interval, Segmentation } from '../../../mol-base/collections/integer'
 
 function getResidueId(comp_id: string, seq_id: number, ins_code: string) {
     return `${comp_id} ${seq_id} ${ins_code}`;

+ 1 - 1
src/mol-data/query.ts

@@ -4,7 +4,7 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Structure from './structure'
+import { Structure } from './structure'
 import Selection from './query/selection'
 
 interface Query { (s: Structure): Selection }

+ 9 - 1
src/mol-data/query/generators.ts

@@ -2,4 +2,12 @@
  * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author David Sehnal <david.sehnal@gmail.com>
- */
+ */
+
+// import Selection from './selection'
+// import AtomSet from '../structure/atom-set'
+// import Atom from '../structure/atom'
+
+// class LinearGroupingBuilder {
+
+// }

+ 11 - 0
src/mol-data/query/query.ts

@@ -0,0 +1,11 @@
+/**
+ * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import { Structure } from '../structure'
+import Selection from './selection'
+
+interface Query { (s: Structure): Selection }
+export default Query

+ 1 - 3
src/mol-data/query/selection.ts

@@ -6,9 +6,7 @@
 
 import Iterator from '../../mol-base/collections/iterator'
 import HashSet from '../../mol-base/collections/hash-set'
-import Structure from './../structure'
-import Atom from './../structure/atom'
-import AtomSet from './../structure/atom-set'
+import { Structure, Atom, AtomSet } from '../structure'
 
 type Selection =
     | Structure // each atom is interpreted as a singleton structure

+ 5 - 19
src/mol-data/structure.ts

@@ -4,24 +4,10 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-//import { Vec3 } from '../mol-base/math/linear-algebra'
-import AtomSet from './structure/atom-set'
+import Atom from './structure/atom'
+import AtomSet from './structure/atom/set'
+import Structure from './structure/structure'
+import Operator from './structure/operator'
 import Unit from './structure/unit'
-import * as Base from './structure/base'
-//import Model from './model'
-//import Operator from './structure/operator'
 
-// TODO: do "single model" version of the structure?
-interface Structure extends Readonly<{
-    units: { readonly [id: number]: Unit },
-    atoms: AtomSet
-}> { }
-
-namespace Structure {
-    export const Empty = Base.Empty;
-    export const ofModel = Base.ofModel;
-    // TODO: "lift" atom set operators
-    // TODO: "diff"
-}
-
-export default Structure
+export { Atom, AtomSet, Structure, Operator, Unit }

+ 2 - 2
src/mol-data/structure/atom.ts

@@ -4,9 +4,9 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import Tuple from '../../mol-base/collections/integer/tuple'
+import { Tuple } from '../../mol-base/collections/integer'
 import Unit from './unit'
-import Structure from '../structure'
+import Structure from './structure'
 
 /** Atom pointer */
 interface Atom { '@type': Tuple['@type'] }

+ 24 - 26
src/mol-data/structure/atom-set.ts → src/mol-data/structure/atom/set.ts

@@ -4,41 +4,39 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import SortedArray from '../../mol-base/collections/integer/sorted-array'
-import OrderedSet from '../../mol-base/collections/integer/ordered-set'
-import Iterator from '../../mol-base/collections/iterator'
-import Atom from './atom'
-import * as Base from './atom-set/base'
-import createBuilder from './atom-set/builder'
+import { OrderedSet, SortedArray, Iterator } from '../../../mol-base/collections/integer'
+import Atom from '../atom'
+import * as Impl from './set/impl'
+import createBuilder from './set/builder'
 
 /** A map-like representation of grouped atom set */
 namespace AtomSet {
-    export const Empty: AtomSet = Base.Empty as any;
+    export const Empty: AtomSet = Impl.Empty as any;
 
-    export const create: (data: Atom | ArrayLike<Atom> | { [unitId: number]: OrderedSet }) => AtomSet = Base.create as any;
+    export const create: (data: Atom | ArrayLike<Atom> | { [unitId: number]: OrderedSet }) => AtomSet = Impl.create as any;
 
-    export const unitCount: (set: AtomSet) => number = Base.keyCount as any;
-    export const unitIds: (set: AtomSet) => SortedArray = Base.getKeys as any;
-    export const unitHas: (set: AtomSet, id: number) => boolean = Base.hasKey as any;
-    export const unitGetId: (set: AtomSet, i: number) => number = Base.getKey as any;
+    export const unitCount: (set: AtomSet) => number = Impl.keyCount as any;
+    export const unitIds: (set: AtomSet) => SortedArray = Impl.getKeys as any;
+    export const unitHas: (set: AtomSet, id: number) => boolean = Impl.hasKey as any;
+    export const unitGetId: (set: AtomSet, i: number) => number = Impl.getKey as any;
 
-    export const unitGetById: (set: AtomSet, key: number) => OrderedSet = Base.getByKey as any;
-    export const unitGetByIndex: (set: AtomSet, i: number) => OrderedSet = Base.getByIndex as any;
+    export const unitGetById: (set: AtomSet, key: number) => OrderedSet = Impl.getByKey as any;
+    export const unitGetByIndex: (set: AtomSet, i: number) => OrderedSet = Impl.getByIndex as any;
 
-    export const atomCount: (set: AtomSet) => number = Base.size as any;
-    export const atomHas: (set: AtomSet, x: Atom) => boolean = Base.hasAtom as any;
-    export const atomIndexOf: (set: AtomSet, x: Atom) => number = Base.indexOf as any;
-    export const atomGetAt: (set: AtomSet, i: number) => Atom = Base.getAt as any;
-    export const atoms: (set: AtomSet) => Iterator<Atom> = Base.values as any;
+    export const atomCount: (set: AtomSet) => number = Impl.size as any;
+    export const atomHas: (set: AtomSet, x: Atom) => boolean = Impl.hasAtom as any;
+    export const atomIndexOf: (set: AtomSet, x: Atom) => number = Impl.indexOf as any;
+    export const atomGetAt: (set: AtomSet, i: number) => Atom = Impl.getAt as any;
+    export const atoms: (set: AtomSet) => Iterator<Atom> = Impl.values as any;
 
-    export const hashCode: (set: AtomSet) => number = Base.hashCode as any;
-    export const areEqual: (a: AtomSet, b: AtomSet) => boolean = Base.areEqual as any;
-    export const areIntersecting: (a: AtomSet, b: AtomSet) => boolean = Base.areIntersecting as any;
+    export const hashCode: (set: AtomSet) => number = Impl.hashCode as any;
+    export const areEqual: (a: AtomSet, b: AtomSet) => boolean = Impl.areEqual as any;
+    export const areIntersecting: (a: AtomSet, b: AtomSet) => boolean = Impl.areIntersecting as any;
 
-    export const union: (a: AtomSet, b: AtomSet) => AtomSet = Base.union as any;
-    export const unionMany: (sets: AtomSet[]) => AtomSet = Base.unionMany as any;
-    export const intersect: (a: AtomSet, b: AtomSet) => AtomSet = Base.intersect as any;
-    export const subtract: (a: AtomSet, b: AtomSet) => AtomSet = Base.subtract as any;
+    export const union: (a: AtomSet, b: AtomSet) => AtomSet = Impl.union as any;
+    export const unionMany: (sets: AtomSet[]) => AtomSet = Impl.unionMany as any;
+    export const intersect: (a: AtomSet, b: AtomSet) => AtomSet = Impl.intersect as any;
+    export const subtract: (a: AtomSet, b: AtomSet) => AtomSet = Impl.subtract as any;
 
     export function SortedBuilder(parent: AtomSet) { return createBuilder(parent, true); }
     export function Builder(parent: AtomSet) { return createBuilder(parent, false); }

+ 3 - 3
src/mol-data/structure/atom-set/builder.ts → src/mol-data/structure/atom/set/builder.ts

@@ -4,9 +4,9 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import AtomSet from '../atom-set'
-import OrderedSet from '../../../mol-base/collections/integer/ordered-set'
-import { sortArray } from '../../../mol-base/collections/sort'
+import AtomSet from '../set'
+import { OrderedSet } from '../../../../mol-base/collections/integer'
+import { sortArray } from '../../../../mol-base/collections/sort'
 
 class Builder {
     private keys: number[] = [];

+ 4 - 7
src/mol-data/structure/atom-set/base.ts → src/mol-data/structure/atom/set/impl.ts

@@ -4,13 +4,10 @@
  * @author David Sehnal <david.sehnal@gmail.com>
  */
 
-import OrderedSet from '../../../mol-base/collections/integer/ordered-set'
-import SortedArray from '../../../mol-base/collections/integer/sorted-array'
-import Iterator from '../../../mol-base/collections/iterator'
-import Interval from '../../../mol-base/collections/integer/interval'
-import { sortArray } from '../../../mol-base/collections/sort'
-import { hash1 } from '../../../mol-base/collections/hash-functions'
-import Atom from '../atom'
+import { SortedArray, Interval, Iterator, OrderedSet } from '../../../../mol-base/collections/integer'
+import { sortArray } from '../../../../mol-base/collections/sort'
+import { hash1 } from '../../../../mol-base/collections/hash-functions'
+import Atom from '../../atom'
 
 /** Long and painful implementation starts here */
 

+ 0 - 0
src/mol-data/structure/atom-set/properties.ts → src/mol-data/structure/atom/set/properties.ts


+ 0 - 37
src/mol-data/structure/base.ts

@@ -1,37 +0,0 @@
-/**
- * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
- *
- * @author David Sehnal <david.sehnal@gmail.com>
- */
-
-import Model from '../model'
-import Structure from '../structure'
-import Unit from './unit'
-import Operator from './operator'
-import AtomSet from './atom-set'
-import OrderedSet from '../../mol-base/collections/integer/ordered-set'
-
-export class StructureBuilder {
-    private units = Object.create(null);
-    private atoms = Object.create(null);
-
-    addUnit(unit: Unit) { this.units[unit.id] = unit; }
-    addAtoms(unitId: number, atoms: OrderedSet) { this.atoms[unitId] = atoms; }
-
-    getStructure(): Structure { return { units: this.units, atoms: AtomSet.create(this.atoms) }; }
-}
-
-export const Empty: Structure = { units: {}, atoms: AtomSet.Empty };
-
-export function ofModel(model: Model): Structure {
-    const chains = model.hierarchy.chainSegments;
-    const builder = new StructureBuilder();
-
-    for (let c = 0; c < chains.count; c++) {
-        const unit = Unit.create(model, Operator.Identity);
-        builder.addUnit(unit);
-        builder.addAtoms(unit.id, OrderedSet.ofBounds(chains.segments[c], chains.segments[c + 1]));
-    }
-
-    return builder.getStructure();
-}

+ 58 - 0
src/mol-data/structure/structure.ts

@@ -0,0 +1,58 @@
+/**
+ * Copyright (c) 2017 molio contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author David Sehnal <david.sehnal@gmail.com>
+ */
+
+import Model from '../model'
+import Unit from './unit'
+import Operator from './operator'
+import AtomSet from './atom/set'
+import { OrderedSet } from '../../mol-base/collections/integer'
+
+interface Structure extends Readonly<{
+    units: { readonly [id: number]: Unit },
+    atoms: AtomSet
+}> { }
+
+namespace Structure {
+    export const Empty = { units: {}, atoms: AtomSet.Empty };
+
+    export function ofModel(model: Model): Structure {
+        const chains = model.hierarchy.chainSegments;
+        const builder = Builder();
+
+        for (let c = 0; c < chains.count; c++) {
+            const unit = Unit.create(model, Operator.Identity);
+            builder.addUnit(unit);
+            builder.addAtoms(unit.id, OrderedSet.ofBounds(chains.segments[c], chains.segments[c + 1]));
+        }
+
+        return builder.getStructure();
+    }
+
+    export interface Builder {
+        addUnit(unit: Unit): void,
+        addAtoms(unitId: number, atoms: OrderedSet): void,
+        getStructure(): Structure,
+        readonly atomCount: number
+    }
+
+    class BuilderImpl implements Builder {
+        private units = Object.create(null);
+        private atoms = Object.create(null);
+        atomCount = 0;
+
+        addUnit(unit: Unit) { this.units[unit.id] = unit; }
+        addAtoms(unitId: number, atoms: OrderedSet) { this.atoms[unitId] = atoms; this.atomCount += OrderedSet.size(atoms); }
+        getStructure(): Structure { return this.atomCount > 0 ? { units: this.units, atoms: AtomSet.create(this.atoms) } : Empty; }
+    }
+
+    export function Builder(): Builder { return new BuilderImpl(); }
+
+
+    // TODO: "lift" atom set operators?
+    // TODO: "diff"
+}
+
+export default Structure

+ 2 - 4
src/perf-tests/sets.ts

@@ -1,8 +1,6 @@
 import * as B from 'benchmark'
-import Tuple from '../mol-base/collections/integer/tuple'
-import OrdSet from '../mol-base/collections/integer/ordered-set'
-import AtomSet from '../mol-data/structure/atom-set'
-import Segmentation from '../mol-base/collections/integer/segmentation'
+import { Tuple, Segmentation, OrderedSet as OrdSet } from '../mol-base/collections/integer'
+import { AtomSet } from '../mol-data/structure'
 
 export namespace Iteration {
     const U = 1000, V = 2500;

+ 2 - 5
src/perf-tests/structure.ts

@@ -11,11 +11,8 @@ import * as fs from 'fs'
 import CIF from '../mol-io/reader/cif'
 
 import Model from '../mol-data/Model'
-import Structure from '../mol-data/structure'
-import OrdSet from '../mol-base/collections/integer/ordered-set'
-import Atom from '../mol-data/structure/atom'
-import AtomSet from '../mol-data/structure/atom-set'
-import Segmentation from '../mol-base/collections/integer/segmentation'
+import { Structure, Atom, AtomSet } from '../mol-data/structure'
+import { OrderedSet as OrdSet, Segmentation } from '../mol-base/collections/integer'
 
 require('util.promisify').shim();
 const readFileAsync = util.promisify(fs.readFile);