mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-19 12:15:29 +08:00
Use nested namespaces (C++17 feature)
This commit is contained in:
@@ -15,443 +15,440 @@
|
||||
#include <QJsonValue>
|
||||
#include <Qt>
|
||||
|
||||
namespace BlackMisc
|
||||
namespace BlackMisc::Aviation
|
||||
{
|
||||
namespace Aviation
|
||||
CAircraftIcaoCodeList::CAircraftIcaoCodeList()
|
||||
{ }
|
||||
|
||||
CAircraftIcaoCodeList::CAircraftIcaoCodeList(const CSequence<CAircraftIcaoCode> &other) :
|
||||
CSequence<CAircraftIcaoCode>(other)
|
||||
{ }
|
||||
|
||||
CAircraftIcaoCodeList::CAircraftIcaoCodeList(std::initializer_list<CAircraftIcaoCode> il) :
|
||||
CSequence<CAircraftIcaoCode>(il)
|
||||
{ }
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByDesignator(const QString &designator, int fuzzySearch) const
|
||||
{
|
||||
CAircraftIcaoCodeList::CAircraftIcaoCodeList()
|
||||
{ }
|
||||
|
||||
CAircraftIcaoCodeList::CAircraftIcaoCodeList(const CSequence<CAircraftIcaoCode> &other) :
|
||||
CSequence<CAircraftIcaoCode>(other)
|
||||
{ }
|
||||
|
||||
CAircraftIcaoCodeList::CAircraftIcaoCodeList(std::initializer_list<CAircraftIcaoCode> il) :
|
||||
CSequence<CAircraftIcaoCode>(il)
|
||||
{ }
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByDesignator(const QString &designator, int fuzzySearch) const
|
||||
if (!fuzzySearch && !CAircraftIcaoCode::isValidDesignator(designator)) { return CAircraftIcaoCodeList(); }
|
||||
if (fuzzySearch && designator.length() < CAircraftIcaoCode::DesignatorMinLength) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
if (!fuzzySearch && !CAircraftIcaoCode::isValidDesignator(designator)) { return CAircraftIcaoCodeList(); }
|
||||
if (fuzzySearch && designator.length() < CAircraftIcaoCode::DesignatorMinLength) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.matchesDesignator(designator, fuzzySearch);
|
||||
});
|
||||
return code.matchesDesignator(designator, fuzzySearch);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CAircraftIcaoCodeList::findBestFuzzyMatchOrDefault(const QString &designator, int cutoff) const
|
||||
{
|
||||
if (designator.length() < CAircraftIcaoCode::DesignatorMinLength) { return CAircraftIcaoCode(); }
|
||||
int best = 0;
|
||||
int current = 0;
|
||||
CAircraftIcaoCode found;
|
||||
const QString d(designator.trimmed().toUpper());
|
||||
for (const CAircraftIcaoCode &code : *this)
|
||||
{
|
||||
if (!code.matchesDesignator(d, cutoff, ¤t)) { continue; }
|
||||
if (current == 100.0) { return code; }
|
||||
if (best < current) { found = code; }
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CAircraftIcaoCodeList::findBestFuzzyMatchOrDefault(const QString &designator, int cutoff) const
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByValidDesignator() const
|
||||
{
|
||||
return this->findBy([](const CAircraftIcaoCode & code)
|
||||
{
|
||||
if (designator.length() < CAircraftIcaoCode::DesignatorMinLength) { return CAircraftIcaoCode(); }
|
||||
int best = 0;
|
||||
int current = 0;
|
||||
CAircraftIcaoCode found;
|
||||
const QString d(designator.trimmed().toUpper());
|
||||
for (const CAircraftIcaoCode &code : *this)
|
||||
return code.hasValidDesignator();
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByInvalidDesignator() const
|
||||
{
|
||||
return this->findBy([](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return !code.hasValidDesignator();
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByDesignatorOrIataCode(const QString &icaoOrIata) const
|
||||
{
|
||||
if (icaoOrIata.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.matchesDesignatorOrIata(icaoOrIata);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByDesignatorIataOrFamily(const QString &icaoIataOrFamily) const
|
||||
{
|
||||
if (icaoIataOrFamily.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.matchesDesignatorIataOrFamily(icaoIataOrFamily);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findEndingWith(const QString &icaoEnding) const
|
||||
{
|
||||
const QString ends = icaoEnding.trimmed().toUpper();
|
||||
if (ends.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
CAircraftIcaoCodeList icaosDesignator;
|
||||
CAircraftIcaoCodeList icaosFamily;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (icao.getDesignator().endsWith(ends))
|
||||
{
|
||||
if (!code.matchesDesignator(d, cutoff, ¤t)) { continue; }
|
||||
if (current == 100.0) { return code; }
|
||||
if (best < current) { found = code; }
|
||||
icaosDesignator.push_back(icao);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByValidDesignator() const
|
||||
{
|
||||
return this->findBy([](const CAircraftIcaoCode & code)
|
||||
else if (icao.getFamily().endsWith(ends))
|
||||
{
|
||||
return code.hasValidDesignator();
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByInvalidDesignator() const
|
||||
{
|
||||
return this->findBy([](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return !code.hasValidDesignator();
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByDesignatorOrIataCode(const QString &icaoOrIata) const
|
||||
{
|
||||
if (icaoOrIata.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.matchesDesignatorOrIata(icaoOrIata);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByDesignatorIataOrFamily(const QString &icaoIataOrFamily) const
|
||||
{
|
||||
if (icaoIataOrFamily.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.matchesDesignatorIataOrFamily(icaoIataOrFamily);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findEndingWith(const QString &icaoEnding) const
|
||||
{
|
||||
const QString ends = icaoEnding.trimmed().toUpper();
|
||||
if (ends.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
CAircraftIcaoCodeList icaosDesignator;
|
||||
CAircraftIcaoCodeList icaosFamily;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (icao.getDesignator().endsWith(ends))
|
||||
{
|
||||
icaosDesignator.push_back(icao);
|
||||
}
|
||||
else if (icao.getFamily().endsWith(ends))
|
||||
{
|
||||
icaosFamily.push_back(icao);
|
||||
}
|
||||
icaosFamily.push_back(icao);
|
||||
}
|
||||
return icaosDesignator.isEmpty() ? icaosFamily : icaosDesignator;
|
||||
}
|
||||
return icaosDesignator.isEmpty() ? icaosFamily : icaosDesignator;
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByIataCode(const QString &iata, int fuzzySearch) const
|
||||
{
|
||||
if (iata.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.matchesIataCode(iata, fuzzySearch);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByFamily(const QString &family, int fuzzySearch) const
|
||||
{
|
||||
if (family.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.matchesFamily(family, fuzzySearch);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByManufacturer(const QString &manufacturer) const
|
||||
{
|
||||
if (manufacturer.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.getManufacturer().startsWith(manufacturer, Qt::CaseInsensitive);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByDescription(const QString &description) const
|
||||
{
|
||||
if (description.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.getModelDescription().startsWith(description, Qt::CaseInsensitive);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findMatchingByAnyDescription(const QString &description) const
|
||||
{
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.matchesAnyDescription(description);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findWithIataCode(bool removeWhenSameAsDesignator) const
|
||||
{
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
if (!code.hasIataCode()) { return false; }
|
||||
return !removeWhenSameAsDesignator || !code.isIataSameAsDesignator();
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findWithFamily(bool removeWhenSameAsDesignator) const
|
||||
{
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
if (!code.hasFamily()) { return false; }
|
||||
return !removeWhenSameAsDesignator || !code.isFamilySameAsDesignator();
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByMilitaryFlag(bool military) const
|
||||
{
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return (code.isMilitary() == military);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CAircraftIcaoCodeList::findFirstByDesignatorAndRank(const QString &designator) const
|
||||
{
|
||||
if (!CAircraftIcaoCode::isValidDesignator(designator)) { return CAircraftIcaoCode(); }
|
||||
CAircraftIcaoCodeList codes(this->findByDesignator(designator));
|
||||
if (codes.isEmpty()) { return CAircraftIcaoCode(); }
|
||||
if (codes.size() < 2) { return codes.front(); }
|
||||
codes.sortBy(&CAircraftIcaoCode::getRank, &CAircraftIcaoCode::getDbKey);
|
||||
return codes.front();
|
||||
}
|
||||
|
||||
bool CAircraftIcaoCodeList::containsDesignator(const QString &designator) const
|
||||
{
|
||||
if (designator.isEmpty()) { return false; }
|
||||
return this->contains(&CAircraftIcaoCode::getDesignator, designator);
|
||||
}
|
||||
|
||||
void CAircraftIcaoCodeList::sortByRank()
|
||||
{
|
||||
this->sortBy(&CAircraftIcaoCode::getRank);
|
||||
}
|
||||
|
||||
void CAircraftIcaoCodeList::sortByDesignatorAndRank()
|
||||
{
|
||||
this->sortBy(&CAircraftIcaoCode::getDesignator, &CAircraftIcaoCode::getRank);
|
||||
}
|
||||
|
||||
void CAircraftIcaoCodeList::sortByDesignatorManufacturerAndRank()
|
||||
{
|
||||
this->sortBy(&CAircraftIcaoCode::getDesignator, &CAircraftIcaoCode::getManufacturer, &CAircraftIcaoCode::getRank);
|
||||
}
|
||||
|
||||
void CAircraftIcaoCodeList::removeInvalidCombinedCodes()
|
||||
{
|
||||
this->removeIf([](const CAircraftIcaoCode & icao) { return !icao.hasValidCombinedType(); });
|
||||
}
|
||||
|
||||
void CAircraftIcaoCodeList::removeDuplicates()
|
||||
{
|
||||
this->removeIf(&CAircraftIcaoCode::isDbDuplicate, true);
|
||||
}
|
||||
|
||||
QStringList CAircraftIcaoCodeList::toCompleterStrings(bool withIataCodes, bool withFamily, bool withCategory, bool sort) const
|
||||
{
|
||||
QStringList c;
|
||||
CAircraftIcaoCodeList icaos(*this);
|
||||
if (sort) { icaos.sortByDesignatorAndRank(); }
|
||||
|
||||
// 3 steps to get a proper sort order of the string list
|
||||
for (const CAircraftIcaoCode &icao : std::as_const(icaos))
|
||||
{
|
||||
c.append(withCategory ? icao.getCombinedIcaoCategoryStringWithKey() : icao.getCombinedIcaoStringWithKey());
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByIataCode(const QString &iata, int fuzzySearch) const
|
||||
if (withFamily)
|
||||
{
|
||||
if (iata.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
const CAircraftIcaoCodeList icaosFamily = icaos.findWithFamily(true);
|
||||
for (const CAircraftIcaoCode &icao : icaosFamily)
|
||||
{
|
||||
return code.matchesIataCode(iata, fuzzySearch);
|
||||
});
|
||||
c.append(icao.getCombinedFamilyStringWithKey());
|
||||
}
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByFamily(const QString &family, int fuzzySearch) const
|
||||
if (withIataCodes)
|
||||
{
|
||||
if (family.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.matchesFamily(family, fuzzySearch);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByManufacturer(const QString &manufacturer) const
|
||||
{
|
||||
if (manufacturer.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.getManufacturer().startsWith(manufacturer, Qt::CaseInsensitive);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByDescription(const QString &description) const
|
||||
{
|
||||
if (description.isEmpty()) { return CAircraftIcaoCodeList(); }
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.getModelDescription().startsWith(description, Qt::CaseInsensitive);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findMatchingByAnyDescription(const QString &description) const
|
||||
{
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return code.matchesAnyDescription(description);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findWithIataCode(bool removeWhenSameAsDesignator) const
|
||||
{
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
if (!code.hasIataCode()) { return false; }
|
||||
return !removeWhenSameAsDesignator || !code.isIataSameAsDesignator();
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findWithFamily(bool removeWhenSameAsDesignator) const
|
||||
{
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
if (!code.hasFamily()) { return false; }
|
||||
return !removeWhenSameAsDesignator || !code.isFamilySameAsDesignator();
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::findByMilitaryFlag(bool military) const
|
||||
{
|
||||
return this->findBy([&](const CAircraftIcaoCode & code)
|
||||
{
|
||||
return (code.isMilitary() == military);
|
||||
});
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CAircraftIcaoCodeList::findFirstByDesignatorAndRank(const QString &designator) const
|
||||
{
|
||||
if (!CAircraftIcaoCode::isValidDesignator(designator)) { return CAircraftIcaoCode(); }
|
||||
CAircraftIcaoCodeList codes(this->findByDesignator(designator));
|
||||
if (codes.isEmpty()) { return CAircraftIcaoCode(); }
|
||||
if (codes.size() < 2) { return codes.front(); }
|
||||
codes.sortBy(&CAircraftIcaoCode::getRank, &CAircraftIcaoCode::getDbKey);
|
||||
return codes.front();
|
||||
}
|
||||
|
||||
bool CAircraftIcaoCodeList::containsDesignator(const QString &designator) const
|
||||
{
|
||||
if (designator.isEmpty()) { return false; }
|
||||
return this->contains(&CAircraftIcaoCode::getDesignator, designator);
|
||||
}
|
||||
|
||||
void CAircraftIcaoCodeList::sortByRank()
|
||||
{
|
||||
this->sortBy(&CAircraftIcaoCode::getRank);
|
||||
}
|
||||
|
||||
void CAircraftIcaoCodeList::sortByDesignatorAndRank()
|
||||
{
|
||||
this->sortBy(&CAircraftIcaoCode::getDesignator, &CAircraftIcaoCode::getRank);
|
||||
}
|
||||
|
||||
void CAircraftIcaoCodeList::sortByDesignatorManufacturerAndRank()
|
||||
{
|
||||
this->sortBy(&CAircraftIcaoCode::getDesignator, &CAircraftIcaoCode::getManufacturer, &CAircraftIcaoCode::getRank);
|
||||
}
|
||||
|
||||
void CAircraftIcaoCodeList::removeInvalidCombinedCodes()
|
||||
{
|
||||
this->removeIf([](const CAircraftIcaoCode & icao) { return !icao.hasValidCombinedType(); });
|
||||
}
|
||||
|
||||
void CAircraftIcaoCodeList::removeDuplicates()
|
||||
{
|
||||
this->removeIf(&CAircraftIcaoCode::isDbDuplicate, true);
|
||||
}
|
||||
|
||||
QStringList CAircraftIcaoCodeList::toCompleterStrings(bool withIataCodes, bool withFamily, bool withCategory, bool sort) const
|
||||
{
|
||||
QStringList c;
|
||||
CAircraftIcaoCodeList icaos(*this);
|
||||
if (sort) { icaos.sortByDesignatorAndRank(); }
|
||||
|
||||
// 3 steps to get a proper sort order of the string list
|
||||
icaos = icaos.findWithIataCode(true);
|
||||
if (sort) { icaos.sortBy(&CAircraftIcaoCode::getIataCode, &CAircraftIcaoCode::getRank); }
|
||||
for (const CAircraftIcaoCode &icao : std::as_const(icaos))
|
||||
{
|
||||
c.append(withCategory ? icao.getCombinedIcaoCategoryStringWithKey() : icao.getCombinedIcaoStringWithKey());
|
||||
c.append(icao.getCombinedIataStringWithKey());
|
||||
}
|
||||
}
|
||||
|
||||
if (withFamily)
|
||||
return c;
|
||||
}
|
||||
|
||||
QSet<QString> CAircraftIcaoCodeList::allDesignators(bool noUnspecified) const
|
||||
{
|
||||
CSetBuilder<QString> c;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (noUnspecified && !icao.hasKnownDesignator()) { continue; }
|
||||
const QString d(icao.getDesignator());
|
||||
c.insert(d);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
QSet<QString> CAircraftIcaoCodeList::allDesignatorsAndKey(bool noUnspecified) const
|
||||
{
|
||||
CSetBuilder<QString> c;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (noUnspecified && !icao.hasKnownDesignator()) { continue; }
|
||||
const QString d(icao.getDesignatorDbKey());
|
||||
c.insert(d);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
QSet<QString> CAircraftIcaoCodeList::allFamilies() const
|
||||
{
|
||||
CSetBuilder<QString> c;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (!icao.hasFamily()) { continue; }
|
||||
const QString d(icao.getFamily());
|
||||
c.insert(d);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
QSet<QString> CAircraftIcaoCodeList::allManufacturers(bool onlyKnownDesignators) const
|
||||
{
|
||||
CSetBuilder<QString> c;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (onlyKnownDesignators && !icao.hasKnownDesignator()) { continue; }
|
||||
const QString m(icao.getManufacturer());
|
||||
if (m.isEmpty()) { continue; }
|
||||
c.insert(m); // checks if already contains m
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
QMap<QString, int> CAircraftIcaoCodeList::countManufacturers() const
|
||||
{
|
||||
QMap<QString, int> count;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (!icao.hasManufacturer()) { continue; }
|
||||
count[icao.getManufacturer()]++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
QPair<QString, int> CAircraftIcaoCodeList::maxCountManufacturer() const
|
||||
{
|
||||
const QMap<QString, int> counts(countManufacturers());
|
||||
if (counts.isEmpty()) return { {}, 0 };
|
||||
const auto pair = *std::max_element(counts.keyValueBegin(), counts.keyValueEnd(), [](const auto & a, const auto & b)
|
||||
{
|
||||
return a.second < b.second;
|
||||
});
|
||||
return { pair.first, pair.second };
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::fromDatabaseJson(const QJsonArray &array, const CAircraftCategoryList &categories, bool ignoreIncompleteAndDuplicates, CAircraftIcaoCodeList *inconsistent)
|
||||
{
|
||||
CAircraftIcaoCodeList codes;
|
||||
for (const QJsonValue &value : array)
|
||||
{
|
||||
CAircraftIcaoCode icao(CAircraftIcaoCode::fromDatabaseJson(value.toObject()));
|
||||
const int catId = icao.getCategory().getDbKey();
|
||||
if (!categories.isEmpty() && catId >= 0)
|
||||
{
|
||||
const CAircraftIcaoCodeList icaosFamily = icaos.findWithFamily(true);
|
||||
for (const CAircraftIcaoCode &icao : icaosFamily)
|
||||
const CAircraftCategory category = categories.findByKey(catId);
|
||||
if (!category.isNull())
|
||||
{
|
||||
c.append(icao.getCombinedFamilyStringWithKey());
|
||||
icao.setCategory(category);
|
||||
}
|
||||
}
|
||||
|
||||
if (withIataCodes)
|
||||
if (!icao.hasSpecialDesignator() && !icao.hasCompleteData())
|
||||
{
|
||||
icaos = icaos.findWithIataCode(true);
|
||||
if (sort) { icaos.sortBy(&CAircraftIcaoCode::getIataCode, &CAircraftIcaoCode::getRank); }
|
||||
for (const CAircraftIcaoCode &icao : std::as_const(icaos))
|
||||
if (ignoreIncompleteAndDuplicates) { continue; }
|
||||
if (inconsistent)
|
||||
{
|
||||
c.append(icao.getCombinedIataStringWithKey());
|
||||
inconsistent->push_back(icao);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
QSet<QString> CAircraftIcaoCodeList::allDesignators(bool noUnspecified) const
|
||||
{
|
||||
CSetBuilder<QString> c;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
if (icao.isDbDuplicate())
|
||||
{
|
||||
if (noUnspecified && !icao.hasKnownDesignator()) { continue; }
|
||||
const QString d(icao.getDesignator());
|
||||
c.insert(d);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
QSet<QString> CAircraftIcaoCodeList::allDesignatorsAndKey(bool noUnspecified) const
|
||||
{
|
||||
CSetBuilder<QString> c;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (noUnspecified && !icao.hasKnownDesignator()) { continue; }
|
||||
const QString d(icao.getDesignatorDbKey());
|
||||
c.insert(d);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
QSet<QString> CAircraftIcaoCodeList::allFamilies() const
|
||||
{
|
||||
CSetBuilder<QString> c;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (!icao.hasFamily()) { continue; }
|
||||
const QString d(icao.getFamily());
|
||||
c.insert(d);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
QSet<QString> CAircraftIcaoCodeList::allManufacturers(bool onlyKnownDesignators) const
|
||||
{
|
||||
CSetBuilder<QString> c;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (onlyKnownDesignators && !icao.hasKnownDesignator()) { continue; }
|
||||
const QString m(icao.getManufacturer());
|
||||
if (m.isEmpty()) { continue; }
|
||||
c.insert(m); // checks if already contains m
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
QMap<QString, int> CAircraftIcaoCodeList::countManufacturers() const
|
||||
{
|
||||
QMap<QString, int> count;
|
||||
for (const CAircraftIcaoCode &icao : *this)
|
||||
{
|
||||
if (!icao.hasManufacturer()) { continue; }
|
||||
count[icao.getManufacturer()]++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
QPair<QString, int> CAircraftIcaoCodeList::maxCountManufacturer() const
|
||||
{
|
||||
const QMap<QString, int> counts(countManufacturers());
|
||||
if (counts.isEmpty()) return { {}, 0 };
|
||||
const auto pair = *std::max_element(counts.keyValueBegin(), counts.keyValueEnd(), [](const auto & a, const auto & b)
|
||||
{
|
||||
return a.second < b.second;
|
||||
});
|
||||
return { pair.first, pair.second };
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::fromDatabaseJson(const QJsonArray &array, const CAircraftCategoryList &categories, bool ignoreIncompleteAndDuplicates, CAircraftIcaoCodeList *inconsistent)
|
||||
{
|
||||
CAircraftIcaoCodeList codes;
|
||||
for (const QJsonValue &value : array)
|
||||
{
|
||||
CAircraftIcaoCode icao(CAircraftIcaoCode::fromDatabaseJson(value.toObject()));
|
||||
const int catId = icao.getCategory().getDbKey();
|
||||
if (!categories.isEmpty() && catId >= 0)
|
||||
if (ignoreIncompleteAndDuplicates) { continue; }
|
||||
if (inconsistent)
|
||||
{
|
||||
const CAircraftCategory category = categories.findByKey(catId);
|
||||
if (!category.isNull())
|
||||
{
|
||||
icao.setCategory(category);
|
||||
}
|
||||
inconsistent->push_back(icao);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!icao.hasSpecialDesignator() && !icao.hasCompleteData())
|
||||
{
|
||||
if (ignoreIncompleteAndDuplicates) { continue; }
|
||||
if (inconsistent)
|
||||
{
|
||||
inconsistent->push_back(icao);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (icao.isDbDuplicate())
|
||||
{
|
||||
if (ignoreIncompleteAndDuplicates) { continue; }
|
||||
if (inconsistent)
|
||||
{
|
||||
inconsistent->push_back(icao);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
codes.push_back(icao);
|
||||
}
|
||||
return codes;
|
||||
codes.push_back(icao);
|
||||
}
|
||||
return codes;
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CAircraftIcaoCodeList::smartAircraftIcaoSelector(const CAircraftIcaoCode &icaoPattern) const
|
||||
{
|
||||
if (icaoPattern.hasValidDbKey())
|
||||
{
|
||||
const int k = icaoPattern.getDbKey();
|
||||
const CAircraftIcaoCode c(this->findByKey(k));
|
||||
if (c.hasCompleteData()) { return c; }
|
||||
}
|
||||
|
||||
CAircraftIcaoCode CAircraftIcaoCodeList::smartAircraftIcaoSelector(const CAircraftIcaoCode &icaoPattern) const
|
||||
// get an initial set of data we can choose from
|
||||
const QString designator(icaoPattern.getDesignator());
|
||||
if (designator.isEmpty()) { return CAircraftIcaoCode(); }
|
||||
CAircraftIcaoCodeList codes;
|
||||
do
|
||||
{
|
||||
if (icaoPattern.hasValidDbKey())
|
||||
{
|
||||
const int k = icaoPattern.getDbKey();
|
||||
const CAircraftIcaoCode c(this->findByKey(k));
|
||||
if (c.hasCompleteData()) { return c; }
|
||||
}
|
||||
codes = this->findByDesignator(designator);
|
||||
if (!codes.isEmpty()) break;
|
||||
|
||||
// get an initial set of data we can choose from
|
||||
const QString designator(icaoPattern.getDesignator());
|
||||
if (designator.isEmpty()) { return CAircraftIcaoCode(); }
|
||||
CAircraftIcaoCodeList codes;
|
||||
do
|
||||
{
|
||||
codes = this->findByDesignator(designator);
|
||||
if (!codes.isEmpty()) break;
|
||||
// now we search if the ICAO designator is actually an IATA code
|
||||
codes = this->findByIataCode(designator);
|
||||
if (!codes.isEmpty()) break;
|
||||
|
||||
// now we search if the ICAO designator is actually an IATA code
|
||||
codes = this->findByIataCode(designator);
|
||||
if (!codes.isEmpty()) break;
|
||||
// search fuzzy and restrict length
|
||||
const CAircraftIcaoCode bestMatch = this->findBestFuzzyMatchOrDefault(designator.length() < 5 ? designator : designator.left(5), 70);
|
||||
if (bestMatch.hasValidDesignator()) { return bestMatch; }
|
||||
|
||||
// search fuzzy and restrict length
|
||||
const CAircraftIcaoCode bestMatch = this->findBestFuzzyMatchOrDefault(designator.length() < 5 ? designator : designator.left(5), 70);
|
||||
if (bestMatch.hasValidDesignator()) { return bestMatch; }
|
||||
// still empty, try to find by family
|
||||
codes = this->findByFamily(designator);
|
||||
if (!codes.isEmpty()) break;
|
||||
|
||||
// still empty, try to find by family
|
||||
codes = this->findByFamily(designator);
|
||||
if (!codes.isEmpty()) break;
|
||||
// by any description
|
||||
codes = this->findMatchingByAnyDescription(designator);
|
||||
}
|
||||
while (false);
|
||||
|
||||
// by any description
|
||||
codes = this->findMatchingByAnyDescription(designator);
|
||||
}
|
||||
while (false);
|
||||
if (codes.isEmpty()) { return icaoPattern; }
|
||||
if (codes.size() == 1) { return codes.front(); }
|
||||
|
||||
if (codes.isEmpty()) { return icaoPattern; }
|
||||
// further reduce by manufacturer
|
||||
codes.sortByRank();
|
||||
if (icaoPattern.hasManufacturer() && codes.contains(&CAircraftIcaoCode::getManufacturer, icaoPattern.getManufacturer()))
|
||||
{
|
||||
const QString m(icaoPattern.getManufacturer());
|
||||
codes = codes.findByManufacturer(m);
|
||||
if (codes.size() == 1) { return codes.front(); }
|
||||
|
||||
// further reduce by manufacturer
|
||||
codes.sortByRank();
|
||||
if (icaoPattern.hasManufacturer() && codes.contains(&CAircraftIcaoCode::getManufacturer, icaoPattern.getManufacturer()))
|
||||
{
|
||||
const QString m(icaoPattern.getManufacturer());
|
||||
codes = codes.findByManufacturer(m);
|
||||
if (codes.size() == 1) { return codes.front(); }
|
||||
|
||||
// intentionally continue here
|
||||
}
|
||||
|
||||
// further reduce by IATA
|
||||
if (icaoPattern.hasIataCode() && codes.contains(&CAircraftIcaoCode::getIataCode, icaoPattern.getIataCode()))
|
||||
{
|
||||
const QString i(icaoPattern.getIataCode());
|
||||
codes = codes.findByIataCode(i);
|
||||
if (codes.size() == 1) { return codes.front(); }
|
||||
|
||||
// intentionally continue here
|
||||
}
|
||||
|
||||
// lucky punch on description?
|
||||
if (icaoPattern.hasModelDescription() && codes.contains(&CAircraftIcaoCode::getModelDescription, icaoPattern.getModelDescription()))
|
||||
{
|
||||
// do not affect codes here, it might return no results
|
||||
const QString d(icaoPattern.getModelDescription());
|
||||
CAircraftIcaoCodeList cm(codes.findByDescription(d));
|
||||
if (cm.size() == 1) { return cm.front(); }
|
||||
}
|
||||
return codes.frontOrDefault(); // sorted by rank
|
||||
// intentionally continue here
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::groupByDesignatorAndManufacturer() const
|
||||
// further reduce by IATA
|
||||
if (icaoPattern.hasIataCode() && codes.contains(&CAircraftIcaoCode::getIataCode, icaoPattern.getIataCode()))
|
||||
{
|
||||
CAircraftIcaoCodeList copy(*this);
|
||||
copy.sortByDesignatorManufacturerAndRank();
|
||||
CAircraftIcaoCodeList grouped; // will contain the entries with the best rank
|
||||
QString designator;
|
||||
QString manufacturer;
|
||||
for (const CAircraftIcaoCode &code : std::as_const(copy))
|
||||
{
|
||||
if (code.getDesignator() != designator || code.getManufacturer() != manufacturer)
|
||||
{
|
||||
designator = code.getDesignator();
|
||||
manufacturer = code.getManufacturer();
|
||||
grouped.push_back(code);
|
||||
}
|
||||
}
|
||||
return grouped;
|
||||
const QString i(icaoPattern.getIataCode());
|
||||
codes = codes.findByIataCode(i);
|
||||
if (codes.size() == 1) { return codes.front(); }
|
||||
|
||||
// intentionally continue here
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// lucky punch on description?
|
||||
if (icaoPattern.hasModelDescription() && codes.contains(&CAircraftIcaoCode::getModelDescription, icaoPattern.getModelDescription()))
|
||||
{
|
||||
// do not affect codes here, it might return no results
|
||||
const QString d(icaoPattern.getModelDescription());
|
||||
CAircraftIcaoCodeList cm(codes.findByDescription(d));
|
||||
if (cm.size() == 1) { return cm.front(); }
|
||||
}
|
||||
return codes.frontOrDefault(); // sorted by rank
|
||||
}
|
||||
|
||||
CAircraftIcaoCodeList CAircraftIcaoCodeList::groupByDesignatorAndManufacturer() const
|
||||
{
|
||||
CAircraftIcaoCodeList copy(*this);
|
||||
copy.sortByDesignatorManufacturerAndRank();
|
||||
CAircraftIcaoCodeList grouped; // will contain the entries with the best rank
|
||||
QString designator;
|
||||
QString manufacturer;
|
||||
for (const CAircraftIcaoCode &code : std::as_const(copy))
|
||||
{
|
||||
if (code.getDesignator() != designator || code.getManufacturer() != manufacturer)
|
||||
{
|
||||
designator = code.getDesignator();
|
||||
manufacturer = code.getManufacturer();
|
||||
grouped.push_back(code);
|
||||
}
|
||||
}
|
||||
return grouped;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user