KPublicTransport

featureutil.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "featureutil_p.h"
7
8using namespace KPublicTransport;
9
10static bool lessThanFeature(const Feature &lhs, const Feature &rhs)
11{
12 if (lhs.type() == rhs.type() && lhs.type() == Feature::Other) {
13 return lhs.name().compare(rhs.name(), Qt::CaseInsensitive) < 0;
14 }
15 return lhs.type() < rhs.type();
16}
17
18void FeatureUtil::set(std::vector<Feature> &member, std::vector<Feature> &&features)
19{
20 std::sort(features.begin(), features.end(), lessThanFeature);
21 member = std::move(features);
22}
23
24Feature FeatureUtil::findByType(const std::vector<Feature> &features, Feature::Type type)
25{
26 const auto it = std::find_if(features.begin(), features.end(), [type](const auto &f) { return f.type() == type; });
27 if (it != features.end()) {
28 return *it;
29 }
30 return {};
31}
32
33std::vector<Feature> FeatureUtil::merge(const std::vector<Feature> &lhs, const std::vector<Feature> &rhs)
34{
35 std::vector<Feature> res;
36 auto lit = lhs.begin();
37 auto rit = rhs.begin();
38
39 while (lit != lhs.end() || rit != rhs.end()) {
40 if (lit == lhs.end()) {
41 res.push_back(*rit++);
42 continue;;
43 }
44 if (rit == rhs.end()) {
45 res.push_back(*lit++);
46 continue;
47 }
48
49 if (Feature::isSame(*lit, *rit)) {
50 res.push_back(Feature::merge(*lit++, *rit++));
51 } else if ((*lit).type() < (*rit).type()) {
52 res.push_back(*lit++);
53 } else {
54 res.push_back(*rit++);
55 }
56 }
57
58 return res;
59}
60
61void FeatureUtil::add(std::vector<Feature> &features, Feature &&feature)
62{
63 if (feature.type() == Feature::NoFeature) {
64 return;
65 }
66
67 auto it = std::lower_bound(features.begin(), features.end(), feature, lessThanFeature);
68 if (it != features.end() && Feature::isSame(*it, feature)) {
69 *it = Feature::merge(*it, feature);
70 } else {
71 features.insert(it, std::move(feature));
72 }
73}
74
75static Feature aggregate(const Feature &lhs, const Feature &rhs)
76{
77 if (lhs.availability() == Feature::Unavailable && (rhs.availability() != Feature::Unavailable && rhs.availability() != Feature::Unknown)) {
78 return rhs;
79 }
80 if (rhs.availability() == Feature::Unavailable && (lhs.availability() != Feature::Unavailable && lhs.availability() != Feature::Unknown)) {
81 return lhs;
82 }
83
84 auto f = Feature::merge(lhs, rhs);
85 f.setQuantity(lhs.quantity() + rhs.quantity());
86 return f;
87}
88
89void FeatureUtil::aggregate(std::vector<Feature> &features, const Feature &feature)
90{
91 auto it = std::lower_bound(features.begin(), features.end(), feature, lessThanFeature);
92 if (it != features.end() && Feature::isSame(*it, feature)) {
93 *it = ::aggregate(*it, feature);
94 } else {
95 features.insert(it, feature);
96 }
97}
An amenity, facility or other relevant property of a vehicle (train, bus, etc), vehicle part (e....
Definition feature.h:20
static Feature merge(const Feature &lhs, const Feature &rhs)
Merge two features referring to the same thing.
Definition feature.cpp:135
QString name
A short human-readable label of this feature.
Definition feature.h:24
static bool isSame(const Feature &lhs, const Feature &rhs)
Returns true if both features refer to the same thing.
Definition feature.cpp:110
int quantity
Amount of instances of this feature.
Definition feature.h:77
std::vector< Feature > features(QStringView coachNumber, QStringView coachClassification)
Determine coach features from a UIC coachNumber and/or coachClassification.
Query operations and data types for accessing realtime public transport information from online servi...
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
CaseInsensitive
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:47:54 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.