fixed queue hash/unique bugs

This commit is contained in:
Brian Long 2020-12-09 22:09:18 -05:00
parent f7505475fb
commit 549e95cd25

View File

@ -15,6 +15,7 @@ public class UniquePriorityFifoQueue<T> implements Queue<T> {
private final Counter counter = new SimpleCounter(); private final Counter counter = new SimpleCounter();
private final Counter fakeCounter = new FakeCounter(); private final Counter fakeCounter = new FakeCounter();
private final TreeSet<PriorityFifoElement<T>> elements; private final TreeSet<PriorityFifoElement<T>> elements;
// TreeSet does not check for uniqueness using hashCode, but using the comparator instead
private final Set<PriorityFifoElement<T>> uelements; private final Set<PriorityFifoElement<T>> uelements;
public UniquePriorityFifoQueue() { public UniquePriorityFifoQueue() {
@ -24,11 +25,7 @@ public class UniquePriorityFifoQueue<T> implements Queue<T> {
public UniquePriorityFifoQueue(Collection<? extends T> c) { public UniquePriorityFifoQueue(Collection<? extends T> c) {
this(); this();
for (T e : c) { this.addAll(c);
PriorityFifoElement<T> element = new PriorityFifoElement<T>(e, this.counter);
this.elements.add(element);
this.uelements.add(element);
}
} }
public UniquePriorityFifoQueue(final Comparator<? super T> comparator) { public UniquePriorityFifoQueue(final Comparator<? super T> comparator) {
@ -46,18 +43,19 @@ public class UniquePriorityFifoQueue<T> implements Queue<T> {
@Override @Override
public boolean add(T e) { public boolean add(T e) {
PriorityFifoElement<T> element = new PriorityFifoElement<T>(e, this.counter); PriorityFifoElement<T> element = new PriorityFifoElement<T>(e, this.counter);
return this.elements.add(element) && this.uelements.add(element); return this.uelements.add(element) && this.elements.add(element);
} }
@Override @Override
public boolean addAll(Collection<? extends T> c) { public boolean addAll(Collection<? extends T> c) {
boolean anyAdded = false;
if (c != null) for (T e : c) { if (c != null) for (T e : c) {
PriorityFifoElement<T> element = new PriorityFifoElement<T>(e, this.counter); PriorityFifoElement<T> element = new PriorityFifoElement<T>(e, this.counter);
this.elements.add(element); if (this.uelements.add(element) && this.elements.add(element))
this.uelements.add(element); anyAdded = true;
} }
return true; return anyAdded;
} }
@Override @Override
@ -117,8 +115,7 @@ public class UniquePriorityFifoQueue<T> implements Queue<T> {
@Override @Override
public boolean offer(T e) { public boolean offer(T e) {
PriorityFifoElement<T> element = new PriorityFifoElement<T>(e, this.counter); return this.add(e);
return this.elements.add(element) && this.uelements.add(element);
} }
@Override @Override