From b02eff36e31f6964151411d7f7008e25b7347118 Mon Sep 17 00:00:00 2001 From: Alexander Rose <alex.rose@rcsb.org> Date: Wed, 30 Jan 2019 14:00:27 -0800 Subject: [PATCH] added SetUtils namespace for mol-util/set --- src/apps/chem-comp-bond/create-table.ts | 4 +- src/mol-canvas3d/canvas3d.ts | 2 +- .../structure/export/categories/utils.ts | 4 +- .../structure/query/queries/filters.ts | 4 +- src/mol-script/runtime/query/table.ts | 4 +- src/mol-util/set.ts | 99 ++++++++++--------- 6 files changed, 64 insertions(+), 53 deletions(-) diff --git a/src/apps/chem-comp-bond/create-table.ts b/src/apps/chem-comp-bond/create-table.ts index c30bc04b2..28dc5000f 100644 --- a/src/apps/chem-comp-bond/create-table.ts +++ b/src/apps/chem-comp-bond/create-table.ts @@ -19,7 +19,7 @@ import { Database, Table, DatabaseCollection, Column } from 'mol-data/db' import CIF from 'mol-io/reader/cif' import { CifWriter } from 'mol-io/writer/cif' import { CCD_Schema } from 'mol-io/reader/cif/schema/ccd' -import { difference } from 'mol-util/set' +import { SetUtils } from 'mol-util/set' import { DefaultMap } from 'mol-util/map' export async function ensureAvailable(path: string, url: string) { @@ -130,7 +130,7 @@ function checkAddingBondsFromPVCD(pvcd: DatabaseCollection<CCD_Schema>) { for (let i = 0, il = parentIds.length; i < il; ++i) { const entryBonds = addChemCompBondToSet(new Set<string>(), chem_comp_bond) const entryAtoms = addChemCompAtomToSet(new Set<string>(), chem_comp_atom) - const extraBonds = difference(ccbSetByParent.get(parentIds[i])!, entryBonds) + const extraBonds = SetUtils.difference(ccbSetByParent.get(parentIds[i])!, entryBonds) extraBonds.forEach(bk => { const [a1, a2] = bk.split('|') if (entryAtoms.has(a1) && entryAtoms.has(a2)) { diff --git a/src/mol-canvas3d/canvas3d.ts b/src/mol-canvas3d/canvas3d.ts index 814503272..60b32ca19 100644 --- a/src/mol-canvas3d/canvas3d.ts +++ b/src/mol-canvas3d/canvas3d.ts @@ -9,7 +9,7 @@ import { now } from 'mol-util/now'; import { Vec3 } from 'mol-math/linear-algebra' import InputObserver from 'mol-util/input/input-observer' -import * as SetUtils from 'mol-util/set' +import { SetUtils } from 'mol-util/set' import Renderer, { RendererStats } from 'mol-gl/renderer' import { RenderObject } from 'mol-gl/render-object' diff --git a/src/mol-model/structure/export/categories/utils.ts b/src/mol-model/structure/export/categories/utils.ts index d0a63367f..17869c5f1 100644 --- a/src/mol-model/structure/export/categories/utils.ts +++ b/src/mol-model/structure/export/categories/utils.ts @@ -5,7 +5,7 @@ */ import { mmCIF_Database, mmCIF_Schema } from 'mol-io/reader/cif/schema/mmcif'; -import { unionMany } from 'mol-util/set'; +import { SetUtils } from 'mol-util/set'; import { Model } from '../../model'; import { Structure } from '../../structure'; import { EntityIndex } from '../../model/indexing'; @@ -20,7 +20,7 @@ export function getModelMmCifCategory<K extends keyof mmCIF_Schema>(model: Model } export function getUniqueResidueNamesFromStructures(structures: Structure[]) { - return unionMany(structures.map(s => s.uniqueResidueNames)); + return SetUtils.unionMany(structures.map(s => s.uniqueResidueNames)); } export function getUniqueEntityIdsFromStructures(structures: Structure[]): Set<string> { diff --git a/src/mol-model/structure/query/queries/filters.ts b/src/mol-model/structure/query/queries/filters.ts index 94aad107e..a64cffc38 100644 --- a/src/mol-model/structure/query/queries/filters.ts +++ b/src/mol-model/structure/query/queries/filters.ts @@ -4,7 +4,7 @@ * @author David Sehnal <david.sehnal@gmail.com> */ -import { isSuperset } from 'mol-util/set'; +import { SetUtils } from 'mol-util/set'; import { Unit } from '../../structure'; import { QueryContext, QueryFn, QueryPredicate } from '../context'; import { StructureQuery } from '../query'; @@ -82,7 +82,7 @@ export function withSameAtomProperties(query: StructureQuery, propertySource: St StructureSelection.forEach(sel, (s, i) => { ctx.currentStructure = s; const currentProps = getCurrentStructureProperties(ctx, props, new Set()); - if (isSuperset(currentProps, propSet)) { + if (SetUtils.isSuperset(currentProps, propSet)) { ret.add(s); } diff --git a/src/mol-script/runtime/query/table.ts b/src/mol-script/runtime/query/table.ts index 9782aeb13..6a4d2be3f 100644 --- a/src/mol-script/runtime/query/table.ts +++ b/src/mol-script/runtime/query/table.ts @@ -8,7 +8,7 @@ import { MolScriptSymbolTable as MolScript } from '../../language/symbol-table'; import { DefaultQueryRuntimeTable, QuerySymbolRuntime, QueryRuntimeArguments } from './compiler'; import { Queries, StructureProperties, StructureElement, QueryContext } from 'mol-model/structure'; import { ElementSymbol } from 'mol-model/structure/model/types'; -import { isSuperset } from 'mol-util/set'; +import { SetUtils } from 'mol-util/set'; import toUpperCase from 'mol-util/upper-case'; import { VdwRadius, AtomWeight, AtomNumber } from 'mol-model/structure/model/properties/atomic'; import { cantorPairing } from 'mol-data/util'; @@ -150,7 +150,7 @@ const symbols = [ // ============= SET ================ C(MolScript.core.set.has, (ctx, v) => v[0](ctx).has(v[1](ctx))), - C(MolScript.core.set.isSubset, (ctx, v) => isSuperset(v[1](ctx) as Set<any>, v[0](ctx) as Set<any>)), + C(MolScript.core.set.isSubset, (ctx, v) => SetUtils.isSuperset(v[1](ctx) as Set<any>, v[0](ctx) as Set<any>)), // ============= FLAGS ================ C(MolScript.core.flags.hasAny, (ctx, v) => { diff --git a/src/mol-util/set.ts b/src/mol-util/set.ts index 7bc7e10d4..34a79112f 100644 --- a/src/mol-util/set.ts +++ b/src/mol-util/set.ts @@ -6,52 +6,63 @@ // TODO remove Array.from workaround when targeting ES6 -/** Test if set a contains all elements of set b. */ -export function isSuperset<T>(setA: Set<T>, setB: Set<T>) { - for (const elm of Array.from(setB)) { - if (!setA.has(elm)) return false; +export namespace SetUtils { + /** Test if set a contains all elements of set b. */ + export function isSuperset<T>(setA: Set<T>, setB: Set<T>) { + for (const elm of Array.from(setB)) { + if (!setA.has(elm)) return false; + } + return true; } - return true; -} - -/** Create set containing elements of both set a and set b. */ -export function union<T>(setA: Set<T>, setB: Set<T>): Set<T> { - const union = new Set(setA); - for (const elem of Array.from(setB)) union.add(elem); - return union; -} - -export function unionMany<T>(sets: Set<T>[]) { - if (sets.length === 0) return new Set<T>(); - if (sets.length === 1) return sets[0]; - const union = new Set(sets[0]); - for (let i = 1; i < sets.length; i++) { - for (const elem of Array.from(sets[i])) union.add(elem); + + /** Create set containing elements of both set a and set b. */ + export function union<T>(setA: Set<T>, setB: Set<T>): Set<T> { + const union = new Set(setA); + for (const elem of Array.from(setB)) union.add(elem); + return union; } - return union; -} - -export function unionManyArrays<T>(arrays: T[][]) { - if (arrays.length === 0) return new Set<T>(); - const union = new Set(arrays[0]); - for (let i = 1; i < arrays.length; i++) { - for (const elem of arrays[i]) union.add(elem); + + export function unionMany<T>(sets: Set<T>[]) { + if (sets.length === 0) return new Set<T>(); + if (sets.length === 1) return sets[0]; + const union = new Set(sets[0]); + for (let i = 1; i < sets.length; i++) { + for (const elem of Array.from(sets[i])) union.add(elem); + } + return union; } - return union; -} - -/** Create set containing elements of set a that are also in set b. */ -export function intersection<T>(setA: Set<T>, setB: Set<T>): Set<T> { - const intersection = new Set(); - for (const elem of Array.from(setB)) { - if (setA.has(elem)) intersection.add(elem); + + export function unionManyArrays<T>(arrays: T[][]) { + if (arrays.length === 0) return new Set<T>(); + const union = new Set(arrays[0]); + for (let i = 1; i < arrays.length; i++) { + for (const elem of arrays[i]) union.add(elem); + } + return union; + } + + /** Create set containing elements of set a that are also in set b. */ + export function intersection<T>(setA: Set<T>, setB: Set<T>): Set<T> { + const intersection = new Set(); + for (const elem of Array.from(setB)) { + if (setA.has(elem)) intersection.add(elem); + } + return intersection; + } + + /** Create set containing elements of set a that are not in set b. */ + export function difference<T>(setA: Set<T>, setB: Set<T>): Set<T> { + const difference = new Set(setA); + for (const elem of Array.from(setB)) difference.delete(elem); + return difference; + } + + /** Test if set a and b contain the same elements. */ + export function areEqual<T>(setA: Set<T>, setB: Set<T>) { + if (setA.size !== setB.size) return false + for (const elm of Array.from(setB)) { + if (!setA.has(elm)) return false; + } + return true; } - return intersection; -} - -/** Create set containing elements of set a that are not in set b. */ -export function difference<T>(setA: Set<T>, setB: Set<T>): Set<T> { - const difference = new Set(setA); - for (const elem of Array.from(setB)) difference.delete(elem); - return difference; } \ No newline at end of file -- GitLab