package com.github.jesg.dither;

/*
 * #%L
 * dither
 * %%
 * Copyright (C) 2015 Jason Gowan
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class CombinatoricHelper {

    public static List<int[]> getCombinations(final int n, final int[] input) {
        final List<int[]> result = new ArrayList<int[]>();
        final int[] scratch = new int[n];

        if (n <= input.length) {
            for (int i = 0; (scratch[i] = i) < n - 1; i++)
                ;
            result.add(_getCombinations(input, scratch));
            for (;;) {
                int i;
                for (i = n - 1; i >= 0 && scratch[i] == input.length - n + i; i--)
                    ;
                if (i < 0) {
                    break;
                } else {
                    scratch[i]++;
                    for (++i; i < n; i++) {
                        scratch[i] = scratch[i - 1] + 1;
                    }
                    result.add(_getCombinations(input, scratch));
                }
            }
        }

        return result;
    }

    private static int[] _getCombinations(final int[] input, final int[] scratch) {
        final int[] result = new int[scratch.length];
        for (int i = 0; i < scratch.length; i++) {
            result[i] = input[scratch[i]];
        }
        return result;
    }


    public static <T> List<List<T>> product(final T[][] args) {
        final int[] tmp = new int[args.length];
        for(int i = 0; i < tmp.length; i++) {
            tmp[i] = args[i].length;
        }

        final int[][] solution = product(tmp);
        final List<List<T>> results = new ArrayList<List<T>>(solution.length);
        for(int i = 0; i < solution.length; i++) {
            final List<T> inner = new ArrayList<T>(args.length);
            results.add(i, inner);
            for(int j = 0; j < args.length; j++) {
                results.get(i).add(j, args[j][solution[i][j]]);
            }
        }
        return results;
    }

    public static int[][] product(final int[] input) {
        int[][] result = _product(input[0], input[1]);
        for (int i = 2; i < input.length; i++) {
            result = _product(result, input[i]);
        }
        return result;
    }

    private static int[][] _product(final int left, final int right) {
        final int[][] result = new int[left * right][];
        int k = 0;
        for (int i = 0; i < left; i++) {
            for (int j = 0; j < right; j++) {
                result[k] = new int[] { i, j };
                k++;
            }
        }
        return result;
    }

    private static int[][] _product(final int[][] left, final int right) {
        final int[][] result = new int[left.length * right][];
        int k = 0;
        for (int i = 0; i < left.length; i++) {
            for (int j = 0; j < right; j++) {
                result[k] = Arrays.copyOf(left[i], left[i].length + 1);
                result[k][left[i].length] = j;
                k++;
            }
        }
        return result;
    }
}

@@ -2615,32 +2615,181 @@
 l int%5B%5D 
-input) %7B%0A       
+ranges) %7B%0A        int length = 1;%0A        for(int i = 0; i %3C ranges.length; i++) %7B%0A            length *= ranges%5Bi%5D;%0A            --ranges%5Bi%5D;%0A        %7D%0A%0A        final
  int%5B%5D%5B%5D
@@ -2799,39 +2799,159 @@
 sult
+s
  = 
-_product(input%5B0%5D, input%5B1%5D)
+new int%5Blength%5D%5Branges.length%5D;%0A        final int%5B%5D scratch = new int%5Branges.length%5D;%0A%0A        int k = 0;%0A        final int max = ranges.length - 1
 ;%0A  
@@ -2951,33 +2951,32 @@
 - 1;%0A        for
- 
 (int i = 2; i %3C 
@@ -2972,20 +2972,151 @@
 i = 
-2; i %3C input
+max;;) %7B%0A%0A            if(i == max) %7B%0A                for(int val = 0; val %3C= ranges%5Bi%5D; val++) %7B%0A                    for(int j = 0; j %3C scratch
 .len
@@ -3112,33 +3112,33 @@
 scratch.length; 
-i
+j
 ++) %7B%0A          
@@ -3143,44 +3143,258 @@
     
-result = _product(result, input
+            results%5Bk%5D%5Bj%5D = scratch%5Bj%5D;%0A                    %7D%0A                    k++;%0A                    scratch%5Bi%5D++;%0A                %7D%0A                scratch%5Bi%5D = 0;%0A                i--;%0A            %7D else if(i == 0 && scratch%5Bi%5D %3E= ranges
 %5Bi%5D)
-;
+ %7B
 %0A   
@@ -3390,34 +3390,32 @@
 s%5Bi%5D) %7B%0A        
-%7D%0A
         return r
@@ -3411,33 +3411,232 @@
    return result
-;
+s;%0A            %7D else if(scratch%5Bi%5D %3C ranges%5Bi%5D) %7B%0A                scratch%5Bi%5D++;%0A                i++;%0A            %7D else %7B%0A                scratch%5Bi%5D = -1;%0A                i--;%0A            %7D%0A        %7D
 %0A    %7D%0A%0A    priv

improve product