Skip to content
Snippets Groups Projects
Commit b02eff36 authored by Alexander Rose's avatar Alexander Rose
Browse files

added SetUtils namespace for mol-util/set

parent b06fbec7
No related branches found
No related tags found
No related merge requests found
......@@ -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)) {
......
......@@ -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'
......
......@@ -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> {
......
......@@ -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);
}
......
......@@ -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) => {
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment