/**
  *
  * Some utilities for JaCoP programs.
  *
  * Hakan Kjellerstrand (hakank@bonetmail.com)
  * See http://www.hakank.org/JaCoP/
  *
  */
import JaCoP.constraints.*;
import JaCoP.core.*;
import JaCoP.search.*;

import java.io.*;
import java.util.*;
import java.text.*;

public class HakankUtil {


    // integer power method
    static int pow( int x, int y) {
        int z = x; 
        for( int i = 1; i < y; i++ ) z *= x;
        return z;
    } // end pow


    // calculating the weights for SumWeights
    public static int[] getWeights(IntVar[] numArray, IntVar num, int base) {
        int n = numArray.length;
        int[] weights = new int[n];
        int w = 1;
        for(int i = 0; i < n; i++) {
            weights[n-i-1] = w;
            w *= base;
        }
        return weights;
        
    } // end getWeights



    //
    // prints all solutions
    //
    public static void printAllSolutions(Search label) {

        System.out.println("\nAll solutions: \n");
        int numSolutions = label.getSolutionListener().solutionsNo();
        System.out.println("Number of Solutions: " + numSolutions);
        for(int s = 1; s <= numSolutions; s++) {
            Domain [] res = label.getSolutionListener().getSolution(s);
            for(int i = 0; i < res.length; i++) {
                System.out.print(res[i] + " ");
            }
            System.out.println();
        }

    }


    //
    // prints all solutions as square matrices
    //
    public static void printAllSolutionsMatrix(Search label) {

        System.out.println("\nAll solutions: \n");
        int numSolutions = label.getSolutionListener().solutionsNo();
        System.out.println("Number of Solutions: " + numSolutions);
        for(int s = 1; s <= numSolutions; s++) {

            Domain [] res = label.getSolutionListener().getSolution(s);

            int rows = (int)Math.sqrt(res.length) ;
            // printMatrix(res, rows, rows);
            System.out.println(res);
            System.out.println();
        }

    }


    //
    // prints a (square) variable matrix
    //
    public static void printMatrix(IntVar[][] y, int rows, int cols) {

        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                System.out.print(y[i][j].value()+ " ");
            }
            System.out.println();
        }

    } // end printMatrix


    //
    // prints an (square) int matrix
    //
    public static void printMatrix(int[] y, int rows, int cols) {

        for(int i = 0; i < rows; i++) {
            for(int j = 0; j < cols; j++) {
                System.out.print(y[rows*i + j] + " ");
            }
            System.out.println();
        }

    } // end printMatrix



    /*
     * From ExampleJaCoP/Example.java
     *
     * It saves current problem in XML. First parameter
     * noSolutions specifies number of solutions. If number
     * of solutions is not known then use value -1. Path can 
     * contain strings like "./". 
     */
     /*
    public static void toXML(Store store, int noSolutions, String path, String name) {
        store.toXCSP2_0();
        store.finalizeXCSP2_0(noSolutions, path, name);	
        System.out.println("Run with: java LoadSolveXML " + path + "/" + name);
    }
     */

    public static void main(String args[]) {


    }
}

