mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 16:56:53 +08:00
refs #568, specialized lists (timestamp, DB objects, model list)
* remaned to latest/oldestObject * JSON functions with prefix * remove by keys * only add models with model string to QStringList
This commit is contained in:
@@ -1017,7 +1017,7 @@ namespace BlackCore
|
||||
auto history = this->m_situationsByCallsign[callsign];
|
||||
if (history.empty()) { return; } // we need one full situation
|
||||
interimSituation.setCurrentUtcTime();
|
||||
interimSituation.setGroundspeed(history.latestValue().getGroundSpeed());
|
||||
interimSituation.setGroundspeed(history.latestObject().getGroundSpeed());
|
||||
}
|
||||
|
||||
// store situation history
|
||||
|
||||
@@ -46,6 +46,50 @@ namespace BlackMisc
|
||||
this->container().sort(BlackMisc::Predicates::MemberLess(&OBJ::getDbKey));
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
QList<int> IDatastoreObjectListWithIntegerKey<OBJ, CONTAINER>::toDbKeyList() const
|
||||
{
|
||||
QList<int> keys;
|
||||
for (const OBJ &obj : ITimestampObjectList<OBJ, CONTAINER>::container())
|
||||
{
|
||||
if (!obj.hasValidDbKey()) { continue; }
|
||||
keys.append(obj.getDbKey());
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
int IDatastoreObjectListWithIntegerKey<OBJ, CONTAINER>::removeObjectsWithKeys(const QList<int> &keys)
|
||||
{
|
||||
if (keys.isEmpty()) { return 0; }
|
||||
if (this->container().isEmpty()) { return 0; }
|
||||
CONTAINER newValues;
|
||||
for (const OBJ &obj : ITimestampObjectList<OBJ, CONTAINER>::container())
|
||||
{
|
||||
if (keys.contains(obj.getDbKey())) { continue; }
|
||||
newValues.push_back(obj);
|
||||
}
|
||||
int delta = this->container().size() - newValues.size();
|
||||
*this = newValues;
|
||||
return delta;
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
int IDatastoreObjectListWithStringKey<OBJ, CONTAINER>::removeObjectsWithKeys(const QStringList &keys)
|
||||
{
|
||||
if (keys.isEmpty()) { return 0; }
|
||||
if (this->container().isEmpty()) { return 0; }
|
||||
CONTAINER newValues;
|
||||
for (const OBJ &obj : ITimestampObjectList<OBJ, CONTAINER>::container())
|
||||
{
|
||||
if (keys.contains(obj.getDbKey(), Qt::CaseInsensitive)) { continue; }
|
||||
newValues.push_back(obj);
|
||||
}
|
||||
int delta = this->container().size() - newValues.size();
|
||||
*this = newValues;
|
||||
return delta;
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
void IDatastoreObjectListWithStringKey<OBJ, CONTAINER>::sortByKey()
|
||||
{
|
||||
@@ -58,6 +102,7 @@ namespace BlackMisc
|
||||
QStringList keys;
|
||||
for (const OBJ &obj : ITimestampObjectList<OBJ, CONTAINER>::container())
|
||||
{
|
||||
if (!obj.hasValidDbKey()) { continue; }
|
||||
keys.append(obj.getDbKey());
|
||||
}
|
||||
return keys;
|
||||
@@ -74,6 +119,17 @@ namespace BlackMisc
|
||||
return container;
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
CONTAINER IDatastoreObjectListWithIntegerKey<OBJ, CONTAINER>::fromDatabaseJson(const QJsonArray &array, const QString &prefix)
|
||||
{
|
||||
CONTAINER container;
|
||||
for (const QJsonValue &value : array)
|
||||
{
|
||||
container.push_back(OBJ::fromDatabaseJson(value.toObject(), prefix));
|
||||
}
|
||||
return container;
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
CONTAINER IDatastoreObjectListWithStringKey<OBJ, CONTAINER>::fromDatabaseJson(const QJsonArray &array)
|
||||
{
|
||||
@@ -85,6 +141,17 @@ namespace BlackMisc
|
||||
return container;
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
CONTAINER IDatastoreObjectListWithStringKey<OBJ, CONTAINER>::fromDatabaseJson(const QJsonArray &array, const QString &prefix)
|
||||
{
|
||||
CONTAINER container;
|
||||
for (const QJsonValue &value : array)
|
||||
{
|
||||
container.push_back(OBJ::fromDatabaseJson(value.toObject(), prefix));
|
||||
}
|
||||
return container;
|
||||
}
|
||||
|
||||
// see here for the reason of thess forward instantiations
|
||||
// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html
|
||||
template class IDatastoreObjectListWithIntegerKey<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>;
|
||||
|
||||
@@ -31,9 +31,18 @@ namespace BlackMisc
|
||||
//! Sort by timestamp
|
||||
void sortByKey();
|
||||
|
||||
//! From DB JSON
|
||||
//! All keys as list
|
||||
QList<int> toDbKeyList() const;
|
||||
|
||||
//! Remove objects with key
|
||||
int removeObjectsWithKeys(const QList<int> &keys);
|
||||
|
||||
//! From DB JSON with default prefixes
|
||||
static CONTAINER fromDatabaseJson(const QJsonArray &array);
|
||||
|
||||
//! From DB JSON
|
||||
static CONTAINER fromDatabaseJson(const QJsonArray &array, const QString &prefix);
|
||||
|
||||
protected:
|
||||
//! Constructor
|
||||
IDatastoreObjectListWithIntegerKey();
|
||||
@@ -53,9 +62,15 @@ namespace BlackMisc
|
||||
//! All keys as string list
|
||||
QStringList toDbKeyList() const;
|
||||
|
||||
//! From DB JSON
|
||||
//! Remove objects with key
|
||||
int removeObjectsWithKeys(const QStringList &keys);
|
||||
|
||||
//! From DB JSON with default prefixes
|
||||
static CONTAINER fromDatabaseJson(const QJsonArray &array);
|
||||
|
||||
//! From DB JSON
|
||||
static CONTAINER fromDatabaseJson(const QJsonArray &array, const QString &prefix);
|
||||
|
||||
protected:
|
||||
//! Constructor
|
||||
IDatastoreObjectListWithStringKey();
|
||||
|
||||
@@ -109,7 +109,7 @@ namespace BlackMisc
|
||||
if (json.contains("publishedModels"))
|
||||
{
|
||||
QJsonValue publishedJson(json.take("publishedModels"));
|
||||
CAircraftModelList published = CAircraftModelList::fromDatabaseJson(publishedJson.toArray());
|
||||
CAircraftModelList published = CAircraftModelList::fromDatabaseJson(publishedJson.toArray(), "");
|
||||
if (!published.isEmpty())
|
||||
{
|
||||
publishedModels.push_back(published);
|
||||
@@ -120,7 +120,7 @@ namespace BlackMisc
|
||||
if (json.contains("skippedModels"))
|
||||
{
|
||||
QJsonValue skippedJson(json.take("skippedModels"));
|
||||
CAircraftModelList skipped = CAircraftModelList::fromDatabaseJson(skippedJson.toArray());
|
||||
CAircraftModelList skipped = CAircraftModelList::fromDatabaseJson(skippedJson.toArray(), "");
|
||||
if (!skipped.isEmpty())
|
||||
{
|
||||
skippedModels.push_back(skipped);
|
||||
|
||||
@@ -109,7 +109,7 @@ namespace BlackMisc
|
||||
int CAircraftModelList::removeModelsWithString(const QStringList &modelStrings, Qt::CaseSensitivity sensitivity)
|
||||
{
|
||||
int cs = this->size();
|
||||
(*this) = (findByNotInModelStrings(modelStrings, sensitivity));
|
||||
(*this) = (this->findByNotInModelStrings(modelStrings, sensitivity));
|
||||
int d = cs - this->size();
|
||||
return d;
|
||||
}
|
||||
@@ -134,7 +134,8 @@ namespace BlackMisc
|
||||
{
|
||||
return this->findBy([ = ](const CAircraftModel & model)
|
||||
{
|
||||
return !modelStrings.contains(model.getModelString(), sensitivity);
|
||||
const bool c = modelStrings.contains(model.getModelString(), sensitivity);
|
||||
return !c;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -143,6 +144,7 @@ namespace BlackMisc
|
||||
QStringList ms;
|
||||
for (const CAircraftModel &model : (*this))
|
||||
{
|
||||
if (!model.hasModelString()) { continue; }
|
||||
ms.append(model.getModelString());
|
||||
}
|
||||
if (sort) { ms.sort(Qt::CaseInsensitive); }
|
||||
|
||||
@@ -100,27 +100,25 @@ namespace BlackMisc
|
||||
{
|
||||
// O(n) comparisons and O(n) copies
|
||||
std::partition_copy(c.begin(), c.end(), std::back_inserter(result[0]), std::back_inserter(result[1]),
|
||||
[msSinceEpoch](const OBJ & obj) { return ! obj.isNewerThan(msSinceEpoch); });
|
||||
[msSinceEpoch](const OBJ & obj) { return ! obj.isNewerThan(msSinceEpoch); });
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
OBJ ITimestampObjectList<OBJ, CONTAINER>::latestValue() const
|
||||
OBJ ITimestampObjectList<OBJ, CONTAINER>::latestObject() const
|
||||
{
|
||||
if (this->container().isEmpty()) { return OBJ(); }
|
||||
CONTAINER copy(container()); // copy
|
||||
copy.sortLatestFirst();
|
||||
return copy.front();
|
||||
auto latest = std::max_element(container().begin(), container().end(), [](const OBJ & a, const OBJ & b) { return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch(); });
|
||||
return *latest;
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
OBJ ITimestampObjectList<OBJ, CONTAINER>::oldestValue() const
|
||||
OBJ ITimestampObjectList<OBJ, CONTAINER>::oldestObject() const
|
||||
{
|
||||
if (this->container().isEmpty()) { return OBJ(); }
|
||||
CONTAINER copy(container()); // copy
|
||||
copy.sortLatestFirst();
|
||||
return copy.back();
|
||||
auto oldest = std::min_element(container().begin(), container().end(), [](const OBJ & a, const OBJ & b) { return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch(); });
|
||||
return *oldest;
|
||||
}
|
||||
|
||||
template <class OBJ, class CONTAINER>
|
||||
|
||||
@@ -49,10 +49,10 @@ namespace BlackMisc
|
||||
QList<CONTAINER> splitByTime(qint64 msSinceEpoch, bool sortedLatestFirst = false) const;
|
||||
|
||||
//! Latest value
|
||||
OBJ latestValue() const;
|
||||
OBJ latestObject() const;
|
||||
|
||||
//! Latest value
|
||||
OBJ oldestValue() const;
|
||||
OBJ oldestObject() const;
|
||||
|
||||
//! Remove objects with timestamp before dateTime
|
||||
void removeBefore(const QDateTime &dateTime);
|
||||
|
||||
Reference in New Issue
Block a user