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 fakeCounter = new FakeCounter();
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;
public UniquePriorityFifoQueue() {
@ -24,11 +25,7 @@ public class UniquePriorityFifoQueue<T> implements Queue<T> {
public UniquePriorityFifoQueue(Collection<? extends T> c) {
this();
for (T e : c) {
PriorityFifoElement<T> element = new PriorityFifoElement<T>(e, this.counter);
this.elements.add(element);
this.uelements.add(element);
}
this.addAll(c);
}
public UniquePriorityFifoQueue(final Comparator<? super T> comparator) {
@ -46,18 +43,19 @@ public class UniquePriorityFifoQueue<T> implements Queue<T> {
@Override
public boolean add(T e) {
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
public boolean addAll(Collection<? extends T> c) {
boolean anyAdded = false;
if (c != null) for (T e : c) {
PriorityFifoElement<T> element = new PriorityFifoElement<T>(e, this.counter);
this.elements.add(element);
this.uelements.add(element);
if (this.uelements.add(element) && this.elements.add(element))
anyAdded = true;
}
return true;
return anyAdded;
}
@Override
@ -117,8 +115,7 @@ public class UniquePriorityFifoQueue<T> implements Queue<T> {
@Override
public boolean offer(T e) {
PriorityFifoElement<T> element = new PriorityFifoElement<T>(e, this.counter);
return this.elements.add(element) && this.uelements.add(element);
return this.add(e);
}
@Override