@Test public void testIterator() { DisjointSets<Integer> sets = new DisjointSets<Integer>(newRange(0, 10)); for (Set<Integer> subset : sets) { assertEquals(1, subset.size()); } Set<Set<Integer>> expected = new HashSet<Set<Integer>>(); sets.union(0, 4); expected.add(newSet(0, 4)); sets.union(1, 8); expected.add(newSet(1, 8)); sets.union(2, 9); expected.add(newSet(2, 9)); sets.union(3, 5); expected.add(newSet(3, 5)); sets.union(6, 7); expected.add(newSet(6, 7)); Set<Set<Integer>> actual = new HashSet<Set<Integer>>(); for (Set<Integer> subset : sets) { assertEquals(2, subset.size()); actual.add(subset); } assertEquals(expected, actual); }
@Override public Iterator<Set<E>> iterator() { return new Iterator<Set<E>>() { private final Iterator<Entry<E>> iterator = map.values().iterator(); private Entry<E> nextRepresentative; { findNextRepresentative(); } @Override public boolean hasNext() { return nextRepresentative != null; } @Override public Set<E> next() { if (nextRepresentative == null) { throw new NoSuchElementException(); } Set<E> result = nextRepresentative.asSet(); findNextRepresentative(); return result; } private void findNextRepresentative() { while(iterator.hasNext()) { Entry<E> candidate = iterator.next(); if (candidate.isRepresentative()) { nextRepresentative = candidate; return; } } nextRepresentative = null; } @Override public void remove() { throw new UnsupportedOperationException(); } }; }
DisjointSets implements Iterable<Set<E>> { @Override public Iterator<Set<E>> iterator() { return new Iterator<Set<E>>() { private final Iterator<Entry<E>> iterator = map.values().iterator(); private Entry<E> nextRepresentative; { findNextRepresentative(); } @Override public boolean hasNext() { return nextRepresentative != null; } @Override public Set<E> next() { if (nextRepresentative == null) { throw new NoSuchElementException(); } Set<E> result = nextRepresentative.asSet(); findNextRepresentative(); return result; } private void findNextRepresentative() { while(iterator.hasNext()) { Entry<E> candidate = iterator.next(); if (candidate.isRepresentative()) { nextRepresentative = candidate; return; } } nextRepresentative = null; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } }
DisjointSets implements Iterable<Set<E>> { @Override public Iterator<Set<E>> iterator() { return new Iterator<Set<E>>() { private final Iterator<Entry<E>> iterator = map.values().iterator(); private Entry<E> nextRepresentative; { findNextRepresentative(); } @Override public boolean hasNext() { return nextRepresentative != null; } @Override public Set<E> next() { if (nextRepresentative == null) { throw new NoSuchElementException(); } Set<E> result = nextRepresentative.asSet(); findNextRepresentative(); return result; } private void findNextRepresentative() { while(iterator.hasNext()) { Entry<E> candidate = iterator.next(); if (candidate.isRepresentative()) { nextRepresentative = candidate; return; } } nextRepresentative = null; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } DisjointSets(Collection<? extends E> c); }
DisjointSets implements Iterable<Set<E>> { @Override public Iterator<Set<E>> iterator() { return new Iterator<Set<E>>() { private final Iterator<Entry<E>> iterator = map.values().iterator(); private Entry<E> nextRepresentative; { findNextRepresentative(); } @Override public boolean hasNext() { return nextRepresentative != null; } @Override public Set<E> next() { if (nextRepresentative == null) { throw new NoSuchElementException(); } Set<E> result = nextRepresentative.asSet(); findNextRepresentative(); return result; } private void findNextRepresentative() { while(iterator.hasNext()) { Entry<E> candidate = iterator.next(); if (candidate.isRepresentative()) { nextRepresentative = candidate; return; } } nextRepresentative = null; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } DisjointSets(Collection<? extends E> c); boolean areTogether(E e1, E e2); void union(E e1, E e2); @Override Iterator<Set<E>> iterator(); static DisjointSets<E> singletonsOf(Class<E> elementType); }
DisjointSets implements Iterable<Set<E>> { @Override public Iterator<Set<E>> iterator() { return new Iterator<Set<E>>() { private final Iterator<Entry<E>> iterator = map.values().iterator(); private Entry<E> nextRepresentative; { findNextRepresentative(); } @Override public boolean hasNext() { return nextRepresentative != null; } @Override public Set<E> next() { if (nextRepresentative == null) { throw new NoSuchElementException(); } Set<E> result = nextRepresentative.asSet(); findNextRepresentative(); return result; } private void findNextRepresentative() { while(iterator.hasNext()) { Entry<E> candidate = iterator.next(); if (candidate.isRepresentative()) { nextRepresentative = candidate; return; } } nextRepresentative = null; } @Override public void remove() { throw new UnsupportedOperationException(); } }; } DisjointSets(Collection<? extends E> c); boolean areTogether(E e1, E e2); void union(E e1, E e2); @Override Iterator<Set<E>> iterator(); static DisjointSets<E> singletonsOf(Class<E> elementType);  }