Use an iterator for remove.

This commit is contained in:
asofold 2015-09-28 13:09:45 +02:00
parent 9efcf01766
commit f70151d8c7
2 changed files with 9 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package fr.neatmonster.nocheatplus.utilities.ds.map; package fr.neatmonster.nocheatplus.utilities.ds.map;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -143,14 +144,15 @@ public abstract class AbstractCoordHashMap<V, E extends fr.neatmonster.nocheatpl
return null; return null;
} }
else { else {
for (int i = 0; i < bucket.size(); i++) { final Iterator<E> it = bucket.iterator();
final E entry = bucket.get(i); while (it.hasNext()) {
final E entry = it.next();
if (entry.hash == hash && x == entry.x && z == entry.z && y == entry.y) { if (entry.hash == hash && x == entry.x && z == entry.z && y == entry.y) {
bucket.remove(entry); it.remove();
size--;
if (bucket.isEmpty()) { if (bucket.isEmpty()) {
entries[slot] = null; entries[slot] = null;
} }
size--;
return entry.value; return entry.value;
} }
} }

View File

@ -30,6 +30,9 @@ public class CoordHashMap<V> extends AbstractCoordHashMap<V, fr.neatmonster.noch
* @param <V> * @param <V>
*/ */
public static class HashIterator<V> implements Iterator<Entry<V>> { public static class HashIterator<V> implements Iterator<Entry<V>> {
// TODO: Switch to store an iterator?
private final CoordHashMap<V> map; private final CoordHashMap<V> map;
private final List<HashEntry<V>>[] entries; private final List<HashEntry<V>>[] entries;