Skip to content
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

Inline Array.map/filter methods #5749

Merged
merged 17 commits into from
Aug 21, 2019
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
14 changes: 12 additions & 2 deletions std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,15 @@ extern class Array<T> {

If `f` is null, the result is unspecified.
**/
function map<S>(f:T->S):Array<S>;
@:runtime inline function map<S>(f:T->S):Array<S> {
#if (cpp && !cppia)
var result = cpp.NativeArray.create(length);
for (i in 0...length) cpp.NativeArray.unsafeSet(result, i, f(cpp.NativeArray.unsafeGet(this, i)));
return result;
#else
return [for (v in this) f(v)];
#end
}

/**
Returns an Array containing those elements of `this` for which `f`
Expand All @@ -284,7 +292,9 @@ extern class Array<T> {

If `f` is null, the result is unspecified.
**/
function filter(f:T->Bool):Array<T>;
@:runtime inline function filter(f:T->Bool):Array<T> {
return [for (v in this) if (f(v)) v];
}

/**
Set the length of the Array.
Expand Down
14 changes: 8 additions & 6 deletions std/cs/_std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,20 @@ final class Array<T> implements ArrayAccess<T> {
return false;
}

public function map<S>(f:T->S):Array<S> {
var ret = [];
for (elt in this)
ret.push(f(elt));
public inline function map<S>(f:T->S):Array<S> {
var ret = alloc(length);
for (i in 0...length)
ret.__unsafe_set(i, f(__unsafe_get(i)));
return ret;
}

public function filter(f:T->Bool):Array<T> {
public inline function filter(f:T->Bool):Array<T> {
var ret = [];
for (elt in this)
for (i in 0...length) {
var elt = __unsafe_get(i);
if (f(elt))
ret.push(elt);
}
return ret;
}

Expand Down
14 changes: 8 additions & 6 deletions std/java/_std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -432,18 +432,20 @@ import java.NativeArray;
}
}

public function map<S>(f:T->S):Array<S> {
var ret = [];
for (elt in this)
ret.push(f(elt));
public inline function map<S>(f:T->S):Array<S> {
var ret = alloc(length);
for (i in 0...length)
ret.__set(i, f(__get(i)));
return ret;
}

public function filter(f:T->Bool):Array<T> {
public inline function filter(f:T->Bool):Array<T> {
var ret = [];
for (elt in this)
for (i in 0...length) {
var elt = __get(i);
if (f(elt))
ret.push(elt);
}
return ret;
}

Expand Down
9 changes: 7 additions & 2 deletions std/js/_std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ extern class Array<T> {
return (cast this).slice();
}

function map<S>(f:T->S):Array<S>;
function filter(f:T->Bool):Array<T>;
@:runtime inline function map<S>(f:T->S):Array<S> {
return [for (v in this) f(v)];
}

@:runtime inline function filter(f:T->Bool):Array<T> {
return [for (v in this) if (f(v)) v];
}

@:runtime inline function iterator():Iterator<T> {
return @:privateAccess HxOverrides.iter(this);
Expand Down
4 changes: 2 additions & 2 deletions std/lua/_std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,11 @@ class Array<T> {
return [for (i in this) i];
}

public function map<S>(f:T->S):Array<S> {
public inline function map<S>(f:T->S):Array<S> {
return [for (i in this) f(i)];
}

public function filter(f:T->Bool):Array<T> {
public inline function filter(f:T->Bool):Array<T> {
return [for (i in this) if (f(i)) i];
}

Expand Down
12 changes: 7 additions & 5 deletions std/neko/_std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,16 @@
return ret;
}

public function map<S>(f:T->S):Array<S> {
var ret = [];
for (elt in this)
ret.push(f(elt));
public inline function map<S>(f:T->S):Array<S> {
var l = length;
var ret = new1(neko.NativeArray.alloc(l), l);
for (i in 0...l) {
ret[i] = f(this[i]);
}
return ret;
}

public function filter(f:T->Bool):Array<T> {
public inline function filter( f : T -> Bool ) : Array<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange spacing here.

var ret = [];
for (elt in this)
if (f(elt))
Expand Down
14 changes: 5 additions & 9 deletions std/php/_std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ final class Array<T> implements ArrayAccess<Int, T> {

public inline function filter(f:T->Bool):Array<T> {
var result = Syntax.arrayDecl();
var i = 0;
while (i < length) {
if (f(arr[i])) {
result.push(arr[i]);
for(item in arr) {
if (f(item)) {
result.push(item);
}
i++;
}
return wrap(result);
}
Expand Down Expand Up @@ -109,10 +107,8 @@ final class Array<T> implements ArrayAccess<Int, T> {

public inline function map<S>(f:T->S):Array<S> {
var result = Syntax.arrayDecl();
var i = 0;
while (i < length) {
result.push(f(arr[i]));
i++;
for(item in arr) {
result.push(f(item));
}
return wrap(result);
}
Expand Down