/
BoundedPriorityQueue.java
45 lines (37 loc) · 1 KB
/
BoundedPriorityQueue.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Copyright 1999-2013 Carnegie Mellon University.
* Portions Copyright 2002 Sun Microsystems, Inc.
* Portions Copyright 2002 Mitsubishi Electric Research Laboratories.
* All Rights Reserved. Use is subject to license terms.
*
* See the file "license.terms" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*
*/
package edu.cmu.sphinx.result;
import java.util.Iterator;
import java.util.TreeSet;
public class BoundedPriorityQueue<T> implements Iterable<T> {
TreeSet<T> items;
int maxSize;
public BoundedPriorityQueue(int maxSize) {
items = new TreeSet<T>();
this.maxSize = maxSize;
}
public void add(T item) {
items.add(item);
if (items.size() > maxSize)
items.pollFirst();
}
public int size() {
return items.size();
}
public T poll() {
return items.pollLast();
}
@Override
public Iterator<T> iterator() {
return items.iterator();
}
}