lazzy.ts
Version:
Fast and lightweight library for lazy operations with iterable objects.
108 lines • 4.12 kB
JavaScript
;
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.balancedChunkAsync = exports.balancedChunk = void 0;
const _1 = require(".");
function swap(array, index) {
const tempChunks = array[index];
array[index] = array[index - 1];
array[index - 1] = tempChunks;
}
function* balancedChunk(iterator, weight, ...select) {
let selector;
if (select[0] !== undefined) {
selector = select[0];
}
else {
selector = (v) => v;
}
let index = 0;
let yieldedChunks = 0;
const chunks = [];
const chunkSums = [];
const sorted = _1.sort(iterator, (a, b) => selector(b) - selector(a));
let x = sorted.next();
while (x.done !== true) {
const n = selector(x.value);
while (chunkSums[index - 1] + n <= weight) {
index--;
}
if (chunkSums[index] == null) {
chunkSums.push(0);
chunks.push([]);
}
chunkSums[index] += n;
chunks[index].push(x.value);
while (index > 0 && chunkSums[index - 1] < chunkSums[index]) {
swap(chunkSums, index);
swap(chunks, index);
index--;
}
if (chunkSums[index] >= weight) {
yieldedChunks++;
yield chunks[index];
}
index = chunks.length;
x = sorted.next();
}
while (yieldedChunks < chunks.length) {
yield chunks[yieldedChunks++];
}
}
exports.balancedChunk = balancedChunk;
function balancedChunkAsync(iterator, weight, ...select) {
return __asyncGenerator(this, arguments, function* balancedChunkAsync_1() {
let selector;
if (select[0] !== undefined) {
selector = select[0];
}
else {
selector = (v) => v;
}
let index = 0;
let yieldedChunks = 0;
const chunks = [];
const chunkSums = [];
const sorted = _1.sortAsync(iterator, (a, b) => selector(b) - selector(a));
let x = yield __await(sorted.next());
while (x.done !== true) {
const n = selector(x.value);
while (chunkSums[index - 1] + n <= weight) {
index--;
}
if (chunkSums[index] == null) {
chunkSums.push(0);
chunks.push([]);
}
chunkSums[index] += n;
chunks[index].push(x.value);
while (index > 0 && chunkSums[index - 1] < chunkSums[index]) {
swap(chunkSums, index);
swap(chunks, index);
index--;
}
if (chunkSums[index] >= weight) {
yieldedChunks++;
yield yield __await(chunks[index]);
}
index = chunks.length;
x = yield __await(sorted.next());
}
while (yieldedChunks < chunks.length) {
yield yield __await(chunks[yieldedChunks++]);
}
});
}
exports.balancedChunkAsync = balancedChunkAsync;
//# sourceMappingURL=balancedChunk.js.map