Implement quirk of Lua reference impl 'next', where removed key do not immediately become invisible to 'next'
This commit is contained in:
parent
77a9beb665
commit
5c6efaf03d
@ -46,6 +46,9 @@ public class TraversableHashMap<K, V> implements Map<K, V> {
|
|||||||
private final Set<Map.Entry<K, V>> entrySet;
|
private final Set<Map.Entry<K, V>> entrySet;
|
||||||
private K firstKey;
|
private K firstKey;
|
||||||
private K lastKey;
|
private K lastKey;
|
||||||
|
// replicate 'next' behavior of reference implementation, where removing key from table does not invalidate it immediately
|
||||||
|
private K lingeringKey;
|
||||||
|
private K lingeringSuccessor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new empty map.
|
* Constructs a new empty map.
|
||||||
@ -133,10 +136,12 @@ public class TraversableHashMap<K, V> implements Map<K, V> {
|
|||||||
Entry<K, V> e = entries.remove(key);
|
Entry<K, V> e = entries.remove(key);
|
||||||
|
|
||||||
if (e != null) {
|
if (e != null) {
|
||||||
|
|
||||||
K prevKey = e.getPreviousKey();
|
K prevKey = e.getPreviousKey();
|
||||||
K nextKey = e.getNextKey();
|
K nextKey = e.getNextKey();
|
||||||
|
|
||||||
|
lingeringKey = (K) key;
|
||||||
|
lingeringSuccessor = nextKey;
|
||||||
|
|
||||||
if (prevKey != null) {
|
if (prevKey != null) {
|
||||||
entries.get(prevKey).setNextKey(nextKey);
|
entries.get(prevKey).setNextKey(nextKey);
|
||||||
} else {
|
} else {
|
||||||
@ -202,6 +207,9 @@ public class TraversableHashMap<K, V> implements Map<K, V> {
|
|||||||
Objects.requireNonNull(key);
|
Objects.requireNonNull(key);
|
||||||
Entry<K, V> e = entries.get(key);
|
Entry<K, V> e = entries.get(key);
|
||||||
if (e == null) {
|
if (e == null) {
|
||||||
|
if (key.equals(lingeringKey))
|
||||||
|
return lingeringSuccessor;
|
||||||
|
|
||||||
throw new NoSuchElementException(key.toString());
|
throw new NoSuchElementException(key.toString());
|
||||||
}
|
}
|
||||||
return e.getNextKey();
|
return e.getNextKey();
|
||||||
|
Loading…
Reference in New Issue
Block a user