Fix issue with compress record

The handling of N-to-M array compression was broken with the addition
of the partial buffer option, which broke the bounds check that was
being used.

Note that this also makes the partial buffer option more consistent;
if, for example, you have
```
record(compress, foo) {
  field(ALG, "N to 1 Average")
  field(INP, "bar NPP")
  field(NSAM, 2)
  field(N, 2)
  field(PBUF, YES)
}
```
(with `bar` having, e.g. length 3), then this will now behave as
expected on both of the samples.
This commit is contained in:
Simon Rose
2024-03-14 09:29:11 +01:00
committed by Andrew Johnson
parent 4966baf423
commit beec00b403
2 changed files with 103 additions and 43 deletions

View File

@@ -149,13 +149,16 @@ static int compare(const void *arg1, const void *arg2)
else return 1;
}
#define min(a, b) ((a) < (b) ? (a) : (b))
static int compress_array(compressRecord *prec,
double *psource, int no_elements)
{
epicsInt32 i,j;
epicsInt32 j;
epicsInt32 n, nnew;
epicsInt32 nsam = prec->nsam;
double value;
epicsUInt32 samples_written = 0;
double value = 0.0;
/* skip out of limit data */
if (prec->ilil < prec->ihil) {
@@ -167,61 +170,54 @@ static int compress_array(compressRecord *prec,
}
if (prec->n <= 0)
prec->n = 1;
if (no_elements < prec->n && prec->pbuf != menuYesNoYES)
return 1; /*dont do anything*/
n = no_elements;
n = prec->n;
/* determine number of samples to take */
if (no_elements < nsam * n)
nnew = (no_elements / n);
else nnew = nsam;
nnew = min(no_elements, nsam * n);
/* compress according to specified algorithm */
switch (prec->alg){
case compressALG_N_to_1_Low_Value:
/* compress N to 1 keeping the lowest value */
for (i = 0; i < nnew; i++) {
while (nnew > 0)
{
if (nnew < n && prec->pbuf != menuYesNoYES)
break;
n = min(n, nnew);
switch (prec->alg)
{
case compressALG_N_to_1_Low_Value:
value = *psource++;
for (j = 1; j < n; j++, psource++) {
for (j = 1; j < n; j++, psource++)
{
if (value > *psource)
value = *psource;
}
put_value(prec, &value, 1);
}
break;
case compressALG_N_to_1_High_Value:
/* compress N to 1 keeping the highest value */
for (i = 0; i < nnew; i++){
break;
case compressALG_N_to_1_High_Value:
value = *psource++;
for (j = 1; j < n; j++, psource++) {
for (j = 1; j < n; j++, psource++)
{
if (value < *psource)
value = *psource;
}
put_value(prec, &value, 1);
}
break;
case compressALG_N_to_1_Average:
/* compress N to 1 keeping the average value */
for (i = 0; i < nnew; i++) {
value = 0;
for (j = 0; j < n; j++, psource++)
break;
case compressALG_N_to_1_Average:
value = *psource++;
for (j = 1; j < n; j++, psource++)
{
value += *psource;
value /= n;
put_value(prec, &value, 1);
}
break;
case compressALG_N_to_1_Median:
/* compress N to 1 keeping the median value */
/* note: sorts source array (OK; it's a work pointer) */
for (i = 0; i < nnew; i++, psource += nnew) {
}
value = value / n;
break;
case compressALG_N_to_1_Median:
/* note: sorts source array (OK; it's a work pointer) */
qsort(psource, n, sizeof(double), compare);
value = psource[n / 2];
put_value(prec, &value, 1);
psource += n;
break;
}
break;
nnew -= n;
put_value(prec, &value, 1);
samples_written++;
}
return 0;
return (samples_written == 0);
}
static int array_average(compressRecord *prec,