| 1 | package tuffy.util; |
| 2 | |
| 3 | import java.util.HashSet; |
| 4 | import java.util.LinkedList; |
| 5 | |
| 6 | public class BoundHashList<T> { |
| 7 | LinkedList<T> list = new LinkedList<T>(); |
| 8 | HashSet<T> set = new HashSet<T>(); |
| 9 | |
| 10 | private int bound = Integer.MAX_VALUE; |
| 11 | |
| 12 | public boolean contains(T e){ |
| 13 | return set.contains(e); |
| 14 | } |
| 15 | |
| 16 | public BoundHashList(int maxSize){ |
| 17 | bound = maxSize; |
| 18 | } |
| 19 | |
| 20 | public boolean add(T e){ |
| 21 | if(set.contains(e)) return false; |
| 22 | if(list.size() >= bound){ |
| 23 | set.remove(list.removeFirst()); |
| 24 | } |
| 25 | list.addLast(e); |
| 26 | set.add(e); |
| 27 | return true; |
| 28 | } |
| 29 | } |