forked from LeenkxTeam/LNXSDK
Update
This commit is contained in:
@ -3,39 +3,87 @@ package haxe;
|
||||
import haxe.iterators.RestIterator;
|
||||
import haxe.iterators.RestKeyValueIterator;
|
||||
import java.NativeArray;
|
||||
import java.lang.System;
|
||||
import java.StdTypes;
|
||||
import java.lang.Object;
|
||||
import java.lang.System;
|
||||
import java.util.Arrays;
|
||||
|
||||
private typedef NativeRest<T> = NativeArray<Object>;
|
||||
private typedef NativeRest<T> = NativeArray<T>;
|
||||
|
||||
@:coreApi
|
||||
abstract Rest<T>(NativeRest<T>) {
|
||||
public var length(get,never):Int;
|
||||
public var length(get, never):Int;
|
||||
|
||||
inline function get_length():Int
|
||||
return this.length;
|
||||
|
||||
@:from static public function of<T>(array:Array<T>):Rest<T> {
|
||||
var native = @:privateAccess array.__a;
|
||||
var result:NativeRest<T>;
|
||||
#if jvm
|
||||
result = (cast native:Object).clone();
|
||||
#else
|
||||
result = new NativeRest<T>(native.length);
|
||||
for(i in 0...native.length)
|
||||
result[i] = cast native[i];
|
||||
#end
|
||||
@:from extern inline static public function of<T>(array:Array<T>):Rest<T> {
|
||||
var result = createNative(array.length);
|
||||
var src:NativeArray<Object> = cast @:privateAccess array.__a;
|
||||
for (i in 0...result.length)
|
||||
result[i] = cast src[i];
|
||||
return new Rest(result);
|
||||
}
|
||||
|
||||
@:noDoc
|
||||
@:generic
|
||||
static function ofNativePrimitive<TBoxed, TRest>(result:NativeRest<TBoxed>, collection:NativeArray<TRest>):Rest<TRest> {
|
||||
for (i in 0...collection.length)
|
||||
result[i] = cast collection[i];
|
||||
return new Rest(cast result);
|
||||
}
|
||||
|
||||
@:noDoc
|
||||
@:from static function ofNativeInt(collection:NativeArray<Int>):Rest<Int>
|
||||
return ofNativePrimitive(new NativeRest<java.lang.Integer>(collection.length), collection);
|
||||
|
||||
@:noDoc
|
||||
@:from static function ofNativeFloat(collection:NativeArray<Float>):Rest<Float>
|
||||
return ofNativePrimitive(new NativeRest<java.lang.Double>(collection.length), collection);
|
||||
|
||||
@:noDoc
|
||||
@:from static function ofNativeBool(collection:NativeArray<Bool>):Rest<Bool>
|
||||
return ofNativePrimitive(new NativeRest<java.lang.Boolean>(collection.length), collection);
|
||||
|
||||
@:noDoc
|
||||
@:from static function ofNativeInt8(collection:NativeArray<Int8>):Rest<Int8>
|
||||
return ofNativePrimitive(new NativeRest<java.lang.Byte>(collection.length), collection);
|
||||
|
||||
@:noDoc
|
||||
@:from static function ofNativeInt16(collection:NativeArray<Int16>):Rest<Int16>
|
||||
return ofNativePrimitive(new NativeRest<java.lang.Short>(collection.length), collection);
|
||||
|
||||
@:noDoc
|
||||
@:from static function ofNativeChar16(collection:NativeArray<Char16>):Rest<Char16>
|
||||
return ofNativePrimitive(new NativeRest<java.lang.Character>(collection.length), collection);
|
||||
|
||||
@:noDoc
|
||||
@:from static function ofNativeHaxeInt64(collection:NativeArray<haxe.Int64>):Rest<haxe.Int64>
|
||||
return ofNativePrimitive(new NativeRest<java.lang.Long>(collection.length), collection);
|
||||
|
||||
@:noDoc
|
||||
@:from static function ofNativeInt64(collection:NativeArray<Int64>):Rest<Int64>
|
||||
return ofNativePrimitive(new NativeRest<java.lang.Long>(collection.length), collection);
|
||||
|
||||
@:noDoc
|
||||
@:from static function ofNative<T>(collection:NativeArray<T>):Rest<T> {
|
||||
return new Rest(collection);
|
||||
}
|
||||
|
||||
inline function new(a:NativeRest<T>):Void
|
||||
this = a;
|
||||
|
||||
/**
|
||||
* Implemented in genjvm (to auto-box primitive types) and genjava
|
||||
*/
|
||||
static function createNative<T>(length:Int):NativeRest<T>
|
||||
return new NativeRest<T>(length);
|
||||
|
||||
@:arrayAccess inline function get(index:Int):T
|
||||
return cast this[index];
|
||||
return this[index];
|
||||
|
||||
@:to public function toArray():Array<T> {
|
||||
return [for(i in 0...this.length) cast this[i]];
|
||||
return [for (i in 0...this.length) this[i]];
|
||||
}
|
||||
|
||||
public inline function iterator():RestIterator<T>
|
||||
@ -44,15 +92,21 @@ abstract Rest<T>(NativeRest<T>) {
|
||||
public inline function keyValueIterator():RestKeyValueIterator<T>
|
||||
return new RestKeyValueIterator<T>(this);
|
||||
|
||||
public function append(item:T):Rest<T> {
|
||||
var result = new NativeRest<T>(this.length + 1);
|
||||
extern inline public function append(item:T):Rest<T> {
|
||||
return _append(createNative(this.length + 1), item);
|
||||
}
|
||||
|
||||
function _append(result:NativeRest<T>, item:T):Rest<T> {
|
||||
System.arraycopy(this, 0, result, 0, this.length);
|
||||
result[this.length] = cast item;
|
||||
return new Rest(result);
|
||||
}
|
||||
|
||||
public function prepend(item:T):Rest<T> {
|
||||
var result = new NativeRest<T>(this.length + 1);
|
||||
extern inline public function prepend(item:T):Rest<T> {
|
||||
return _prepend(createNative(this.length + 1), item);
|
||||
}
|
||||
|
||||
function _prepend(result:NativeRest<T>, item:T):Rest<T> {
|
||||
System.arraycopy(this, 0, result, 1, this.length);
|
||||
result[0] = cast item;
|
||||
return new Rest(result);
|
||||
@ -61,4 +115,4 @@ abstract Rest<T>(NativeRest<T>) {
|
||||
public function toString():String {
|
||||
return toArray().toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,34 @@
|
||||
package haxe.atomic;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
abstract AtomicBool(AtomicBoolean) {
|
||||
public inline function new(value:Bool) {
|
||||
this = new AtomicBoolean(value);
|
||||
}
|
||||
|
||||
public inline function compareExchange(expected:Bool, replacement:Bool):Bool {
|
||||
// Java's compareAndSet returns a boolean, so do a CAS loop to be able to return the original value without a potential race condition
|
||||
|
||||
var original;
|
||||
var real_replacement;
|
||||
do {
|
||||
original = this.get();
|
||||
real_replacement = original == expected ? replacement : original;
|
||||
} while (!this.compareAndSet(original, real_replacement));
|
||||
return original;
|
||||
}
|
||||
|
||||
public inline function exchange(value:Bool):Bool {
|
||||
return this.getAndSet(value);
|
||||
}
|
||||
|
||||
public inline function load():Bool {
|
||||
return this.get();
|
||||
}
|
||||
|
||||
public inline function store(value:Bool):Bool {
|
||||
this.set(value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
64
Kha/Tools/windows_x64/std/java/_std/haxe/atomic/AtomicInt.hx
Normal file
64
Kha/Tools/windows_x64/std/java/_std/haxe/atomic/AtomicInt.hx
Normal file
@ -0,0 +1,64 @@
|
||||
package haxe.atomic;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
abstract AtomicInt(AtomicInteger) {
|
||||
public inline function new(value:Int) {
|
||||
this = new AtomicInteger(value);
|
||||
}
|
||||
|
||||
private inline function cas_loop(value:Int, op:(a:Int, b:Int) -> Int):Int {
|
||||
var val;
|
||||
|
||||
do {
|
||||
val = this.get();
|
||||
} while (!this.compareAndSet(val, op(val, value)));
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
public inline function add(b:Int):Int {
|
||||
return this.getAndAdd(b);
|
||||
}
|
||||
|
||||
public inline function sub(b:Int):Int {
|
||||
return this.getAndAdd(-b);
|
||||
}
|
||||
|
||||
public inline function and(b:Int):Int {
|
||||
return cas_loop(b, (a:Int, b:Int) -> a & b);
|
||||
}
|
||||
|
||||
public inline function or(b:Int):Int {
|
||||
return cas_loop(b, (a:Int, b:Int) -> a | b);
|
||||
}
|
||||
|
||||
public inline function xor(b:Int):Int {
|
||||
return cas_loop(b, (a:Int, b:Int) -> a ^ b);
|
||||
}
|
||||
|
||||
public inline function compareExchange(expected:Int, replacement:Int):Int {
|
||||
// Java's compareAndSet returns a boolean, so do a CAS loop to be able to return the original value without a potential race condition
|
||||
|
||||
var original;
|
||||
var real_replacement;
|
||||
do {
|
||||
original = this.get();
|
||||
real_replacement = original == expected ? replacement : original;
|
||||
} while (!this.compareAndSet(original, real_replacement));
|
||||
return original;
|
||||
}
|
||||
|
||||
public inline function exchange(value:Int):Int {
|
||||
return this.getAndSet(value);
|
||||
}
|
||||
|
||||
public inline function load():Int {
|
||||
return this.get();
|
||||
}
|
||||
|
||||
public inline function store(value:Int):Int {
|
||||
this.set(value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package haxe.atomic;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
abstract AtomicObject<T:{}>(AtomicReference<T>) {
|
||||
public inline function new(value:T) {
|
||||
this = new AtomicReference(value);
|
||||
}
|
||||
|
||||
public inline function compareExchange(expected:T, replacement:T):T {
|
||||
// Java's compareAndSet returns a boolean, so do a CAS loop to be able to return the original value without a potential race condition
|
||||
|
||||
var original;
|
||||
var real_replacement;
|
||||
do {
|
||||
original = this.get();
|
||||
real_replacement = original == expected ? replacement : original;
|
||||
} while (!this.compareAndSet(original, real_replacement));
|
||||
return original;
|
||||
}
|
||||
|
||||
public inline function exchange(value:T):T {
|
||||
return this.getAndSet(value);
|
||||
}
|
||||
|
||||
public inline function load():T {
|
||||
return this.get();
|
||||
}
|
||||
|
||||
public inline function store(value:T):T {
|
||||
this.set(value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -364,7 +364,7 @@ import java.NativeArray;
|
||||
|
||||
public function toString():String {
|
||||
var s = new StringBuf();
|
||||
s.add("{");
|
||||
s.add("[");
|
||||
var it = keys();
|
||||
for (i in it) {
|
||||
s.add(i);
|
||||
@ -373,7 +373,7 @@ import java.NativeArray;
|
||||
if (it.hasNext())
|
||||
s.add(", ");
|
||||
}
|
||||
s.add("}");
|
||||
s.add("]");
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
|
||||
@ -378,7 +378,7 @@ import java.NativeArray;
|
||||
|
||||
public function toString():String {
|
||||
var s = new StringBuf();
|
||||
s.add("{");
|
||||
s.add("[");
|
||||
var it = keys();
|
||||
for (i in it) {
|
||||
s.add(Std.string(i));
|
||||
@ -387,7 +387,7 @@ import java.NativeArray;
|
||||
if (it.hasNext())
|
||||
s.add(", ");
|
||||
}
|
||||
s.add("}");
|
||||
s.add("]");
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
|
||||
@ -377,7 +377,7 @@ import java.NativeArray;
|
||||
|
||||
public function toString():String {
|
||||
var s = new StringBuf();
|
||||
s.add("{");
|
||||
s.add("[");
|
||||
var it = keys();
|
||||
for (i in it) {
|
||||
s.add(i);
|
||||
@ -386,7 +386,7 @@ import java.NativeArray;
|
||||
if (it.hasNext())
|
||||
s.add(", ");
|
||||
}
|
||||
s.add("}");
|
||||
s.add("]");
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
|
||||
@ -404,7 +404,7 @@ import java.lang.ref.ReferenceQueue;
|
||||
|
||||
public function toString():String {
|
||||
var s = new StringBuf();
|
||||
s.add("{");
|
||||
s.add("[");
|
||||
var it = keys();
|
||||
for (i in it) {
|
||||
s.add(Std.string(i));
|
||||
@ -413,7 +413,7 @@ import java.lang.ref.ReferenceQueue;
|
||||
if (it.hasNext())
|
||||
s.add(", ");
|
||||
}
|
||||
s.add("}");
|
||||
s.add("]");
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user