6#ifndef SWIFT_MISC_TIMESTAMPOBJECTLIST_H
7#define SWIFT_MISC_TIMESTAMPOBJECTLIST_H
44 return QStringLiteral(
"Min: %1ms Max: %2ms Mean: %3ms").
arg(
min).
arg(
max).
arg(
mean, 0,
'f', 2);
50 template <
class OBJ,
class CONTAINER>
53 static_assert(std::is_base_of_v<ITimestampBased, OBJ>,
"OBJ needs to implement ITimestampBased");
70 return this->
container().findBy([&](
const OBJ &obj) {
return obj.isOlderThan(msSinceEpoch); });
76 const CONTAINER before = this->
findBefore(msSinceEpoch);
81 return before.latestObject();
104 return this->
container().findBy([&](
const OBJ &obj) {
return obj.isNewerThan(msSinceEpoch); });
116 const CONTAINER after = this->
findAfter(msSinceEpoch);
121 return after.oldestObject();
127 return this->
container().findBy([&](
const OBJ &obj) {
return !obj.hasValidTimestamp(); });
137 const auto closest = std::min_element(this->
container().cbegin(), this->
container().cend(),
153 tsObj.setCurrentUtcTime();
162 tsObj.setMSecsSinceEpoch(msSinceEpoch);
171 if (tsObj.hasValidTimestamp())
175 tsObj.setCurrentUtcTime();
222 return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch();
236 return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch();
247 return this->
container().removeIf([&](
const OBJ &obj) {
return obj.isOlderThan(msSinceEpoch); });
254 return this->
container().removeIf([&](
const OBJ &obj) {
return obj.isOlderThan(epoch); });
270 Q_ASSERT_X(maxElements < 0 || maxElements > 1, Q_FUNC_INFO,
"Max.value wrong range");
272 if (replaceSameTimestamp && !c.isEmpty() && c[0].getMSecsSinceEpoch() == value.getMSecsSinceEpoch())
277 c.truncate(maxElements);
284 c.truncate(maxElements - 1);
286 const bool needSort = !c.isEmpty() && value.isOlderThan(c.front());
310 const qint64 newMs = newObject.getMSecsSinceEpoch();
311 const qint64 oldMs = this->
container().back().getMSecsSinceEpoch();
323 OBJ newObjectCopy(newObject);
324 newObjectCopy.setMSecsSinceEpoch(newTsMsSinceEpoch);
325 this->
container().push_back(newObjectCopy);
335 qint64 currentMs = startTimeStampMs;
338 it->setMSecsSinceEpoch(currentMs);
339 currentMs += deltaTimeMs;
349 if (obj.getMSecsSinceEpoch() == newObject.getMSecsSinceEpoch())
369 if (!obj.hasValidTimestamp())
373 if (obj.getMSecsSinceEpoch() < max)
377 max = obj.getMSecsSinceEpoch();
390 qint64 min = std::numeric_limits<qint64>::max();
393 if (!obj.hasValidTimestamp())
397 if (obj.getMSecsSinceEpoch() > min)
401 min = obj.getMSecsSinceEpoch();
415 obj.addMsecs(msToAdd);
429 if (container.size() < 2)
435 if (
container.m_tsSortHint == NoTimestampSortHint)
438 copy.sortLatestFirst();
439 copy.m_tsSortHint = TimestampLatestFirst;
440 return copy.getTimestampDifferenceMinMaxMean();
443 mmm.
max = std::numeric_limits<qint64>::min();
444 mmm.
min = std::numeric_limits<qint64>::max();
456 mmm.
max = std::max(diff, mmm.
max);
457 mmm.
min = std::min(diff, mmm.
min);
473 const CONTAINER &
container()
const {
return static_cast<const CONTAINER &
>(*this); }
476 CONTAINER &
container() {
return static_cast<CONTAINER &
>(*this); }
483 template <
class OBJ,
class CONTAINER>
486 static_assert(std::is_base_of_v<ITimestampWithOffsetBased, OBJ>,
"OBJ needs to implement ITimestampBased");
492 NoAdjustedTimestampSortHint,
493 AdjustedTimestampLatestFirst
499 this->
container().sortAdjustedOldestFirst();
507 copy.sortAdjustedLatestFirst();
518 CONTAINER copy(alreadySortedLatestFirst ? this->
container() :
549 obj.addMsecsToOffsetTime(msToAdd);
556 Q_ASSERT_X(maxElements < 0 || maxElements > 1, Q_FUNC_INFO,
"Max.value wrong range");
558 if (replaceSameTimestamp && !c.isEmpty() && c[0].getMSecsSinceEpoch() == value.getMSecsSinceEpoch())
563 c.truncate(maxElements);
570 c.truncate(maxElements - 1);
572 const bool needSort = !c.isEmpty() && value.isOlderThanAdjusted(c.front());
583 int maxElements = -1)
605 const qint64 minReqOs =
608 const qint64 os = qMax(minReqOs + 1, avgOs);
621 int maxElements = -1)
640 const qint64 osTime = value.getTimeOffsetMs();
641 const qint64 os = -1 * qAbs(deltaTimeMs < 0 ? osTime : deltaTimeMs);
644 SWIFT_VERIFY_X(os < 0, Q_FUNC_INFO,
"Need negative offset time to prefill time");
647 for (
int i = 1; i < elements; i++)
650 copy.addMsecs(os * i);
671 if (!obj.hasValidTimestamp())
675 if (obj.getAdjustedMSecsSinceEpoch() < max)
679 max = obj.getAdjustedMSecsSinceEpoch();
692 qint64 min = std::numeric_limits<qint64>::max();
695 if (!obj.hasValidTimestamp())
699 if (obj.getAdjustedMSecsSinceEpoch() > min)
703 min = obj.getAdjustedMSecsSinceEpoch();
723 return after.oldestAdjustedObject();
737 if (before.isEmpty())
741 return before.latestAdjustedObject();
773 return a.getAdjustedMSecsSinceEpoch() < b.getAdjustedMSecsSinceEpoch();
791 return a.getAdjustedMSecsSinceEpoch() < b.getAdjustedMSecsSinceEpoch();
848 if (container.size() < 1)
853 mmm.
max = std::numeric_limits<qint64>::min();
854 mmm.
min = std::numeric_limits<qint64>::max();
860 if (!
object.hasNonZeroOffsetTime())
864 const qint64 os =
object.getTimeOffsetMs();
865 mmm.
max = std::max(os, mmm.
max);
866 mmm.
min = std::min(os, mmm.
min);
static bool isLocalDeveloperDebugBuild()
Local build for developers.
qint64 getMSecsSinceEpoch() const
Timestamp as ms value.
qint64 getTimeDifferenceMs(qint64 compareTime) const
Time difference in ms.
qint64 getAbsTimeDifferenceMs(qint64 compareTime) const
Time difference in ms.
OBJ findObjectBeforeOrDefault(qint64 msSinceEpoch) const
Object before timestamp or default (older).
qint64 latestTimestampMsecsSinceEpoch() const
Latest timestamp.
QDateTime latestTimestamp() const
Latest timestamp.
void push_backOverrideTimestamp(const OBJ &newObject, qint64 newTsMsSinceEpoch)
Push back, but set new timestamp.
void setSortHint(HintTimestampSort hint)
Set the hint.
HintTimestampSort m_tsSortHint
sort hint
void sortOldestFirst()
Sort by timestamp.
OBJ oldestObject() const
Latest object.
int removeBefore(qint64 msSinceEpoch)
Remove objects with timestamp before dateTime.
CONTAINER findBeforeNowMinusOffset(qint64 msOffset) const
List of objects before now - offset.
int replaceIfSameTimestamp(const OBJ &newObject)
Replace if an object has the same timestamp.
CONTAINER findAfter(const QDateTime &dateTime) const
List of objects after dateTime (newer).
void addMsecs(qint64 msToAdd)
Adds a time to all values.
qint64 oldestTimestampMsecsSinceEpoch() const
Oldest timestamp.
CONTAINER findBeforeAndRemove(qint64 msSinceEpoch)
Get objects before msSinceEpoch and remove those.
MillisecondsMinMaxMean getTimestampDifferenceMinMaxMean() const
Difference of timestamp values.
CONTAINER findAfter(qint64 msSinceEpoch) const
List of objects after msSinceEpoch (newer).
ITimestampObjectList()=default
Constructor.
void setCurrentUtcTime()
Set all timestamps to now.
OBJ findObjectAfterOrDefault(qint64 msSinceEpoch) const
List of objects after msSinceEpoch (newer).
void setInvalidTimestampsToCurrentUtcTime()
Set invalid timestamps to now.
OBJ latestObject() const
Latest object.
void sortLatestFirst()
Sort by timestamp.
const CONTAINER & container() const
Container.
HintTimestampSort
Hint if the list is sorted.
void push_backIncreaseTimestamp(const OBJ &newObject)
Push back and increase the timestamp at least by +1ms if equal to last element.
int removeBefore(const QDateTime &dateTime)
Remove objects with timestamp before dateTime.
int removeOlderThanNowMinusOffset(qint64 offsetMs)
Remove objects older than seconds.
bool isSortedLatestLast() const
Is completely sorted: latest last.
void push_frontKeepLatestFirst(const OBJ &value, bool replaceSameTimestamp=true, int maxElements=-1)
Insert as first element by keeping maxElements and the latest first.
QDateTime oldestTimestamp() const
Oldest timestamp.
CONTAINER findBefore(const QDateTime &dateTime) const
List of objects before dateTime (older).
CONTAINER findBefore(qint64 msSinceEpoch) const
List of objects before msSinceEpoch (older).
bool hasInvalidTimestamps() const
Has invalid timestamp.
void setUtcTime(qint64 msSinceEpoch)
Set all timestamps to given time.
CONTAINER findInvalidTimestamps() const
Objects without valid timestamp.
bool isSortedLatestFirst() const
Is completely sorted: latest last.
CONTAINER & container()
Container.
void setNewTimestampStartingLast(qint64 startTimeStampMs, qint64 deltaTimeMs)
Set new timestamps starting with the last element.
CONTAINER findAfterNowMinusOffset(qint64 msOffset) const
List of objects before now - offset.
OBJ findClosestTimeDistance(qint64 msSinceEpoch) const
Find closest (or default).
qint64 getAdjustedMSecsSinceEpoch() const
Timestamp with offset added for interpolation.
qint64 getTimeOffsetMs() const
Milliseconds to add to timestamp for interpolation.
bool isNewerThanAdjusted(const ITimestampWithOffsetBased &otherTimestampObj) const
Is this newer than other?
bool isOlderThanAdjusted(const ITimestampWithOffsetBased &otherTimestampObj) const
Is this older than other?
qint64 getAdjustedTimeDifferenceMs(qint64 compareTime) const
Time difference in ms (this -> compare).
void setTimeOffsetMs(qint64 offset)
Milliseconds to add to timestamp for interpolation.
OBJ findClosestTimeDistanceAdjusted(qint64 msSinceEpoch) const
Closest adjusted time difference.
bool containsZeroOrNegativeOffsetTime() const
Any negative or zero offset time?
CONTAINER getLatestAdjustedTwoObjects(bool alreadySortedLatestFirst=false) const
Get the latest 2 values.
HintAdjustedTimestampSort
Hint if the list is sorted.
QDateTime oldestAdjustedTimestamp() const
Oldest adjusted timestamp.
bool isSortedAdjustedLatestFirst() const
Is completely sorted: latest last.
OBJ findObjectBeforeAdjustedOrDefault(qint64 msSinceEpoch) const
Object before timestamp (older).
void setAdjustedSortHint(HintAdjustedTimestampSort hint)
Set the hint.
CONTAINER findBeforeAdjusted(qint64 msSinceEpoch) const
List of objects before msSinceEpoch (older).
OBJ latestAdjustedObject() const
Latest adjusted object.
bool isSortedAdjustedLatestLast() const
Is completely sorted: latest last.
MillisecondsMinMaxMean getOffsetMinMaxMean() const
Difference of timestamp values.
void addMsecsToOffset(qint64 msToAdd)
Adds a time to all offset values.
void push_frontKeepLatestAdjustedFirst(const OBJ &value, bool replaceSameTimestamp=true, int maxElements=-1)
Insert as first element by keeping maxElements and the latest first.
qint64 oldestAdjustedTimestampMsecsSinceEpoch() const
Oldest adjusted timestamp.
void sortAdjustedLatestFirst()
Sort by adjusted timestamp.
bool containsNegativeOffsetTime() const
Any negative offset time?
CONTAINER getSortedAdjustedLatestFirst() const
As sorted copy.
void prefillLatestAdjustedFirst(const OBJ &value, int elements, qint64 deltaTimeMs=-1)
Prefill with elements.
QDateTime latestAdjustedTimestamp() const
Latest adjusted timestamp.
OBJ oldestAdjustedObject() const
Oldest adjusted object.
void push_frontKeepLatestFirstAdjustOffset(const OBJ &value, bool replaceSameTimestamp=true, int maxElements=-1)
Insert as first element by keeping maxElements and the latest first.
HintAdjustedTimestampSort m_tsAdjustedSortHint
sort hint
void push_frontKeepLatestFirstIgnoreOverlapping(const OBJ &value, bool replaceSameTimestamp=true, int maxElements=-1)
Add value, but ignore overlapping (past) values.
qint64 latestAdjustedTimestampMsecsSinceEpoch() const
Latest adjusted timestamp.
CONTAINER findAfterAdjusted(qint64 msSinceEpoch) const
List of objects after msSinceEpoch (newer).
OBJ findObjectAfterAdjustedOrDefault(qint64 msSinceEpoch) const
List of objects after msSinceEpoch (newer).
ITimestampWithOffsetObjectList()=default
Constructor.
void sortAdjustedOldestFirst()
Sort by adjusted timestamp.
auto MemberLess(Ts... vs)
Predicate which compares the return values of some member functions of two objects.
Free functions in swift::misc.
T::const_iterator begin(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
qint64 currentMSecsSinceEpoch()
bool isValid() const const
qint64 toMSecsSinceEpoch() const const
QString arg(Args &&... args) const const
Milliseconds minimum/maximum/mean.
bool isValid() const
Valid?
void reset()
Reset the values.
double mean
Mean (average).
QString asString() const
As string.
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.