mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 07:02:13 +00:00
Add Array#concat and add a return type for Array#push (#214)
This commit is contained in:
parent
c11605d10c
commit
d864977a1a
@ -181,6 +181,25 @@ export class Array<T> {
|
||||
return newLength;
|
||||
}
|
||||
|
||||
concat(items: Array<T>): Array<T> {
|
||||
var thisLen: isize = this.length_;
|
||||
var otherLen = (items == null) ? 0 : items.length_;
|
||||
var outLen = thisLen + otherLen;
|
||||
var out: Array<T> = new Array<T>(outLen);
|
||||
|
||||
if (thisLen) {
|
||||
memory.copy(changetype<usize>(out.buffer_) + HEADER_SIZE,
|
||||
changetype<usize>(this.buffer_) + HEADER_SIZE,
|
||||
<usize>(thisLen << alignof<T>()));
|
||||
}
|
||||
if (otherLen) {
|
||||
memory.copy(changetype<usize>(out.buffer_) + HEADER_SIZE + <usize>(thisLen << alignof<T>()),
|
||||
changetype<usize>(items.buffer_) + HEADER_SIZE,
|
||||
<usize>(otherLen << alignof<T>()));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
pop(): T {
|
||||
var length = this.length_;
|
||||
if (length < 1) throw new RangeError("Array is empty");
|
||||
|
3
std/assembly/index.d.ts
vendored
3
std/assembly/index.d.ts
vendored
@ -444,7 +444,8 @@ declare class Array<T> {
|
||||
includes(searchElement: T, fromIndex?: i32): bool;
|
||||
indexOf(searchElement: T, fromIndex?: i32): i32;
|
||||
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
|
||||
push(element: T): void;
|
||||
push(element: T): i32;
|
||||
concat(items: T[]): T[];
|
||||
pop(): T;
|
||||
forEach(callbackfn: (value: T, index: i32, array: Array<T>) => void): void;
|
||||
map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U>;
|
||||
|
3
std/portable/index.d.ts
vendored
3
std/portable/index.d.ts
vendored
@ -253,7 +253,8 @@ declare class Array<T> {
|
||||
includes(searchElement: T, fromIndex?: i32): bool;
|
||||
indexOf(searchElement: T, fromIndex?: i32): i32;
|
||||
lastIndexOf(searchElement: T, fromIndex?: i32): i32;
|
||||
push(element: T): void;
|
||||
push(element: T): i32;
|
||||
concat(items: T[]): T[];
|
||||
pop(): T;
|
||||
forEach(callbackfn: (value: T, index: i32, array: Array<T>) => void): void;
|
||||
map<U>(callbackfn: (value: T, index: i32, array: Array<T>) => U): Array<U>;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -102,6 +102,49 @@ assert(arr[0] == 43);
|
||||
assert(arr[1] == 44);
|
||||
assert(arr[2] == 45);
|
||||
|
||||
// Array#concat ///////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
var other = new Array<i32>();
|
||||
|
||||
var out = arr.concat(other);
|
||||
assert(internalCapacity<i32>(arr) == 3);
|
||||
assert(arr.length == 3);
|
||||
assert(out.length == 3);
|
||||
|
||||
out.concat([]);
|
||||
assert(internalCapacity<i32>(arr) == 3);
|
||||
|
||||
assert(out[0] == 43);
|
||||
assert(out[1] == 44);
|
||||
assert(out[2] == 45);
|
||||
|
||||
other.push(46);
|
||||
other.push(47);
|
||||
|
||||
out = arr.concat(other);
|
||||
|
||||
assert(internalCapacity<i32>(arr) == 3);
|
||||
assert(other.length == 2);
|
||||
assert(out.length == 5);
|
||||
assert(out[0] == 43);
|
||||
assert(out[1] == 44);
|
||||
assert(out[2] == 45);
|
||||
assert(out[3] == 46);
|
||||
assert(out[4] == 47);
|
||||
|
||||
out.pop();
|
||||
assert(out.length == 4);
|
||||
|
||||
out = arr.concat(null);
|
||||
assert(out.length == 3);
|
||||
assert(out[2] == 45);
|
||||
|
||||
var source: i32[] = [];
|
||||
assert(source.length == 0);
|
||||
out = source.concat(arr);
|
||||
assert(out.length == 3);
|
||||
assert(source.length == 0);
|
||||
|
||||
// Array#unshift ///////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
arr.unshift(42);
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user