@rimbu/bimap
Version:
A bidirectional immutable Map of keys and values for TypeScript
199 lines • 8.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BiMapBuilder = void 0;
var tslib_1 = require("tslib");
var base_1 = require("@rimbu/base");
var common_1 = require("@rimbu/common");
var stream_1 = require("@rimbu/stream");
var custom_1 = require("@rimbu/stream/custom");
var BiMapBuilder = /** @class */ (function () {
function BiMapBuilder(context, source) {
var _this = this;
this.context = context;
this.source = source;
this._lock = 0;
this.getValue = function (key, otherwise) {
if (undefined !== _this.source)
return _this.source.getValue(key, otherwise);
return _this.keyValueMap.get(key, otherwise);
};
// prettier-ignore
this.hasKey = function (key) {
var token = Symbol();
return token !== _this.getValue(key, token);
};
this.getKey = function (value, otherwise) {
if (undefined !== _this.source)
return _this.source.getKey(value, otherwise);
return _this.valueKeyMap.get(value, otherwise);
};
// prettier-ignore
this.hasValue = function (value) {
var token = Symbol();
return token !== _this.getKey(value, token);
};
this.set = function (key, value) {
return _this.addEntry([key, value]);
};
this.addEntry = function (entry) {
_this.checkLock();
var _a = tslib_1.__read(entry, 2), key = _a[0], value = _a[1];
var token = Symbol();
var oldValue = _this.keyValueMap.get(key, token);
var oldKey = _this.valueKeyMap.get(value, token);
if (token !== oldKey && token !== oldValue) {
if (Object.is(oldKey, key) && Object.is(oldValue, value))
return false;
_this.keyValueMap.removeKey(oldKey);
_this.keyValueMap.addEntry(entry);
_this.valueKeyMap.removeKey(oldValue);
_this.valueKeyMap.set(value, key);
}
else if (token !== oldValue) {
if (!Object.is(oldValue, value)) {
_this.valueKeyMap.removeKey(oldValue);
_this.valueKeyMap.set(value, key);
}
_this.keyValueMap.addEntry(entry);
}
else if (token !== oldKey) {
if (!Object.is(oldKey, key)) {
_this.keyValueMap.removeKey(oldKey);
_this.keyValueMap.addEntry(entry);
}
_this.valueKeyMap.set(value, key);
}
else {
_this.keyValueMap.addEntry(entry);
_this.valueKeyMap.set(value, key);
}
_this.source = undefined;
return true;
};
this.addEntries = function (source) {
_this.checkLock();
return stream_1.Stream.from(source).filterPure({ pred: _this.addEntry }).count() > 0;
};
this.removeKey = function (key, otherwise) {
_this.checkLock();
if (!_this.context.keyValueContext.isValidKey(key)) {
return (0, common_1.OptLazy)(otherwise);
}
var token = Symbol();
var value = _this.keyValueMap.removeKey(key, token);
if (token === value)
return (0, common_1.OptLazy)(otherwise);
_this.valueKeyMap.removeKey(value);
_this.source = undefined;
return value;
};
// prettier-ignore
this.removeKeys = function (keys) {
_this.checkLock();
if ((0, custom_1.isEmptyStreamSourceInstance)(keys))
return false;
var notFound = Symbol();
return (stream_1.Stream.from(keys)
.mapPure(_this.removeKey, notFound)
.countElement(notFound, { negate: true }) > 0);
};
this.removeValue = function (value, otherwise) {
_this.checkLock();
if (!_this.context.valueKeyContext.isValidKey(value)) {
return (0, common_1.OptLazy)(otherwise);
}
var token = Symbol();
var key = _this.valueKeyMap.removeKey(value, token);
if (token === key)
return (0, common_1.OptLazy)(otherwise);
_this.keyValueMap.removeKey(key);
_this.source = undefined;
return key;
};
// prettier-ignore
this.removeValues = function (values) {
_this.checkLock();
if ((0, custom_1.isEmptyStreamSourceInstance)(values))
return false;
var notFound = Symbol();
return (stream_1.Stream.from(values)
.mapPure(_this.removeValue, notFound)
.countElement(notFound, { negate: true }) > 0);
};
this.forEach = function (f, options) {
if (options === void 0) { options = {}; }
var _a = options.state, state = _a === void 0 ? (0, common_1.TraverseState)() : _a;
_this._lock++;
if (!_this.isEmpty && !state.halted) {
if (undefined !== _this.source)
_this.source.forEach(f, { state: state });
else
_this.keyValueMap.forEach(f, { state: state });
}
_this._lock--;
};
this.build = function () {
if (undefined !== _this.source)
return _this.source;
if (_this.size === 0)
return _this.context.empty();
return _this.context.createNonEmptyImpl(_this.keyValueMap.build().assumeNonEmpty(), _this.valueKeyMap.build().assumeNonEmpty());
};
}
BiMapBuilder.prototype.checkLock = function () {
if (this._lock)
base_1.RimbuError.throwModifiedBuilderWhileLoopingOverItError();
};
Object.defineProperty(BiMapBuilder.prototype, "keyValueMap", {
get: function () {
if (undefined === this._keyValueMap) {
if (undefined === this.source) {
this._keyValueMap = this.context.keyValueContext.builder();
this._valueKeyMap = this.context.valueKeyContext.builder();
}
else {
this._keyValueMap = this.source.keyValueMap.toBuilder();
this._valueKeyMap = this.source.valueKeyMap.toBuilder();
}
}
return this._keyValueMap;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BiMapBuilder.prototype, "valueKeyMap", {
get: function () {
if (undefined === this._valueKeyMap) {
if (undefined === this.source) {
this._keyValueMap = this.context.keyValueContext.builder();
this._valueKeyMap = this.context.valueKeyContext.builder();
}
else {
this._keyValueMap = this.source.keyValueMap.toBuilder();
this._valueKeyMap = this.source.valueKeyMap.toBuilder();
}
}
return this._valueKeyMap;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BiMapBuilder.prototype, "size", {
get: function () {
var _a, _b;
return (_b = (_a = this.source) === null || _a === void 0 ? void 0 : _a.size) !== null && _b !== void 0 ? _b : this.keyValueMap.size;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BiMapBuilder.prototype, "isEmpty", {
get: function () {
return this.size === 0;
},
enumerable: false,
configurable: true
});
return BiMapBuilder;
}());
exports.BiMapBuilder = BiMapBuilder;
//# sourceMappingURL=builder.cjs.map