D2Array
import { D2Array } from "jsr:@ende/data-structures/unstable-2d-array";class ยท experimental
class D2Array<T> implements Iterable<T[]>A 2d array. Unlike a normal array, it does not grow dynamically,
and needs to be manually resized with the resize method.
It can never have the width or height be 0.
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>(3, 3, false);
arr.raw[0]![0] = true; // set the top left to true
const slice = arr.slice(0, 0, 2, 2);
assertEquals(slice.width, 2);
assertEquals(slice.height, 2);
assertEquals(arr.raw[0]![0], true);
arr.insert(0, 0, new D2Array<boolean>(2, 2, true)); // set all values from 0,0 to 1,1 to true
for (const row of arr) {
console.log(row);
}
Constructors
constructor
new D2Array(width: number, height: number, initialValue: T)Create a new blank 2d array with the provided value as init value
width: numberThe width of the 2d array
height: numberThe height of the 2d array
initialValue: TThe value to use to initialize the 2d array, also used when resizing
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>(3, 3, false);
assertEquals(arr.raw, [
[false, false, false],
[false, false, false],
[false, false, false],
]);
new D2Array(value: readonly T[][], initialValue: T)Create a new 2d array from an existing array
value: readonly T[][]The array to use
initialValue: TThe value to use when resizing
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>([
[false, false, false],
[false, false, false],
[false, false, false],
], false);
assertEquals(arr.raw, [
[false, false, false],
[false, false, false],
[false, false, false],
]);
new D2Array(widthOrValue: number | (readonly T[][]), heightOrInitialValue: number | T, initialValue?: T)implementation TODO(kt3k): Remove this jsdoc when the issue below is resolved. https://github.com/denoland/deno/issues/30037
widthOrValue: number | (readonly T[][])The width of the 2d array, or the array to use
heightOrInitialValue: number | TThe height of the 2d array, or the initial value to use
initialValue?: TThe initial value to use when resizing the
Properties
initialValue
initialValue: TThe initial value used when initializing or resizing the array
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>(3, 3, false);
assertEquals(arr.initialValue, false);
raw
raw: T[][]The raw underlying value
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>(3, 3, false);
assertEquals(arr.raw, [
[false, false, false],
[false, false, false],
[false, false, false],
]);
Methods
slice
slice(x: number = 0, y: number = 0, width?: number, height?: number): D2Array<T>Slice the 2D array. Like slice on a normal array, this will create a new D2Array. If no arguments are specified, it will slice the entire D2Array.
x: number = 00-based index at which to start on the X axis (0 is left-most)
y: number = 00-based index at which to start on the Y axis (0 is top-most)
width?: numberthe amount of elements to take on the X axis
height?: numberthe amount of elements to take on the X axis
Returns
A new D2Array containing the sliced values.
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>(3, 3, false);
arr.raw[0]![0] = true; // set the top left to true
const slice = arr.slice(0, 0, 2, 2);
assertEquals(slice.width, 2);
assertEquals(slice.height, 2);
assertEquals(arr.raw[0]![0], true);
resize
resize(width: number, height: number): voidResize the 2D array. The origin for the resize is the top left corner, or rather the first element.
width: numberthe new width of the 2d array. If smaller than current width, the 2d array will lose items. If larger than current width, the 2d array will add instances of
initialValueto fill up.height: numberthe new width of the 2d array. If smaller than current height, the 2d array will lose items. If larger than current height, the 2d array will add instances of
initialValueto fill up.
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>(3, 3, false);
arr.raw[0]![0] = true; // set the top left to true
arr.resize(2, 2);
assertEquals(arr.width, 2);
assertEquals(arr.height, 2);
assertEquals(arr.raw[0]![0], true);
insert
insert(x: number, y: number, value: readonly T[][] | D2Array<T>): voidInsert another 2d array at specific coordinates. If the inserted 2d array is greater than the 2d array this method is called on, the inserted value will be trimmed to fit the current 2d array.
x: numberThe X coordinate to place the 2d array, 0-based.
y: numberThe Y coordinate to place the 2d array, 0-based.
value: readonly T[][] | D2Array<T>The 2d array to insert at the provided coordinates.
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>(5, 5, false);
arr.insert(2, 2, new D2Array<boolean>(3, 3, true));
assertEquals(arr.raw, [
[false, false, false, false, false],
[false, false, false, false, false],
[false, false, true, true, true],
[false, false, true, true, true],
[false, false, true, true, true],
]);
width
get width(): numberThe width of the 2d array.
Returns
The width of the 2d array.
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>(3, 4, false);
assertEquals(arr.width, 3);
height
get height(): numberThe height of the 2d array.
Returns
The height of the 2d array.
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>(3, 4, false);
assertEquals(arr.height, 4);
[Symbol.iterator]
[Symbol.iterator](): Iterator<T[]>Iterate over the underlying raw array
Returns
An iterator over the rows of the 2d array.
Example: Usage
import { D2Array } from "@ende/data-structures/unstable-2d-array";
import { assertEquals } from "@ende/assert";
const arr = new D2Array<boolean>(3, 3, false);
for (const row of arr) {
assertEquals(row.length, 3);
}