KPublicTransport

loadutil.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "loadutil_p.h"
8
9#include <QDebug>
10
11using namespace KPublicTransport;
12
13std::vector<LoadInfo> LoadUtil::merge(const std::vector<LoadInfo> &lhs, const std::vector<LoadInfo> &rhs)
14{
15 // any result is better than none
16 if (lhs.empty()) {
17 return rhs;
18 }
19 if (rhs.empty()) {
20 return lhs;
21 }
22
23 std::vector<LoadInfo> result;
24 result.reserve(std::max(lhs.size(), rhs.size()));
25 std::copy(lhs.begin(), lhs.end(), std::back_inserter(result));
26
27 const auto classOrder = [](const auto &loadL, const auto &loadR) {
28 return loadL.seatingClass() < loadR.seatingClass();
29 };
30 std::sort(result.begin(), result.end(), classOrder);
31
32 for (const auto &l : rhs) {
33 const auto it = std::lower_bound(result.begin(), result.end(), l, classOrder);
34 if (it == result.end() || (*it).seatingClass() != l.seatingClass()) {
35 result.insert(it, l);
36 } else {
37 (*it).setLoad(std::max((*it).load(), l.load()));
38 }
39 }
40
41 // class-less vs. class-specific information
42 if (result.size() > 1 && result.front().seatingClass().isEmpty()) {
43 // TODO check that this doesn't reduce the highest load we have?
44 result.erase(result.begin());
45 }
46
47 return result;
48}
Query operations and data types for accessing realtime public transport information from online servi...
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.