aboutsummaryrefslogtreecommitdiff
path: root/events
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2016-04-11 12:43:32 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2016-04-11 12:45:00 +0900
commitdef5a08b4d360f6035ad4c420f0206ad6f96e0bd (patch)
tree65eea09fa684406cd6cc2a47bcba69e74b8cb3e8 /events
parent8045e36b6b8597120f46e0d4d42cf87ac790351b (diff)
downloadlibquotient-def5a08b4d360f6035ad4c420f0206ad6f96e0bd.tar.gz
libquotient-def5a08b4d360f6035ad4c420f0206ad6f96e0bd.zip
Factor out the code that searches an insertion point in a timeline.
This is used once in the library and, I guess, twice more in the Quaternion. Implemented as a template function that is equally suitable for Event and Message, and any container that supports STL-style iterators (QList and other Qt containers do).
Diffstat (limited to 'events')
-rw-r--r--events/event.h18
1 files changed, 18 insertions, 0 deletions
diff --git a/events/event.h b/events/event.h
index b25b1378..6a8d0e89 100644
--- a/events/event.h
+++ b/events/event.h
@@ -19,6 +19,8 @@
#ifndef QMATRIXCLIENT_EVENT_H
#define QMATRIXCLIENT_EVENT_H
+#include <algorithm>
+
#include <QtCore/QString>
#include <QtCore/QDateTime>
#include <QtCore/QJsonObject>
@@ -53,6 +55,22 @@ namespace QMatrixClient
class Private;
Private* d;
};
+
+ /**
+ * Finds a place in the timeline where a new event/message could be inserted.
+ * @return an iterator to an item with the earliest timestamp after
+ * the one of 'item'; or timeline.end(), if all events are earlier
+ */
+ template <class ItemT, template <typename> class ContT>
+ typename ContT<ItemT *>::iterator
+ findInsertionPos(ContT<ItemT *> & timeline, const ItemT *item)
+ {
+ return std::lower_bound (timeline.begin(), timeline.end(), item,
+ [](const ItemT * a, const ItemT * b) {
+ return a->timestamp() < b->timestamp();
+ }
+ );
+ }
}
#endif // QMATRIXCLIENT_EVENT_H