package nxm.sys.libm;

import nxm.sys.inc.Constants;

/**
   Performs the Wrap operation on arrays. <br>
   See {@link Operator} for a description of the available I/O Modes.
   @author  Auto-Generated
   @see     Operator
*/
@SuppressWarnings("cast")
public final class Wrap extends Operator implements Constants {

  /* Parameter declarations. */
  int klast; double vlast;

  /** Create a new instance. */
  public Wrap() {
    super();
  }

  /** Create a new instance.
      @param flags The processing flags to use.
   */
  public Wrap (int flags) {
    super(flags);
  }

  /** Create a new instance.
      @param scale The scale factor to use.
   */
  public Wrap (double scale) {
    super(scale);
  }

  /** Create a new instance.
      @param flags The processing flags to use.
      @param scale The scale factor to use.
   */
  public Wrap (int flags, double scale) {
    super(flags,scale);
  }

  public final int validate (char type, int n, int mode) {
    switch (mode) {
      case SS: return n;
      default: return NOSUPPORT;
    }
  }

  public final int process (double[][] v, int n, int mode) {
    switch (mode) {
      case SS: SS(v[0],v[1],n, scale); return n;
      default:  return NOSUPPORT;
    }
  }

  public final int process (double[][] v, int n, int mode, double scale) {
    switch (mode) {
      case SS: SS(v[0],v[1],n, scale); return n;
      default:  return NOSUPPORT;
    }
  }

  /** Apply this operator to mode {@link Operator#SS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param n     The number of elements to operate on.
      @param scale The scale factor.
      @see #process(double[][],int,int,double)
   */
  public final void SS (double[] a, double[] b, int n, double scale) {
    double v1, vd, v2=(double)vlast, s=(double)scale, ss=s+s; int kw=klast;
      for (int i=0; i<n; i++) {
        v1=v2; v2=a[i]; vd=v2-v1; if(vd<-s) kw++; else if (vd>s) kw--; b[i]=v2+kw*ss;
      }
    vlast=(double)v2; klast=kw;
  }

  public final int process (float[][] v, int n, int mode) {
    switch (mode) {
      case SS: SS(v[0],v[1],n, scale); return n;
      default:  return NOSUPPORT;
    }
  }

  public final int process (float[][] v, int n, int mode, double scale) {
    switch (mode) {
      case SS: SS(v[0],v[1],n, scale); return n;
      default:  return NOSUPPORT;
    }
  }

  /** Apply this operator to mode {@link Operator#SS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param n     The number of elements to operate on.
      @param scale The scale factor.
      @see #process(float[][],int,int,double)
   */
  public final void SS (float[] a, float[] b, int n, double scale) {
    float v1, vd, v2=(float)vlast, s=(float)scale, ss=s+s; int kw=klast;
      for (int i=0; i<n; i++) {
        v1=v2; v2=a[i]; vd=v2-v1; if(vd<-s) kw++; else if (vd>s) kw--; b[i]=v2+kw*ss;
      }
    vlast=(double)v2; klast=kw;
  }

  public final int process (int[][] v, int n, int mode) {
    switch (mode) {
      case SS: SS(v[0],v[1],n, scale); return n;
      default:  return NOSUPPORT;
    }
  }

  public final int process (int[][] v, int n, int mode, double scale) {
    switch (mode) {
      case SS: SS(v[0],v[1],n, scale); return n;
      default:  return NOSUPPORT;
    }
  }

  /** Apply this operator to mode {@link Operator#SS} data.
      @param a     The first  operand.
      @param b     The second  operand.
      @param n     The number of elements to operate on.
      @param scale The scale factor.
      @see #process(int[][],int,int,double)
   */
  public final void SS (int[] a, int[] b, int n, double scale) {
    int v1, vd, v2=(int)vlast, s=(int)scale, ss=s+s; int kw=klast;
      for (int i=0; i<n; i++) {
        v1=v2; v2=a[i]; vd=v2-v1; if(vd<-s) kw++; else if (vd>s) kw--; b[i]=v2+kw*ss;
      }
    vlast=(double)v2; klast=kw;
  }

  public int getInputs() { return 1; }
  public int getOutputs() { return 1; }
}
