import { assert } from "../../assert.js";

/**
 * @template T
 * @param {T[]} array
 * @param {function(T):number} elementHashFunction
 * @param {*} [thisArg]
 * @returns {number}
 */
export function computeHashArray(array, elementHashFunction, thisArg) {
    assert.isArrayLike(array, 'array');
    assert.isFunction(elementHashFunction, 'elementHashFunction');

    const numArguments = array.length;
    let hash = numArguments;

    for (let i = 0; i < numArguments; i++) {

        const singleValue = array[i];

        const elementHash = elementHashFunction.call(thisArg, singleValue);

        hash = ((hash << 5) - hash) + elementHash;
        hash |= 0; // Convert to 32bit integer
    }

    return hash;
}
