UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

37 lines (31 loc) 1.14 kB
import { assert } from "../../assert.js"; /** * Useful for computing hashes of large arrays, can pick a relevant stride and skip large chunks of memory while still capturing good amount of unique information from evenly-spaced areas of the array * @template T * @param {T[]|Uint32Array|Uint16Array|Uint8Array} array * @param {number} offset * @param {number} length * @param {number} stride * @param {function(T):number} elementHash * @param {*} [elementHashContext] * @return {number} */ export function computeStridedArrayHash( array, offset, length, stride, elementHash, elementHashContext ) { assert.isFunction(elementHash, 'elementHash'); let hash = length; for (let i = offset; i < length; i += stride) { const value = elementHash.call(elementHashContext, array[i]); /** * Simple hashing scheme, multiplying existing hash by a prime and adding next value * (h<<5) - h === h*31 * @type {number} * @ignore */ hash = ((hash << 5) - hash) + value; } // force uint32 return hash >>> 0; }