molstar
Version:
A comprehensive macromolecular library.
67 lines • 2.48 kB
JavaScript
/**
* Copyright (c) 2017-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author David Sehnal <david.sehnal@gmail.com>
*/
import { IntTuple as Tuple } from '../tuple';
export var Empty = Tuple.Zero;
export function ofRange(min, max) { return max < min ? Tuple.create(min, min) : Tuple.create(min, max + 1); }
export function ofBounds(start, end) { return end <= start ? Tuple.create(start, start) : Tuple.create(start, end); }
export function ofLength(length) { return length < 0 ? Tuple.create(0, 0) : Tuple.create(0, length); }
export var is = Tuple.is;
export var start = Tuple.fst;
export var end = Tuple.snd;
export var min = Tuple.fst;
export function max(i) { return Tuple.snd(i) - 1; }
export var size = Tuple.diff;
export var hashCode = Tuple.hashCode;
export var toString = Tuple.toString;
export function has(int, v) { return Tuple.fst(int) <= v && v < Tuple.snd(int); }
/** Returns the index of `x` in `set` or -1 if not found. */
export function indexOf(int, x) { var m = start(int); return x >= m && x < end(int) ? x - m : -1; }
export function getAt(int, i) { return Tuple.fst(int) + i; }
export var areEqual = Tuple.areEqual;
export function areIntersecting(a, b) {
var sa = size(a), sb = size(b);
if (sa === 0 && sb === 0)
return true;
return sa > 0 && sb > 0 && max(a) >= min(b) && min(a) <= max(b);
}
export function isSubInterval(a, b) {
if (!size(a))
return size(b) === 0;
if (!size(b))
return true;
return start(a) <= start(b) && end(a) >= end(b);
}
export function findPredecessorIndex(int, v) {
var s = start(int);
if (v <= s)
return 0;
var e = end(int);
if (v >= e)
return e - s;
return v - s;
}
export function findPredecessorIndexInInterval(int, v, bounds) {
var bS = start(bounds);
var s = start(int);
if (v <= bS + s)
return bS;
var bE = end(bounds);
if (v >= bE + s)
return bE;
return v - s;
}
export function findRange(int, min, max) {
return ofBounds(findPredecessorIndex(int, min), findPredecessorIndex(int, max + 1));
}
export function intersect(a, b) {
if (!areIntersecting(a, b))
return Empty;
return ofBounds(Math.max(start(a), start(b)), Math.min(end(a), end(b)));
}
export function intersectionSize(a, b) {
return size(findRange(a, min(b), max(b)));
}
//# sourceMappingURL=interval.js.map