194 lines
6.9 KiB
JavaScript
194 lines
6.9 KiB
JavaScript
"use strict";
|
|
var __extends = (this && this.__extends) || (function () {
|
|
var extendStatics = function (d, b) {
|
|
extendStatics = Object.setPrototypeOf ||
|
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
return extendStatics(d, b);
|
|
};
|
|
return function (d, b) {
|
|
if (typeof b !== "function" && b !== null)
|
|
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
extendStatics(d, b);
|
|
function __() { this.constructor = d; }
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
};
|
|
})();
|
|
var __read = (this && this.__read) || function (o, n) {
|
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
if (!m) return o;
|
|
var i = m.call(o), r, ar = [], e;
|
|
try {
|
|
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
}
|
|
catch (error) { e = { error: error }; }
|
|
finally {
|
|
try {
|
|
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
}
|
|
finally { if (e) throw e.error; }
|
|
}
|
|
return ar;
|
|
};
|
|
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
if (ar || !(i in from)) {
|
|
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
ar[i] = from[i];
|
|
}
|
|
}
|
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.UniqueMap = void 0;
|
|
//================================================================
|
|
/**
|
|
* @packageDocumentation
|
|
* @module std.base
|
|
*/
|
|
//================================================================
|
|
var MapContainer_1 = require("./MapContainer");
|
|
var ErrorGenerator_1 = require("../../internal/exception/ErrorGenerator");
|
|
/**
|
|
* Basic map container blocking duplicated key.
|
|
*
|
|
* @template Key Key type
|
|
* @template T Mapped type
|
|
* @template Source Derived type extending this {@link UniqueMap}
|
|
* @template IteratorT Iterator type
|
|
* @template ReverseT Reverse iterator type
|
|
*
|
|
* @author Jeongho Nam - https://github.com/samchon
|
|
*/
|
|
var UniqueMap = /** @class */ (function (_super) {
|
|
__extends(UniqueMap, _super);
|
|
function UniqueMap() {
|
|
return _super !== null && _super.apply(this, arguments) || this;
|
|
}
|
|
/* ---------------------------------------------------------
|
|
ACCESSORS
|
|
--------------------------------------------------------- */
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
UniqueMap.prototype.count = function (key) {
|
|
return this.find(key).equals(this.end()) ? 0 : 1;
|
|
};
|
|
/**
|
|
* Get a value.
|
|
*
|
|
* @param key Key to search for.
|
|
* @return The value mapped by the key.
|
|
*/
|
|
UniqueMap.prototype.get = function (key) {
|
|
var it = this.find(key);
|
|
if (it.equals(this.end()) === true)
|
|
throw ErrorGenerator_1.ErrorGenerator.key_nout_found(this, "get", key);
|
|
return it.second;
|
|
};
|
|
/**
|
|
* Take a value.
|
|
*
|
|
* Get a value, or set the value and returns it.
|
|
*
|
|
* @param key Key to search for.
|
|
* @param generator Value generator when the matched key not found
|
|
* @returns Value, anyway
|
|
*/
|
|
UniqueMap.prototype.take = function (key, generator) {
|
|
var it = this.find(key);
|
|
return it.equals(this.end())
|
|
? this.emplace(key, generator()).first.second
|
|
: it.second;
|
|
};
|
|
/**
|
|
* Set a value with key.
|
|
*
|
|
* @param key Key to be mapped or search for.
|
|
* @param val Value to insert or assign.
|
|
*/
|
|
UniqueMap.prototype.set = function (key, val) {
|
|
this.insert_or_assign(key, val);
|
|
};
|
|
UniqueMap.prototype.insert = function () {
|
|
var args = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
args[_i] = arguments[_i];
|
|
}
|
|
return _super.prototype.insert.apply(this, __spreadArray([], __read(args), false));
|
|
};
|
|
UniqueMap.prototype._Insert_by_range = function (first, last) {
|
|
for (var it = first; !it.equals(last); it = it.next())
|
|
this.emplace(it.value.first, it.value.second);
|
|
};
|
|
UniqueMap.prototype.insert_or_assign = function () {
|
|
var args = [];
|
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
args[_i] = arguments[_i];
|
|
}
|
|
if (args.length === 2) {
|
|
return this._Insert_or_assign_with_key_value(args[0], args[1]);
|
|
}
|
|
else if (args.length === 3) {
|
|
// INSERT OR ASSIGN AN ELEMENT
|
|
return this._Insert_or_assign_with_hint(args[0], args[1], args[2]);
|
|
}
|
|
};
|
|
UniqueMap.prototype._Insert_or_assign_with_key_value = function (key, value) {
|
|
var ret = this.emplace(key, value);
|
|
if (ret.second === false)
|
|
ret.first.second = value;
|
|
return ret;
|
|
};
|
|
UniqueMap.prototype._Insert_or_assign_with_hint = function (hint, key, value) {
|
|
var ret = this.emplace_hint(hint, key, value);
|
|
if (ret.second !== value)
|
|
ret.second = value;
|
|
return ret;
|
|
};
|
|
UniqueMap.prototype.extract = function (param) {
|
|
if (param instanceof this.end().constructor)
|
|
return this._Extract_by_iterator(param);
|
|
else
|
|
return this._Extract_by_key(param);
|
|
};
|
|
UniqueMap.prototype._Extract_by_key = function (key) {
|
|
var it = this.find(key);
|
|
if (it.equals(this.end()) === true)
|
|
throw ErrorGenerator_1.ErrorGenerator.key_nout_found(this, "extract", key);
|
|
var ret = it.value;
|
|
this._Erase_by_range(it);
|
|
return ret;
|
|
};
|
|
UniqueMap.prototype._Extract_by_iterator = function (it) {
|
|
if (it.equals(this.end()) === true)
|
|
return this.end();
|
|
this._Erase_by_range(it);
|
|
return it;
|
|
};
|
|
UniqueMap.prototype._Erase_by_key = function (key) {
|
|
var it = this.find(key);
|
|
if (it.equals(this.end()) === true)
|
|
return 0;
|
|
this._Erase_by_range(it);
|
|
return 1;
|
|
};
|
|
/* ---------------------------------------------------------
|
|
UTILITY
|
|
--------------------------------------------------------- */
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
UniqueMap.prototype.merge = function (source) {
|
|
for (var it = source.begin(); !it.equals(source.end());)
|
|
if (this.has(it.first) === false) {
|
|
this.insert(it.value);
|
|
it = source.erase(it);
|
|
}
|
|
else
|
|
it = it.next();
|
|
};
|
|
return UniqueMap;
|
|
}(MapContainer_1.MapContainer));
|
|
exports.UniqueMap = UniqueMap;
|
|
//# sourceMappingURL=UniqueMap.js.map
|