UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

43 lines (37 loc) 1.32 kB
import { assert } from "../../assert.js"; /** * Swap two sections between arrays, can be used to move elements in the same array * @template T * @param {T[]|ArrayLike<T>|TypedArray|Uint8ClampedArray|Uint8Array|Uint32Array|Float32Array} a * @param {number} a_offset * @param {T[]|ArrayLike<T>|TypedArray|Uint8ClampedArray|Uint8Array|Uint32Array|Float32Array} b * @param {number} b_offset * @param {number} length How many elements should be moved * @returns {void} * * @example * const array = [1, 2, 3, 4, 5]; * array_swap(array, 0, array, 2, 1); // array === [4, 2, 3, 1, 5] * @example * const array_a = ["hello", "world"]; * const array_b = ["foo", "bar"]; * array_swap(array_a, 1, array_b, 0, 1); // array_a === ["hello", "foo"], array_b === ["world", "bar"] * @example * const array = [1, 2, 3, 4, 5]; * array_swap(array, 0, array, 2, 2); // array === [3, 4, 1, 2, 5] */ export function array_swap( a, a_offset, b, b_offset, length ) { assert.isNonNegativeInteger(a_offset, 'a_offset'); assert.isNonNegativeInteger(b_offset, 'b_offset'); for (let k = 0; k < length; k++) { const i = a_offset + k; const j = b_offset + k; const swap = b[j]; b[j] = a[i]; a[i] = swap; } }