[Kst] kdeextragear-2/kst/kst/datasources/planck
George Staikos
staikos at kde.org
Wed Feb 9 19:06:38 CET 2005
CVS commit by staikos:
add the binary search code for finding time indices in planck objects
M +25 -0 planck.cpp 1.36
M +77 -0 planckobj.cpp 1.27
M +2 -0 planckobj.h 1.9
--- kdeextragear-2/kst/kst/datasources/planck/planck.cpp #1.35:1.36
@@ -236,17 +236,42 @@ bool PlanckSource::supportsTimeConversio
int PlanckSource::sampleForTime(const QDateTime& time, bool *ok) {
+ if (!_valid) {
+ if (ok) {
+ *ok = false;
+ }
+ return -1;
+ }
}
int PlanckSource::sampleForTime(int seconds, bool *ok) {
+ if (!_valid) {
+ if (ok) {
+ *ok = false;
+ }
+ return -1;
+ }
+ return _planckObject->sampleForTime(seconds, ok);
}
QDateTime PlanckSource::timeForSample(int sample, bool *ok) {
+ if (!_valid) {
+ if (ok) {
+ *ok = false;
+ }
+ return QDateTime();
+ }
}
int PlanckSource::relativeTimeForSample(int sample, bool *ok) {
+ if (!_valid) {
+ if (ok) {
+ *ok = false;
+ }
+ return -1;
+ }
}
--- kdeextragear-2/kst/kst/datasources/planck/planckobj.cpp #1.26:1.27
@@ -322,4 +322,81 @@ ObjectGroup *Object::findGroup(const QSt
+int Object::sampleForTime(int search, bool *ok) {
+ PIOLONG location;
+ bool found = false;
+
+ if (_tt == Undetermined) {
+ _tt = typeOfTime();
+ }
+
+ Planck::ObjectGroup *g = _groupInfo[_group];
+ if (!g || _tt == Unknown) {
+ if (ok) {
+ *ok = false;
+ }
+ return -1;
+ }
+
+ PIOLONG FirstIndex = *g->firstIndex;
+ PIOLONG LastIndex = *g->lastIndex;
+ double left = fetchTimePoint(FirstIndex);
+ double right = fetchTimePoint(LastIndex);
+
+ if (search >= left && search <= right) {
+ if (search == left) {
+ found = true;
+ location = FirstIndex;
+ } else if (search == right) {
+ found = true;
+ location = LastIndex;
+ } else {
+ while (FirstIndex < LastIndex) {
+ PIOLONG point = (LastIndex - FirstIndex) / 2 + FirstIndex;
+ if (LastIndex - FirstIndex < 3) {
+ double left = fetchTimePoint(FirstIndex);
+ double right = fetchTimePoint(LastIndex);
+ double mid = fetchTimePoint(point);
+ found = true;
+ double ld = fabs(left - search);
+ double rd = fabs(right - search);
+ double md = fabs(mid - search);
+ if (ld <= rd && ld <= md) {
+ location = FirstIndex;
+ } else if (md <= ld && md <= rd) {
+ location = point;
+ } else if (rd <= ld && rd <= md) {
+ location = LastIndex;
+ } else {
+ location = -1;
+ }
+ break;
+ }
+
+ double pt = fetchTimePoint(point);
+ if (pt > search) {
+ LastIndex = point - 1;
+ } else if (pt < search) {
+ FirstIndex = point + 1;
+ } else if (pt == search) {
+ found = true;
+ location = point;
+ break;
+ }
+ if (LastIndex == FirstIndex) {
+ found = true;
+ location = FirstIndex;
+ }
+ }
+ }
+ }
+
+ if (ok) {
+ *ok = found;
+ }
+
+ return location;
+}
+
+
/*
* We may want to consider storing with a Qt datatype instead so we can
--- kdeextragear-2/kst/kst/datasources/planck/planckobj.h #1.8:1.9
@@ -50,4 +50,6 @@ class Object : public Source {
double fetchTimePoint(long);
+ int sampleForTime(int seconds, bool *ok);
+
private:
// We lazy load the groups, hence mutable
More information about the Kst
mailing list