MCPcopy Create free account
hub / github.com/denoland/std / constructor

Method constructor

data_structures/unstable_2d_array.ts:114–143  ·  view source on GitHub ↗

implementation * TODO(kt3k): Remove this jsdoc when the issue below is resolved. * https://github.com/denoland/deno/issues/30037 * * @param widthOrValue The width of the 2d array, or the array to use * @param heightOrInitialValue The height of the 2d array, or the initial value to use

(
    widthOrValue: number | (readonly T[][]),
    heightOrInitialValue: number | T,
    initialValue?: T,
  )

Source from the content-addressed store, hash-verified

112 * @param initialValue The initial value to use when resizing the
113 */
114 constructor(
115 widthOrValue: number | (readonly T[][]),
116 heightOrInitialValue: number | T,
117 initialValue?: T,
118 ) {
119 if (Array.isArray(widthOrValue)) {
120 assert((widthOrValue.length) > 0, "Height must be greater than 0");
121 assert((widthOrValue[0].length) > 0, "Width must be greater than 0");
122
123 this.raw = [];
124
125 for (let i = 0; i < widthOrValue.length; i++) {
126 this.raw.push(widthOrValue[i]!.slice());
127 }
128
129 this.initialValue = heightOrInitialValue as T;
130 } else {
131 assert((widthOrValue as number) > 0, "Width must be greater than 0");
132 assert(
133 (heightOrInitialValue as number) > 0,
134 "Height must be greater than 0",
135 );
136
137 this.raw = Array.from(
138 { length: heightOrInitialValue as number },
139 () => Array(widthOrValue as number).fill(initialValue as number),
140 );
141 this.initialValue = initialValue!;
142 }
143 }
144
145 /**
146 * Slice the 2D array. Like slice on a normal array, this will create a new D2Array.

Callers

nothing calls this directly

Calls 4

assertFunction · 0.90
pushMethod · 0.45
sliceMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected