UNPKG

molstar

Version:

A comprehensive macromolecular library.

61 lines 1.92 kB
"use strict"; /** * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.combinations = exports.CombinationIterator = void 0; function P(m, n) { var p = 1; while (n--) p *= m--; return p; } function C(m, n) { if (n > m) return 0; return P(m, n) / P(n, n); } function nextIndex(n) { var smallest = n & -n; var ripple = n + smallest; var newSmallest = ripple & -ripple; var ones = ((newSmallest / smallest) >> 1) - 1; return ripple | ones; } ; var CombinationIterator = /** @class */ (function () { function CombinationIterator(array, count) { this.array = array; this.hasNext = false; this.index = (1 << count) - 1; this.size = C(array.length, count); this.maxIndex = 1 << array.length, this.value = new Array(count); this.hasNext = count > 0 && count <= array.length; } CombinationIterator.prototype.move = function () { if (this.hasNext) { var i = 0, j = 0, n = this.index; for (; n; n >>>= 1, i++) { if (n & 1) this.value[j++] = this.array[i]; } this.index = nextIndex(this.index); this.hasNext = this.index < this.maxIndex; } return this.value; }; return CombinationIterator; }()); exports.CombinationIterator = CombinationIterator; function combinations(array, count) { var out = []; var combinationIt = new CombinationIterator(array, count); while (combinationIt.hasNext) out.push(combinationIt.move().slice()); return out; } exports.combinations = combinations; //# sourceMappingURL=combination.js.map