[Kst] branches/work/kst/1.6/kst/src/plugins/bin
Andrew Walker
arwalker at sumusltd.com
Thu Sep 27 00:30:46 CEST 2007
SVN commit 717472 by arwalker:
avoid invalid values or crash when bin size is longer than input vector size
M +31 -22 bin.cpp
--- branches/work/kst/1.6/kst/src/plugins/bin/bin.cpp #717471:717472
@@ -40,36 +40,45 @@
}
-//Bin the elements into the given size bins, additional elements at the end of the
-//input vector are ignored.
-//Returns -1 on error, 0 on success.
+// bin the elements into the given size bins
+// additional elements at the end of the input vector are ignored
bool Bin::algorithm() {
- KstVectorPtr input = inputVector(INPUT);
- KstScalarPtr size = inputScalar(SIZE);
- KstVectorPtr bins = outputVector(BINS);
+ KstVectorPtr input = inputVector(INPUT);
+ KstScalarPtr size = inputScalar(SIZE);
+ KstVectorPtr bins = outputVector(BINS);
+ bool rc = false;
- //Make sure there is at least 1 element in the input vector
- //Make sure the bin size is at least 1
- if (input->length() < 1 || size->value() < 1.0) {
- return -1;
- }
+ if (input->length() > 0) {
+ int binSize = (int)size->value();
- // allocate the lengths
- bins->resize(int(input->length() / size->value()), false);
+ if (binSize < 1.0) {
+ binSize = 1.0;
+ }
+ if (binSize > input->length()) {
+ binSize = input->length();
+ }
- //now bin the data
- for (int i=0; i<bins->length(); i++) {
- bins->value()[i] = 0.0;
+ // allocate the lengths
+ bins->resize(input->length() / binSize, false);
- //add up the elements for this bin
- for (int j=0; j<size->value(); j++) {
- bins->value()[i] += input->value()[int(i*size->value()+j)];
+ // now bin the data
+ for (int i=0; i<bins->length(); i++) {
+ bins->value()[i] = 0.0;
+
+ // add up the elements for this bin
+ for (int j=0; j<binSize; j++) {
+ bins->value()[i] += input->value()[i*binSize+j];
+ }
+
+ // find the mean
+ bins->value()[i] /= (double)binSize;
}
- //find the mean
- bins->value()[i] /= size->value();
+
+ rc = true;
}
- return true;
+
+ return rc;
}
More information about the Kst
mailing list