UNPKG

@rimbu/sorted

Version:

Immutable SortedMap and SortedSet implementations for TypeScript

166 lines 6.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SortedSetBuilder = void 0; var tslib_1 = require("tslib"); var stream_1 = require("@rimbu/stream"); var common_1 = require("@rimbu/sorted/common"); var SortedSetBuilder = /** @class */ (function (_super) { tslib_1.__extends(SortedSetBuilder, _super); function SortedSetBuilder(context, source, _entries, _children, size) { if (size === void 0) { size = (_a = source === null || source === void 0 ? void 0 : source.size) !== null && _a !== void 0 ? _a : 0; } var _a; var _this = _super.call(this) || this; _this.context = context; _this.source = source; _this._entries = _entries; _this._children = _children; _this.size = size; // prettier-ignore _this.has = function (value) { if (!_this.context.comp.isComparable(value)) return false; if (undefined !== _this.source) return _this.source.has(value); var index = _this.context.findIndex(value, _this.entries); if (index >= 0) return true; if (!_this.hasChildren) return false; var childIndex = common_1.SortedIndex.next(index); var child = _this.children[childIndex]; return child.has(value); }; _this.add = function (value) { _this.checkLock(); var result = _this.addInternal(value); _this.normalize(); return result; }; _this.addAll = function (source) { _this.checkLock(); return stream_1.Stream.from(source).filterPure({ pred: _this.add }).count() > 0; }; // prettier-ignore _this.remove = function (value) { _this.checkLock(); if (!_this.context.comp.isComparable(value)) return false; var result = _this.removeInternal(value); _this.normalize(); return result; }; // prettier-ignore _this.removeAll = function (values) { _this.checkLock(); return stream_1.Stream.from(values).filterPure({ pred: _this.remove }).count() > 0; }; _this.build = function () { if (undefined !== _this.source) return _this.source; if (_this.size === 0) return _this.context.empty(); if (!_this.hasChildren) { return _this.context.leaf(_this.entries.slice()); } return _this.context.inner(_this.entries.slice(), _this.children.map(function (child) { return child.build(); }), _this.size); }; return _this; } SortedSetBuilder.prototype.createNew = function (source, entries, children, size) { return new SortedSetBuilder(this.context, source, entries, children, size); }; SortedSetBuilder.prototype.prepareMutate = function () { var _this = this; if (undefined === this._entries) { if (undefined !== this.source) { if (this.context.isSortedSetEmpty(this.source)) { this._entries = []; this._children = []; } else if (this.context.isSortedSetLeaf(this.source)) { this._entries = this.source.entries.slice(); } else if (this.context.isSortedSetInner(this.source)) { this._entries = this.source.entries.slice(); this._children = this.source.children.map(function (child) { return _this.createNew(child); }); } } if (undefined === this._entries) this._entries = []; } }; Object.defineProperty(SortedSetBuilder.prototype, "children", { get: function () { this.prepareMutate(); return this._children; }, set: function (value) { this.prepareMutate(); this.source = undefined; this._children = value; }, enumerable: false, configurable: true }); SortedSetBuilder.prototype.addInternal = function (value) { var entryIndex = this.context.findIndex(value, this.entries); if (entryIndex >= 0) { return false; } var childIndex = common_1.SortedIndex.next(entryIndex); if (!this.hasChildren) { this.source = undefined; this.size++; this.entries.splice(childIndex, 0, value); return true; } var child = this.children[childIndex]; var preSize = child.size; var changed = child.addInternal(value); if (!changed) return false; this.source = undefined; this.size += child.size - preSize; this.normalizeChildDecrease(childIndex); return changed; }; SortedSetBuilder.prototype.removeInternal = function (value) { if (this.size === 0) return false; var entryIndex = this.context.findIndex(value, this.entries); if (entryIndex >= 0) { this.source = undefined; this.size--; if (!this.hasChildren) { this.entries.splice(entryIndex, 1); return true; } var leftChild = this.children[entryIndex]; var rightChild = this.children[entryIndex + 1]; if (leftChild.size >= rightChild.size) { this.entries[entryIndex] = leftChild.deleteMax(); this.normalizeChildIncrease(entryIndex); } else { this.entries[entryIndex] = rightChild.deleteMin(); this.normalizeChildIncrease(entryIndex + 1); } return true; } if (!this.hasChildren) return false; var childIndex = common_1.SortedIndex.next(entryIndex); var child = this.children[childIndex]; var preSize = child.size; var changed = child.removeInternal(value); if (!changed) return false; this.source = undefined; this.size += child.size - preSize; this.normalizeChildIncrease(childIndex); return true; }; return SortedSetBuilder; }(common_1.SortedBuilder)); exports.SortedSetBuilder = SortedSetBuilder; //# sourceMappingURL=builder.cjs.map