import { uint16_array_hash } from "./uint16_array_hash.js";
import { uint32_array_hash } from "./uint32_array_hash.js";
import { uint8_array_hash } from "./uint8_array_hash.js";

/**
 *
 * @param {ArrayBuffer} buffer
 * @param {number} offset
 * @param {number} length
 * @return {number}
 */
export function array_buffer_hash(buffer, offset, length) {

    const alignment_mask = offset | length;

    if ((alignment_mask & 3) === 0) {
        // 4-byte alignment
        return uint32_array_hash(
            new Uint32Array(buffer, offset, length >>> 2),
            0,
            length >>> 2
        );
    } else if ((alignment_mask & 2) === 0) {
        // 2-byte alignment
        return uint16_array_hash(
            new Uint16Array(buffer, offset, length >>> 1),
            0,
            length >>> 1
        );
    } else {
        // 1-byte
        return uint8_array_hash(
            new Uint8Array(buffer, offset, length),
            0,
            length
        );
    }
}

