|
3 | 3 | import javax.annotation.Nonnull; |
4 | 4 | import javax.annotation.Nullable; |
5 | 5 | import java.util.Arrays; |
| 6 | +import java.util.Collections; |
6 | 7 | import java.util.HashSet; |
| 8 | +import java.util.Iterator; |
7 | 9 | import java.util.Set; |
8 | 10 |
|
9 | 11 | /** |
@@ -118,4 +120,62 @@ public static <T> Set<T> of(T[] values) { |
118 | 120 | public static <T> Set<T> ofVar(T... values) { |
119 | 121 | return of(values); |
120 | 122 | } |
| 123 | + |
| 124 | + /** |
| 125 | + * @param set1 |
| 126 | + * Some set. |
| 127 | + * @param set2 |
| 128 | + * Some other set. |
| 129 | + * @param <T> |
| 130 | + * Item type. |
| 131 | + * |
| 132 | + * @return Iterator over all values among both sets. |
| 133 | + */ |
| 134 | + @Nonnull |
| 135 | + public static <T> Iterator<T> iterator(@Nullable Set<T> set1, @Nullable Set<T> set2) { |
| 136 | + if (set1 == null && set2 == null) |
| 137 | + return Collections.emptyIterator(); |
| 138 | + else if (set1 == null) |
| 139 | + return set2.iterator(); |
| 140 | + else if (set2 == null) |
| 141 | + return set1.iterator(); |
| 142 | + return new MergedSetIterator<>(set1, set2); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Set iterator visiting the unique items of two distinct sets. |
| 147 | + * |
| 148 | + * @param <T> |
| 149 | + * Item type. |
| 150 | + */ |
| 151 | + private static class MergedSetIterator<T> implements Iterator<T> { |
| 152 | + private final Iterator<T> leftIt, rightIt; |
| 153 | + private final int[] seen; |
| 154 | + private int i; |
| 155 | + |
| 156 | + public MergedSetIterator(@Nonnull Set<T> left, @Nonnull Set<T> right) { |
| 157 | + leftIt = left.iterator(); |
| 158 | + rightIt = right.iterator(); |
| 159 | + seen = new int[left.size() + right.size()]; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public boolean hasNext() { |
| 164 | + return leftIt.hasNext() || rightIt.hasNext(); |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public T next() { |
| 169 | + T next = leftIt.hasNext() ? leftIt.next() : rightIt.next(); |
| 170 | + int nextHash = next.hashCode(); |
| 171 | + int max = i; |
| 172 | + i++; |
| 173 | + for (int j = 0; j < max; j++) { |
| 174 | + if (seen[j] == nextHash) |
| 175 | + return next(); |
| 176 | + } |
| 177 | + seen[i] = nextHash; |
| 178 | + return next; |
| 179 | + } |
| 180 | + } |
121 | 181 | } |
0 commit comments