swift
vpilotmodelruleset.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <tuple>
7 
8 #include <QList>
9 #include <QSet>
10 #include <QStringList>
11 
12 #include "misc/range.h"
14 
15 SWIFT_DEFINE_COLLECTION_MIXINS(swift::misc::simulation::fscommon, CVPilotModelRule, CVPilotModelRuleSet)
16 
17 namespace swift::misc::simulation::fscommon
18 {
21  {}
22 
24  {
25  return this->findBy(&CVPilotModelRule::getModelName, modelName.trimmed().toUpper());
26  }
27 
29  {
30  return this->findFirstByOrDefault(&CVPilotModelRule::getModelName, modelName.trimmed().toUpper());
31  }
32 
34  {
35  QString mn(modelName.trimmed().toUpper());
36  return this->findBy(
37  [=](const CVPilotModelRule &rule) { return rule.getModelName().startsWith(mn, Qt::CaseInsensitive); });
38  }
39 
40  QStringList CVPilotModelRuleSet::toUpper(const QStringList &stringList)
41  {
42  QStringList upper;
43  upper.reserve(stringList.size());
44  for (const QString &s : stringList) { upper.append(s.toUpper()); }
45  return upper;
46  }
47 
49  {
50  QStringList ms;
51  ms.reserve(size());
52  for (const CVPilotModelRule &rule : (*this)) { ms.append(rule.getModelName()); }
53  ms.sort(Qt::CaseInsensitive);
54  return ms;
55  }
56 
58  {
59  QStringList distributors;
60  for (const CVPilotModelRule &rule : (*this))
61  {
62  QString d(rule.getDistributor());
63  if (distributors.contains(d)) { continue; }
64  distributors.append(d);
65  }
66  distributors.sort(Qt::CaseInsensitive);
67  return distributors;
68  }
69 
70  int CVPilotModelRuleSet::removeModels(const QStringList &modelsToBeRemoved)
71  {
72  QStringList knownModels(getSortedModelNames());
73  if (knownModels.isEmpty()) { return 0; }
74 
75  QStringList remove(toUpper(modelsToBeRemoved));
76  remove.sort();
77 
78  QSet<QString> removeSet(knownModels.begin(), knownModels.end());
79  removeSet &= QSet<QString>(remove.begin(), remove.end());
80  int c = 0;
81  for (const QString &model : std::as_const(removeSet))
82  {
83  c += this->removeIf(&CVPilotModelRule::getModelName, model);
84  }
85  return c;
86  }
87 
88  int CVPilotModelRuleSet::keepModels(const QStringList &modelsToBeKept)
89  {
90  QStringList knownModels(getSortedModelNames());
91  if (knownModels.isEmpty()) { return 0; }
92 
93  QStringList keep(toUpper(modelsToBeKept));
94  keep.sort();
95 
96  QSet<QString> removeSet(knownModels.begin(), knownModels.end());
97  removeSet.subtract(QSet<QString>(keep.begin(), keep.end()));
98  int c = 0;
99  for (const QString &model : removeSet) { c += this->removeIf(&CVPilotModelRule::getModelName, model); }
100  return c;
101  }
102 
104  {
105  QStringList modelNames;
106  CAircraftModelList models;
107  for (const CVPilotModelRule &rule : *this)
108  {
109  QString m(rule.getModelName());
110  if (m.isEmpty()) { continue; }
111  if (modelNames.contains(m, Qt::CaseInsensitive))
112  {
113  CAircraftModel model(rule.toAircraftModel());
114  for (CAircraftModel &exisitingModel : models)
115  {
116  if (!exisitingModel.matchesModelString(m, Qt::CaseInsensitive)) { continue; }
117  exisitingModel.updateMissingParts(model);
118  break;
119  }
120  }
121  else
122  {
123  models.push_back(rule.toAircraftModel());
124  modelNames.append(m);
125  }
126  }
127  return models;
128  }
129 } // namespace swift::misc::simulation::fscommon
size_type size() const
Returns number of elements in the collection.
Definition: collection.h:185
void remove(const CVPilotModelRule &object)
Efficient remove using the find and erase of the implementation container. Typically O(log n).
Definition: collection.h:307
int removeIf(Predicate p)
Remove elements for which a given predicate returns true.
Definition: collection.h:321
auto findBy(Predicate p) const
Return a copy containing only those elements for which a given predicate returns true.
Definition: range.h:410
auto findFirstByOrDefault(Predicate p, const Value &def) const
Return a copy of the first element for which a given predicate returns true, or a default value if th...
Definition: range.h:70
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
Aircraft model (used by another pilot, my models on disk)
Definition: aircraftmodel.h:71
Value object encapsulating a list of aircraft models.
Value object encapsulating information of software distributor.
CAircraftModel toAircraftModel() const
Convert into aircraft model.
const QString & getModelName() const
Get model name.
Value object reading a set of vPilot rules.
int removeModels(const QStringList &modelsToBeRemoved)
Removed given models.
QStringList getSortedDistributors() const
List of distributors.
simulation::CAircraftModelList toAircraftModels() const
To aircraft models.
CVPilotModelRuleSet findModelsStartingWith(const QString &modelName) const
Find models starting with.
int keepModels(const QStringList &modelsToBeKept)
Keep given models (if in list)
CVPilotModelRuleSet findByModelName(const QString &modelName) const
Find by model string.
CVPilotModelRule findFirstByModelName(const QString &modelName) const
Find first by model string.
#define SWIFT_DEFINE_COLLECTION_MIXINS(Namespace, T, Set)
Explicit template definition of mixins for a CCollection subclass.
Definition: collection.h:63