Commit 549e95cd authored by Brian Long's avatar Brian Long
Browse files

fixed queue hash/unique bugs

parent f7505475
Loading
Loading
Loading
Loading
+8 −11
Original line number Diff line number Diff line
@@ -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