I have a problem that is really kind of a general programming question, but my implementation is in Java, so I will provide my examples that way
I have a class like
How about generating the product lazily, ie. only create the tuple when you're accessing it?
/**
* A random access view of tuples of a cartesian product of ArrayLists
*
* Orders tuples in the natural order of the cartesian product
*
* @param T the type for both the values and the stored tuples, ie. values of the cartesian factors are singletons
* While the type of input sets is List with elements being treated as singletons
*
*/
abstract public class CartesianProductView extends AbstractList {
private final List> factors;
private final int size;
/**
* @param factors the length of the factors (ie. the elements of the factors argument) should not change,
* otherwise get may not return all tuples, or throw exceptions when trying to access the factors outside of range
*/
public CartesianProductView(List> factors) {
this.factors = new ArrayList<>(factors);
Collections.reverse(this.factors);
int acc = 1;
for (Iterator> iter = this.factors.iterator(); iter.hasNext(); ) {
acc *= iter.next().size();
}
this.size = acc;
}
@Override
public T get(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException(String.format("index %d > size() %d", index, size()));
}
T acc = null;
for (Iterator> iter = factors.iterator(); iter.hasNext();) {
List set = iter.next();
acc = makeTupleOrSingleton(set.get(index % set.size()), acc);
index /= set.size();
}
return acc;
}
@Override
public int size() {
return size;
}
private T makeTupleOrSingleton(T left, T right) {
if (right == null) {
return left;
}
return makeTuple(left, right);
}
/**
*
* @param left a singleton of a value
* @param right a tuple of values taken from the cartesian product factors, with null representing the empty set
* @return the sum of left and right, with the value of left being put in front
*/
abstract protected T makeTuple(T left, T right);
}
and use it like this
final List> l1 = new ArrayList>() {{ add(singletonList("a")); add(singletonList("b")); add(singletonList("c")); }};
final List> l2 = new ArrayList>() {{ add(singletonList("X")); add(singletonList("Y")); }};
final List> l3 = new ArrayList>() {{ add(singletonList("1")); add(singletonList("2")); add(singletonList("3")); add(singletonList("4")); }};
List>> in = new ArrayList>>() {{ add(l1); add(l2); add(l3); }};
List> a = new CartesianProductView>(in) {
@Override
protected List makeTuple(final List left, final List right) {
return new ArrayList() {{ add(left.get(0)); addAll(right); }};
}
};
System.out.println(a);
The result:
[[a, X, 1], [a, X, 2], [a, X, 3], [a, X, 4], [a, Y, 1], [a, Y, 2], [a, Y, 3], [a, Y, 4], [b, X, 1], [b, X, 2], [b, X, 3], [b, X, 4], [b, Y, 1], [b, Y, 2], [b, Y, 3], [b, Y, 4], [c, X, 1], [c, X, 2], [c, X, 3], [c, X, 4], [c, Y, 1], [c, Y, 2], [c, Y, 3], [c, Y, 4]]
As an added bonus, you can use it join strings all with all:
final List l1 = new ArrayList() {{ add("a"); add("b"); add("c"); }};
final List l2 = new ArrayList() {{ add("X"); add("Y"); }};
final List l3 = new ArrayList() {{ add("1"); add("2"); add("3"); add("4"); }};
List> in = new ArrayList>() {{ add(l1); add(l2); add(l3); }};
List a = new CartesianProductView(in) {
@Override
protected String makeTuple(String left, String right) {
return String.format("%s%s", left, right);
}
};
System.out.println(a);
The result:
[aX1, aX2, aX3, aX4, aY1, aY2, aY3, aY4, bX1, bX2, bX3, bX4, bY1, bY2, bY3, bY4, cX1, cX2, cX3, cX4, cY1, cY2, cY3, cY4]