IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 23, 2020, 11:34:29 AM (6 years ago)
Author:
eugene
Message:

add string-vector ops

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/stack_math.c

    r41341 r41363  
    272272  if (V1[0].vector[0].Nelements != V2[0].vector[0].Nelements) {
    273273    return (FALSE);
     274  }
     275
     276  if ((V1->vector->type == OPIHI_STR) && (V2->vector->type == OPIHI_STR)) {
     277    int status = LL_binary (OUT, V1, V2, op);
     278    return status;
    274279  }
    275280
     
    13151320  return (TRUE);
    13161321
     1322}
     1323
     1324// vector string OP scalar string (called if V1 is vector, V2 is string)
     1325// V1 MUST be a string-valued vector
     1326int LW_binary (StackVar *OUT, StackVar *V1, StackVar *V2, char *op) {
     1327
     1328  char line[512]; // this is only used to report an error
     1329
     1330  if (V1->vector->type != OPIHI_STR) {
     1331    snprintf (line, 512, "error: binary operation between numerical vector and string not defined");
     1332    push_error (line);
     1333    return (FALSE);
     1334  }
     1335
     1336  /* only 'N' and 'E' are allowed for WW_binary operations. anything else is either a
     1337     syntax error or is a string which looks like a math expression. */
     1338
     1339  if ((op[0] != 'N') && (op[0] != 'E')) {
     1340    snprintf (line, 512, "error: op %c not defined for vector string operations!", op[0]);
     1341    push_error (line);
     1342    return (FALSE);
     1343  }
     1344
     1345  int Nx = V1[0].vector[0].Nelements;
     1346
     1347  OUT[0].vector = InitVector ();
     1348  OUT[0].type = ST_VECTOR_TMP;   /*** <<--- says this is a temporary matrix ***/
     1349
     1350  /* evaluate stack will only call WW_binary with one numerical value,
     1351     and only in the case that the string did not parse to a number
     1352     thus: string == number -> false */
     1353
     1354  // output vector is an integer (result of strcmp test)
     1355  MatchVector (OUT[0].vector, V1[0].vector, OPIHI_INT);
     1356  opihi_int *out = OUT[0].vector[0].elements.Int;
     1357
     1358  // remove the quotes around the name (string value) here
     1359  char *M2 = clean_stack_name(V2[0].name);
     1360
     1361  for (int i = 0; i < Nx; i++, out++) {
     1362    char *M1 =  V1[0].vector[0].elements.Str[i];
     1363    if (op[0] == 'E') {
     1364      //     *out = (M1 == NULL) || strcmp(M1, M2) ? 0 : 1;
     1365      *out = strcmp(M1, M2) ? 0 : 1;
     1366    }
     1367    if (op[0] == 'N') {
     1368      *out = strcmp(M1, M2) ? 1 : 0;
     1369    }
     1370  }
     1371
     1372  clear_stack (V1);
     1373  clear_stack (V2);
     1374  FREE (M2);
     1375
     1376  /* at the end, V1 and V2 are deleted only if they were temporary */
     1377  return (TRUE);
     1378}
     1379
     1380// vector string OP scalar string (called if V2 is vector, V1 is string)
     1381// V2 MUST be a string-valued vector
     1382int WL_binary (StackVar *OUT, StackVar *V1, StackVar *V2, char *op) {
     1383
     1384  char line[512]; // this is only used to report an error
     1385
     1386  if (V2->vector->type != OPIHI_STR) {
     1387    snprintf (line, 512, "error: binary operation between numerical vector and string not defined");
     1388    push_error (line);
     1389    return (FALSE);
     1390  }
     1391
     1392  /* only 'N' and 'E' are allowed for WW_binary operations. anything else is either a
     1393     syntax error or is a string which looks like a math expression. */
     1394
     1395  if ((op[0] != 'N') && (op[0] != 'E')) {
     1396    snprintf (line, 512, "error: op %c not defined for vector string operations!", op[0]);
     1397    push_error (line);
     1398    return (FALSE);
     1399  }
     1400
     1401  int Nx = V2[0].vector[0].Nelements;
     1402
     1403  OUT[0].vector = InitVector ();
     1404  OUT[0].type = ST_VECTOR_TMP;   /*** <<--- says this is a temporary matrix ***/
     1405
     1406  /* evaluate stack will only call WW_binary with one numerical value,
     1407     and only in the case that the string did not parse to a number
     1408     thus: string == number -> false */
     1409
     1410  // output vector is an integer (result of strcmp test)
     1411  MatchVector (OUT[0].vector, V2[0].vector, OPIHI_INT);
     1412  opihi_int *out = OUT[0].vector[0].elements.Int;
     1413
     1414  // remove the quotes around the name (string value) here
     1415  char *M1 = clean_stack_name(V1[0].name);
     1416
     1417  for (int i = 0; i < Nx; i++, out++) {
     1418    char *M2 =  V2[0].vector[0].elements.Str[i];
     1419    if (op[0] == 'E') {
     1420      *out = strcmp(M1, M2) ? 0 : 1;
     1421    }
     1422    if (op[0] == 'N') {
     1423      *out = strcmp(M1, M2) ? 1 : 0;
     1424    }
     1425  }
     1426
     1427  clear_stack (V1);
     1428  clear_stack (V2);
     1429  FREE (M1);
     1430
     1431  /* at the end, V1 and V2 are deleted only if they were temporary */
     1432  return (TRUE);
     1433}
     1434
     1435// vector string OP vector string (called by VV_binary above if both V1 & V2 are vector strings)
     1436int LL_binary (StackVar *OUT, StackVar *V1, StackVar *V2, char *op) {
     1437
     1438  char line[512]; // this is only used to report an error
     1439
     1440  if ((V1->vector->type != OPIHI_STR) || (V2->vector->type != OPIHI_STR)) {
     1441    snprintf (line, 512, "error: binary operations between numerical vector and string vector not defined");
     1442    push_error (line);
     1443    return (FALSE);
     1444  }
     1445
     1446  /* only 'N' and 'E' are allowed for WW_binary operations. anything else is either a
     1447     syntax error or is a string which looks like a math expression. */
     1448
     1449  if ((op[0] != 'N') && (op[0] != 'E')) {
     1450    snprintf (line, 512, "error: op %c not defined for vector string operations!", op[0]);
     1451    push_error (line);
     1452    return (FALSE);
     1453  }
     1454
     1455  int Nx = V1[0].vector[0].Nelements;
     1456
     1457  OUT[0].vector = InitVector ();
     1458  OUT[0].type = ST_VECTOR_TMP;   /*** <<--- says this is a temporary matrix ***/
     1459
     1460  // output vector is an integer (result of strcmp test)
     1461  MatchVector (OUT[0].vector, V1[0].vector, OPIHI_INT);
     1462  opihi_int *out = OUT[0].vector[0].elements.Int;
     1463
     1464  char **M1 =  V1[0].vector[0].elements.Str;
     1465  char **M2 =  V2[0].vector[0].elements.Str;
     1466
     1467  for (int i = 0; i < Nx; i++, out++, M1++, M2++) {
     1468    if (op[0] == 'E') {
     1469      *out = strcmp(*M1, *M2) ? 0 : 1;
     1470    }
     1471    if (op[0] == 'N') {
     1472      *out = strcmp(*M1, *M2) ? 1 : 0;
     1473    }
     1474  }
     1475
     1476  clear_stack (V1);
     1477  clear_stack (V2);
     1478
     1479  /* at the end, V1 and V2 are deleted only if they were temporary */
     1480  return (TRUE);
    13171481}
    13181482
Note: See TracChangeset for help on using the changeset viewer.