"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SortedSet = void 0;
/**
 * @deprecated This functionality was not meant to be exported and will be removed in a future release
 * @internal
 */
class SortedSet {
    constructor() {
        this.keySortedItems = [];
    }
    get size() {
        return this.keySortedItems.length;
    }
    get items() {
        return this.keySortedItems;
    }
    addOrUpdate(newItem, update) {
        const position = this.findItemPosition(newItem);
        if (position.exists) {
            update?.(this.keySortedItems[position.index], newItem);
        }
        else {
            this.keySortedItems.splice(position.index, 0, newItem);
        }
    }
    remove(item) {
        const position = this.findItemPosition(item);
        if (position.exists) {
            this.keySortedItems.splice(position.index, 1);
            return true;
        }
        return false;
    }
    has(item) {
        const position = this.findItemPosition(item);
        return position.exists;
    }
    findItemPosition(item) {
        if (this.keySortedItems.length === 0) {
            return { exists: false, index: 0 };
        }
        let start = 0;
        let end = this.keySortedItems.length - 1;
        const itemKey = this.getKey(item);
        let index = -1;
        while (start <= end) {
            index = start + Math.floor((end - start) / 2);
            const indexKey = this.getKey(this.keySortedItems[index]);
            if (indexKey > itemKey) {
                if (start === index) {
                    return { exists: false, index };
                }
                end = index - 1;
            }
            else if (indexKey < itemKey) {
                if (index === end) {
                    return { exists: false, index: index + 1 };
                }
                start = index + 1;
            }
            else if (indexKey === itemKey) {
                return { exists: true, index };
            }
        }
        return { exists: false, index };
    }
}
exports.SortedSet = SortedSet;
//# sourceMappingURL=sortedSet.cjs.map