Skip to content
Snippets Groups Projects
Commit fa5d1251 authored by David Sehnal's avatar David Sehnal
Browse files

OrderedSet.forEach

parent 22dcd689
No related branches found
No related tags found
No related merge requests found
......@@ -157,4 +157,10 @@ describe('ordered set', () => {
testEq('subtract AA', OrderedSet.subtract(arr136, arr136), []);
testEq('subtract AA1', OrderedSet.subtract(arr136, OrderedSet.ofSortedArray([2, 3, 4, 6, 7])), [1]);
testEq('subtract AA2', OrderedSet.subtract(arr136, OrderedSet.ofSortedArray([0, 1, 6])), [3]);
it('foreach', () => {
const int = OrderedSet.ofBounds(1, 3), set = OrderedSet.ofSortedArray([2, 3, 4]);
expect(OrderedSet.forEach(int, (v, i, ctx) => ctx[i] = v, [] as number[])).toEqual([1, 2]);
expect(OrderedSet.forEach(set, (v, i, ctx) => ctx[i] = v, [] as number[])).toEqual([2, 3, 4]);
})
});
\ No newline at end of file
......@@ -253,4 +253,18 @@ function subtractIS(a: I, b: S) {
}
for (let i = last + 1; i <= max; i++) ret[offset++] = i;
return ofSortedArray(ret);
}
export function forEach(set: OrderedSetImpl, f: (value: number, i: number, ctx: any) => void, ctx: any) {
if (I.is(set)) {
const start = I.min(set);
for (let i = start, _i = I.max(set); i <= _i; i++) {
f(i, i - start, ctx);
}
} else {
for (let i = 0, _i = set.length; i < _i; i++) {
f(set[i], i, ctx);
}
}
return ctx;
}
\ No newline at end of file
......@@ -35,6 +35,10 @@ namespace OrderedSet {
export const findPredecessorIndex: (set: OrderedSet, x: number) => number = Base.findPredecessorIndex as any;
export const findPredecessorIndexInInterval: (set: OrderedSet, x: number, range: Interval) => number = Base.findPredecessorIndexInInterval as any;
export const findRange: (set: OrderedSet, min: number, max: number) => Interval = Base.findRange as any;
export function forEach<Ctx>(set: OrderedSet, f: (v: number, i: number, ctx: Ctx) => void, ctx?: Ctx): Ctx {
return Base.forEach(set as any, f, ctx);
}
}
interface OrderedSet { '@type': 'int-interval' | 'int-sorted-array' }
......
......@@ -7,7 +7,7 @@
import { Column, Table } from 'mol-data/db';
import { Interval, Segmentation } from 'mol-data/int';
import { Spacegroup, SpacegroupCell, SymmetryOperator } from 'mol-math/geometry';
import { Vec3, Tensor, Mat4 } from 'mol-math/linear-algebra';
import { Vec3, Tensor } from 'mol-math/linear-algebra';
import { Task } from 'mol-task';
import UUID from 'mol-util/uuid';
import Format from '../format';
......
......@@ -25,7 +25,7 @@ const _esCache = (function () {
}
return cache;
}());
export interface ElementSymbol extends String { '@type': 'element-symbol' }
export type ElementSymbol = string & { '@type': 'element-symbol' }
export function ElementSymbol(s: string): ElementSymbol {
return _esCache[s] || s.toUpperCase();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment