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[] ranges) {
        int length = 1;
        for(int i = 0; i < ranges.length; i++) {
            length *= ranges[i];
            --ranges[i];
        }

        final int[][] results = new int[length][ranges.length];
        final int[] scratch = new int[ranges.length];

        int k = 0;
        final int max = ranges.length - 1;
        for(int i = max;;) {

            if(i == max) {
                for(int val = 0; val <= ranges[i]; val++) {
                    for(int j = 0; j < scratch.length; j++) {
                        results[k][j] = scratch[j];
                    }
                    k++;
                    scratch[i]++;
                }
                scratch[i] = 0;
                i--;
            } else if(i == 0 && scratch[i] >= ranges[i]) {
                return results;
            } else if(scratch[i] < ranges[i]) {
                scratch[i]++;
                i++;
            } else {
                scratch[i] = -1;
                i--;
            }
        }
    }

    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;
    }
}

@@ -692,33 +692,8 @@
 st;%0A
-import java.util.Arrays;%0A
 impo
@@ -3604,808 +3604,7 @@
    %7D
-%0A%0A    private static int%5B%5D%5B%5D _product(final int left, final int right) %7B%0A        final int%5B%5D%5B%5D result = new int%5Bleft * right%5D%5B%5D;%0A        int k = 0;%0A        for (int i = 0; i %3C left; i++) %7B%0A            for (int j = 0; j %3C right; j++) %7B%0A                result%5Bk%5D = new int%5B%5D %7B i, j %7D;%0A                k++;%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    private static int%5B%5D%5B%5D _product(final int%5B%5D%5B%5D left, final int right) %7B%0A        final int%5B%5D%5B%5D result = new int%5Bleft.length * right%5D%5B%5D;%0A        int k = 0;%0A        for (int i = 0; i %3C left.length; i++) %7B%0A            for (int j = 0; j %3C right; j++) %7B%0A                result%5Bk%5D = Arrays.copyOf(left%5Bi%5D, left%5Bi%5D.length + 1);%0A                result%5Bk%5D%5Bleft%5Bi%5D.length%5D = j;%0A                k++;%0A            %7D%0A        %7D%0A        return result;%0A    %7D
 %0A%7D%0A

Remove unused private methods