01: package prefuse.data.util;
02:
03: import java.util.BitSet;
04: import java.util.NoSuchElementException;
05:
06: import prefuse.util.collections.IntIterator;
07:
08: /**
09: * IntIterator over rows that ensures that no duplicates appear in the
10: * iteration. Uses a bitset to note rows it has has seen and not pass along
11: * duplicate row values.
12: *
13: * @author <a href="http://jheer.org">jeffrey heer</a>
14: */
15: public class UniqueRowIterator extends IntIterator {
16:
17: private IntIterator m_iter;
18: private int m_next;
19: private BitSet m_visited;
20:
21: /**
22: * Create a new UniqueRowIterator.
23: * @param iter a source iterator over table rows
24: */
25: public UniqueRowIterator(IntIterator iter) {
26: m_iter = iter;
27: m_visited = new BitSet();
28: advance();
29: }
30:
31: private void advance() {
32: int r = -1;
33: while (r == -1 && m_iter.hasNext()) {
34: r = m_iter.nextInt();
35: if (visit(r))
36: r = -1;
37: }
38: m_next = r;
39: }
40:
41: private boolean visit(int row) {
42: if (m_visited.get(row)) {
43: return true;
44: } else {
45: m_visited.set(row);
46: return false;
47: }
48: }
49:
50: /**
51: * @see java.util.Iterator#hasNext()
52: */
53: public boolean hasNext() {
54: return m_next != -1;
55: }
56:
57: /**
58: * @see prefuse.util.collections.LiteralIterator#nextInt()
59: */
60: public int nextInt() {
61: if (m_next == -1)
62: throw new NoSuchElementException();
63: int retval = m_next;
64: advance();
65: return retval;
66: }
67:
68: /**
69: * Not supported.
70: * @see java.util.Iterator#remove()
71: */
72: public void remove() {
73: throw new UnsupportedOperationException();
74: }
75:
76: } // end of class UniqueRowIterator
|