Skip to content

[NFC] Refactor fill methos of all arrays #2406

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 8 additions & 52 deletions std/assembly/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { BLOCK_MAXSIZE } from "./rt/common";
import { Runtime } from "shared/runtime";
import { COMPARATOR, SORT } from "./util/sort";
import { REVERSE } from "./util/bytes";
import { REVERSE, FILL } from "./util/bytes";
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
import { idof, isArray as builtin_isArray } from "./builtins";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_ILLEGALGENTYPE, E_EMPTYARRAY, E_HOLEYARRAY } from "./util/error";
Expand Down Expand Up @@ -154,56 +154,12 @@ export class Array<T> {
return value;
}

fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this {
var ptr = this.dataStart;
var len = this.length_;
start = start < 0 ? max(len + start, 0) : min(start, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): Array<T> {
if (isManaged<T>()) {
for (; start < end; ++start) {
store<usize>(ptr + (<usize>start << alignof<T>()), changetype<usize>(value));
__link(changetype<usize>(this), changetype<usize>(value), true);
}
} else if (sizeof<T>() == 1) {
if (start < end) {
memory.fill(
ptr + <usize>start,
u8(value),
<usize>(end - start)
);
}
FILL<usize>(this.dataStart, this.length_, changetype<usize>(value), start, end);
__link(changetype<usize>(this), changetype<usize>(value), false);
} else {
if (ASC_SHRINK_LEVEL <= 1) {
if (isInteger<T>()) {
// @ts-ignore
if (value == <T>0 | value == <T>-1) {
if (start < end) {
memory.fill(
ptr + (<usize>start << alignof<T>()),
u8(value),
<usize>(end - start) << alignof<T>()
);
}
return this;
}
} else if (isFloat<T>()) {
// for floating non-negative zeros we can use fast memory.fill
if ((sizeof<T>() == 4 && reinterpret<u32>(f32(value)) == 0) ||
(sizeof<T>() == 8 && reinterpret<u64>(f64(value)) == 0)) {
if (start < end) {
memory.fill(
ptr + (<usize>start << alignof<T>()),
0,
<usize>(end - start) << alignof<T>()
);
}
return this;
}
}
}
for (; start < end; ++start) {
store<T>(ptr + (<usize>start << alignof<T>()), value);
}
FILL<T>(this.dataStart, this.length_, value, start, end);
}
return this;
}
Expand Down Expand Up @@ -295,7 +251,7 @@ export class Array<T> {
return out;
}

copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): this {
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): Array<T> {
var ptr = this.dataStart;
var len = this.length_;

Expand Down Expand Up @@ -466,12 +422,12 @@ export class Array<T> {
return result;
}

reverse(): this {
reverse(): Array<T> {
REVERSE<T>(this.dataStart, this.length_);
return this;
}

sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): Array<T> {
SORT<T>(this.dataStart, this.length_, comparator);
return this;
}
Expand Down
60 changes: 8 additions & 52 deletions std/assembly/staticarray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
import { Runtime } from "shared/runtime";
import { COMPARATOR, SORT } from "./util/sort";
import { REVERSE } from "./util/bytes";
import { REVERSE, FILL } from "./util/bytes";
import { idof } from "./builtins";
import { Array } from "./array";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_HOLEYARRAY } from "./util/error";
Expand Down Expand Up @@ -141,61 +141,17 @@ export class StaticArray<T> {
}
}

fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this {
var ptr = changetype<usize>(this);
var len = this.length;
start = start < 0 ? max(len + start, 0) : min(start, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): StaticArray<T> {
if (isManaged<T>()) {
for (; start < end; ++start) {
store<usize>(ptr + (<usize>start << alignof<T>()), changetype<usize>(value));
__link(changetype<usize>(this), changetype<usize>(value), true);
}
} else if (sizeof<T>() == 1) {
if (start < end) {
memory.fill(
ptr + <usize>start,
u8(value),
<usize>(end - start)
);
}
FILL<usize>(changetype<usize>(this), this.length, changetype<usize>(value), start, end);
__link(changetype<usize>(this), changetype<usize>(value), false);
} else {
if (ASC_SHRINK_LEVEL <= 1) {
if (isInteger<T>()) {
// @ts-ignore
if (value == <T>0 | value == <T>-1) {
if (start < end) {
memory.fill(
ptr + (<usize>start << alignof<T>()),
u8(value),
<usize>(end - start) << alignof<T>()
);
}
return this;
}
} else if (isFloat<T>()) {
// for floating non-negative zeros we can use fast memory.fill
if ((sizeof<T>() == 4 && reinterpret<u32>(f32(value)) == 0) ||
(sizeof<T>() == 8 && reinterpret<u64>(f64(value)) == 0)) {
if (start < end) {
memory.fill(
ptr + (<usize>start << alignof<T>()),
0,
<usize>(end - start) << alignof<T>()
);
}
return this;
}
}
}
for (; start < end; ++start) {
store<T>(ptr + (<usize>start << alignof<T>()), value);
}
FILL<T>(changetype<usize>(this), this.length, value, start, end);
}
return this;
}

copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): this {
copyWithin(target: i32, start: i32, end: i32 = i32.MAX_VALUE): StaticArray<T> {
var ptr = changetype<usize>(this);
var len = this.length;

Expand Down Expand Up @@ -386,7 +342,7 @@ export class StaticArray<T> {
return false;
}

sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): StaticArray<T> {
SORT<T>(changetype<usize>(this), this.length, comparator);
return this;
}
Expand All @@ -403,7 +359,7 @@ export class StaticArray<T> {
return <string>unreachable();
}

reverse(): this {
reverse(): StaticArray<T> {
REVERSE<T>(changetype<usize>(this), this.length);
return this;
}
Expand Down
Loading