| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.base.Function;
|
| 4 | import com.google.common.collect.Iterables;
|
| 5 | import com.google.common.collect.Lists;
|
| 6 | import com.google.common.collect.UnmodifiableIterator;
|
| 7 | import java.lang.reflect.Array;
|
| 8 | import java.util.Arrays;
|
| 9 | import java.util.Collections;
|
| 10 | import java.util.Iterator;
|
| 11 | import java.util.List;
|
| 12 | import java.util.NoSuchElementException;
|
| 13 |
|
| 14 | public class Cartesian
|
| 15 | {
|
| 16 | public static <T> Iterable<T[]> cartesianProduct(Class<T> clazz, Iterable <? extends Iterable <? extends T >> sets)
|
| 17 | {
|
| 18 | return new Cartesian.Product(clazz, (Iterable[])toArray(Iterable.class, sets));
|
| 19 | }
|
| 20 |
|
| 21 | public static <T> Iterable<List<T>> cartesianProduct(Iterable <? extends Iterable <? extends T >> sets)
|
| 22 | {
|
| 23 | return arraysAsLists(cartesianProduct(Object.class, sets));
|
| 24 | }
|
| 25 |
|
| 26 | private static <T> Iterable<List<T>> arraysAsLists(Iterable<Object[]> arrays)
|
| 27 | {
|
| 28 | return Iterables.transform(arrays, new Cartesian.GetList());
|
| 29 | }
|
| 30 |
|
| 31 | private static <T> T[] toArray(Class <? super T > clazz, Iterable <? extends T > it)
|
| 32 | {
|
| 33 | List<T> list = Lists.<T>newArrayList();
|
| 34 |
|
| 35 | for (T t : it)
|
| 36 | {
|
| 37 | list.add(t);
|
| 38 | }
|
| 39 |
|
| 40 | return (T[])((Object[])list.toArray(createArray(clazz, list.size())));
|
| 41 | }
|
| 42 |
|
| 43 | private static <T> T[] createArray(Class <? super T > p_179319_0_, int p_179319_1_)
|
| 44 | {
|
| 45 | return (T[])((Object[])((Object[])Array.newInstance(p_179319_0_, p_179319_1_)));
|
| 46 | }
|
| 47 |
|
| 48 | static class GetList<T> implements Function<Object[], List<T>>
|
| 49 | {
|
| 50 | private GetList()
|
| 51 | {
|
| 52 | }
|
| 53 |
|
| 54 | public List<T> apply(Object[] p_apply_1_)
|
| 55 | {
|
| 56 | return Arrays.<T>asList((T[])p_apply_1_);
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | static class Product<T> implements Iterable<T[]>
|
| 61 | {
|
| 62 | private final Class<T> clazz;
|
| 63 | private final Iterable <? extends T > [] iterables;
|
| 64 |
|
| 65 | private Product(Class<T> clazz, Iterable <? extends T > [] iterables)
|
| 66 | {
|
| 67 | this.clazz = clazz;
|
| 68 | this.iterables = iterables;
|
| 69 | }
|
| 70 |
|
| 71 | public Iterator<T[]> iterator()
|
| 72 | {
|
| 73 | return (Iterator<T[]>)(this.iterables.length <= 0 ? Collections.singletonList((Object[])Cartesian.createArray(this.clazz, 0)).iterator() : new Cartesian.Product.ProductIterator(this.clazz, this.iterables));
|
| 74 | }
|
| 75 |
|
| 76 | static class ProductIterator<T> extends UnmodifiableIterator<T[]>
|
| 77 | {
|
| 78 | private int index;
|
| 79 | private final Iterable <? extends T > [] iterables;
|
| 80 | private final Iterator <? extends T > [] iterators;
|
| 81 | private final T[] results;
|
| 82 |
|
| 83 | private ProductIterator(Class<T> clazz, Iterable <? extends T > [] iterables)
|
| 84 | {
|
| 85 | this.index = -2;
|
| 86 | this.iterables = iterables;
|
| 87 | this.iterators = (Iterator[])Cartesian.createArray(Iterator.class, this.iterables.length);
|
| 88 |
|
| 89 | for (int i = 0; i < this.iterables.length; ++i)
|
| 90 | {
|
| 91 | this.iterators[i] = iterables[i].iterator();
|
| 92 | }
|
| 93 |
|
| 94 | this.results = Cartesian.createArray(clazz, this.iterators.length);
|
| 95 | }
|
| 96 |
|
| 97 | private void endOfData()
|
| 98 | {
|
| 99 | this.index = -1;
|
| 100 | Arrays.fill(this.iterators, (Object)null);
|
| 101 | Arrays.fill(this.results, (Object)null);
|
| 102 | }
|
| 103 |
|
| 104 | public boolean hasNext()
|
| 105 | {
|
| 106 | if (this.index == -2)
|
| 107 | {
|
| 108 | this.index = 0;
|
| 109 |
|
| 110 | for (Iterator <? extends T > iterator1 : this.iterators)
|
| 111 | {
|
| 112 | if (!iterator1.hasNext())
|
| 113 | {
|
| 114 | this.endOfData();
|
| 115 | break;
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | return true;
|
| 120 | }
|
| 121 | else
|
| 122 | {
|
| 123 | if (this.index >= this.iterators.length)
|
| 124 | {
|
| 125 | for (this.index = this.iterators.length - 1; this.index >= 0; --this.index)
|
| 126 | {
|
| 127 | Iterator <? extends T > iterator = this.iterators[this.index];
|
| 128 |
|
| 129 | if (iterator.hasNext())
|
| 130 | {
|
| 131 | break;
|
| 132 | }
|
| 133 |
|
| 134 | if (this.index == 0)
|
| 135 | {
|
| 136 | this.endOfData();
|
| 137 | break;
|
| 138 | }
|
| 139 |
|
| 140 | iterator = this.iterables[this.index].iterator();
|
| 141 | this.iterators[this.index] = iterator;
|
| 142 |
|
| 143 | if (!iterator.hasNext())
|
| 144 | {
|
| 145 | this.endOfData();
|
| 146 | break;
|
| 147 | }
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | return this.index >= 0;
|
| 152 | }
|
| 153 | }
|
| 154 |
|
| 155 | public T[] next()
|
| 156 | {
|
| 157 | if (!this.hasNext())
|
| 158 | {
|
| 159 | throw new NoSuchElementException();
|
| 160 | }
|
| 161 | else
|
| 162 | {
|
| 163 | while (this.index < this.iterators.length)
|
| 164 | {
|
| 165 | this.results[this.index] = this.iterators[this.index].next();
|
| 166 | ++this.index;
|
| 167 | }
|
| 168 |
|
| 169 | return (T[])((Object[])this.results.clone());
|
| 170 | }
|
| 171 | }
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|