[Kst] extragear/graphics/kst
Adam Treat
treat at kde.org
Wed Oct 18 22:51:25 CEST 2006
SVN commit 596909 by treat:
*Modify testcases as per barth.
*Refactor rvector to pass tests and clean up the source code
M +41 -48 src/libkst/kstrvector.cpp
M +0 -2 src/libkst/kstrvector.h
M +11 -7 tests/testrvector.cpp
--- trunk/extragear/graphics/kst/src/libkst/kstrvector.cpp #596908:596909
@@ -121,7 +121,6 @@
int in_n, int in_skip, bool in_DoSkip,
bool in_DoAve) {
_saveable = true;
- _dontUseSkipAccel = false;
_numSamples = 0;
_scalars["sum"]->setValue(0.0);
_scalars["sumsquared"]->setValue(0.0);
@@ -170,7 +169,6 @@
Skip = 1;
}
- _dontUseSkipAccel = false;
_file = in_file;
ReqF0 = in_f0;
ReqNF = in_n;
@@ -370,7 +368,6 @@
void KstRVector::reset() { // must be called with a lock
- _dontUseSkipAccel = false;
if (_file) {
SPF = _file->samplesPerFrame(_field);
}
@@ -428,11 +425,6 @@
KstObject::UpdateType KstRVector::doUpdate(bool force) {
- int i, k, shift, n_read=0;
- int ave_nread;
- int new_f0, new_nf;
- bool start_past_eof = false;
-
checkIntegrity();
if (DoSkip && Skip < 2 && SPF == 1) {
@@ -443,12 +435,15 @@
return NO_CHANGE;
}
+ int new_f0, new_nf, new_size;
+ bool start_past_eof = false;
+
// set new_nf and new_f0
int fc = _file->frameCount(_field);
- if (ReqNF < 1) { // read to end of file
+ if (readToEOF()) { // read to end of file
new_f0 = ReqF0;
new_nf = fc - new_f0;
- } else if (ReqF0 < 0) { // count back from end of file
+ } else if (countFromEOF()) { // count back from end of file
new_nf = fc;
if (new_nf > ReqNF) {
new_nf = ReqNF;
@@ -469,11 +464,12 @@
}
if (DoSkip) {
- // change new_f0 and new_nf so they both lie on skip boundaries
+ // change new_nf so it lies on the skip boundary
if (new_f0 != 0) {
new_f0 = ((new_f0-1)/Skip+1)*Skip;
}
- new_nf = (new_nf/Skip)*Skip;
+ new_nf = int((floor(double(fc - (new_f0 + 1)) / (double)Skip) * (double)Skip) + 1);
+ new_size = new_nf / Skip + 1;
}
if (NF == new_nf && F0 == new_f0 && !force) {
@@ -484,6 +480,8 @@
if (new_f0 < F0 || new_f0 >= F0 + NF) { // No useful data around.
reset();
} else { // shift stuff rather than re-read
+ int shift = 0;
+
if (DoSkip) {
shift = (new_f0 - F0)/Skip;
NF -= (new_f0 - F0);
@@ -495,49 +493,43 @@
}
// FIXME: use memmove()
- for (i = 0; i < _numSamples; i++) {
+ for (int i = 0; i < _numSamples; ++i) {
_v[i] = _v[i+shift];
}
}
+ int n_read=0;
+
if (DoSkip) {
+
// reallocate V if necessary
//kstdDebug() << "new_nf = " << new_nf << " and skip = " << Skip << " so new_nf/Skip+1 = " << (new_nf / Skip + 1) << endl;
- if (new_nf / Skip != _size) {
- bool rc = resize(new_nf/Skip);
- if (!rc) {
- // FIXME: handle failed resize
+ if (new_size != _size) {
+ if (!resize(new_size)) {
+ abort();
}
}
- // for debugging: _dontUseSkipAccel = true;
- if (!_dontUseSkipAccel) {
- int rc;
+
+ // for debugging: useSkipAccel = false;
+ bool useSkipAccel = DoAve ? false : true;
+
+ if (useSkipAccel) {
int lastRead = -1;
- if (DoAve) {
- // We don't support boxcar inside data sources yet.
- _dontUseSkipAccel = true;
+ int rc = _file->readField(_v + _numSamples, _field, new_f0, (new_nf - NF)/Skip, Skip, &lastRead);
+ if (rc == -9999) {
+ useSkipAccel = false;
} else {
- rc = _file->readField(_v + _numSamples, _field, new_f0, (new_nf - NF)/Skip, Skip, &lastRead);
- if (rc != -9999) {
- //kstdDebug() << "USED SKIP FOR READ - " << _field << " - rc=" << rc << " for Skip=" << Skip << " s=" << new_f0 << " n=" << (new_nf - NF)/Skip << endl;
- if (rc >= 0) {
- n_read = rc;
- } else {
- n_read = 0;
- }
- } else {
- _dontUseSkipAccel = true;
- }
+ //kstdDebug() << "USED SKIP FOR READ - " << _field << " - rc=" << rc << " for Skip=" << Skip << " s=" << new_f0 << " n=" << (new_nf - NF)/Skip << endl;
+ n_read = rc > 0 ? rc : 0; //FIXME is rc ever less than zero at this point?
}
}
- if (_dontUseSkipAccel) {
- n_read = 0;
- /** read each sample from the File */
+
+ if (!useSkipAccel) {
//kstdDebug() << "NF = " << NF << " numsamples = " << _numSamples << " new_f0 = " << new_f0 << endl;
double *t = _v + _numSamples;
int new_nf_Skip = new_nf - Skip;
if (DoAve) {
- for (i = NF; new_nf_Skip >= i; i += Skip) {
+ for (int i = NF; new_nf_Skip >= i; i += Skip) {
/* enlarge AveReadBuf if necessary */
if (N_AveReadBuf < Skip*SPF) {
N_AveReadBuf = Skip*SPF;
@@ -546,8 +538,8 @@
// FIXME: handle failed resize
}
}
- ave_nread = _file->readField(AveReadBuf, _field, new_f0+i, Skip);
- for (k = 1; k < ave_nread; k++) {
+ int ave_nread = _file->readField(AveReadBuf, _field, new_f0+i, Skip);
+ for (int k = 1; k < ave_nread; k++) {
AveReadBuf[0] += AveReadBuf[k];
}
if (ave_nread > 0) {
@@ -557,18 +549,20 @@
++t;
}
} else {
- for (i = NF; new_nf_Skip >= i; i += Skip) {
- //kstdDebug() << "readField " << _field << " start=" << new_f0 + i << " n=-1" << endl;
- n_read += _file->readField(t++, _field, new_f0 + i, -1);
+ int index = new_f0; //begin
+ for (int i = 0; i < new_size; ++i) {
+ //kstdDebug() << "readField " << _field << " start=" << index << " n=-1" << endl;
+ n_read += _file->readField(t++, _field, index, -1);
+ index += Skip;
}
}
}
+
} else {
+
// reallocate V if necessary
if ((new_nf - 1)*SPF + 1 != _size) {
- bool rc = resize((new_nf - 1)*SPF + 1);
- if (!rc) {
- // FIXME: handle failed resize
+ if (!resize((new_nf - 1)*SPF + 1)) {
abort();
}
}
@@ -612,7 +606,7 @@
if (_numSamples != _size && !(_numSamples == 0 && _size == 1)) {
//kstdDebug() << "SET DIRTY since _numSamples = " << _numSamples << " but _size = " << _size << endl;
_dirty = true;
- for (i = _numSamples; i < _size; i++) {
+ for (int i = _numSamples; i < _size; i++) {
_v[i] = _v[0];
}
}
@@ -671,7 +665,6 @@
_file->unlock();
KST::dataSourceList.lock().writeLock();
KST::dataSourceList.remove(_file);
- _dontUseSkipAccel = false;
_file = newsrc;
_file->writeLock();
KST::dataSourceList.append(_file);
--- trunk/extragear/graphics/kst/src/libkst/kstrvector.h #596908:596909
@@ -163,8 +163,6 @@
void reset(); // must be called with a lock
void checkIntegrity(); // must be called with a lock
-
- bool _dontUseSkipAccel;
};
typedef KstSharedPtr<KstRVector> KstRVectorPtr;
--- trunk/extragear/graphics/kst/tests/testrvector.cpp #596908:596909
@@ -323,22 +323,23 @@
doTestV("countFromEOF", false, rvp->countFromEOF());
doTestV("doSkip", true, rvp->doSkip());
doTestV("skip", 5, rvp->skip());
+ doTestV("doAve", false, rvp->doAve());
}
{
//Skip FIVE frames...
KstRVectorPtr rvp = new KstRVector(dsp, "INDEX", "RVTestDirfile", 3, -1, 5, true, false);
rvp->update(0);
- //We should have length equal to three... items {3, 8, 13}
- doTestV("length", 3, rvp->length());
- doTestV("item #0", 3, rvp->value(0));
- doTestV("item #1", 8, rvp->value(1));
- doTestV("item #2", 13, rvp->value(2));
+ //We should have length equal to three... items {5, 10}
+ doTestV("length", 2, rvp->length());
+ doTestV("item #0", 5, rvp->value(0));
+ doTestV("item #1", 10, rvp->value(1));
+ //doTestV("item #2", 13, rvp->value(2));
//The numFrames should still report 11 as it lies on the skip boundary
- doTestV("numFrames", 11, rvp->numFrames());
+ doTestV("numFrames", 6, rvp->numFrames());
//The startFrame should report 3 as it lies on the skip boundary and was requested
- doTestV("startFrame", 3, rvp->startFrame());
+ doTestV("startFrame", 5, rvp->startFrame());
doTestV("reqNumFrames", -1, rvp->reqNumFrames());
doTestV("reqStartFrame", 3, rvp->reqStartFrame());
@@ -347,6 +348,7 @@
doTestV("countFromEOF", false, rvp->countFromEOF());
doTestV("doSkip", true, rvp->doSkip());
doTestV("skip", 5, rvp->skip());
+ doTestV("doAve", false, rvp->doAve());
}
{
//Skip FIVE frames...
@@ -371,6 +373,7 @@
doTestV("countFromEOF", false, rvp->countFromEOF());
doTestV("doSkip", true, rvp->doSkip());
doTestV("skip", 5, rvp->skip());
+ doTestV("doAve", false, rvp->doAve());
}
{
//Skip FIVE frames and countFromEOF()...
@@ -394,6 +397,7 @@
doTestV("countFromEOF", true, rvp->countFromEOF());
doTestV("doSkip", true, rvp->doSkip());
doTestV("skip", 5, rvp->skip());
+ doTestV("doAve", false, rvp->doAve());
}
}
}
More information about the Kst
mailing list