IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 17, 2023, 4:26:43 PM (3 years ago)
Author:
eugene
Message:

merging from trunk: add STR to vector concat

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20230313/Ohana/src/opihi/cmd.data/concat.c

    r20936 r42514  
    3232  if ((ivec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);
    3333 
    34   // we have 4 cases: (ivec == flt or int) and (ovec == flt or int)
     34  // if the output vector is empty, inherit the type of the input vector
     35  if (!ovec->Nelements) { ResetVector (ovec, ivec->type, ovec->Nelements); }
     36
     37  // we have 3 input and 3 output types : INT, FLT, STR
     38
     39  // both are strings
     40  if ((ovec[0].type == OPIHI_STR) && (ivec[0].type == OPIHI_STR)) {
     41    REALLOCATE (ovec->elements.Str, char *, MAX(1, ovec->Nelements + ivec->Nelements));
     42    for (j = ovec[0].Nelements, i = 0; i < ivec[0].Nelements; i++, j++) {
     43      ovec[0].elements.Str[j] = strcreate(ivec[0].elements.Str[i]);
     44    }
     45    goto done;
     46  }
     47
     48  // if only one is a string, raise an error
     49  if ((ovec[0].type == OPIHI_STR) || (ivec[0].type == OPIHI_STR)) {
     50    gprint (GP_ERR, "ERROR: cannot mix string and numerical vectors\n");
     51    return FALSE;
     52  }
     53
     54  // both vectors are floats
    3555  if ((ovec[0].type == OPIHI_FLT) && (ivec[0].type == OPIHI_FLT)) {
    3656    REALLOCATE (ovec[0].elements.Flt, opihi_flt, ovec[0].Nelements + ivec[0].Nelements);
     
    3858      ovec[0].elements.Flt[j] = ivec[0].elements.Flt[i];
    3959    }
     60    goto done;
    4061  }
     62  // both vectors are ints
     63  if ((ovec[0].type == OPIHI_INT) && (ivec[0].type == OPIHI_INT)) {
     64    REALLOCATE (ovec[0].elements.Int, opihi_int, ovec[0].Nelements + ivec[0].Nelements);
     65    for (j = ovec[0].Nelements, i = 0; i < ivec[0].Nelements; i++, j++) {
     66      ovec[0].elements.Int[j] = ivec[0].elements.Int[i];
     67    }
     68    goto done;
     69  }
     70
     71  // if output vector is a float and input is an int, cast the int values to the float
    4172  if ((ovec[0].type == OPIHI_FLT) && (ivec[0].type == OPIHI_INT)) {
    4273    REALLOCATE (ovec[0].elements.Flt, opihi_flt, ovec[0].Nelements + ivec[0].Nelements);
     
    4475      ovec[0].elements.Flt[j] = ivec[0].elements.Int[i];
    4576    }
    46   }
    47   if ((ovec[0].type == OPIHI_INT) && (ivec[0].type == OPIHI_INT)) {
    48     REALLOCATE (ovec[0].elements.Int, opihi_int, ovec[0].Nelements + ivec[0].Nelements);
    49     for (j = ovec[0].Nelements, i = 0; i < ivec[0].Nelements; i++, j++) {
    50       ovec[0].elements.Int[j] = ivec[0].elements.Int[i];
    51     }
     77    goto done;
    5278  }
    5379
     80  // the output vector is an int and input is a float:
    5481  // this case forces ovec to be raised to FLT
    5582  if ((ovec[0].type == OPIHI_INT) && (ivec[0].type == OPIHI_FLT)) {
     
    6491      ovec[0].elements.Flt[j] = ivec[0].elements.Flt[i];
    6592    }
     93    goto done;
    6694  }
    6795
     96done:
    6897  ovec[0].Nelements += ivec[0].Nelements;
    6998  return (TRUE);
Note: See TracChangeset for help on using the changeset viewer.