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

Merge pull request #3580 from proletariatgames/no_map_cache

C#: Optionally disable lookup caching in C# Map implementations
Cauê Waneck 10 жил өмнө
parent
commit
977ad7499e

+ 25 - 4
std/cs/_std/haxe/ds/IntMap.hx

@@ -46,12 +46,16 @@ import cs.NativeArray;
 	private var nOccupied:Int;
 	private var upperBound:Int;
 
+#if !no_map_cache
 	private var cachedKey:Int;
 	private var cachedIndex:Int;
+#end
 
 	public function new() : Void
 	{
+#if !no_map_cache
 		cachedIndex = -1;
+#end
 	}
 
 	public function set( key : Int, value : T ) : Void
@@ -135,17 +139,20 @@ import cs.NativeArray;
 	public function get( key : Int ) : Null<T>
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
-
+#end
 			return vals[idx];
 		}
 
@@ -155,17 +162,20 @@ import cs.NativeArray;
 	private function getDefault( key : Int, def : T ) : T
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
-
+#end
 			return vals[idx];
 		}
 
@@ -175,16 +185,20 @@ import cs.NativeArray;
 	public function exists( key : Int ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return true;
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
+#end
 
 			return true;
 		}
@@ -195,7 +209,9 @@ import cs.NativeArray;
 	public function remove( key : Int ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (! (cachedKey == key && ( (idx = cachedIndex) != -1 )))
+#end
 		{
 			idx = lookup(key);
 		}
@@ -204,9 +220,10 @@ import cs.NativeArray;
 		{
 			return false;
 		} else {
+#if !no_map_cache
 			if (cachedKey == key)
 				cachedIndex = -1;
-
+#end
 			if (!isEither(flags, idx))
 			{
 				setIsDelTrue(flags, idx);
@@ -253,9 +270,11 @@ import cs.NativeArray;
 
 		if (j != 0)
 		{ //rehashing is required
+#if !no_map_cache
 			//resetting cache
 			cachedKey = 0;
 			cachedIndex = -1;
+#end
 
 			j = -1;
 			var nBuckets = nBuckets, _keys = _keys, vals = vals, flags = flags;
@@ -438,8 +457,10 @@ private class IntMapKeyIterator<T> {
 
 	public function next():Int {
 		var ret = m._keys[i];
+#if !no_map_cache
 		m.cachedIndex = i;
 		m.cachedKey = ret;
+#end
 		i++;
 		return ret;
 	}
@@ -471,4 +492,4 @@ private class IntMapValueIterator<T> {
 	public inline function next():T {
 		return m.vals[i++];
 	}
-}
+}

+ 97 - 43
std/cs/_std/haxe/ds/ObjectMap.hx

@@ -46,8 +46,10 @@ import cs.NativeArray;
 	private var nOccupied:Int;
 	private var upperBound:Int;
 
+#if !no_map_cache
 	private var cachedKey:K;
 	private var cachedIndex:Int;
+#end
 
 #if DEBUG_HASHTBL
 	private var totalProbes:Int;
@@ -58,7 +60,9 @@ import cs.NativeArray;
 
 	public function new() : Void
 	{
+#if !no_map_cache
 		cachedIndex = -1;
+#end
 	}
 
 	public function set( key : K, value : V ) : Void
@@ -122,8 +126,10 @@ import cs.NativeArray;
 			vals[x] = value;
 		}
 
+#if !no_map_cache
 		cachedIndex = x;
 		cachedKey = key;
+#end
 	}
 
 	@:final private function lookup( key : K ) : Int
@@ -188,9 +194,11 @@ import cs.NativeArray;
 
 		if (j != 0)
 		{ //rehashing is required
+#if !no_map_cache
 			//resetting cache
 			cachedKey = null;
 			cachedIndex = -1;
+#end
 
 			j = -1;
 			var nBuckets = nBuckets, _keys = _keys, vals = vals, hashes = hashes;
@@ -263,16 +271,20 @@ import cs.NativeArray;
 	public function get( key : K ) : Null<V>
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
+#end
 
 			return vals[idx];
 		}
@@ -283,16 +295,20 @@ import cs.NativeArray;
 	private function getDefault( key : K, def : V ) : V
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
+#end
 
 			return vals[idx];
 		}
@@ -303,16 +319,20 @@ import cs.NativeArray;
 	public function exists( key : K ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return true;
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
+#end
 
 			return true;
 		}
@@ -323,7 +343,9 @@ import cs.NativeArray;
 	public function remove( key : K ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (! (cachedKey == key && ( (idx = cachedIndex) != -1 )))
+#end
 		{
 			idx = lookup(key);
 		}
@@ -332,8 +354,10 @@ import cs.NativeArray;
 		{
 			return false;
 		} else {
+#if !no_map_cache
 			if (cachedKey == key)
 				cachedIndex = -1;
+#end
 
 			hashes[idx] = FLAG_DEL;
 			_keys[idx] = null;
@@ -350,29 +374,7 @@ import cs.NativeArray;
 	**/
 	public function keys() : Iterator<K>
 	{
-		var i = 0;
-		var len = nBuckets;
-		return {
-			hasNext: function() {
-				for (j in i...len)
-				{
-					if (!isEither(hashes[j]))
-					{
-						i = j;
-						return true;
-					}
-				}
-				return false;
-			},
-			next: function() {
-				var ret = _keys[i];
-				cachedIndex = i;
-				cachedKey = ret;
-
-				i = i + 1;
-				return ret;
-			}
-		};
+		return new ObjectMapKeyIterator(this);
 	}
 
 	/**
@@ -381,26 +383,7 @@ import cs.NativeArray;
 	**/
 	public function iterator() : Iterator<V>
 	{
-		var i = 0;
-		var len = nBuckets;
-		return {
-			hasNext: function() {
-				for (j in i...len)
-				{
-					if (!isEither(hashes[j]))
-					{
-						i = j;
-						return true;
-					}
-				}
-				return false;
-			},
-			next: function() {
-				var ret = vals[i];
-				i = i + 1;
-				return ret;
-			}
-		};
+		return new ObjectMapValueIterator(this);
 	}
 
 	/**
@@ -485,4 +468,75 @@ import cs.NativeArray;
 	}
 }
 
+@:access(haxe.ds.ObjectMap)
+@:final @:keep
+private class ObjectMapKeyIterator<T:{},V> {
+	var m:ObjectMap<T,V>;
+	var i:Int;
+	var len:Int;
+
+	public function new(m:ObjectMap<T,V>) {
+		this.i = 0;
+		this.m = m;
+		this.len = m.nBuckets;
+	}
+
+	public function hasNext():Bool {
+		for (j in i...len)
+		{
+			if (!ObjectMap.isEither(m.hashes[j]))
+			{
+				i = j;
+				return true;
+			}
+		}
+		return false;
+	}
+
+	public function next() : T {
+		var ret = m._keys[i];
+
+#if !no_map_cache
+		m.cachedIndex = i;
+		m.cachedKey = ret;
+#end
+
+		i = i + 1;
+		return ret;
+	}
+}
+
+@:access(haxe.ds.ObjectMap)
+@:final @:keep
+private class ObjectMapValueIterator<K:{},T> {
+	var m:ObjectMap<K,T>;
+	var i:Int;
+	var len:Int;
+
+	public function new(m:ObjectMap<K,T>)
+	{
+		this.i = 0;
+		this.m = m;
+		this.len = m.nBuckets;
+	}
+
+	public function hasNext() : Bool {
+		for (j in i...len)
+		{
+			if (!ObjectMap.isEither(m.hashes[j]))
+			{
+				i = j;
+				return true;
+			}
+		}
+		return false;
+	}
+
+	public inline function next():T {
+		var ret = m.vals[i];
+		i = i + 1;
+		return ret;
+	}
+}
+
 private typedef HashType = Int;

+ 26 - 4
std/cs/_std/haxe/ds/StringMap.hx

@@ -46,8 +46,10 @@ import cs.NativeArray;
 	private var nOccupied:Int;
 	private var upperBound:Int;
 
+#if !no_map_cache
 	private var cachedKey:String;
 	private var cachedIndex:Int;
+#end
 
 #if DEBUG_HASHTBL
 	private var totalProbes:Int;
@@ -58,7 +60,9 @@ import cs.NativeArray;
 
 	public function new() : Void
 	{
+#if !no_map_cache
 		cachedIndex = -1;
+#end
 	}
 
 	public function set( key : String, value : T ) : Void
@@ -122,8 +126,10 @@ import cs.NativeArray;
 			vals[x] = value;
 		}
 
+#if !no_map_cache
 		cachedIndex = x;
 		cachedKey = key;
+#end
 	}
 
 	@:final private function lookup( key : String ) : Int
@@ -189,8 +195,10 @@ import cs.NativeArray;
 		if (j != 0)
 		{ //rehashing is required
 			//resetting cache
+#if !no_map_cache
 			cachedKey = null;
 			cachedIndex = -1;
+#end
 
 			j = -1;
 			var nBuckets = nBuckets, _keys = _keys, vals = vals, hashes = hashes;
@@ -263,17 +271,19 @@ import cs.NativeArray;
 	public function get( key : String ) : Null<T>
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
-
+#end
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
-
+#end
 			return vals[idx];
 		}
 
@@ -283,16 +293,20 @@ import cs.NativeArray;
 	private function getDefault( key : String, def : T ) : T
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return vals[idx];
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
+#end
 
 			return vals[idx];
 		}
@@ -303,17 +317,20 @@ import cs.NativeArray;
 	public function exists( key : String ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
 		{
 			return true;
 		}
+#end
 
 		idx = lookup(key);
 		if (idx != -1)
 		{
+#if !no_map_cache
 			cachedKey = key;
 			cachedIndex = idx;
-
+#end
 			return true;
 		}
 
@@ -323,7 +340,9 @@ import cs.NativeArray;
 	public function remove( key : String ) : Bool
 	{
 		var idx = -1;
+#if !no_map_cache
 		if (! (cachedKey == key && ( (idx = cachedIndex) != -1 )))
+#end
 		{
 			idx = lookup(key);
 		}
@@ -332,9 +351,10 @@ import cs.NativeArray;
 		{
 			return false;
 		} else {
+#if !no_map_cache
 			if (cachedKey == key)
 				cachedIndex = -1;
-
+#end
 			hashes[idx] = FLAG_DEL;
 			_keys[idx] = null;
 			vals[idx] = null;
@@ -471,8 +491,10 @@ private class StringMapKeyIterator<T> {
 
 	public function next():String {
 		var ret = m._keys[i];
+#if !no_map_cache
 		m.cachedIndex = i;
 		m.cachedKey = ret;
+#end
 		i++;
 		return ret;
 	}