Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Can you read Java code, and translate it yourself? </p> <pre><code>import java.util.*; class CartesianIterator &lt;T&gt; implements Iterator &lt;List &lt;T&gt;&gt; { private final List &lt;List &lt;T&gt;&gt; lilio; private int current = 0; private final long last; public CartesianIterator (final List &lt;List &lt;T&gt;&gt; llo) { lilio = llo; long product = 1L; for (List &lt;T&gt; lio: lilio) product *= lio.size (); last = product; } public boolean hasNext () { return current != last; } public List &lt;T&gt; next () { ++current; return get (current - 1, lilio); } public void remove () { ++current; } private List&lt;T&gt; get (final int n, final List &lt;List &lt;T&gt;&gt; lili) { switch (lili.size ()) { case 0: return new ArrayList &lt;T&gt; (); // no break past return; default: { List &lt;T&gt; inner = lili.get (0); List &lt;T&gt; lo = new ArrayList &lt;T&gt; (); lo.add (inner.get (n % inner.size ())); lo.addAll (get (n / inner.size (), lili.subList (1, lili.size ()))); return lo; } } } } class CartesianIterable &lt;T&gt; implements Iterable &lt;List &lt;T&gt;&gt; { private List &lt;List &lt;T&gt;&gt; lilio; public CartesianIterable (List &lt;List &lt;T&gt;&gt; llo) { lilio = llo; } public Iterator &lt;List &lt;T&gt;&gt; iterator () { return new CartesianIterator &lt;T&gt; (lilio); } } class CartesianIteratorTest { public static void main (String[] args) { List &lt;Character&gt; la = Arrays.asList (new Character [] {'a', 'b'}); List &lt;Character&gt; lb = Arrays.asList (new Character [] {'b', 'c'}); List &lt;Character&gt; lc = Arrays.asList (new Character [] {'c', 'a'}); List &lt;List &lt;Character&gt;&gt; llc = new ArrayList &lt;List &lt;Character&gt;&gt; (); llc.add (la); llc.add (lb); llc.add (lc); CartesianIterable &lt;Character&gt; ci = new CartesianIterable &lt;Character&gt; (llc); for (List&lt;Character&gt; lo: ci) show (lo); la = Arrays.asList (new Character [] {'x', 'y', 'z'}); lb = Arrays.asList (new Character [] {'b'}); lc = Arrays.asList (new Character [] {'c'}); llc = new ArrayList &lt;List &lt;Character&gt;&gt; (); llc.add (la); llc.add (lb); llc.add (lc); ci = new CartesianIterable &lt;Character&gt; (llc); for (List&lt;Character&gt; lo: ci) show (lo); } public static void show (List &lt;Character&gt; lo) { System.out.print ("("); for (Object o: lo) System.out.print (o); System.out.println (")"); } } </code></pre> <p>I don't know whether iterator-similar thing exists in C. The <code>iterable</code> is most probably of no help for you. </p> <p>The idea of the code is, to have a counter, which creates an index over all elements of the resultset, and calculate the element, bound to that value. </p> <p>If we have 3 groups (1,2,3)(4,5)(6,7,8), we have 18=3x2x3 results we expect. We can, for Iterator position 7 for example calculate the result as follows:</p> <pre><code>7 % 3 = 1 =&gt; (1,2,3)[1] = 2 (number modulo 1st group size) 7 / 3 = 2 (int division) (number div 1st group size) 2 % 2 = 0 =&gt; (4,5)[0] = 4 (rest modulo 2nd group size) 2 / 2 = 0 0 % 3 = 0 =&gt; (7,8,9) =&gt; 7 idx g1 g2 g3 0 1 4 6 1 2 4 6 2 3 4 6 3 1 5 6 4 2 5 6 5 3 5 6 6 1 4 7 7 2 4 7 8 3 4 7 9 1 5 7 10 2 5 7 11 3 5 7 12 1 4 8 13 2 4 8 14 3 4 8 15 1 5 8 16 2 5 8 17 3 5 8 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload