# include "data.h"

int uniq (int argc, char **argv) {
  
  int Nnew, i, j, found;
  Vector *ivec, *ovec;

  if (argc != 3) {
    gprint (GP_ERR, "USAGE: uniq (in) (out)\n");
    return (FALSE);
  }

  if ((ivec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
  if ((ovec = SelectVector (argv[2], ANYVECTOR, TRUE)) == NULL) return (FALSE);

  /* allocate the maximum possible needed */
  ResetVector (ovec, ivec->type, ivec->Nelements);

  Nnew = 0;

  if (ivec->type == OPIHI_FLT) {
    opihi_flt *v1 = ivec[0].elements.Flt;
    for (i = 0; i < ivec[0].Nelements; i++, v1++) {
      opihi_flt *v2 = ovec[0].elements.Flt;
      found = FALSE;
      for (j = 0; !found && (j < Nnew); j++, v2++) {
	if (*v1 == *v2) found = TRUE;
      }
      if (!found) {
	ovec[0].elements.Flt[Nnew] = *v1;
	Nnew ++;
      }
    }
  } else {
    opihi_int *v1 = ivec[0].elements.Int;
    for (i = 0; i < ivec[0].Nelements; i++, v1++) {
      opihi_int *v2 = ovec[0].elements.Int;
      found = FALSE;
      for (j = 0; !found && (j < Nnew); j++, v2++) {
	if (*v1 == *v2) found = TRUE;
      }
      if (!found) {
	ovec[0].elements.Int[Nnew] = *v1;
	Nnew ++;
      }
    }
  }

  // free up extra memory
  ResetVector (ovec, ivec->type, Nnew);

  return (TRUE);
}

