* 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. * * @example Usage * ```ts * import { D2Array } from "@std/data-structures/unstable-2d-array"; * import { assertEquals } from "@std
(
x: number = 0,
y: number = 0,
width?: number,
height?: number,
)
| 166 | * @returns A new {@linkcode D2Array} containing the sliced values. |
| 167 | */ |
| 168 | slice( |
| 169 | x: number = 0, |
| 170 | y: number = 0, |
| 171 | width?: number, |
| 172 | height?: number, |
| 173 | ): D2Array<T> { |
| 174 | const actualHeight = Math.min(height ?? Infinity, this.raw.length - y); |
| 175 | const actualWidth = Math.min(width ?? Infinity, this.raw[0]!.length - x); |
| 176 | |
| 177 | assert((actualWidth as number) > 0, "Width must be greater than 0"); |
| 178 | assert((actualHeight as number) > 0, "Height must be greater than 0"); |
| 179 | |
| 180 | const out: T[][] = []; |
| 181 | |
| 182 | for (let i = y; i < y + actualHeight; i++) { |
| 183 | const row = this.raw[i]!; |
| 184 | out.push(row.slice(x, x + actualWidth)); |
| 185 | } |
| 186 | |
| 187 | return new D2Array(out, this.initialValue); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Resize the 2D array. |
no test coverage detected