swift
Loading...
Searching...
No Matches
callsignobjectlist.h
Go to the documentation of this file.
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#ifndef SWIFT_MISC_AVIATION_CALLSIGNOBJECTLIST_H
7#define SWIFT_MISC_AVIATION_CALLSIGNOBJECTLIST_H
8
9#include <QHash>
10#include <QMap>
11#include <QtGlobal>
12
14#include "misc/predicates.h"
16
17namespace swift::misc::aviation
18{
20 template <class OBJ, class CONTAINER>
22 {
23 public:
25 bool containsCallsign(const CCallsign &callsign) const
26 {
27 return this->container().contains(&OBJ::getCallsign, callsign);
28 }
29
31 int applyIfCallsign(const CCallsign &callsign, const CPropertyIndexVariantMap &variantMap,
32 bool skipEqualValues = true)
33 {
34 return this->container().applyIf(&OBJ::getCallsign, callsign, variantMap, skipEqualValues);
35 }
36
39 {
40 CCallsignSet cs;
41 for (const OBJ &obj : this->container())
42 {
43 cs.push_back(obj.getCallsign());
44 }
45 return cs;
46 }
47
49 QStringList getCallsignStrings(bool sorted = false) const
50 {
51 return this->getCallsigns().getCallsignStrings(sorted);
52 }
53
55 QString getCallsignsAsString(const QString &separator, bool sorted = false) const
56 {
57 if (this->container().isEmpty())
58 {
59 return {};
60 }
61 const QStringList callsigns = this->getCallsignStrings(sorted);
62 return callsigns.join(separator);
63 }
64
66 CONTAINER findByCallsign(const CCallsign &callsign) const
67 {
68 return this->container().findBy(&OBJ::getCallsign, callsign);
69 }
70
72 CONTAINER findByCallsigns(const CCallsignSet &callsigns) const
73 {
74 return this->container().findBy(predicates::MemberIsAnyOf(&OBJ::getCallsign, callsigns));
75 }
76
78 OBJ findFirstByCallsign(const CCallsign &callsign, const OBJ &ifNotFound = {}) const
79 {
80 return this->container().findFirstByOrDefault(&OBJ::getCallsign, callsign, ifNotFound);
81 }
82
84 OBJ findLastByCallsign(const CCallsign &callsign, const OBJ &ifNotFound = {}) const
85 {
86 for (auto current = container().end(); current != container().begin(); /* Do nothing */)
87 {
88 --current;
89 if (current->getCallsign() == callsign)
90 {
91 return *current;
92 }
93 }
94 return ifNotFound;
95 }
96
98 CONTAINER findBySuffix(const QString &suffix) const
99 {
100 CONTAINER r;
101 if (suffix.isEmpty())
102 {
103 return r;
104 }
105 const QString sfxUpper(suffix.trimmed().toUpper());
106 r = this->container().findBy(
107 [=](const OBJ &csObj) { return (csObj.getCallsign().getSuffix() == sfxUpper); });
108 return r;
109 }
110
112 int firstIndexOfCallsign(const CCallsign &callsign)
113 {
114 for (int i = 0; i < this->container().size(); i++)
115 {
116 if (this->container()[i].getCallsign() == callsign)
117 {
118 return i;
119 }
120 }
121 return -1;
122 }
123
125 int removeByCallsign(const CCallsign &callsign)
126 {
127 return this->container().removeIf(&OBJ::getCallsign, callsign);
128 }
129
131 int removeByCallsigns(const CCallsignSet &callsigns)
132 {
133 return this->container().removeIf([&](const OBJ &obj) { return callsigns.contains(obj.getCallsign()); });
134 }
135
139 {
140 QMap<QString, int> r; // sorted by key
141 for (const OBJ &csObj : this->container())
142 {
143 const QString s = csObj.getCallsign().getSuffix();
144 if (s.isEmpty())
145 {
146 continue;
147 }
148 if (r.contains(s))
149 {
150 r[s] = r[s] + 1;
151 }
152 else
153 {
154 r.insert(s, 1);
155 }
156 }
157 return r;
158 }
159
163 {
164 QStringList suffixes;
165 for (const OBJ &csObj : this->container())
166 {
167 const QString s = csObj.getCallsign().getSuffix();
168 if (s.isEmpty() || suffixes.contains(s, Qt::CaseInsensitive))
169 {
170 continue;
171 }
172 suffixes << s;
173 }
174 return suffixes;
175 }
176
179 {
180 CONTAINER copyContainer(container());
181 copyContainer.sortByCallsign();
183 CCallsign cs;
184 for (const OBJ &csObj : copyContainer)
185 {
186 if (csObj.getCallsign().isEmpty())
187 {
188 Q_ASSERT(false); // there should be no empty callsigns
189 continue;
190 }
191 if (cs != csObj.getCallsign())
192 {
193 cs = csObj.getCallsign();
194 CONTAINER perCallsign({ csObj });
195 result.insert(cs, perCallsign);
196 }
197 else
198 {
199 result[cs].push_back(csObj);
200 }
201 }
202 return result;
203 }
204
206 int replaceOrAddObjectByCallsign(const OBJ &otherObject)
207 {
208 const CCallsign cs(otherObject.getCallsign());
209 if (cs.isEmpty())
210 {
211 return 0;
212 }
213 CONTAINER &copy(this->container());
214 copy.removeByCallsign(cs);
215 copy.push_back(otherObject);
216 return 1;
217 }
218
220 int replaceOrAddObjectsByCallsign(const CONTAINER &others)
221 {
222 if (others.isEmpty())
223 {
224 return 0;
225 }
226 int c = 0;
227 CONTAINER copy(this->container());
228 for (const OBJ &obj : others)
229 {
230 const CCallsign cs(obj.getCallsign());
231 if (cs.isEmpty())
232 {
233 continue;
234 }
235 copy.removeByCallsign(cs);
236 copy.push_back(obj);
237 c++;
238 }
239 *this = copy;
240 return c;
241 }
242
244 int incrementalUpdateOrAdd(const OBJ &objectBeforeChanges, const CPropertyIndexVariantMap &changedValues)
245 {
246 int c {};
247 const CCallsign cs = objectBeforeChanges.getCallsign();
248 if (this->containsCallsign(cs))
249 {
250 if (changedValues.isEmpty())
251 {
252 return 0;
253 }
254 c = this->container().applyIf(&OBJ::getCallsign, cs, changedValues);
255 }
256 else
257 {
258 c = 1;
259 if (changedValues.isEmpty())
260 {
261 this->container().push_back(objectBeforeChanges);
262 }
263 else
264 {
265 OBJ objectAdded(objectBeforeChanges);
266 objectAdded.apply(changedValues);
267 this->container().push_back(objectAdded);
268 }
269 }
270 return c;
271 }
272
274 void sortByCallsign() { container().sortBy(&OBJ::getCallsign); }
275
278 {
280 for (const OBJ &obj : this->container())
281 {
282 map.insert(obj.getCallsign(), obj);
283 }
284 return map;
285 }
286
289 {
291 for (const OBJ &obj : this->container())
292 {
293 if (obj.getCallsign().isEmpty())
294 {
295 continue;
296 }
297 hash.insert(obj.getCallsign(), obj);
298 }
299 return hash;
300 }
301
303 CONTAINER sortedByCallsign() const
304 {
305 CONTAINER copy(this->container());
306 copy.sortByCallsign();
307 return copy;
308 }
309
310 protected:
312 ICallsignObjectList() {} // NOLINT(modernize-use-equals-default)
313
315 const CONTAINER &container() const { return static_cast<const CONTAINER &>(*this); }
316
318 CONTAINER &container() { return static_cast<CONTAINER &>(*this); }
319 };
320} // namespace swift::misc::aviation
321
322#endif // SWIFT_MISC_AVIATION_CALLSIGNOBJECTLIST_H
iterator push_back(const T &value)
Synonym for insert.
Definition collection.h:244
Specialized value object compliant map for variants, based on indexes.
bool contains(const T &object) const
Return true if there is an element equal to given object. Uses the most efficient implementation avai...
Definition range.h:109
Value object encapsulating information of a callsign.
Definition callsign.h:30
bool isEmpty() const
Is empty?
Definition callsign.h:63
Value object for a set of callsigns.
Definition callsignset.h:26
QStringList getCallsignStrings(bool sorted=false) const
The callsign strings.
int removeByCallsigns(const CCallsignSet &callsigns)
Remove all objects with callsigns.
int firstIndexOfCallsign(const CCallsign &callsign)
First found index of callsign, otherwise -1.
int replaceOrAddObjectsByCallsign(const CONTAINER &others)
Replace or add objects by callsign.
CONTAINER findByCallsign(const CCallsign &callsign) const
Find 0..n stations by callsign.
OBJ findLastByCallsign(const CCallsign &callsign, const OBJ &ifNotFound={}) const
Find the back object by callsign, if none return given one.
OBJ findFirstByCallsign(const CCallsign &callsign, const OBJ &ifNotFound={}) const
Find the first aircraft by callsign, if none return given one.
QMap< CCallsign, OBJ > asCallsignMap() const
Turn into callsign map.
const CONTAINER & container() const
Container.
int applyIfCallsign(const CCallsign &callsign, const CPropertyIndexVariantMap &variantMap, bool skipEqualValues=true)
Apply for given callsign.
QStringList getSuffixes() const
All suffixes, in the order of the list.
CONTAINER findBySuffix(const QString &suffix) const
All with given suffix, empty suffixes ignored.
int removeByCallsign(const CCallsign &callsign)
Remove all objects with callsign.
int replaceOrAddObjectByCallsign(const OBJ &otherObject)
Replace or add objects by callsign.
QHash< CCallsign, OBJ > asCallsignHash() const
Turn into callsign hash.
CONTAINER sortedByCallsign() const
Copy of list sorted by callsign.
int incrementalUpdateOrAdd(const OBJ &objectBeforeChanges, const CPropertyIndexVariantMap &changedValues)
Incremental update or add object.
QString getCallsignsAsString(const QString &separator, bool sorted=false) const
Get callsigns as strings.
QHash< CCallsign, CONTAINER > splitPerCallsign() const
Split into 0..n containers as per callsign.
swift::misc::aviation::CCallsignSet getCallsigns() const
All callsigns.
bool containsCallsign(const CCallsign &callsign) const
Contains callsign?
QMap< QString, int > getSuffixesAndCount() const
All suffixes with their respective count.
CONTAINER findByCallsigns(const CCallsignSet &callsigns) const
Find 0..n aircraft matching any of a set of callsigns.
QStringList getCallsignStrings(bool sorted=false) const
Get callsign string list.
auto MemberIsAnyOf(T memberFunc, const C &container)
Returns a predicate that returns true if the value returned by its argument's member function can be ...
Definition predicates.h:75
T::const_iterator end(const LockFreeReader< T > &reader)
Non-member begin() and end() for so LockFree containers can be used in ranged for loops.
Definition lockfree.h:264
QHash< Key, T >::iterator insert(Key &&key, T &&value)
bool contains(const Key &key) const const
QMap< Key, T >::iterator insert(QMap< Key, T >::const_iterator pos, const Key &key, const T &value)
bool isEmpty() const const
QString toUpper() const const
QString trimmed() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QString join(QChar separator) const const
CaseInsensitive