File indexing completed on 2024-12-29 04:50:10

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "trainstationdb.h"
0008 #include "trainstationdb_data.cpp"
0009 
0010 #include <QString>
0011 
0012 #include <algorithm>
0013 #include <cstring>
0014 
0015 using namespace KItinerary;
0016 using namespace KItinerary::KnowledgeDb;
0017 
0018 namespace KItinerary {
0019 namespace KnowledgeDb {
0020 
0021 static const auto trainstation_table_size = sizeof(trainstation_table) / sizeof(TrainStation);
0022 static_assert(trainstation_table_size < (1 << (sizeof(TrainStationIndex) * 8)), "TrainStationIndex data type too small!");
0023 
0024 }
0025 }
0026 
0027 template <typename Id, std::size_t Size>
0028 static TrainStation lookupStation(Id id, const TrainStationIdIndex<Id>(&tab)[Size])
0029 {
0030     const auto it = std::lower_bound(std::begin(tab), std::end(tab), id);
0031     if (it == std::end(tab) || (*it).stationId != id) {
0032         return {};
0033     }
0034 
0035     return trainstation_table[(*it).stationIndex.value()];
0036 }
0037 
0038 TrainStation KnowledgeDb::stationForIbnr(IBNR ibnr)
0039 {
0040     return lookupStation(ibnr, ibnr_table);
0041 }
0042 
0043 TrainStation KnowledgeDb::stationForUic(UICStation uic)
0044 {
0045     return lookupStation(uic, uic_table);
0046 }
0047 
0048 TrainStation KnowledgeDb::stationForSncfStationId(SncfStationId sncfId)
0049 {
0050     return lookupStation(sncfId, sncfStationId_table);
0051 }
0052 
0053 TrainStation KnowledgeDb::stationForIndianRailwaysStationCode(const QString &code)
0054 {
0055     const auto codeStr = code.toUtf8();
0056     const auto it = std::lower_bound(std::begin(indianRailwaysSationCode_index), std::end(indianRailwaysSationCode_index), codeStr, [](auto lhs, const QByteArray &rhs) {
0057         return strcmp(indianRailwaysSationCode_stringtable + lhs.offset, rhs.constData()) < 0;
0058     });
0059     if (it == std::end(indianRailwaysSationCode_index) || strcmp(indianRailwaysSationCode_stringtable + (*it).offset, codeStr.constData()) != 0) {
0060         return {};
0061     }
0062 
0063     return trainstation_table[(*it).stationIndex.value()];
0064 }
0065 
0066 TrainStation KnowledgeDb::stationForVRStationCode(VRStationCode vrStation)
0067 {
0068     return lookupStation(vrStation, vrfiConnexionsId_table);
0069 }
0070 
0071 TrainStation KnowledgeDb::stationForBenerailId(BenerailStationId id)
0072 {
0073     return lookupStation(id, benerail_table);
0074 }
0075 
0076 TrainStation KnowledgeDb::stationForIataCode(IataCode iataCode)
0077 {
0078     return lookupStation(iataCode, iata_table);
0079 }
0080 
0081 TrainStation KnowledgeDb::stationForAmtrakStationCode(AmtrakStationCode code)
0082 {
0083     return lookupStation(code, amtrak_table);
0084 }
0085 
0086 TrainStation KnowledgeDb::stationForViaRailStationCode(ViaRailStationCode code)
0087 {
0088     return lookupStation(code, viarail_table);
0089 }
0090 
0091 TrainStation KnowledgeDb::stationForUkRailwayStationCode(UKRailwayStationCode code)
0092 {
0093     return lookupStation(code, uk_table);
0094 }