@rimbu/stream
Version:
Efficient structure representing a sequence of elements, with powerful operations for TypeScript
692 lines • 26.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MapPureIterator = exports.MapIterator = exports.AppendIterator = exports.PrependIterator = exports.ZipAllWithItererator = exports.ZipWithIterator = exports.UnfoldIterator = exports.RandomIntIterator = exports.RandomIterator = exports.RangeDownIterator = exports.RangeUpIterator = exports.FilterApplyIterator = exports.MapApplyIterator = exports.AlwaysIterator = exports.ArrayReverseIterator = exports.ArrayIterator = exports.RepeatIterator = exports.DropIterator = exports.TakeIterator = exports.DropWhileIterator = exports.CollectIterator = exports.FilterPureIterator = exports.FilterIterator = exports.IndexedIterator = exports.ConcatIterator = exports.TransformerFastIterator = exports.ReducerFastIterator = exports.FastIteratorBase = exports.emptyFastIterator = exports.fixedDoneIteratorResult = void 0;
exports.isFastIterator = isFastIterator;
var tslib_1 = require("tslib");
var base_1 = require("@rimbu/base");
var common_1 = require("@rimbu/common");
var custom_1 = require("@rimbu/stream/custom");
exports.fixedDoneIteratorResult = Object.freeze({
done: true,
value: undefined,
});
exports.emptyFastIterator = Object.freeze({
fastNext: function (otherwise) {
return (0, common_1.OptLazy)(otherwise);
},
next: function () {
return exports.fixedDoneIteratorResult;
},
});
function isFastIterator(iterator) {
return "fastNext" in iterator;
}
/**
* A base class for `FastIterator` instances, that takes implements the default `next`
* function based on the abstract `fastNext` function.
*/
var FastIteratorBase = /** @class */ (function () {
function FastIteratorBase() {
}
FastIteratorBase.prototype.next = function () {
var done = Symbol('Done');
var value = this.fastNext(done);
if (done === value)
return exports.fixedDoneIteratorResult;
return { value: value, done: false };
};
return FastIteratorBase;
}());
exports.FastIteratorBase = FastIteratorBase;
var ReducerFastIterator = /** @class */ (function (_super) {
tslib_1.__extends(ReducerFastIterator, _super);
function ReducerFastIterator(sourceIterator, reducerInstance) {
var _this = _super.call(this) || this;
_this.sourceIterator = sourceIterator;
_this.reducerInstance = reducerInstance;
return _this;
}
ReducerFastIterator.prototype.fastNext = function (otherwise) {
if (this.reducerInstance.halted) {
return (0, common_1.OptLazy)(otherwise);
}
var done = Symbol('done');
var nextInput = this.sourceIterator.fastNext(done);
if (done === nextInput) {
this.reducerInstance.halt();
return (0, common_1.OptLazy)(otherwise);
}
this.reducerInstance.next(nextInput);
return this.reducerInstance.getOutput();
};
return ReducerFastIterator;
}(FastIteratorBase));
exports.ReducerFastIterator = ReducerFastIterator;
var TransformerFastIterator = /** @class */ (function (_super) {
tslib_1.__extends(TransformerFastIterator, _super);
function TransformerFastIterator(sourceIterator, transformerInstance) {
var _this = _super.call(this) || this;
_this.sourceIterator = sourceIterator;
_this.transformerInstance = transformerInstance;
_this.___done = false;
return _this;
}
TransformerFastIterator.prototype.fastNext = function (otherwise) {
if (this.___done) {
return (0, common_1.OptLazy)(otherwise);
}
var done = Symbol('done');
var nextValue;
while (undefined === this.___currentValues ||
done === (nextValue = this.___currentValues.fastNext(done))) {
if (this.transformerInstance.halted) {
this.___done = true;
return (0, common_1.OptLazy)(otherwise);
}
var nextSource = this.sourceIterator.fastNext(done);
if (done === nextSource) {
if (this.transformerInstance.halted) {
this.___done = true;
return (0, common_1.OptLazy)(otherwise);
}
this.___done = true;
this.transformerInstance.halt();
}
else {
this.transformerInstance.next(nextSource);
}
var nextValuesSource = this.transformerInstance.getOutput();
this.___currentValues =
(0, custom_1.fromStreamSource)(nextValuesSource)[Symbol.iterator]();
}
return nextValue;
};
return TransformerFastIterator;
}(FastIteratorBase));
exports.TransformerFastIterator = TransformerFastIterator;
var ConcatIterator = /** @class */ (function (_super) {
tslib_1.__extends(ConcatIterator, _super);
function ConcatIterator(source, otherSources, streamSourceHelpers) {
var _this = _super.call(this) || this;
_this.source = source;
_this.otherSources = otherSources;
_this.streamSourceHelpers = streamSourceHelpers;
_this.sourceIndex = 0;
_this.iterator = source[Symbol.iterator]();
return _this;
}
ConcatIterator.prototype.fastNext = function (otherwise) {
var done = Symbol('Done');
var value;
var length = this.otherSources.length;
var streamSourceHelpers = this.streamSourceHelpers;
while (done === (value = this.iterator.fastNext(done))) {
if (this.sourceIndex >= length)
return (0, common_1.OptLazy)(otherwise);
var nextSource = this.otherSources[this.sourceIndex++];
while (streamSourceHelpers.isEmptyStreamSourceInstance(nextSource)) {
if (this.sourceIndex >= length) {
return (0, common_1.OptLazy)(otherwise);
}
nextSource = this.otherSources[this.sourceIndex++];
}
this.iterator = streamSourceHelpers
.fromStreamSource(nextSource)[Symbol.iterator]();
}
return value;
};
return ConcatIterator;
}(FastIteratorBase));
exports.ConcatIterator = ConcatIterator;
var IndexedIterator = /** @class */ (function (_super) {
tslib_1.__extends(IndexedIterator, _super);
function IndexedIterator(source, startIndex) {
var _this = _super.call(this) || this;
_this.source = source;
_this.startIndex = startIndex;
_this.index = _this.startIndex;
return _this;
}
IndexedIterator.prototype.fastNext = function (otherwise) {
var done = Symbol('Done');
var value = this.source.fastNext(done);
if (done === value) {
return (0, common_1.OptLazy)(otherwise);
}
return [this.index++, value];
};
return IndexedIterator;
}(FastIteratorBase));
exports.IndexedIterator = IndexedIterator;
var FilterIterator = /** @class */ (function (_super) {
tslib_1.__extends(FilterIterator, _super);
function FilterIterator(source, pred, negate) {
var _this = _super.call(this) || this;
_this.source = source;
_this.pred = pred;
_this.negate = negate;
_this.state = (0, common_1.TraverseState)();
return _this;
}
FilterIterator.prototype.fastNext = function (otherwise) {
var state = this.state;
if (state.halted)
return (0, common_1.OptLazy)(otherwise);
var done = Symbol('Done');
var value;
var source = this.source;
var pred = this.pred;
var halt = state.halt;
var negate = this.negate;
while (!state.halted && done !== (value = source.fastNext(done))) {
if (pred(value, state.nextIndex(), halt) !== negate)
return value;
}
return (0, common_1.OptLazy)(otherwise);
};
return FilterIterator;
}(FastIteratorBase));
exports.FilterIterator = FilterIterator;
var FilterPureIterator = /** @class */ (function (_super) {
tslib_1.__extends(FilterPureIterator, _super);
function FilterPureIterator(source, pred, args, negate) {
var _this = _super.call(this) || this;
_this.source = source;
_this.pred = pred;
_this.args = args;
_this.negate = negate;
return _this;
}
FilterPureIterator.prototype.fastNext = function (otherwise) {
var done = Symbol('Done');
var value;
var source = this.source;
var pred = this.pred;
var args = this.args;
var negate = this.negate;
while (done !== (value = source.fastNext(done))) {
if (pred.apply(void 0, tslib_1.__spreadArray([value], tslib_1.__read(args), false)) !== negate)
return value;
}
return (0, common_1.OptLazy)(otherwise);
};
return FilterPureIterator;
}(FastIteratorBase));
exports.FilterPureIterator = FilterPureIterator;
var CollectIterator = /** @class */ (function (_super) {
tslib_1.__extends(CollectIterator, _super);
function CollectIterator(source, collectFun) {
var _this = _super.call(this) || this;
_this.source = source;
_this.collectFun = collectFun;
_this.state = (0, common_1.TraverseState)();
return _this;
}
CollectIterator.prototype.fastNext = function (otherwise) {
var state = this.state;
if (state.halted)
return (0, common_1.OptLazy)(otherwise);
var halt = state.halt;
var done = Symbol('Done');
var value;
var source = this.source;
var collectFun = this.collectFun;
while (!state.halted && done !== (value = source.fastNext(done))) {
var result = collectFun(value, state.nextIndex(), common_1.CollectFun.Skip, halt);
if (common_1.CollectFun.Skip === result)
continue;
return result;
}
return (0, common_1.OptLazy)(otherwise);
};
return CollectIterator;
}(FastIteratorBase));
exports.CollectIterator = CollectIterator;
var DropWhileIterator = /** @class */ (function (_super) {
tslib_1.__extends(DropWhileIterator, _super);
function DropWhileIterator(source, pred, negate) {
var _this = _super.call(this) || this;
_this.source = source;
_this.pred = pred;
_this.negate = negate;
_this.pass = false;
_this.index = 0;
return _this;
}
DropWhileIterator.prototype.fastNext = function (otherwise) {
var source = this.source;
if (this.pass)
return source.fastNext(otherwise);
var done = Symbol('Done');
var value;
var negate = this.negate;
while (done !== (value = source.fastNext(done))) {
this.pass = this.pred(value, this.index++) === negate;
if (this.pass)
return value;
}
return (0, common_1.OptLazy)(otherwise);
};
return DropWhileIterator;
}(FastIteratorBase));
exports.DropWhileIterator = DropWhileIterator;
var TakeIterator = /** @class */ (function (_super) {
tslib_1.__extends(TakeIterator, _super);
function TakeIterator(source, amount) {
var _this = _super.call(this) || this;
_this.source = source;
_this.amount = amount;
_this.i = 0;
return _this;
}
TakeIterator.prototype.fastNext = function (otherwise) {
if (this.i++ >= this.amount)
return (0, common_1.OptLazy)(otherwise);
return this.source.fastNext(otherwise);
};
return TakeIterator;
}(FastIteratorBase));
exports.TakeIterator = TakeIterator;
var DropIterator = /** @class */ (function (_super) {
tslib_1.__extends(DropIterator, _super);
function DropIterator(source, amount) {
var _this = _super.call(this) || this;
_this.source = source;
_this.amount = amount;
_this.remain = amount;
return _this;
}
DropIterator.prototype.fastNext = function (otherwise) {
var source = this.source;
if (this.remain <= 0)
return source.fastNext(otherwise);
var done = Symbol('Done');
var value;
while (done !== (value = source.fastNext(done))) {
if (this.remain-- <= 0)
return value;
}
return (0, common_1.OptLazy)(otherwise);
};
return DropIterator;
}(FastIteratorBase));
exports.DropIterator = DropIterator;
var RepeatIterator = /** @class */ (function (_super) {
tslib_1.__extends(RepeatIterator, _super);
function RepeatIterator(source, amount) {
var _this = _super.call(this) || this;
_this.source = source;
_this.amount = amount;
_this.isEmpty = true;
_this.iterator = source[Symbol.iterator]();
_this.remain = amount;
return _this;
}
RepeatIterator.prototype.fastNext = function (otherwise) {
var done = Symbol('Done');
var value = this.iterator.fastNext(done);
if (done !== value) {
this.isEmpty = false;
return value;
}
if (this.isEmpty)
return (0, common_1.OptLazy)(otherwise);
if (undefined !== this.remain) {
this.remain--;
if (this.remain <= 0)
return (0, common_1.OptLazy)(otherwise);
}
this.iterator = this.source[Symbol.iterator]();
value = this.iterator.fastNext(done);
if (done === value)
return (0, common_1.OptLazy)(otherwise);
return value;
};
return RepeatIterator;
}(FastIteratorBase));
exports.RepeatIterator = RepeatIterator;
var ArrayIterator = /** @class */ (function (_super) {
tslib_1.__extends(ArrayIterator, _super);
function ArrayIterator(array, startIndex, endIndex) {
var _this = _super.call(this) || this;
_this.array = array;
_this.startIndex = startIndex;
_this.endIndex = endIndex;
_this.i = startIndex;
return _this;
}
ArrayIterator.prototype.fastNext = function (otherwise) {
if (this.i > this.endIndex)
return (0, common_1.OptLazy)(otherwise);
return this.array[this.i++];
};
return ArrayIterator;
}(FastIteratorBase));
exports.ArrayIterator = ArrayIterator;
var ArrayReverseIterator = /** @class */ (function (_super) {
tslib_1.__extends(ArrayReverseIterator, _super);
function ArrayReverseIterator(array, startIndex, endIndex) {
var _this = _super.call(this) || this;
_this.array = array;
_this.startIndex = startIndex;
_this.i = endIndex;
return _this;
}
ArrayReverseIterator.prototype.fastNext = function (otherwise) {
if (this.i < this.startIndex)
return (0, common_1.OptLazy)(otherwise);
return this.array[this.i--];
};
return ArrayReverseIterator;
}(FastIteratorBase));
exports.ArrayReverseIterator = ArrayReverseIterator;
var AlwaysIterator = /** @class */ (function (_super) {
tslib_1.__extends(AlwaysIterator, _super);
function AlwaysIterator(value) {
var _this = _super.call(this) || this;
_this.value = value;
return _this;
}
AlwaysIterator.prototype.fastNext = function () {
return this.value;
};
return AlwaysIterator;
}(FastIteratorBase));
exports.AlwaysIterator = AlwaysIterator;
var MapApplyIterator = /** @class */ (function (_super) {
tslib_1.__extends(MapApplyIterator, _super);
function MapApplyIterator(source, f, args, streamSourceHelpers) {
var _this = _super.call(this) || this;
_this.f = f;
_this.args = args;
_this.iter = streamSourceHelpers.fromStreamSource(source)[Symbol.iterator]();
return _this;
}
MapApplyIterator.prototype.fastNext = function (otherwise) {
var done = Symbol();
var next = this.iter.fastNext(done);
var args = this.args;
if (done === next)
return (0, common_1.OptLazy)(otherwise);
return this.f.apply(this, tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(next), false), tslib_1.__read(args), false));
};
return MapApplyIterator;
}(FastIteratorBase));
exports.MapApplyIterator = MapApplyIterator;
var FilterApplyIterator = /** @class */ (function (_super) {
tslib_1.__extends(FilterApplyIterator, _super);
function FilterApplyIterator(source, pred, args, negate, streamSourceHelpers) {
var _this = _super.call(this) || this;
_this.pred = pred;
_this.args = args;
_this.negate = negate;
_this.iter = streamSourceHelpers.fromStreamSource(source)[Symbol.iterator]();
return _this;
}
FilterApplyIterator.prototype.fastNext = function (otherwise) {
var done = Symbol();
var next;
var pred = this.pred;
var iter = this.iter;
var args = this.args;
var negate = this.negate;
while (done !== (next = iter.fastNext(done))) {
if (pred.apply(void 0, tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(next), false), tslib_1.__read(args), false)) !== negate)
return next;
}
return (0, common_1.OptLazy)(otherwise);
};
return FilterApplyIterator;
}(FastIteratorBase));
exports.FilterApplyIterator = FilterApplyIterator;
var RangeUpIterator = /** @class */ (function (_super) {
tslib_1.__extends(RangeUpIterator, _super);
function RangeUpIterator(start, end, delta) {
if (start === void 0) { start = 0; }
var _this = _super.call(this) || this;
_this.start = start;
_this.end = end;
_this.delta = delta;
_this.state = start;
return _this;
}
RangeUpIterator.prototype.fastNext = function (otherwise) {
if (undefined !== this.end) {
if (this.state > this.end) {
return (0, common_1.OptLazy)(otherwise);
}
}
var currentState = this.state;
this.state += this.delta;
return currentState;
};
return RangeUpIterator;
}(FastIteratorBase));
exports.RangeUpIterator = RangeUpIterator;
var RangeDownIterator = /** @class */ (function (_super) {
tslib_1.__extends(RangeDownIterator, _super);
function RangeDownIterator(start, end, delta) {
if (start === void 0) { start = 0; }
var _this = _super.call(this) || this;
_this.start = start;
_this.end = end;
_this.delta = delta;
_this.state = start;
return _this;
}
RangeDownIterator.prototype.fastNext = function (otherwise) {
if (undefined !== this.end) {
if (this.state < this.end) {
return (0, common_1.OptLazy)(otherwise);
}
}
var currentState = this.state;
this.state += this.delta;
return currentState;
};
return RangeDownIterator;
}(FastIteratorBase));
exports.RangeDownIterator = RangeDownIterator;
var RandomIterator = /** @class */ (function (_super) {
tslib_1.__extends(RandomIterator, _super);
function RandomIterator() {
return _super !== null && _super.apply(this, arguments) || this;
}
RandomIterator.prototype.fastNext = function () {
return Math.random();
};
return RandomIterator;
}(FastIteratorBase));
exports.RandomIterator = RandomIterator;
var RandomIntIterator = /** @class */ (function (_super) {
tslib_1.__extends(RandomIntIterator, _super);
function RandomIntIterator(min, max) {
var _this = _super.call(this) || this;
_this.min = min;
_this.max = max;
_this.width = max - min;
return _this;
}
RandomIntIterator.prototype.fastNext = function () {
return this.min + Math.round(Math.random() * this.width);
};
return RandomIntIterator;
}(FastIteratorBase));
exports.RandomIntIterator = RandomIntIterator;
var UnfoldIterator = /** @class */ (function (_super) {
tslib_1.__extends(UnfoldIterator, _super);
function UnfoldIterator(init, getNext) {
var _this = _super.call(this) || this;
_this.getNext = getNext;
_this.index = 0;
_this.current = init;
return _this;
}
UnfoldIterator.prototype.fastNext = function (otherwise) {
var current = this.current;
if (base_1.Token === current)
return (0, common_1.OptLazy)(otherwise);
if (this.index === 0) {
this.index++;
return current;
}
var next = this.getNext(current, this.index++, base_1.Token);
this.current = next;
if (base_1.Token === next)
return (0, common_1.OptLazy)(otherwise);
return next;
};
return UnfoldIterator;
}(FastIteratorBase));
exports.UnfoldIterator = UnfoldIterator;
var ZipWithIterator = /** @class */ (function (_super) {
tslib_1.__extends(ZipWithIterator, _super);
function ZipWithIterator(iterables, zipFun, streamSourceHelpers) {
var _this = _super.call(this) || this;
_this.iterables = iterables;
_this.zipFun = zipFun;
_this.sources = iterables.map(function (source) {
return streamSourceHelpers.fromStreamSource(source)[Symbol.iterator]();
});
return _this;
}
ZipWithIterator.prototype.fastNext = function (otherwise) {
var result = [];
var sourceIndex = -1;
var sources = this.sources;
var done = Symbol('Done');
while (++sourceIndex < sources.length) {
var value = sources[sourceIndex].fastNext(done);
if (done === value)
return (0, common_1.OptLazy)(otherwise);
result.push(value);
}
return this.zipFun.apply(this, tslib_1.__spreadArray([], tslib_1.__read(result), false));
};
return ZipWithIterator;
}(FastIteratorBase));
exports.ZipWithIterator = ZipWithIterator;
var ZipAllWithItererator = /** @class */ (function (_super) {
tslib_1.__extends(ZipAllWithItererator, _super);
function ZipAllWithItererator(fillValue, iters, zipFun, streamSourceHelpers) {
var _this = _super.call(this) || this;
_this.fillValue = fillValue;
_this.iters = iters;
_this.zipFun = zipFun;
_this.allDone = false;
_this.sources = iters.map(function (o) {
return streamSourceHelpers.fromStreamSource(o)[Symbol.iterator]();
});
return _this;
}
ZipAllWithItererator.prototype.fastNext = function (otherwise) {
if (this.allDone)
return (0, common_1.OptLazy)(otherwise);
var result = [];
var sourceIndex = -1;
var sources = this.sources;
var done = Symbol('Done');
var anyNotDone = false;
var fillValue = this.fillValue;
while (++sourceIndex < sources.length) {
var value = sources[sourceIndex].fastNext(done);
if (done === value) {
result.push((0, common_1.OptLazy)(fillValue));
}
else {
anyNotDone = true;
result.push(value);
}
}
if (!anyNotDone) {
this.allDone = true;
return (0, common_1.OptLazy)(otherwise);
}
return this.zipFun.apply(this, tslib_1.__spreadArray([], tslib_1.__read(result), false));
};
return ZipAllWithItererator;
}(FastIteratorBase));
exports.ZipAllWithItererator = ZipAllWithItererator;
var PrependIterator = /** @class */ (function (_super) {
tslib_1.__extends(PrependIterator, _super);
function PrependIterator(source, item) {
var _this = _super.call(this) || this;
_this.source = source;
_this.item = item;
_this.prependDone = false;
return _this;
}
PrependIterator.prototype.fastNext = function (otherwise) {
if (this.prependDone) {
return this.source.fastNext(otherwise);
}
this.prependDone = true;
return (0, common_1.OptLazy)(this.item);
};
return PrependIterator;
}(FastIteratorBase));
exports.PrependIterator = PrependIterator;
var AppendIterator = /** @class */ (function (_super) {
tslib_1.__extends(AppendIterator, _super);
function AppendIterator(source, item) {
var _this = _super.call(this) || this;
_this.source = source;
_this.item = item;
_this.appendDone = false;
return _this;
}
AppendIterator.prototype.fastNext = function (otherwise) {
if (this.appendDone)
return (0, common_1.OptLazy)(otherwise);
var done = Symbol('Done');
var value = this.source.fastNext(done);
if (done !== value)
return value;
this.appendDone = true;
return (0, common_1.OptLazy)(this.item);
};
return AppendIterator;
}(FastIteratorBase));
exports.AppendIterator = AppendIterator;
var MapIterator = /** @class */ (function (_super) {
tslib_1.__extends(MapIterator, _super);
function MapIterator(source, mapFun) {
var _this = _super.call(this) || this;
_this.source = source;
_this.mapFun = mapFun;
_this.state = (0, common_1.TraverseState)();
return _this;
}
MapIterator.prototype.fastNext = function (otherwise) {
var state = this.state;
if (state.halted)
return (0, common_1.OptLazy)(otherwise);
var done = Symbol('Done');
var next = this.source.fastNext(done);
if (done === next)
return (0, common_1.OptLazy)(otherwise);
return this.mapFun(next, state.nextIndex());
};
return MapIterator;
}(FastIteratorBase));
exports.MapIterator = MapIterator;
var MapPureIterator = /** @class */ (function (_super) {
tslib_1.__extends(MapPureIterator, _super);
function MapPureIterator(source, mapFun, args) {
var _this = _super.call(this) || this;
_this.source = source;
_this.mapFun = mapFun;
_this.args = args;
return _this;
}
MapPureIterator.prototype.fastNext = function (otherwise) {
var done = Symbol('Done');
var next = this.source.fastNext(done);
if (done === next)
return (0, common_1.OptLazy)(otherwise);
return this.mapFun.apply(this, tslib_1.__spreadArray([next], tslib_1.__read(this.args), false));
};
return MapPureIterator;
}(FastIteratorBase));
exports.MapPureIterator = MapPureIterator;
//# sourceMappingURL=fast-iterator-custom.cjs.map