/**
 * A simple map-like data structure that uses an array for storage.
 * This class is not memory efficient for sparse lookups, as it uses an array
 * internally and will allocate memory for all indices up to the highest key used.
 * It is however very fast for lookups.
 * @remarks It is not possible to store `undefined` values in this map, as `undefined`
 * is used to represent keys that have not been set. This class is only intended to be used
 * where the key is mostly a sequential number.
 */
export class IndexedMap {
    _items;
    /**
     * Constructs a new IndexedMap.
     * @param initialCapacity The initial capacity of the map. Defaults to `0`.
     */
    constructor(initialCapacity = 0) {
        this._items = new Array(initialCapacity);
    }
    /**
     * Sets the value for the given key.
     * @param key The key to set the value for.
     * @param value The value to set.
     * @returns The value that was set.
     */
    set(key, value) {
        // If the key is larger than the current length of the array, we need to fill the array with undefined values.
        // Use push to fill the array. Do not use spread operator as it won't work if we need to fill up a large number.
        // Use the for loop instead.
        if (key >= this._items.length) {
            for (let i = this._items.length; i <= key; i++) {
                this._items.push(undefined);
            }
        }
        this._items[key] = value;
        return value;
    }
    /**
     * Gets the value for the given key.
     * @param key The key to get the value for.
     * @returns The value for the key, or `undefined` if the key does not exist.
     */
    get(key) {
        return this._items[key];
    }
    /**
     * Checks if the map contains the given key.
     * @param key The key to check for.
     * @returns `true` if the key exists in the map, otherwise `false`.
     */
    has(key) {
        return key < this._items.length && this._items[key] !== undefined;
    }
    /**
     * Gets the current capacity of the map. This is not the number of items stored in the map,
     * but rather the size of the internal array.
     * @returns The current capacity of the map.
     */
    get capacity() {
        return this._items.length;
    }
    /**
     * Clears all items from the map.
     * This is a heavy operation as it has to zero out the memory used by.
     */
    clear() {
        // Zero out the array to free memory. Setting length to 0 is not enough because
        // then we have to grow the array again which is expensive too. Filling with undefined
        // allows us to keep the array size but free the memory used by the items.
        this._items.fill(undefined);
    }
}
//# sourceMappingURL=IndexedMap.js.map