소스 검색

[php] optimized iterators of `Map` and native arrays

Alexander Kuzmenko 7 년 전
부모
커밋
8613268950
6개의 변경된 파일30개의 추가작업 그리고 36개의 파일을 삭제
  1. 1 0
      extra/CHANGES.txt
  2. 2 25
      std/php/NativeArray.hx
  3. 2 3
      std/php/NativeAssocArray.hx
  4. 22 0
      std/php/NativeIndexedArray.hx
  5. 2 7
      std/php/_std/Type.hx
  6. 1 1
      std/php/_std/haxe/ds/IntMap.hx

+ 1 - 0
extra/CHANGES.txt

@@ -6,6 +6,7 @@ XXXX-XX-XX:
 	all : [breaking] disallowed static variables that have no type-hint and expression (#6440)
 	php : Optimized haxe.ds.Vector (VectorData is not Array anymore)
 	php : Optimized `Map.copy()` and `Array.copy()`
+	php : Optimized iterators of `Map` and native arrays.
 	php : Support native PHP generators. See `php.Syntax.yield()` and `php.Generator`
 	js : Updated jQuery extern (js.jquery.*). Supporting jQuery up to 3.3.1.
 

+ 2 - 25
std/php/NativeArray.hx

@@ -40,28 +40,5 @@ using php.Global;
 	@:arrayAccess function setByBool(key:Bool, val:Dynamic):Dynamic;
 
 	public inline function iterator()
-		return new NativeArrayIterator(this);
-}
-
-/**
-	Allows iterating over native PHP array with Haxe for-loop
-**/
-private class NativeArrayIterator {
-	var arr:NativeArray;
-	var hasMore:Bool;
-
-	public inline function new( a:NativeArray ) {
-		arr = a;
-		hasMore = (arr.reset() != false);
-	}
-
-	public inline function hasNext() : Bool {
-		return hasMore;
-	}
-
-	public inline function next() : Dynamic {
-		var result = arr.current();
-		hasMore = (arr.next() != false);
-		return result;
-	}
-}
+		return (this:NativeIndexedArray<Dynamic>).iterator();
+}

+ 2 - 3
std/php/NativeAssocArray.hx

@@ -34,7 +34,6 @@ abstract NativeAssocArray<T>(NativeArray) from NativeArray to NativeArray {
 	inline function set(key:String, val:T):T
 		return this[key] = val;
 
-    //TODO
-    // @:to function toMap():Map<String,T>
-    // @:from static function fromMap<T>(map:Map<String,T>):NativeAssocArray<T>
+	public inline function iterator()
+		return (cast Global.array_values(this):NativeIndexedArray<T>).iterator();
 }

+ 22 - 0
std/php/NativeIndexedArray.hx

@@ -37,6 +37,9 @@ abstract NativeIndexedArray<T>(NativeArray) from NativeArray to NativeArray {
 	public inline function push(val:T)
 		Syntax.code('{0}[] = {1}', this, val);
 
+	public inline function iterator():NativeIndexedArrayIterator<T>
+		return new NativeIndexedArrayIterator(this);
+
 	@:to
 	inline function toHaxeArray():Array<T>
 		return @:privateAccess Array.wrap(this);
@@ -44,4 +47,23 @@ abstract NativeIndexedArray<T>(NativeArray) from NativeArray to NativeArray {
 	@:from
 	static inline function fromHaxeArray<T>(a:Array<T>):NativeIndexedArray<T>
 		return @:privateAccess a.arr;
+}
+
+private class NativeIndexedArrayIterator<T> {
+	var length:Int;
+	var current:Int = 0;
+	var data:NativeIndexedArray<T>;
+
+	public inline function new(data:NativeIndexedArray<T>) {
+		length = Global.count(data);
+		this.data = data;
+	}
+
+	public inline function hasNext():Bool {
+		return current < length;
+	}
+
+	public inline function next():T {
+		return data[current++];
+	}
 }

+ 2 - 7
std/php/_std/Type.hx

@@ -178,8 +178,7 @@ enum ValueType {
 		var reflection = new ReflectionClass(Syntax.nativeClassName(c));
 
 		var methods = new NativeArray();
-		for (m in reflection.getMethods()) {
-			var method:ReflectionMethod = m;
+		for (method in reflection.getMethods()) {
 			if (!method.isStatic()) {
 				var name = method.getName();
 				if (!isServiceFieldName(name)) {
@@ -189,8 +188,7 @@ enum ValueType {
 		}
 
 		var properties = new NativeArray();
-		for (p in reflection.getProperties()) {
-			var property:ReflectionProperty = p;
+		for (property in reflection.getProperties()) {
 			if (!property.isStatic()) {
 				var name = property.getName();
 				if (!isServiceFieldName(name)) {
@@ -215,8 +213,6 @@ enum ValueType {
 
 		var methods = new NativeArray();
 		for (m in reflection.getMethods(ReflectionMethod.IS_STATIC)) {
-			//TODO: report an issue on invalid type inference for iteration over `NativeIndexedArray`
-			var m:ReflectionMethod = m;
 			var name = m.getName();
 			if (!isServiceFieldName(name) && phpName == m.getDeclaringClass().getName()) {
 				methods.array_push(name);
@@ -225,7 +221,6 @@ enum ValueType {
 
 		var properties = new NativeArray();
 		for (p in reflection.getProperties(ReflectionProperty.IS_STATIC)) {
-			var p:ReflectionMethod = p;
 			var name = p.getName();
 			if (!isServiceFieldName(name) && phpName == p.getDeclaringClass().getName()) {
 				properties.array_push(name);

+ 1 - 1
std/php/_std/haxe/ds/IntMap.hx

@@ -99,4 +99,4 @@ import php.NativeIndexedArray;
 
 		return '{' + Global.implode(', ', parts) + '}';
 	}
-}
+}