2
0
Эх сурвалжийг харах

DynamicAccess key/value iterator (#7769)

* DynamicAccess key/value iterator

* Make it faster, maybe
Jens Fischer 6 жил өмнө
parent
commit
ca79e3082c

+ 14 - 5
std/haxe/DynamicAccess.hx

@@ -39,9 +39,9 @@ abstract DynamicAccess<T>(Dynamic<T>) from Dynamic<T> to Dynamic<T> {
 	/**
 	/**
 		Returns a value by specified `key`.
 		Returns a value by specified `key`.
 
 
-		If the structure does not contain the given key, null is returned.
+		If the structure does not contain the given key, `null` is returned.
 
 
-		If `key` is null, the result is unspecified.
+		If `key` is `null`, the result is unspecified.
 	**/
 	**/
 	@:arrayAccess
 	@:arrayAccess
 	public inline function get(key:String):Null<T> {
 	public inline function get(key:String):Null<T> {
@@ -59,7 +59,7 @@ abstract DynamicAccess<T>(Dynamic<T>) from Dynamic<T> to Dynamic<T> {
 
 
 		Returns the given value.
 		Returns the given value.
 
 
-		If `key` is null, the result is unspecified.
+		If `key` is `null`, the result is unspecified.
 	**/
 	**/
 	@:arrayAccess
 	@:arrayAccess
 	public inline function set(key:String, value:T):T {
 	public inline function set(key:String, value:T):T {
@@ -74,7 +74,7 @@ abstract DynamicAccess<T>(Dynamic<T>) from Dynamic<T> to Dynamic<T> {
 	/**
 	/**
 		Tells if the structure contains a specified `key`.
 		Tells if the structure contains a specified `key`.
 
 
-		If `key` is null, the result is unspecified.
+		If `key` is `null`, the result is unspecified.
 	**/
 	**/
 	public inline function exists(key:String):Bool return Reflect.hasField(this, key);
 	public inline function exists(key:String):Bool return Reflect.hasField(this, key);
 
 
@@ -83,7 +83,7 @@ abstract DynamicAccess<T>(Dynamic<T>) from Dynamic<T> to Dynamic<T> {
 
 
 		Returns true, if `key` was present in structure, or false otherwise.
 		Returns true, if `key` was present in structure, or false otherwise.
 
 
-		If `key` is null, the result is unspecified.
+		If `key` is `null`, the result is unspecified.
 	**/
 	**/
 	public inline function remove(key:String):Bool return Reflect.deleteField(this, key);
 	public inline function remove(key:String):Bool return Reflect.deleteField(this, key);
 
 
@@ -96,4 +96,13 @@ abstract DynamicAccess<T>(Dynamic<T>) from Dynamic<T> to Dynamic<T> {
 		Returns a shallow copy of the structure
 		Returns a shallow copy of the structure
 	**/
 	**/
 	public inline function copy():DynamicAccess<T> return Reflect.copy(this);
 	public inline function copy():DynamicAccess<T> return Reflect.copy(this);
+
+	/**
+		Returns an Iterator over the keys and values of this `DynamicAccess`.
+
+		The order of values is undefined.
+	**/
+	public inline function keyValueIterator():KeyValueIterator<String, T> {
+		return new haxe.iterators.DynamicAccessKeyValueIterator(this);
+	}
 }
 }

+ 52 - 0
std/haxe/iterators/DynamicAccessKeyValueIterator.hx

@@ -0,0 +1,52 @@
+/*
+ * Copyright (C)2005-2019 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package haxe.iterators;
+
+/**
+	This Key/Value iterator can be used to iterate over `haxe.DynamicAccess`.
+**/
+class DynamicAccessKeyValueIterator<T> {
+	final access:DynamicAccess<T>;
+	final keys:Array<String>;
+	var index:Int;
+
+	public inline function new(access:DynamicAccess<T>) {
+		this.access = access;
+		this.keys = access.keys();
+		index = 0;
+	}
+
+	/**
+		See `Iterator.hasNext`
+	**/
+ 	public inline function hasNext():Bool {
+		return index < keys.length;
+	}
+
+	/**
+		See `Iterator.next`
+	**/
+ 	public inline function next():{key:String, value:T} {
+		var key = keys[index++];
+		return {value: access[key], key: key};
+	}
+}

+ 20 - 0
tests/unit/src/unitstd/haxe/DynamicAccess.unit.hx

@@ -36,3 +36,23 @@ map["test"] == 2;
 
 
 var d:Dynamic<Int> = map;
 var d:Dynamic<Int> = map;
 d.test == 2;
 d.test == 2;
+
+var map = new haxe.DynamicAccess();
+map["a"] = 1;
+map["b"] = 2;
+map["c"] = 3;
+
+var keys = [];
+var values = [];
+for (key => value in map) {
+	keys.push(key);
+	values.push(value);
+}
+keys.length == 3;
+keys[0] in ["a", "b", "c"];
+keys[1] in ["a", "b", "c"];
+keys[2] in ["a", "b", "c"];
+values.length == 3;
+values[0] in [1, 2, 3];
+values[1] in [1, 2, 3];
+values[2] in [1, 2, 3];