@rimbu/proximity
Version:
Immutable ProximityMap implementation for TypeScript
119 lines • 4.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProximityMapBuilder = void 0;
var wrapping_cjs_1 = require("./wrapping.cjs");
/**
* Mutable builder used to efficiently construct new immutable {@link ProximityMap} instances.<br/>
* <br/>
* The builder stores entries in an internal `HashMap.Builder` and only wraps the result
* into a `ProximityMap` when {@link ProximityMapBuilder.build | build} (or
* {@link ProximityMapBuilder.buildMapValues | buildMapValues}) is called.
*
* @typeparam K - the key type
* @typeparam V - the value type
*/
var ProximityMapBuilder = /** @class */ (function () {
function ProximityMapBuilder(context, source) {
var _this = this;
this.context = context;
this.source = source;
/**
* Applying `get()` to the Builder does NOT apply the proximity algorithm - which would
* be pointless at this construction stage; the internal, hash-based builder
* is queried instead
*
*/
this.get = function (key, otherwise) {
if (undefined !== _this.source)
return _this.source.get(key, otherwise);
return _this.internalBuilder.get(key, otherwise);
};
// prettier-ignore
this.hasKey = function (key) {
return _this.internalBuilder.hasKey(key);
};
this.forEach = function (f, options) {
if (options === void 0) { options = {}; }
_this.internalBuilder.forEach(f, options);
};
this.addEntry = function (entry) {
var hasChanged = _this.internalBuilder.addEntry(entry);
if (hasChanged) {
_this.source = undefined;
}
return hasChanged;
};
this.addEntries = function (entries) {
var hasChanged = _this.internalBuilder.addEntries(entries);
if (hasChanged) {
_this.source = undefined;
}
return hasChanged;
};
this.set = function (key, value) {
var hasChanged = _this.internalBuilder.set(key, value);
if (hasChanged) {
_this.source = undefined;
}
return hasChanged;
};
this.removeKey = function (key, otherwise) {
if (_this.internalBuilder.hasKey(key)) {
_this.source = undefined;
}
return _this.internalBuilder.removeKey(key, otherwise);
};
// prettier-ignore
this.removeKeys = function (keys) {
var hasChanged = _this.internalBuilder.removeKeys(keys);
if (hasChanged) {
_this.source = undefined;
}
return hasChanged;
};
this.modifyAt = function (key, options) {
var hasChanged = _this.internalBuilder.modifyAt(key, options);
if (hasChanged) {
_this.source = undefined;
}
return hasChanged;
};
// prettier-ignore
this.updateAt = function (key, update, otherwise) {
var previousValue = _this.internalBuilder.updateAt(key, update, otherwise);
var newValue = _this.internalBuilder.get(key);
if (!Object.is(previousValue, newValue)) {
_this.source = undefined;
}
return previousValue;
};
this.build = function () {
return _this.source !== undefined
? _this.source
: (0, wrapping_cjs_1.wrapHashMap)(_this.context, _this.internalBuilder.build());
};
// prettier-ignore
this.buildMapValues = function (mapFun) {
return (0, wrapping_cjs_1.wrapHashMap)(_this.context, _this.internalBuilder.buildMapValues(mapFun));
};
this.internalBuilder = context.hashMapContext.builder();
this.internalBuilder.addEntries(source);
}
Object.defineProperty(ProximityMapBuilder.prototype, "size", {
get: function () {
return this.internalBuilder.size;
},
enumerable: false,
configurable: true
});
Object.defineProperty(ProximityMapBuilder.prototype, "isEmpty", {
get: function () {
return this.internalBuilder.isEmpty;
},
enumerable: false,
configurable: true
});
return ProximityMapBuilder;
}());
exports.ProximityMapBuilder = ProximityMapBuilder;
//# sourceMappingURL=builder.cjs.map