/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reedSolomon;

/**
 * This class is used to determine rows of Vandermonde matrix used for backup
 * and restore operations.
 *
 * This special form of matrix helps to evaluate polynomial in each value of
 * finite field.
 *
 * This matrix is intended for use with Galois fields.
 *
 * @author Marcel Duris
 */
public class VandermondeMatrix {

    private int n;
    private int[] xi;
    private int[] row;
    private iField f;
    private boolean first = true;
    int pos = 0;

    /**
     * Prepares Vandermonde matrix over given field.
     * 
     * @param w parameter used to determine size of finite field
     * @see GaloisField
     */
    public VandermondeMatrix(int w) {
        this(w, 1 << w - 1);
    }

    /**
     * Prepares Vandermonde matrix. Limits length of each row.
     *
     * @param w parameter used to determine size of finite field
     * @param n maximum lenght of each row
     * @see GaloisField
     */
    public VandermondeMatrix(int w, int n) {
        this.n = n;
        f = new GaloisField(w);
        xi = new int[n];
        row = new int[n];
        for (int i = 0; i < n; i++) {
            xi[i] = i + 1;
            row[i] = 1;
        }
    }

    /**
     * Returns next row of matrix.
     *
     * @return array representing next row of Vandermonde matrix
     */
    public int[] getNextRow() {
        if (first) {
            first = false;
            return row;
        }
        for (int i = 0; i < n; i++) {
            row[i] = f.multiply(row[i], xi[i]);
        }
        return row;
    }
}
