Skip to content

Commit aec8eb3

Browse files
authored
[NFC] Refactor fill methos of all arrays (#2406)
1 parent 2a20187 commit aec8eb3

20 files changed

+968
-1128
lines changed

std/assembly/array.ts

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { BLOCK_MAXSIZE } from "./rt/common";
44
import { Runtime } from "shared/runtime";
55
import { COMPARATOR, SORT } from "./util/sort";
6-
import { REVERSE } from "./util/bytes";
6+
import { REVERSE, FILL } from "./util/bytes";
77
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
88
import { idof, isArray as builtin_isArray } from "./builtins";
99
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_ILLEGALGENTYPE, E_EMPTYARRAY, E_HOLEYARRAY } from "./util/error";
@@ -154,56 +154,12 @@ export class Array<T> {
154154
return value;
155155
}
156156

157-
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this {
158-
var ptr = this.dataStart;
159-
var len = this.length_;
160-
start = start < 0 ? max(len + start, 0) : min(start, len);
161-
end = end < 0 ? max(len + end, 0) : min(end, len);
157+
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> {
162158
if (isManaged<T>()) {
163-
for (; start < end; ++start) {
164-
store<usize>(ptr + (<usize>start << alignof<T>()), changetype<usize>(value));
165-
__link(changetype<usize>(this), changetype<usize>(value), true);
166-
}
167-
} else if (sizeof<T>() == 1) {
168-
if (start < end) {
169-
memory.fill(
170-
ptr + <usize>start,
171-
u8(value),
172-
<usize>(end - start)
173-
);
174-
}
159+
FILL<usize>(this.dataStart, this.length_, changetype<usize>(value), start, end);
160+
__link(changetype<usize>(this), changetype<usize>(value), false);
175161
} else {
176-
if (ASC_SHRINK_LEVEL <= 1) {
177-
if (isInteger<T>()) {
178-
// @ts-ignore
179-
if (value == <T>0 | value == <T>-1) {
180-
if (start < end) {
181-
memory.fill(
182-
ptr + (<usize>start << alignof<T>()),
183-
u8(value),
184-
<usize>(end - start) << alignof<T>()
185-
);
186-
}
187-
return this;
188-
}
189-
} else if (isFloat<T>()) {
190-
// for floating non-negative zeros we can use fast memory.fill
191-
if ((sizeof<T>() == 4 && reinterpret<u32>(f32(value)) == 0) ||
192-
(sizeof<T>() == 8 && reinterpret<u64>(f64(value)) == 0)) {
193-
if (start < end) {
194-
memory.fill(
195-
ptr + (<usize>start << alignof<T>()),
196-
0,
197-
<usize>(end - start) << alignof<T>()
198-
);
199-
}
200-
return this;
201-
}
202-
}
203-
}
204-
for (; start < end; ++start) {
205-
store<T>(ptr + (<usize>start << alignof<T>()), value);
206-
}
162+
FILL<T>(this.dataStart, this.length_, value, start, end);
207163
}
208164
return this;
209165
}
@@ -295,7 +251,7 @@ export class Array<T> {
295251
return out;
296252
}
297253

298-
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): this {
254+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Array<T> {
299255
var ptr = this.dataStart;
300256
var len = this.length_;
301257

@@ -466,12 +422,12 @@ export class Array<T> {
466422
return result;
467423
}
468424

469-
reverse(): this {
425+
reverse(): Array<T> {
470426
REVERSE<T>(this.dataStart, this.length_);
471427
return this;
472428
}
473429

474-
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
430+
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): Array<T> {
475431
SORT<T>(this.dataStart, this.length_, comparator);
476432
return this;
477433
}

std/assembly/staticarray.ts

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
44
import { Runtime } from "shared/runtime";
55
import { COMPARATOR, SORT } from "./util/sort";
6-
import { REVERSE } from "./util/bytes";
6+
import { REVERSE, FILL } from "./util/bytes";
77
import { idof } from "./builtins";
88
import { Array } from "./array";
99
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_HOLEYARRAY } from "./util/error";
@@ -141,61 +141,17 @@ export class StaticArray<T> {
141141
}
142142
}
143143

144-
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this {
145-
var ptr = changetype<usize>(this);
146-
var len = this.length;
147-
start = start < 0 ? max(len + start, 0) : min(start, len);
148-
end = end < 0 ? max(len + end, 0) : min(end, len);
144+
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): StaticArray<T> {
149145
if (isManaged<T>()) {
150-
for (; start < end; ++start) {
151-
store<usize>(ptr + (<usize>start << alignof<T>()), changetype<usize>(value));
152-
__link(changetype<usize>(this), changetype<usize>(value), true);
153-
}
154-
} else if (sizeof<T>() == 1) {
155-
if (start < end) {
156-
memory.fill(
157-
ptr + <usize>start,
158-
u8(value),
159-
<usize>(end - start)
160-
);
161-
}
146+
FILL<usize>(changetype<usize>(this), this.length, changetype<usize>(value), start, end);
147+
__link(changetype<usize>(this), changetype<usize>(value), false);
162148
} else {
163-
if (ASC_SHRINK_LEVEL <= 1) {
164-
if (isInteger<T>()) {
165-
// @ts-ignore
166-
if (value == <T>0 | value == <T>-1) {
167-
if (start < end) {
168-
memory.fill(
169-
ptr + (<usize>start << alignof<T>()),
170-
u8(value),
171-
<usize>(end - start) << alignof<T>()
172-
);
173-
}
174-
return this;
175-
}
176-
} else if (isFloat<T>()) {
177-
// for floating non-negative zeros we can use fast memory.fill
178-
if ((sizeof<T>() == 4 && reinterpret<u32>(f32(value)) == 0) ||
179-
(sizeof<T>() == 8 && reinterpret<u64>(f64(value)) == 0)) {
180-
if (start < end) {
181-
memory.fill(
182-
ptr + (<usize>start << alignof<T>()),
183-
0,
184-
<usize>(end - start) << alignof<T>()
185-
);
186-
}
187-
return this;
188-
}
189-
}
190-
}
191-
for (; start < end; ++start) {
192-
store<T>(ptr + (<usize>start << alignof<T>()), value);
193-
}
149+
FILL<T>(changetype<usize>(this), this.length, value, start, end);
194150
}
195151
return this;
196152
}
197153

198-
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): this {
154+
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): StaticArray<T> {
199155
var ptr = changetype<usize>(this);
200156
var len = this.length;
201157

@@ -386,7 +342,7 @@ export class StaticArray<T> {
386342
return false;
387343
}
388344

389-
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
345+
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): StaticArray<T> {
390346
SORT<T>(changetype<usize>(this), this.length, comparator);
391347
return this;
392348
}
@@ -403,7 +359,7 @@ export class StaticArray<T> {
403359
return <string>unreachable();
404360
}
405361

406-
reverse(): this {
362+
reverse(): StaticArray<T> {
407363
REVERSE<T>(changetype<usize>(this), this.length);
408364
return this;
409365
}

0 commit comments

Comments
 (0)