Add Array#fill (#250)

This commit is contained in:
Max Graey
2018-09-19 01:59:22 +03:00
committed by Daniel Wirtz
parent 9c770d801e
commit 3f035395cd
6 changed files with 2822 additions and 1516 deletions

View File

@ -99,6 +99,25 @@ export class Array<T> {
if (isManaged<T>()) __gc_link(changetype<usize>(this), changetype<usize>(value)); // tslint:disable-line
}
fill(value: T, start: i32 = 0, end: i32 = i32.MAX_VALUE): this {
var buffer = this.buffer_;
var len = this.length_;
start = start < 0 ? max(len + start, 0) : min(start, len);
end = end < 0 ? max(len + end, 0) : min(end, len);
if (sizeof<T>() == 1) {
memory.fill(
changetype<usize>(buffer) + start + HEADER_SIZE,
<u8>value,
<usize>(end - start)
);
} else {
for (; start < end; ++start) {
storeUnsafe<T,T>(buffer, start, value);
}
}
return this;
}
includes(searchElement: T, fromIndex: i32 = 0): bool {
var length = this.length_;
if (length == 0 || fromIndex >= length) return false;

View File

@ -436,6 +436,7 @@ declare class Array<T> {
length: i32;
/** Constructs a new array. */
constructor(capacity?: i32);
fill(value: T, start?: i32, end?: i32): this;
every(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
findIndex(predicate: (element: T, index: i32, array?: Array<T>) => bool): i32;
includes(searchElement: T, fromIndex?: i32): bool;

View File

@ -247,6 +247,7 @@ declare class Array<T> {
[key: number]: T;
length: i32;
constructor(capacity?: i32);
fill(value: T, start?: i32, end?: i32): this;
every(callbackfn: (element: T, index: i32, array?: Array<T>) => bool): bool;
findIndex(predicate: (element: T, index: i32, array?: Array<T>) => bool): i32;
includes(searchElement: T, fromIndex?: i32): bool;