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

Vector.fill #10687

Merged
merged 9 commits into from
Mar 25, 2023
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
45 changes: 44 additions & 1 deletion std/haxe/ds/Vector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ abstract Vector<T>(VectorData<T>) {

If `length` is less than or equal to 0, the result is unspecified.
**/
public inline function new(length:Int) {
extern overload public inline function new(length:Int) {
RblSb marked this conversation as resolved.
Show resolved Hide resolved
#if flash10
this = new flash.Vector<T>(length, true);
#elseif neko
Expand All @@ -86,6 +86,43 @@ abstract Vector<T>(VectorData<T>) {
#end
}

/**
Creates a new Vector of length `length` filled with `defaultValue` elements.

Can be faster than `new Vector(length)` for iteration on some targets for non-nullable elements.

If `length` is less than or equal to 0, the result is unspecified.
**/
extern overload public inline function new(length:Int, defaultValue:T):Vector<T> {
#if js
this = [for (_ in 0...length) defaultValue];
#elseif python
this = python.Syntax.code("[{0}]*{1}", defaultValue, length);
#else

#if flash10
this = new flash.Vector<T>(length, true);
#elseif neko
this = untyped __dollar__amake(length);
#elseif cs
this = new cs.NativeArray(length);
#elseif java
this = new java.NativeArray(length);
#elseif cpp
this = NativeArray.create(length);
#elseif lua
this = untyped __lua_table__({length: length});
#elseif eval
this = new eval.Vector(length);
#else
this = [];
untyped this.length = length;
#end
fill(defaultValue);

#end
}

/**
Returns the value at index `index`.

Expand Down Expand Up @@ -141,6 +178,12 @@ abstract Vector<T>(VectorData<T>) {
#end
}

/**
Sets all `length` elements of `this` Vector to `value`.
**/
public inline function fill(value:T):Void
for (i in 0...length) this[i] = value;

/**
Copies `length` of elements from `src` Vector, beginning at `srcPos` to
`dest` Vector, beginning at `destPos`
Expand Down
11 changes: 10 additions & 1 deletion std/hl/_std/haxe/ds/Vector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ private typedef VectorData<T> = Array<T>

@:coreApi
abstract Vector<T>(VectorData<T>) {
public inline function new(length:Int) {
extern overload public inline function new(length:Int) {
this = [];
if (length > 0)
this[length - 1] = @:nullSafety(Off) cast null;
}

extern overload public inline function new(length:Int, defaultValue:T):Vector<T> {
this = [
for (i in 0...length) defaultValue
];
}

@:op([]) public inline function get(index:Int):T {
return this[index];
}
Expand All @@ -46,6 +52,9 @@ abstract Vector<T>(VectorData<T>) {
return this.length;
}

public inline function fill(value:T):Void
for (i in 0...length) this[i] = value;

public static inline function blit<T>(src:Vector<T>, srcPos:Int, dest:Vector<T>, destPos:Int, len:Int):Void {
(cast dest : hl.types.ArrayBase.ArrayAccess).blit(destPos, (cast src : hl.types.ArrayBase.ArrayAccess), srcPos, len);
}
Expand Down
21 changes: 14 additions & 7 deletions std/php/_std/haxe/ds/Vector.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ private class PhpVectorData<T> {
public var length:Int;
public var data:NativeIndexedArray<T>;

public inline function new(length:Int) {
public inline function new(length:Int, data:NativeIndexedArray<T>) {
this.length = length;
data = new NativeIndexedArray();
this.data = data;
}
}

private typedef VectorData<T> = PhpVectorData<T>;

@:coreApi
abstract Vector<T>(VectorData<T>) {
public var length(get, never):Int;

public inline function new(length:Int) {
this = new VectorData(length);
extern overload public inline function new(length:Int) {
this = new VectorData(length, new NativeIndexedArray());
}

extern overload public inline function new(length:Int, defaultValue:T):Vector<T> {
this = new VectorData(length, Global.array_fill(0, length, defaultValue));
}

@:op([]) public inline function get(index:Int):T {
Expand All @@ -55,6 +60,9 @@ abstract Vector<T>(VectorData<T>) {
return this.length;
}

public inline function fill(value:T):Void
this.data = Global.array_fill(0, length, value);

public static function blit<T>(src:Vector<T>, srcPos:Int, dest:Vector<T>, destPos:Int, len:Int):Void {
if (src == dest) {
if (srcPos < destPos) {
Expand Down Expand Up @@ -99,8 +107,7 @@ abstract Vector<T>(VectorData<T>) {
}

static public inline function fromArrayCopy<T>(array:Array<T>):Vector<T> {
var vectorData = new VectorData(array.length);
vectorData.data = @:privateAccess array.arr;
var vectorData = new VectorData(array.length, @:privateAccess array.arr);
return cast vectorData;
}

Expand All @@ -127,7 +134,7 @@ abstract Vector<T>(VectorData<T>) {
return result;
}

public inline function sort<T>(f:T->T->Int):Void {
public inline function sort(f:T->T->Int):Void {
Global.usort(this.data, f);
}
}
24 changes: 24 additions & 0 deletions tests/misc/projects/Issue10687/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Main {
var foo = new Foo();
var fooExtern = new Foo();
var fooAb = new FooExtern();
var vector = new haxe.ds.Vector(1);
var vector2 = new haxe.ds.Vector(1, 0);
static function main() {}
}

class Foo {
public var length = Std.random(1);
public inline function new() {}
}

class FooExtern {
public var length = Std.random(1);
extern public inline function new() {}
}

abstract FooAb({length:Int}) {
extern public inline function new() {
this = {length: Std.random(1)};
}
}
2 changes: 2 additions & 0 deletions tests/misc/projects/Issue10687/compile-fail.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--main Main
--interp
1 change: 1 addition & 0 deletions tests/misc/projects/Issue10687/compile-fail.hxml.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Main.hx:4: characters 14-29 : Extern constructor could not be inlined
14 changes: 14 additions & 0 deletions tests/unit/src/unitstd/haxe/ds/Vector.unit.hx
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ vec3[4] == 4;
vec3[5] == 5;
vec3[6] == 6;

var vec5 = new haxe.ds.Vector(3, 5);
vec5[0] == 5;
vec5[1] == 5;
vec5[2] == 5;
vec5.fill(1);
vec5[0] == 1;
vec5[1] == 1;
vec5[2] == 1;

var vec5 = new haxe.ds.Vector(3, true);
vec5[0] == true;
vec5[1] == true;
vec5[2] == true;

var vec5 = haxe.ds.Vector.fromArrayCopy([0,1,2,3,4]);
haxe.ds.Vector.blit(vec5, 0, vec5, 1, 4);
vec5[0] == 0;
Expand Down