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:
Klaus Basan
2016-01-08 00:17:16 +01:00
parent 5a17506a97
commit 72256cf86f
7 changed files with 100 additions and 18 deletions

View File

@@ -1017,7 +1017,7 @@ namespace BlackCore
auto history = this->m_situationsByCallsign[callsign]; auto history = this->m_situationsByCallsign[callsign];
if (history.empty()) { return; } // we need one full situation if (history.empty()) { return; } // we need one full situation
interimSituation.setCurrentUtcTime(); interimSituation.setCurrentUtcTime();
interimSituation.setGroundspeed(history.latestValue().getGroundSpeed()); interimSituation.setGroundspeed(history.latestObject().getGroundSpeed());
} }
// store situation history // store situation history

View File

@@ -46,6 +46,50 @@ namespace BlackMisc
this->container().sort(BlackMisc::Predicates::MemberLess(&OBJ::getDbKey)); 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> template <class OBJ, class CONTAINER>
void IDatastoreObjectListWithStringKey<OBJ, CONTAINER>::sortByKey() void IDatastoreObjectListWithStringKey<OBJ, CONTAINER>::sortByKey()
{ {
@@ -58,6 +102,7 @@ namespace BlackMisc
QStringList keys; QStringList keys;
for (const OBJ &obj : ITimestampObjectList<OBJ, CONTAINER>::container()) for (const OBJ &obj : ITimestampObjectList<OBJ, CONTAINER>::container())
{ {
if (!obj.hasValidDbKey()) { continue; }
keys.append(obj.getDbKey()); keys.append(obj.getDbKey());
} }
return keys; return keys;
@@ -74,6 +119,17 @@ namespace BlackMisc
return container; 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> template <class OBJ, class CONTAINER>
CONTAINER IDatastoreObjectListWithStringKey<OBJ, CONTAINER>::fromDatabaseJson(const QJsonArray &array) CONTAINER IDatastoreObjectListWithStringKey<OBJ, CONTAINER>::fromDatabaseJson(const QJsonArray &array)
{ {
@@ -85,6 +141,17 @@ namespace BlackMisc
return container; 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 // see here for the reason of thess forward instantiations
// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html // http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html
template class IDatastoreObjectListWithIntegerKey<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>; template class IDatastoreObjectListWithIntegerKey<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>;

View File

@@ -31,9 +31,18 @@ namespace BlackMisc
//! Sort by timestamp //! Sort by timestamp
void sortByKey(); 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); static CONTAINER fromDatabaseJson(const QJsonArray &array);
//! From DB JSON
static CONTAINER fromDatabaseJson(const QJsonArray &array, const QString &prefix);
protected: protected:
//! Constructor //! Constructor
IDatastoreObjectListWithIntegerKey(); IDatastoreObjectListWithIntegerKey();
@@ -53,9 +62,15 @@ namespace BlackMisc
//! All keys as string list //! All keys as string list
QStringList toDbKeyList() const; 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); static CONTAINER fromDatabaseJson(const QJsonArray &array);
//! From DB JSON
static CONTAINER fromDatabaseJson(const QJsonArray &array, const QString &prefix);
protected: protected:
//! Constructor //! Constructor
IDatastoreObjectListWithStringKey(); IDatastoreObjectListWithStringKey();

View File

@@ -109,7 +109,7 @@ namespace BlackMisc
if (json.contains("publishedModels")) if (json.contains("publishedModels"))
{ {
QJsonValue publishedJson(json.take("publishedModels")); QJsonValue publishedJson(json.take("publishedModels"));
CAircraftModelList published = CAircraftModelList::fromDatabaseJson(publishedJson.toArray()); CAircraftModelList published = CAircraftModelList::fromDatabaseJson(publishedJson.toArray(), "");
if (!published.isEmpty()) if (!published.isEmpty())
{ {
publishedModels.push_back(published); publishedModels.push_back(published);
@@ -120,7 +120,7 @@ namespace BlackMisc
if (json.contains("skippedModels")) if (json.contains("skippedModels"))
{ {
QJsonValue skippedJson(json.take("skippedModels")); QJsonValue skippedJson(json.take("skippedModels"));
CAircraftModelList skipped = CAircraftModelList::fromDatabaseJson(skippedJson.toArray()); CAircraftModelList skipped = CAircraftModelList::fromDatabaseJson(skippedJson.toArray(), "");
if (!skipped.isEmpty()) if (!skipped.isEmpty())
{ {
skippedModels.push_back(skipped); skippedModels.push_back(skipped);

View File

@@ -109,7 +109,7 @@ namespace BlackMisc
int CAircraftModelList::removeModelsWithString(const QStringList &modelStrings, Qt::CaseSensitivity sensitivity) int CAircraftModelList::removeModelsWithString(const QStringList &modelStrings, Qt::CaseSensitivity sensitivity)
{ {
int cs = this->size(); int cs = this->size();
(*this) = (findByNotInModelStrings(modelStrings, sensitivity)); (*this) = (this->findByNotInModelStrings(modelStrings, sensitivity));
int d = cs - this->size(); int d = cs - this->size();
return d; return d;
} }
@@ -134,7 +134,8 @@ namespace BlackMisc
{ {
return this->findBy([ = ](const CAircraftModel & model) 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; QStringList ms;
for (const CAircraftModel &model : (*this)) for (const CAircraftModel &model : (*this))
{ {
if (!model.hasModelString()) { continue; }
ms.append(model.getModelString()); ms.append(model.getModelString());
} }
if (sort) { ms.sort(Qt::CaseInsensitive); } if (sort) { ms.sort(Qt::CaseInsensitive); }

View File

@@ -100,27 +100,25 @@ namespace BlackMisc
{ {
// O(n) comparisons and O(n) copies // O(n) comparisons and O(n) copies
std::partition_copy(c.begin(), c.end(), std::back_inserter(result[0]), std::back_inserter(result[1]), 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; return result;
} }
template <class OBJ, class CONTAINER> template <class OBJ, class CONTAINER>
OBJ ITimestampObjectList<OBJ, CONTAINER>::latestValue() const OBJ ITimestampObjectList<OBJ, CONTAINER>::latestObject() const
{ {
if (this->container().isEmpty()) { return OBJ(); } if (this->container().isEmpty()) { return OBJ(); }
CONTAINER copy(container()); // copy auto latest = std::max_element(container().begin(), container().end(), [](const OBJ & a, const OBJ & b) { return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch(); });
copy.sortLatestFirst(); return *latest;
return copy.front();
} }
template <class OBJ, class CONTAINER> template <class OBJ, class CONTAINER>
OBJ ITimestampObjectList<OBJ, CONTAINER>::oldestValue() const OBJ ITimestampObjectList<OBJ, CONTAINER>::oldestObject() const
{ {
if (this->container().isEmpty()) { return OBJ(); } if (this->container().isEmpty()) { return OBJ(); }
CONTAINER copy(container()); // copy auto oldest = std::min_element(container().begin(), container().end(), [](const OBJ & a, const OBJ & b) { return a.getMSecsSinceEpoch() < b.getMSecsSinceEpoch(); });
copy.sortLatestFirst(); return *oldest;
return copy.back();
} }
template <class OBJ, class CONTAINER> template <class OBJ, class CONTAINER>

View File

@@ -49,10 +49,10 @@ namespace BlackMisc
QList<CONTAINER> splitByTime(qint64 msSinceEpoch, bool sortedLatestFirst = false) const; QList<CONTAINER> splitByTime(qint64 msSinceEpoch, bool sortedLatestFirst = false) const;
//! Latest value //! Latest value
OBJ latestValue() const; OBJ latestObject() const;
//! Latest value //! Latest value
OBJ oldestValue() const; OBJ oldestObject() const;
//! Remove objects with timestamp before dateTime //! Remove objects with timestamp before dateTime
void removeBefore(const QDateTime &dateTime); void removeBefore(const QDateTime &dateTime);