UNPKG

@thi.ng/vectors

Version:

Optimized 2d/3d/4d and arbitrary length vector operations, support for memory mapping/layouts

74 lines 1.97 kB
import type { ReadonlyVec } from "./api.js"; /** * Returns an unsigned 32-bit hash code for the given vector. * * @remarks * The hash is the reduction of `hash = H * hash + murmur(x)`, where `murmur(x)` * is the partial Murmur3 hash of a single vector component's bitwise * representation and `H` an optional hash factor, by default Knuth's 0x9e3779b1 * (see TAOCP, section 6.4, page 516). If the vector `v` is empty (length 0), * the function returns -1. * * Hashes for zero-vectors: * * - `[0]`: 1209856430 * - `[0, 0]`: 3623989185 * - `[0, 0, 0]`: 4192292821 * - `[0, 0, 0, 0]`: 2558592725 * * @example * Hash collisions: * * ```ts tangle:../export/hash.ts * import { hash } from "@thi.ng/vectors"; * import { * conj, map, normRange, permutations, range2d, transduce * } from "@thi.ng/transducers"; * * // integer grid coords * let uniq = transduce(map(hash), conj(), range2d(0x1000, 0x1000)).size * * console.log(uniq); * // 16744420 * * // collision rate * console.log((1 - uniq / (0x1000 ** 2)) * 100); * // 0.1955 % * * // normalized grid coords * uniq = transduce( * map(hash), * conj(), * permutations(normRange(1000), normRange(1000)) * ).size * * console.log(uniq); * // 1001895 * * // collision rate * console.log((1 - uniq / (1001 ** 2)) * 100); * // 0.0106 % * ``` * * - https://github.com/thi-ng/c-thing/blob/develop/src/math/hashfn.c * - [`thi.ng/morton`](https://thi.ng/morton) for Z-curve ordered hashing * * @param v - vector * @param hash - hash factor */ export declare const hash: (v: ReadonlyVec, H?: number) => number; /** * Computes unsigned int hash code for given 2D coordinates. * * @param x * @param y */ export declare const hash2: (x: number, y: number) => number; /** * Computes unsigned int hash code for given 3D coordinates. * * @param x * @param y */ export declare const hash3: (x: number, y: number, z: number) => number; //# sourceMappingURL=hash.d.ts.map