/* nag_approx_quantiles_arbitrary (g01apc) Example Program.
 *
 * Copyright 2014 Numerical Algorithms Group.
 *
 * Mark 23, 2011.
 */

#include <nag.h>
#include <stdio.h>
#include <nag_stdlib.h>
#include <nagg01.h>

int main(void)
{
  /* Scalars */
  Integer     exit_status = 0;
  Integer     i, ind, licomm, lrcomm, nb, np, nq, ierr;
  double      eps;
  Nag_Boolean repeat;
  /* Arrays */
  double      *q = 0, *qv = 0, *rcomm = 0, *trcomm = 0, *rv = 0;
  Integer     *icomm = 0, *ticomm = 0;
  /* Nag Types */
  NagError    fail;

  INIT_FAIL(fail);

  printf("nag_approx_quantiles_arbitrary (g01apc) Example Program Results\n");

  /* Skip heading in data file */
  scanf("%*[^\n]");

  /* Read in the problem size */
  scanf("%lf%*[^\n] ", &eps);
  scanf("%ld%*[^\n] ", &nq);

  if (!(qv = NAG_ALLOC(nq, double)) ||
      !(q = NAG_ALLOC(nq, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Read in the quantiles that are required */
  for (i = 0; i < nq; ++i)
    scanf("%lf", &q[i]);
  scanf("%*[^\n]");

  /* Going to be reading in the data in blocks of size 20 */
  nb = 20;

  /* Make an initial allocation to the communication arrays */
  lrcomm = 100;
  licomm = 400;
  if (!(rcomm = NAG_ALLOC(lrcomm, double)) ||
      !(icomm = NAG_ALLOC(licomm, Integer)) ||
      !(rv = NAG_ALLOC(nb, double))) {
    printf("Allocation failure\n");
    exit_status = -1;
    goto END;
  }

  /* Start looping across the data */
  ind = 0;
  repeat = Nag_TRUE;

  while(repeat) {
    /* Read in the blocks of data, each of size nb */
    for (i = 0; i < nb; ++i) {
      ierr = scanf("%lf", &rv[i]);
      if (ierr == EOF || ierr == 0) {
        /* We've read in the last block of data */
        repeat = Nag_FALSE;

        /* Set nb to the size of the last block of data */
        nb = i;
        break;
      }
    }

    /* No data read in, so stop */
    if (nb == 0) break;

    do {
      /* Update the summaries based on the current block of data */
      nag_approx_quantiles_arbitrary(&ind, rv, nb, eps, &np, q, qv,
                                     nq, rcomm, lrcomm, icomm, licomm, &fail);
      if (fail.code != NE_NOERROR) {
        printf(
               "Error from nag_approx_quantiles_arbitrary (g01apc).\n%s\n",
               fail.message);
        exit_status = 1;
        goto END;
      }

      if (ind==2){
        /* At least one of the communication arrays are too small */

        if (lrcomm < icomm[0]) {
          /* Need to make rcomm larger */

          /* Allocate memory a real communication array of the new
             size (held in icomm[0]) */
          if (!(trcomm = NAG_ALLOC(icomm[0], double))) {
            printf("Allocation failure\n");
            exit_status = -1;
            goto END;
          }

          /* Copy the old information into the new array */
          for (i = 0; i < lrcomm; ++i)
            trcomm[i] = rcomm[i];

          /* Set lrcomm to the new size */
          lrcomm = icomm[0];

          /* Free up the old communication array */
          NAG_FREE(rcomm);

          /* Set rcomm to the new array */
          rcomm = trcomm;
        }

        if (licomm < icomm[1]) {
          /* Need to make icomm larger */

          /* Allocate memory to an integer communication array of the new
             size (held in icomm[1]) */
          if (!(ticomm = NAG_ALLOC(icomm[1], Integer))) {
            printf("Allocation failure\n");
            exit_status = -1;
            goto END;
          }

          /* Copy the old information into the new array */
          for (i = 0; i < licomm; ++i)
            ticomm[i] = icomm[i];

          /* Set lrcomm to the new size */
          licomm = icomm[1];

          /* Free up the old communication array */
          NAG_FREE(icomm);

          /* Set icomm to the new array */
          icomm = ticomm;
        }
      }

      /* If ind == 2 then we want to call the routine again, with the same
         block of data */
    } while (ind==2);
  }

  /* Call the routine again to calculate quantiles specified in vector q */
  ind = 3;
  nag_approx_quantiles_arbitrary(&ind, rv, nb, eps, &np, q, qv,
                                 nq, rcomm, lrcomm, icomm, licomm, &fail);
  if (fail.code != NE_NOERROR) {
    printf("Error from nag_approx_quantiles_arbitrary (g01apc).\n%s\n",
           fail.message);
    exit_status = 1;
    goto END;
  }

  /* Print the results */
  printf("\n    Input data:\n");
  printf("    %ld observations\n", np);
  printf("    eps = %5.2f\n", eps);
  printf("    Quantile   Result\n\n");
  for (i = 0; i < nq; ++i) {
    printf("  %7.2f    %7.2f\n", q[i], qv[i]);
  }

 END:
  NAG_FREE(rv);
  NAG_FREE(q);
  NAG_FREE(qv);
  NAG_FREE(rcomm);
  NAG_FREE(icomm);

  return exit_status;
}