KPublicTransport

attributionutil.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "attributionutil_p.h"
8
9#include <KPublicTransport/Attribution>
10
11#include <QUrl>
12
13using namespace KPublicTransport;
14
15static bool attrLessThan(const Attribution &lhs, const Attribution &rhs)
16{
17 const auto nameCmp = QString::compare(lhs.name(), rhs.name(), Qt::CaseInsensitive);
18 if (nameCmp == 0) {
19 return QString::compare(lhs.license(), rhs.license(), Qt::CaseInsensitive) < 0;
20 }
21 return nameCmp < 0;
22}
23
24static bool attrEqual(const Attribution &lhs, const Attribution &rhs)
25{
26 const auto nameCmp = QString::compare(lhs.name(), rhs.name(), Qt::CaseInsensitive);
27 if (nameCmp == 0) {
28 return QString::compare(lhs.license(), rhs.license(), Qt::CaseInsensitive) == 0;
29 }
30 return nameCmp == 0;
31}
32
33void AttributionUtil::sort(std::vector<Attribution> &attrs)
34{
35 std::sort(attrs.begin(), attrs.end(), attrLessThan);
36}
37
38void AttributionUtil::merge(std::vector<Attribution> &left, std::vector<Attribution> &&right)
39{
40 if (left.empty()) {
41 left = std::move(right);
42 sort(left);
43 } else {
44 for (const auto &a : right) {
45 merge(left, a);
46 }
47 }
48}
49
50void AttributionUtil::merge(std::vector<Attribution> &left, const std::vector<Attribution> &right)
51{
52 for (const auto &a : right) {
53 merge(left, a);
54 }
55}
56
57void AttributionUtil::merge(std::vector<Attribution> &left, const Attribution &right)
58{
59 if (right.isEmpty()) {
60 return;
61 }
62
63 const auto it = std::lower_bound(left.begin(), left.end(), right, attrLessThan);
64 if (it != left.end() && attrEqual(*it, right)) {
65 merge(*it, right);
66 } else {
67 left.insert(it, right);
68 }
69}
70
71void AttributionUtil::merge(Attribution &left, const Attribution &right)
72{
73 if (!left.url().isValid() && right.url().isValid()) {
74 left.setUrl(right.url());
75 }
76 if (!left.hasLicense() && right.hasLicense()) {
77 left.setLicense(right.license());
78 left.setLicenseUrl(right.licenseUrl());
79 }
80}
Copyright and license information about the provided data.
Definition attribution.h:29
QString name
Name of the entity providing the data.
Definition attribution.h:32
QString license
Name of the license for the provided data.
Definition attribution.h:36
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
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:10:39 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.