MATSIM
PseudoRemovePriorityQueue.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * FastRemovePriorityQueue.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.collections;
22 
23 import java.io.Serializable;
24 import java.util.Comparator;
25 import java.util.Iterator;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28 import java.util.PriorityQueue;
29 
48 public class PseudoRemovePriorityQueue<E> implements RouterPriorityQueue<E> {
49 
50  private final PriorityQueue<PseudoEntry<E>> delegate;
51  /*package*/ final Map<E, PseudoEntry<E>> lastEntry;
52 
53  public PseudoRemovePriorityQueue(final int initialCapacity) {
54  this.delegate = new PriorityQueue<PseudoEntry<E>>(initialCapacity, new PseudoComparator<E>());
55  this.lastEntry = new LinkedHashMap<E, PseudoEntry<E>>(initialCapacity);
56 // this.lastEntry = new IdentityHashMap<E, PseudoEntry<E>>(initialCapacity);
57  }
58 
65  @Override
66  public boolean add(final E o, final double priority) {
67  if (o == null) {
68  throw new NullPointerException();
69  }
70  PseudoEntry<E> entry = new PseudoEntry<E>(o, priority);
71  if (this.lastEntry.containsKey(o)) {
72  return false;
73  }
74  if (this.delegate.add(entry)) {
75  this.lastEntry.put(o, entry);
76  return true;
77  }
78  return false; // this should never happen
79  }
80 
88  @Override
89  public E poll() {
90  PseudoEntry<E> entry = this.delegate.poll();
91  while ((entry != null) && (!entry.valid)) {
92  entry = this.delegate.poll();
93  }
94  if (entry == null) {
95  return null;
96  }
97  this.lastEntry.remove(entry.value);
98  return entry.value;
99  }
100 
108  @Override
109  public boolean remove(final E o) {
110  PseudoEntry<E> entry = this.lastEntry.remove(o);
111  if ((entry != null) && (entry.valid)) {
112  entry.valid = false;
113  return true;
114  }
115  return false;
116  }
117 
125  @Override
126  public int size() {
127  return this.lastEntry.size();
128  }
129 
130  @Override
131  public E peek() {
132  PseudoEntry<E> entry = this.delegate.peek();
133  while ((entry != null) && (!entry.valid)) {
134  this.delegate.poll();
135  entry = this.delegate.peek();
136  }
137  if (entry == null) {
138  return null;
139  }
140  return entry.value;
141  }
142 
143  @Override
144  public boolean isEmpty() {
145  return this.lastEntry.isEmpty();
146  }
147 
148  @Override
149  public boolean decreaseKey(E value, double priority) {
150  /*
151  * PriorityQueue does not support decreaseKey method. Therefore remove and re-add
152  * the object.
153  */
154  this.remove(value);
155  return this.add(value, priority);
156  }
157 
158  @Override
159  public void reset() {
160  this.delegate.clear();
161  this.lastEntry.clear();
162  }
163 
171  @Override
172  public Iterator<E> iterator() {
173  return new Iterator<E>() {
174 
175  final Iterator<E> iterDelegate = PseudoRemovePriorityQueue.this.lastEntry.keySet().iterator();
176 
177  @Override
178  public boolean hasNext() {
179  return this.iterDelegate.hasNext();
180  }
181 
182  @Override
183  public E next() {
184  return this.iterDelegate.next();
185  }
186 
187  @Override
188  public void remove() {
189  throw new UnsupportedOperationException();
190  }
191  };
192  }
193 
194  private static class PseudoEntry<E> {
195  final E value;
196  final double priority;
197  boolean valid = true;
198 
199  public PseudoEntry(final E value, final double priority) {
200  this.value = value;
201  this.priority = priority;
202  }
203  }
204 
205  /*package*/ static class PseudoComparator<T> implements Comparator<PseudoEntry<T>>, Serializable {
206  private static final long serialVersionUID = 1L;
207  @Override
208  public int compare(final PseudoEntry<T> o1, final PseudoEntry<T> o2) {
209  return Double.compare(o1.priority, o2.priority);
210  }
211  }
212 
213 }
PseudoEntry(final E value, final double priority)