# include "addstar.h"

/* build the initial links assuming the table is sorted, 
   not partial, and has a correct set of average[].offset,Nm values */
int *init_measure_links (Average *average, int Naverage, Measure *measure, int Nmeasure) {

  int i, j, N;
  int *next;

  N = 0;

  ALLOCATE (next, int, Nmeasure);
  for (i = 0; i < Naverage; i++, N++) {
    for (j = 0; j < average[i].Nm - 1; j++, N++) {
      next[N] = N + 1;
      if (N >= Nmeasure) {
	fprintf (stderr, "WARNING: N out of bounds (1)\n");
      }
    }
    next[N] = -1;
    if (N >= Nmeasure) {
      fprintf (stderr, "WARNING: N out of bounds (2)\n");
    }

    if (N >= Nmeasure) {
      fprintf (stderr, "overflow in init_measure_links\n");
      abort ();
    }
  }
  return (next);
}

/* build the initial links assuming the table is sorted */
int *init_missing_links (Average *average, int Naverage, Missing *missing, int Nmissing) {

  int i, j, N;
  int *next;

  N = 0;

  ALLOCATE (next, int, Nmissing);
  for (i = 0; i < Naverage; i++) {
    for (j = 0; j < average[i].Nn - 1; j++, N++) {
      next[N] = N + 1;
    }
    if (average[i].Nn > 0) {
      next[N] = -1;
      if (N >= Nmissing) {
	fprintf (stderr, "overflow in init_missing_links");
	abort ();
      }
      N++;
    }

  }
  return (next);
}

/* average[].offset, average[].Nm are valid within an addstar run */
int add_meas_link (Average *average, int *next, int Nmeasure, int NMEASURE) {

  int k, m;

  /* if we have trouble, check validity of next[m] : m < Nmeasure */
  m = average[0].offset;  

  for (k = 0; k < average[0].Nm - 1; k++)  {
    m = next[m];
    if (m >= NMEASURE) {
      fprintf (stderr, "WARNING: m out of bounds (3)\n");
    }
  }

  /* set up references */
  next[Nmeasure] = -1;
  if (Nmeasure >= NMEASURE) {
    fprintf (stderr, "WARNING: Nmeasure out of bounds (1)\n");
  }

  if (m == -1) {
    average[0].offset = Nmeasure;
  } else {
    next[m] = Nmeasure;
    if (m >= NMEASURE) {
      fprintf (stderr, "WARNING: m out of bounds (4)\n");
    }
  }

  return (TRUE);
}

int add_miss_link (Average *average, int *next, int Nmissing) {

  int k, m;

  /* there may be 0 Nmiss; this is not true for Nmeas */
  if (average[0].Nn < 1) {
    average[0].missing = Nmissing;
    next[Nmissing] = -1;
    return (TRUE);
  }

  m = average[0].missing;  
  for (k = 0; k < average[0].Nn - 1; k++) m = next[m];
  /* set up references */
  next[Nmissing] = -1;
  next[m] = Nmissing;
  return (TRUE);
}

/* construct measure links which are valid FOR THIS LOAD
 * - if we have a full load, we will get links which can
 *   be used by other programs (eg, relphot, etc)
 * - if we have a partial load, the links are only valid
 *   for that partial load
 */ 

int *build_measure_links (Average *average, int Naverage, Measure *measure, int Nmeasure) {

  int i, m, k, Nm, averef;
  int *next;

  ALLOCATE (next, int, Nmeasure);

  /* reset the Nm, offset values for average */
  for (i = 0; i < Naverage; i++) {
    average[i].offset = -1;
    average[i].Nm     =  0;
  }

  for (Nm = 0; Nm < Nmeasure; Nm++) {
    averef = measure[Nm].averef;
    m = average[averef].offset;  
    next[Nm] = -1;

    if (m == -1) { /* no links yet for source */
      average[averef].offset = Nm;
      average[averef].Nm     = 1;
      continue;
    }

    for (k = 0; next[m] != -1; k++) {
      m = next[m];
      if (m >= Nmeasure) {
	fprintf (stderr, "WARNING: m out of bounds (1)\n");
      }
    }

    average[averef].Nm = k + 2;
    next[m] = Nm;
    if (m >= Nmeasure) {
      fprintf (stderr, "WARNING: m out of bounds (2)\n");
    }
  }
  return (next);
}

/* Missing does not carry enough information to reconstruct the links
   we must always save the missing table, if it exists */

Measure *sort_measure (Average *average, int Naverage, Measure *measure, int Nmeasure, int *next) {

  int i, k, n, N;
  Measure *tmpmeasure;

  /* fix order of Measure (memory intensive, but fast) */
  N = 0; 
  ALLOCATE (tmpmeasure, Measure, Nmeasure);
  for (i = 0; i < Naverage; i++) {
    n = average[i].offset;
    average[i].offset = N;
    for (k = 0; k < average[i].Nm; k++, N++) {
      tmpmeasure[N] = measure[n]; 
      tmpmeasure[N].averef = i;
      n = next[n];
    }
  }
  free (measure);
  return (tmpmeasure);
}

Missing *sort_missing (Average *average, int Naverage, Missing *missing, int Nmissing, int *next) {

  int i, k, n, N;
  Missing *tmpmissing;

  /* fix order of Missing (memory intensive, but fast) */
  N = 0; 
  ALLOCATE (tmpmissing, Missing, Nmissing);
  for (i = 0; i < Naverage; i++) {
    n = average[i].missing;
    average[i].missing = N;
    for (k = 0; k < average[i].Nn; k++, N++) {
      tmpmissing[N] = missing[n]; 
      n = next[n];
    }
  }
  free (missing);
  return (tmpmissing);
}
