Răsfoiți Sursa

Merge pull request #3179 from nadako/list_iter_inline

Inline List iterator
Dan Korostelev 11 ani în urmă
părinte
comite
9eba9b92a6
2 a modificat fișierele cu 28 adăugiri și 35 ștergeri
  1. 22 34
      std/List.hx
  2. 6 1
      std/php/_std/List.hx

+ 22 - 34
std/List.hx

@@ -158,40 +158,8 @@ class List<T> {
 	/**
 		Returns an iterator on the elements of the list.
 	**/
-	public function iterator() : Iterator<T> {
-		#if (java || cs || python)
-		var h = h;
-		return cast {
-			hasNext : function() {
-				return (h != null);
-			},
-			next : function() {
-				{
-					if( h == null )
-						return null;
-					var x = h[0];
-					h = h[1];
-					return x;
-				}
-			}
-		}
-		#else
-		return cast {
-			h : h,
-			hasNext : function() {
-				return untyped (__this__.h != null);
-			},
-			next : function() {
-				untyped {
-					if( __this__.h == null )
-						return null;
-					var x = __this__.h[0];
-					__this__.h = __this__.h[1];
-					return x;
-				}
-			}
-		}
-		#end
+	public inline function iterator() : ListIterator<T> {
+		return new ListIterator<T>(h);
 	}
 
 	/**
@@ -268,3 +236,23 @@ class List<T> {
 	}
 
 }
+
+private class ListIterator<T> {
+	var head:Array<Dynamic>;
+	var val:Dynamic;
+
+	public inline function new(head:Array<Dynamic>) {
+		this.head = head;
+		this.val = null;
+	}
+
+	public inline function hasNext():Bool {
+		return head != null;
+	}
+
+	public inline function next():T {
+		val = head[0];
+		head = head[1];
+		return val;
+	}
+}

+ 6 - 1
std/php/_std/List.hx

@@ -97,7 +97,7 @@
 		return false;
 	}
 
-	public function iterator() : Iterator<T> {
+	public function iterator() : ListIterator<T> {
 		return untyped __call__("new _hx_list_iterator", this);
 	}
 
@@ -158,3 +158,8 @@
 		return iterator();
 	}
 }
+
+@:coreType private extern class ListIterator<T> {
+	function hasNext():Bool;
+	function next():T;
+}