@rimbu/bimap
Version:
A bidirectional immutable Map of keys and values for TypeScript
318 lines • 12.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BiMapNonEmptyImpl = exports.BiMapEmpty = void 0;
var tslib_1 = require("tslib");
var map_custom_1 = require("@rimbu/collection-types/map-custom");
var common_1 = require("@rimbu/common");
var stream_1 = require("@rimbu/stream");
var custom_1 = require("@rimbu/stream/custom");
var BiMapEmpty = /** @class */ (function (_super) {
tslib_1.__extends(BiMapEmpty, _super);
function BiMapEmpty(context) {
var _this = _super.call(this) || this;
_this.context = context;
return _this;
}
Object.defineProperty(BiMapEmpty.prototype, "size", {
get: function () {
return 0;
},
enumerable: false,
configurable: true
});
Object.defineProperty(BiMapEmpty.prototype, "keyValueMap", {
get: function () {
return this.context.keyValueContext.empty();
},
enumerable: false,
configurable: true
});
Object.defineProperty(BiMapEmpty.prototype, "valueKeyMap", {
get: function () {
return this.context.valueKeyContext.empty();
},
enumerable: false,
configurable: true
});
BiMapEmpty.prototype.hasKey = function () {
return false;
};
BiMapEmpty.prototype.hasValue = function () {
return false;
};
BiMapEmpty.prototype.getValue = function (key, otherwise) {
return (0, common_1.OptLazy)(otherwise);
};
BiMapEmpty.prototype.getKey = function (value, otherwise) {
return (0, common_1.OptLazy)(otherwise);
};
BiMapEmpty.prototype.set = function (key, value) {
return this.addEntry([key, value]);
};
BiMapEmpty.prototype.addEntry = function (entry) {
return new BiMapNonEmptyImpl(this.context, this.context.keyValueContext.of(entry), this.context.valueKeyContext.of([entry[1], entry[0]]));
};
BiMapEmpty.prototype.addEntries = function (entries) {
return this.context.from(entries);
};
BiMapEmpty.prototype.removeKey = function () {
return this;
};
BiMapEmpty.prototype.removeKeyAndGet = function () {
return undefined;
};
BiMapEmpty.prototype.removeKeys = function () {
return this;
};
BiMapEmpty.prototype.removeValue = function () {
return this;
};
BiMapEmpty.prototype.removeValueAndGet = function () {
return undefined;
};
BiMapEmpty.prototype.removeValues = function () {
return this;
};
BiMapEmpty.prototype.updateKeyAtValue = function () {
return this;
};
BiMapEmpty.prototype.updateValueAtKey = function () {
return this;
};
BiMapEmpty.prototype.streamKeys = function () {
return stream_1.Stream.empty();
};
BiMapEmpty.prototype.streamValues = function () {
return stream_1.Stream.empty();
};
BiMapEmpty.prototype.toBuilder = function () {
return this.context.builder();
};
BiMapEmpty.prototype.toString = function () {
return "BiMap()";
};
BiMapEmpty.prototype.toJSON = function () {
return {
dataType: this.context.typeTag,
value: [],
};
};
return BiMapEmpty;
}(map_custom_1.EmptyBase));
exports.BiMapEmpty = BiMapEmpty;
var BiMapNonEmptyImpl = /** @class */ (function (_super) {
tslib_1.__extends(BiMapNonEmptyImpl, _super);
function BiMapNonEmptyImpl(context, keyValueMap, valueKeyMap) {
var _this = _super.call(this) || this;
_this.context = context;
_this.keyValueMap = keyValueMap;
_this.valueKeyMap = valueKeyMap;
return _this;
}
BiMapNonEmptyImpl.prototype.copy = function (keyValueMap, valueKeyMap) {
if (keyValueMap === void 0) { keyValueMap = this.keyValueMap; }
if (valueKeyMap === void 0) { valueKeyMap = this.valueKeyMap; }
if (keyValueMap === this.keyValueMap && valueKeyMap === this.valueKeyMap)
return this;
return new BiMapNonEmptyImpl(this.context, keyValueMap, valueKeyMap);
};
BiMapNonEmptyImpl.prototype.copyE = function (keyValueMap, valueKeyMap) {
if (keyValueMap === void 0) { keyValueMap = this.keyValueMap; }
if (valueKeyMap === void 0) { valueKeyMap = this.valueKeyMap; }
if (keyValueMap.nonEmpty() && valueKeyMap.nonEmpty()) {
return new BiMapNonEmptyImpl(this.context, keyValueMap, valueKeyMap);
}
return this.context.empty();
};
Object.defineProperty(BiMapNonEmptyImpl.prototype, "size", {
get: function () {
return this.keyValueMap.size;
},
enumerable: false,
configurable: true
});
BiMapNonEmptyImpl.prototype.asNormal = function () {
return this;
};
BiMapNonEmptyImpl.prototype.stream = function () {
return this.keyValueMap.stream();
};
BiMapNonEmptyImpl.prototype.streamKeys = function () {
return this.keyValueMap.streamKeys();
};
BiMapNonEmptyImpl.prototype.streamValues = function () {
return this.valueKeyMap.streamKeys();
};
BiMapNonEmptyImpl.prototype.hasKey = function (key) {
var token = Symbol();
return token !== this.getValue(key, token);
};
BiMapNonEmptyImpl.prototype.hasValue = function (value) {
var token = Symbol();
return token !== this.getKey(value, token);
};
BiMapNonEmptyImpl.prototype.getValue = function (key, otherwise) {
return this.keyValueMap.get(key, otherwise);
};
BiMapNonEmptyImpl.prototype.getKey = function (value, otherwise) {
return this.valueKeyMap.get(value, otherwise);
};
BiMapNonEmptyImpl.prototype.set = function (key, value) {
return this.addEntry([key, value]);
};
BiMapNonEmptyImpl.prototype.addEntry = function (entry) {
var _a = tslib_1.__read(entry, 2), key = _a[0], value = _a[1];
var removeKeyResult = this.keyValueMap.removeKeyAndGet(key);
var removeValueResult = this.valueKeyMap.removeKeyAndGet(value);
if (undefined === removeKeyResult && undefined === removeValueResult) {
// key and value were not present
return this.copy(this.keyValueMap.addEntry(entry), this.valueKeyMap.set(value, key));
}
if (undefined !== removeKeyResult && undefined !== removeValueResult) {
var _b = tslib_1.__read(removeKeyResult, 2), removedKeyValueMap = _b[0], oldValue = _b[1];
var _c = tslib_1.__read(removeValueResult, 2), removedValueKeyMap = _c[0], oldKey = _c[1];
// check if existing entry was not same
if (Object.is(oldKey, key) && Object.is(oldValue, value))
return this;
var newKeyValueMap_1 = removedKeyValueMap
.removeKey(oldKey)
.addEntry(entry);
var newValueKeyMap_1 = removedValueKeyMap
.removeKey(oldValue)
.set(value, key);
return this.copy(newKeyValueMap_1, newValueKeyMap_1);
}
var newKeyValueMap = (undefined === removeValueResult
? this.keyValueMap
: this.keyValueMap.removeKey(removeValueResult[1])).addEntry(entry);
var newValueKeyMap = (undefined === removeKeyResult
? this.valueKeyMap
: this.valueKeyMap.removeKey(removeKeyResult[1])).set(value, key);
return this.copy(newKeyValueMap, newValueKeyMap);
};
BiMapNonEmptyImpl.prototype.addEntries = function (entries) {
if ((0, custom_1.isEmptyStreamSourceInstance)(entries))
return this;
var builder = this.toBuilder();
builder.addEntries(entries);
return builder.build();
};
BiMapNonEmptyImpl.prototype.removeKey = function (key) {
var removeKeyResult = this.keyValueMap.removeKeyAndGet(key);
if (undefined === removeKeyResult)
return this;
var _a = tslib_1.__read(removeKeyResult, 2), newKeyValueMap = _a[0], oldValue = _a[1];
if (this.size === 1)
return this.context.empty();
var newValueKeyMap = this.valueKeyMap.removeKey(oldValue);
return this.copy(newKeyValueMap.assumeNonEmpty(), newValueKeyMap.assumeNonEmpty());
};
BiMapNonEmptyImpl.prototype.removeKeyAndGet = function (key) {
var removeKeyResult = this.keyValueMap.removeKeyAndGet(key);
if (undefined === removeKeyResult)
return undefined;
var _a = tslib_1.__read(removeKeyResult, 2), newKeyValueMap = _a[0], oldValue = _a[1];
if (this.size === 1)
return [this.context.empty(), oldValue];
var newValueKeyMap = this.valueKeyMap.removeKey(oldValue);
return [
this.copy(newKeyValueMap.assumeNonEmpty(), newValueKeyMap.assumeNonEmpty()),
oldValue,
];
};
BiMapNonEmptyImpl.prototype.removeKeys = function (keys) {
if ((0, custom_1.isEmptyStreamSourceInstance)(keys))
return this;
var builder = this.toBuilder();
builder.removeKeys(keys);
return builder.build();
};
BiMapNonEmptyImpl.prototype.removeValue = function (value) {
var removeResult = this.valueKeyMap.removeKeyAndGet(value);
if (undefined === removeResult)
return this;
var _a = tslib_1.__read(removeResult, 2), newValueKeyMap = _a[0], oldKey = _a[1];
if (this.size === 1)
return this.context.empty();
var newKeyValueMap = this.keyValueMap.removeKey(oldKey);
return this.copy(newKeyValueMap.assumeNonEmpty(), newValueKeyMap.assumeNonEmpty());
};
BiMapNonEmptyImpl.prototype.removeValueAndGet = function (value) {
var removeValueResult = this.valueKeyMap.removeKeyAndGet(value);
if (undefined === removeValueResult)
return undefined;
var _a = tslib_1.__read(removeValueResult, 2), newValueKeyMap = _a[0], oldKey = _a[1];
if (this.size === 1)
return [this.context.empty(), oldKey];
var newKeyValueMap = this.keyValueMap.removeKey(oldKey);
return [
this.copy(newKeyValueMap.assumeNonEmpty(), newValueKeyMap.assumeNonEmpty()),
oldKey,
];
};
BiMapNonEmptyImpl.prototype.removeValues = function (values) {
if ((0, custom_1.isEmptyStreamSourceInstance)(values))
return this;
var builder = this.toBuilder();
builder.removeValues(values);
return builder.build();
};
BiMapNonEmptyImpl.prototype.updateValueAtKey = function (key, valueUpdate) {
var token = Symbol();
var currentValue = this.getValue(key, token);
if (token === currentValue)
return this;
var newValue = (0, common_1.Update)(currentValue, valueUpdate);
if (Object.is(newValue, currentValue))
return this;
return this.set(key, newValue);
};
BiMapNonEmptyImpl.prototype.updateKeyAtValue = function (keyUpdate, value) {
var token = Symbol();
var result = this.getKey(value, token);
if (token === result)
return this;
var newKey = (0, common_1.Update)(result, keyUpdate);
if (Object.is(newKey, result))
return this;
return this.set(newKey, value);
};
BiMapNonEmptyImpl.prototype.forEach = function (f, options) {
if (options === void 0) { options = {}; }
var _a = options.state, state = _a === void 0 ? (0, common_1.TraverseState)() : _a;
if (state.halted)
return;
this.keyValueMap.forEach(f, { state: state });
};
BiMapNonEmptyImpl.prototype.filter = function (pred, options) {
if (options === void 0) { options = {}; }
var builder = this.context.builder();
builder.addEntries(this.stream().filter(pred, options));
if (builder.size === this.size)
return this;
return builder.build();
};
BiMapNonEmptyImpl.prototype.toBuilder = function () {
return this.context.createBuilder(this);
};
BiMapNonEmptyImpl.prototype.toArray = function () {
return this.keyValueMap.toArray();
};
BiMapNonEmptyImpl.prototype.toString = function () {
return this.stream().join({
start: "BiMap(",
sep: ', ',
end: ")",
valueToString: function (entry) { return "".concat(entry[0], " <-> ").concat(entry[1]); },
});
};
BiMapNonEmptyImpl.prototype.toJSON = function () {
return {
dataType: this.context.typeTag,
value: this.toArray(),
};
};
return BiMapNonEmptyImpl;
}(map_custom_1.NonEmptyBase));
exports.BiMapNonEmptyImpl = BiMapNonEmptyImpl;
//# sourceMappingURL=immutable.cjs.map