|
@@ -46,8 +46,10 @@ import cs.NativeArray;
|
|
private var nOccupied:Int;
|
|
private var nOccupied:Int;
|
|
private var upperBound:Int;
|
|
private var upperBound:Int;
|
|
|
|
|
|
|
|
+#if !no_map_cache
|
|
private var cachedKey:K;
|
|
private var cachedKey:K;
|
|
private var cachedIndex:Int;
|
|
private var cachedIndex:Int;
|
|
|
|
+#end
|
|
|
|
|
|
#if DEBUG_HASHTBL
|
|
#if DEBUG_HASHTBL
|
|
private var totalProbes:Int;
|
|
private var totalProbes:Int;
|
|
@@ -58,7 +60,9 @@ import cs.NativeArray;
|
|
|
|
|
|
public function new() : Void
|
|
public function new() : Void
|
|
{
|
|
{
|
|
|
|
+#if !no_map_cache
|
|
cachedIndex = -1;
|
|
cachedIndex = -1;
|
|
|
|
+#end
|
|
}
|
|
}
|
|
|
|
|
|
public function set( key : K, value : V ) : Void
|
|
public function set( key : K, value : V ) : Void
|
|
@@ -122,8 +126,10 @@ import cs.NativeArray;
|
|
vals[x] = value;
|
|
vals[x] = value;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if !no_map_cache
|
|
cachedIndex = x;
|
|
cachedIndex = x;
|
|
cachedKey = key;
|
|
cachedKey = key;
|
|
|
|
+#end
|
|
}
|
|
}
|
|
|
|
|
|
@:final private function lookup( key : K ) : Int
|
|
@:final private function lookup( key : K ) : Int
|
|
@@ -188,9 +194,11 @@ import cs.NativeArray;
|
|
|
|
|
|
if (j != 0)
|
|
if (j != 0)
|
|
{ //rehashing is required
|
|
{ //rehashing is required
|
|
|
|
+#if !no_map_cache
|
|
//resetting cache
|
|
//resetting cache
|
|
cachedKey = null;
|
|
cachedKey = null;
|
|
cachedIndex = -1;
|
|
cachedIndex = -1;
|
|
|
|
+#end
|
|
|
|
|
|
j = -1;
|
|
j = -1;
|
|
var nBuckets = nBuckets, _keys = _keys, vals = vals, hashes = hashes;
|
|
var nBuckets = nBuckets, _keys = _keys, vals = vals, hashes = hashes;
|
|
@@ -263,16 +271,20 @@ import cs.NativeArray;
|
|
public function get( key : K ) : Null<V>
|
|
public function get( key : K ) : Null<V>
|
|
{
|
|
{
|
|
var idx = -1;
|
|
var idx = -1;
|
|
|
|
+#if !no_map_cache
|
|
if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
|
|
if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
|
|
{
|
|
{
|
|
return vals[idx];
|
|
return vals[idx];
|
|
}
|
|
}
|
|
|
|
+#end
|
|
|
|
|
|
idx = lookup(key);
|
|
idx = lookup(key);
|
|
if (idx != -1)
|
|
if (idx != -1)
|
|
{
|
|
{
|
|
|
|
+#if !no_map_cache
|
|
cachedKey = key;
|
|
cachedKey = key;
|
|
cachedIndex = idx;
|
|
cachedIndex = idx;
|
|
|
|
+#end
|
|
|
|
|
|
return vals[idx];
|
|
return vals[idx];
|
|
}
|
|
}
|
|
@@ -283,16 +295,20 @@ import cs.NativeArray;
|
|
private function getDefault( key : K, def : V ) : V
|
|
private function getDefault( key : K, def : V ) : V
|
|
{
|
|
{
|
|
var idx = -1;
|
|
var idx = -1;
|
|
|
|
+#if !no_map_cache
|
|
if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
|
|
if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
|
|
{
|
|
{
|
|
return vals[idx];
|
|
return vals[idx];
|
|
}
|
|
}
|
|
|
|
+#end
|
|
|
|
|
|
idx = lookup(key);
|
|
idx = lookup(key);
|
|
if (idx != -1)
|
|
if (idx != -1)
|
|
{
|
|
{
|
|
|
|
+#if !no_map_cache
|
|
cachedKey = key;
|
|
cachedKey = key;
|
|
cachedIndex = idx;
|
|
cachedIndex = idx;
|
|
|
|
+#end
|
|
|
|
|
|
return vals[idx];
|
|
return vals[idx];
|
|
}
|
|
}
|
|
@@ -303,16 +319,20 @@ import cs.NativeArray;
|
|
public function exists( key : K ) : Bool
|
|
public function exists( key : K ) : Bool
|
|
{
|
|
{
|
|
var idx = -1;
|
|
var idx = -1;
|
|
|
|
+#if !no_map_cache
|
|
if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
|
|
if (cachedKey == key && ( (idx = cachedIndex) != -1 ))
|
|
{
|
|
{
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
+#end
|
|
|
|
|
|
idx = lookup(key);
|
|
idx = lookup(key);
|
|
if (idx != -1)
|
|
if (idx != -1)
|
|
{
|
|
{
|
|
|
|
+#if !no_map_cache
|
|
cachedKey = key;
|
|
cachedKey = key;
|
|
cachedIndex = idx;
|
|
cachedIndex = idx;
|
|
|
|
+#end
|
|
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
@@ -323,7 +343,9 @@ import cs.NativeArray;
|
|
public function remove( key : K ) : Bool
|
|
public function remove( key : K ) : Bool
|
|
{
|
|
{
|
|
var idx = -1;
|
|
var idx = -1;
|
|
|
|
+#if !no_map_cache
|
|
if (! (cachedKey == key && ( (idx = cachedIndex) != -1 )))
|
|
if (! (cachedKey == key && ( (idx = cachedIndex) != -1 )))
|
|
|
|
+#end
|
|
{
|
|
{
|
|
idx = lookup(key);
|
|
idx = lookup(key);
|
|
}
|
|
}
|
|
@@ -332,8 +354,10 @@ import cs.NativeArray;
|
|
{
|
|
{
|
|
return false;
|
|
return false;
|
|
} else {
|
|
} else {
|
|
|
|
+#if !no_map_cache
|
|
if (cachedKey == key)
|
|
if (cachedKey == key)
|
|
cachedIndex = -1;
|
|
cachedIndex = -1;
|
|
|
|
+#end
|
|
|
|
|
|
hashes[idx] = FLAG_DEL;
|
|
hashes[idx] = FLAG_DEL;
|
|
_keys[idx] = null;
|
|
_keys[idx] = null;
|
|
@@ -350,29 +374,7 @@ import cs.NativeArray;
|
|
**/
|
|
**/
|
|
public function keys() : Iterator<K>
|
|
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>
|
|
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;
|
|
private typedef HashType = Int;
|