import {Point} from '..' import {PointPair} from '../math/geometry/pointPair' import {PointPairMap} from './pointPairMap' import {PointSet} from './PointSet' export function substractSets(a: Set, b: Set): Set { const ret = new Set() for (const u of a) { if (!b.has(u)) ret.add(u) } return ret } export function substractPointSets(a: PointSet, b: PointSet): PointSet { const ret = new PointSet() for (const u of a) { if (!b.has(u)) ret.add(u) } return ret } export function uniteSets(a: Set, b: Set): Set { const ret = new Set(a) for (const v of b) { ret.add(v) } return ret } export function addRange(array: Array, addedIterable: Iterable) { for (const t of addedIterable) array.push(t) } export function setIntersection(a: Set, b: Set): Set { const ret = new Set() if (a.size < b.size) { for (const t of a) { if (b.has(t)) ret.add(t) } } else { for (const t of b) { if (a.has(t)) ret.add(t) } } return ret } export function setIntersectionOfArray(arr: Set[]): Set { if (arr.length === 0) return new Set() let ret = arr[0] for (let i = 1; i < arr.length; i++) { ret = setIntersection(ret, arr[i]) } return ret } export function insertRange(collection: Set, addedArray: Iterable) { for (const t of addedArray) collection.add(t) } export function setsAreEqual(a: Set, b: Set): boolean { if (a.size !== b.size) return false for (const u of a) if (!b.has(u)) return false return true } /** return the concatenated array of items */ export function flattenArray(arr: ReadonlyArray, callback: (elem: T) => Iterable): U[] { const ret = [] for (const f of arr) { for (const u of callback(f)) ret.push(u) } return ret } /** adds val to map.get(key) if the key exists, otherwise creates the key pair and * executes the former instruction */ export function addToMapOfSets(map: Map>, key: K, val: V) { let s = map.get(key) if (!s) { s = new Set() map.set(key, s) } s.add(val) } export function addToMapOfArrays(map: Map>, key: K, val: V) { let s = map.get(key) if (!s) { s = new Array() map.set(key, s) } s.push(val) } export function addToPointPairMap(map: PointPairMap>, key: PointPair, val: V) { let s = map.get(key) if (!s) { s = new Set() map.set(key, s) } s.add(val) } export function addToPointMapTuple(map: PointPairMap>, key: [Point, Point], val: V) { addToPointPairMap(map, new PointPair(key[0], key[1]), val) } export function removeFromPointPairMap(map: PointPairMap>, key: PointPair, val: V) { const s = map.get(key) if (s) s.delete(val) } export function removeFromPointPairMapTuple(map: PointPairMap>, key: [Point, Point], val: V) { removeFromPointPairMap(map, new PointPair(key[0], key[1]), val) } export function removeFromArray(arr: T[], OverlapRemovalNode: T) { const i = arr.findIndex((a: T) => a === OverlapRemovalNode) if (i >= 0) { arr.splice(i, 1) } }