"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 __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MultiMapTree = void 0;
//================================================================
/**
 * @packageDocumentation
 * @module std.internal
 */
//================================================================
var MapTree_1 = require("./MapTree");
var uid_1 = require("../../functional/uid");
var MultiMapTree = /** @class */ (function (_super) {
    __extends(MultiMapTree, _super);
    /* ---------------------------------------------------------
        CONSTRUCTOR
    --------------------------------------------------------- */
    function MultiMapTree(source, comp) {
        return _super.call(this, source, comp, function (x, y) {
            var ret = comp(x.first, y.first);
            if (!ret && !comp(y.first, x.first))
                return (0, uid_1.get_uid)(x) < (0, uid_1.get_uid)(y);
            else
                return ret;
        }) || this;
    }
    MultiMapTree.prototype.insert = function (val) {
        // ISSUE UID BEFORE INSERTION
        (0, uid_1.get_uid)(val);
        _super.prototype.insert.call(this, val);
    };
    /* ---------------------------------------------------------
        FINDERS
    --------------------------------------------------------- */
    MultiMapTree.prototype._Nearest_by_key = function (key, equal_mover) {
        // NEED NOT TO ITERATE
        if (this.root_ === null)
            return null;
        //----
        // ITERATE
        //----
        var ret = this.root_;
        var matched = null;
        while (true) {
            var it = ret.value;
            var my_node = null;
            // COMPARE
            if (this.key_comp()(key, it.first))
                my_node = ret.left;
            else if (this.key_comp()(it.first, key))
                my_node = ret.right;
            else {
                // EQUAL, RESERVE THAT POINT
                matched = ret;
                my_node = equal_mover(ret);
            }
            // ULTIL CHILD NODE EXISTS
            if (my_node === null)
                break;
            else
                ret = my_node;
        }
        // RETURNS -> MATCHED OR NOT
        return matched !== null ? matched : ret;
    };
    MultiMapTree.prototype.nearest_by_key = function (key) {
        return this._Nearest_by_key(key, function (node) {
            return node.left;
        });
    };
    MultiMapTree.prototype.upper_bound = function (key) {
        // FIND MATCHED NODE
        var node = this._Nearest_by_key(key, function (node) {
            return node.right;
        });
        if (node === null)
            // NOTHING
            return this.source().end();
        // MUST BE it.first > key
        var it = node.value;
        if (this.key_comp()(key, it.first))
            return it;
        else
            return it.next();
    };
    return MultiMapTree;
}(MapTree_1.MapTree));
exports.MultiMapTree = MultiMapTree;
//# sourceMappingURL=MultiMapTree.js.map