iteragain
Version:
Javascript Iterable/Iterator/Generator-function utilities.
43 lines • 1.59 kB
JavaScript
import toArray from '../toArray';
/** Returns the cartesian product of the input `iterators`. */
var ProductIterator = /** @class */ (function () {
function ProductIterator(iterators, repeat) {
var _a;
this.done = false;
this.pools = [];
this.i = 0;
var iterated = iterators.map(toArray);
for (var i = 0; i < repeat; i++)
(_a = this.pools).push.apply(_a, iterated);
this.indices = new Array(this.pools.length).fill(0);
}
ProductIterator.prototype[Symbol.iterator] = function () {
return this;
};
ProductIterator.prototype.next = function () {
var _this = this;
if (this.done)
return { done: true, value: undefined };
var value = this.indices.map(function (v, i) { return _this.pools[i][v]; });
for (this.i = 0; this.i < this.indices.length; this.i++) {
if (this.indices[this.pools.length - this.i - 1] < this.pools[this.pools.length - this.i - 1].length - 1) {
this.indices[this.pools.length - this.i - 1]++;
break;
}
}
if (this.i === this.indices.length) {
this.done = true;
return { done: false, value: value };
}
this.i--;
while (this.i >= 0) {
this.indices[this.pools.length - this.i - 1] = 0;
this.i--;
}
return { done: false, value: value };
};
return ProductIterator;
}());
export { ProductIterator };
export default ProductIterator;
//# sourceMappingURL=ProductIterator.js.map