Ref T297, calculate ts differences ITimestampObjectList::getTimestampDifferenceMinMaxMean

This commit is contained in:
Klaus Basan
2018-07-31 02:38:13 +02:00
parent e20ee986a1
commit abafbc335a
3 changed files with 72 additions and 1 deletions

View File

@@ -49,6 +49,15 @@ namespace BlackMisc
//! Time difference in ms
qint64 getTimeDifferenceMs(qint64 compareTime) const { return compareTime - this->getMSecsSinceEpoch(); }
//! Time difference in ms
qint64 getTimeDifferenceMs(const ITimestampBased &compare) const { return compare.getMSecsSinceEpoch() - this->getMSecsSinceEpoch(); }
//! Time difference in ms
qint64 getAbsTimeDifferenceMs(qint64 compareTime) const { return qAbs(this->getTimeDifferenceMs(compareTime)); }
//! Time difference in ms
qint64 getAbsTimeDifferenceMs(const ITimestampBased &compare) const { return qAbs(this->getTimeDifferenceMs(compare)); }
//! Set to null
void setTimestampToNull();

View File

@@ -37,6 +37,7 @@
#include <limits>
#include <iterator>
#include <type_traits>
#include <numeric>
using namespace BlackConfig;
@@ -330,6 +331,45 @@ namespace BlackMisc
}
}
template<class OBJ, class CONTAINER>
MillisecondsMinMaxMean ITimestampObjectList<OBJ, CONTAINER>::getTimestampDifferenceMinMaxMean() const
{
MillisecondsMinMaxMean mmm;
mmm.reset();
if (this->container().size() < 2) { return mmm; }
if (m_tsSortHint == NoTimestampSortHint)
{
CONTAINER copy(this->container());
copy.sortLatestFirst();
copy.m_tsSortHint = TimestampLatestFirst;
return copy.getTimestampDifferenceMinMaxMean();
}
mmm.max = std::numeric_limits<qint64>::min();
mmm.min = std::numeric_limits<qint64>::max();
qint64 mean = 0;
int c = 0;
OBJ last;
for (const OBJ &object : this->container())
{
c++;
last = object;
if (c < 2) { continue; }
const ITimestampBased &l = last;
const ITimestampBased &o = object;
const qint64 diff = l.getAbsTimeDifferenceMs(o);
if (diff > mmm.max) { mmm.max = diff; }
if (diff < mmm.min) { mmm.min = diff; }
mean += diff;
}
mmm.mean = mean / c;
return mmm;
}
template <class OBJ, class CONTAINER>
void ITimestampWithOffsetObjectList<OBJ, CONTAINER>::sortAdjustedLatestFirst()
{

View File

@@ -21,6 +21,23 @@ class QDateTime;
namespace BlackMisc
{
//! Milliseconds minimum/maximum/mean
struct MillisecondsMinMaxMean
{
qint64 min; //!< Minimum
qint64 max; //!< Maximum
double mean; //!< Mean (average)
//! Valid?
bool isValid() const { return min >= 0 && max >= 0; }
//! Reset the values
void reset() { min = -1; max = -1; mean = -1;}
//! As string
QString asString() const { static const QString s("Min: %1 Max: %2 Mean: %3"); return s.arg(min).arg(max).arg(mean, 0, 'f', 2); }
};
//! List of objects with timestamp.
//! Such objects should implement \sa ITimestampBased
template<class OBJ, class CONTAINER> class ITimestampObjectList
@@ -30,7 +47,8 @@ namespace BlackMisc
enum HintTimestampSort
{
NoTimestampSortHint,
TimestampLatestFirst
TimestampLatestFirst,
TimestampLatestLast
};
//! List of objects before dateTime (older)
@@ -125,6 +143,10 @@ namespace BlackMisc
//! Adds a time to all values
void addMsecs(qint64 msToAdd);
//! Difference of timestamp values
//! \cond timestamp list has to be sorted to get meaninful values
MillisecondsMinMaxMean getTimestampDifferenceMinMaxMean() const;
//! Set the hint
void setSortHint(HintTimestampSort hint) { m_tsSortHint = hint; }