File indexing completed on 2024-05-12 04:42:44

0001 /*
0002     SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "loadutil_p.h"
0008 
0009 #include <QDebug>
0010 
0011 using namespace KPublicTransport;
0012 
0013 std::vector<LoadInfo> LoadUtil::merge(const std::vector<LoadInfo> &lhs, const std::vector<LoadInfo> &rhs)
0014 {
0015     // any result is better than none
0016     if (lhs.empty()) {
0017         return rhs;
0018     }
0019     if (rhs.empty()) {
0020         return lhs;
0021     }
0022 
0023     std::vector<LoadInfo> result;
0024     result.reserve(std::max(lhs.size(), rhs.size()));
0025     std::copy(lhs.begin(), lhs.end(), std::back_inserter(result));
0026 
0027     const auto classOrder = [](const auto &loadL, const auto &loadR) {
0028         return loadL.seatingClass() < loadR.seatingClass();
0029     };
0030     std::sort(result.begin(), result.end(), classOrder);
0031 
0032     for (const auto &l : rhs) {
0033         const auto it = std::lower_bound(result.begin(), result.end(), l, classOrder);
0034         if (it == result.end() || (*it).seatingClass() != l.seatingClass()) {
0035             result.insert(it, l);
0036         } else {
0037             (*it).setLoad(std::max((*it).load(), l.load()));
0038         }
0039     }
0040 
0041     // class-less vs. class-specific information
0042     if (result.size() > 1 && result.front().seatingClass().isEmpty()) {
0043         // TODO check that this doesn't reduce the highest load we have?
0044         result.erase(result.begin());
0045     }
0046 
0047     return result;
0048 }