Implement quirk of Lua reference impl 'next', where removed key do not immediately become invisible to 'next'

This commit is contained in:
DBotThePony 2024-06-27 21:16:58 +07:00
parent 77a9beb665
commit 5c6efaf03d
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -46,6 +46,9 @@ public class TraversableHashMap<K, V> implements Map<K, V> {
private final Set<Map.Entry<K, V>> entrySet;
private K firstKey;
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.
@ -133,10 +136,12 @@ public class TraversableHashMap<K, V> implements Map<K, V> {
Entry<K, V> e = entries.remove(key);
if (e != null) {
K prevKey = e.getPreviousKey();
K nextKey = e.getNextKey();
lingeringKey = (K) key;
lingeringSuccessor = nextKey;
if (prevKey != null) {
entries.get(prevKey).setNextKey(nextKey);
} else {
@ -202,6 +207,9 @@ public class TraversableHashMap<K, V> implements Map<K, V> {
Objects.requireNonNull(key);
Entry<K, V> e = entries.get(key);
if (e == null) {
if (key.equals(lingeringKey))
return lingeringSuccessor;
throw new NoSuchElementException(key.toString());
}
return e.getNextKey();