From 04848ed5d32da03282b631dcd0fcebeb28ef0259 Mon Sep 17 00:00:00 2001 From: MichaelBrunner Date: Sat, 17 Aug 2024 11:34:44 +0200 Subject: [PATCH] =?UTF-8?q?error:=20error:=20expected=20primary-expression?= =?UTF-8?q?=20before=20=E2=80=98)=E2=80=99=20token=20=20=20=20=20=20=20=20?= =?UTF-8?q?=20=20=20=20=20=20=20189=20|=20=20=20=20=20=20=20const=20auto?= =?UTF-8?q?=20tripIdsArray=20=3D=20routeContainer.trips()=20|=20std::views?= =?UTF-8?q?::keys=20|=20std::ranges::to>();?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geometry/test/CMakeLists.txt | 7 +- raptor/src/RaptorRouter.cpp | 83 +++++++++++--------- raptor/src/utils/RaptorDataBuilder.cpp | 102 +++++++++++++------------ raptor/test/CMakeLists.txt | 14 ---- schedule/test/CMakeLists.txt | 3 - 5 files changed, 105 insertions(+), 104 deletions(-) diff --git a/geometry/test/CMakeLists.txt b/geometry/test/CMakeLists.txt index a04d71a..ceed3e7 100644 --- a/geometry/test/CMakeLists.txt +++ b/geometry/test/CMakeLists.txt @@ -3,9 +3,6 @@ project("geometry_tests" ) -include(CTest) -enable_testing() - add_executable(${PROJECT_NAME}) target_sources(${PROJECT_NAME} PRIVATE @@ -37,3 +34,7 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/geometry/ + + + + diff --git a/raptor/src/RaptorRouter.cpp b/raptor/src/RaptorRouter.cpp index 2aa4501..18736c4 100644 --- a/raptor/src/RaptorRouter.cpp +++ b/raptor/src/RaptorRouter.cpp @@ -26,70 +26,62 @@ namespace raptor { constexpr types::raptorInt MIN_WALKING_TIME_TO_TARGET = 0; constexpr types::raptorInt MAX_DIFFERENCE_IN_SOURCE_STOP_TIMES = 24 * 60 * 60; - void checkNonNullOrEmptyStops(const std::map& stops, const std::string& labelSource) { - if (stops.empty()) - { + void checkNonNullOrEmptyStops(const std::map& stops, const std::string& labelSource) + { + if (stops.empty()) { throw std::invalid_argument(labelSource + " stops must not be empty."); } } - void validateSourceStopTimes(const std::map& sourceStops) { - for (const auto& entry : sourceStops) - { - if (entry.second == 0) - { + void validateSourceStopTimes(const std::map& sourceStops) + { + for (const auto& entry : sourceStops) { + if (entry.second == 0) { throw std::invalid_argument("Source stop times must not be null."); } } - if (const auto minMaxPair = std::minmax_element(sourceStops.begin(), sourceStops.end(), [](const auto& a, const auto& b) { return a.second < b.second; }); minMaxPair.second->second - minMaxPair.first->second > MAX_DIFFERENCE_IN_SOURCE_STOP_TIMES) - { + if (const auto minMaxPair = std::minmax_element(sourceStops.begin(), sourceStops.end(), [](const auto& a, const auto& b) { return a.second < b.second; }); minMaxPair.second->second - minMaxPair.first->second > MAX_DIFFERENCE_IN_SOURCE_STOP_TIMES) { throw std::invalid_argument("Difference between source stop times must be less than 24 hours."); } } - void validateStopPermutations(const std::map& sourceStops, const std::map& targetStops) { + void validateStopPermutations(const std::map& sourceStops, const std::map& targetStops) + { // Ensure departure and arrival stops are not the same std::set sourceKeys; - for (const auto& key : sourceStops | std::views::keys) - { + for (const auto& key : sourceStops | std::views::keys) { sourceKeys.insert(key); } - for (const auto& stop : targetStops | std::views::keys) - { - if (sourceKeys.contains(stop)) - { + for (const auto& stop : targetStops | std::views::keys) { + if (sourceKeys.contains(stop)) { throw std::invalid_argument("Source and target stop IDs must not be the same."); } } } - std::map validateStopsAndGetIndices(const std::map& stops, const std::unordered_map& stopsToIdx) { - if (stops.empty()) - { + std::map validateStopsAndGetIndices(const std::map& stops, const std::unordered_map& stopsToIdx) + { + if (stops.empty()) { throw std::invalid_argument("At least one stop ID must be provided."); } std::map validStopIds; - for (const auto& [stopId, time] : stops) - { + for (const auto& [stopId, time] : stops) { if (auto it = stopsToIdx.find(stopId); - it != stopsToIdx.end()) - { + it != stopsToIdx.end()) { getConsoleLogger(LoggerName::RAPTOR)->info(std::to_string(it->second)); getConsoleLogger(LoggerName::RAPTOR)->info(std::to_string(time)); validStopIds[it->second] = time; } - else - { + else { getConsoleLogger(LoggerName::RAPTOR)->error(std::format("Stop ID {} not found in lookup, removing from query.", stopId)); } } - if (validStopIds.empty()) - { + if (validStopIds.empty()) { throw std::invalid_argument("No valid stops provided."); } @@ -99,11 +91,14 @@ namespace raptor { } RaptorRouter::RaptorRouter(RaptorData raptorData) - : raptorData(std::move(raptorData)) {} + : raptorData(std::move(raptorData)) + { + } std::vector> RaptorRouter::routeEarliestArrival(const std::map& departureStops, const std::map& arrivalStops, - const config::QueryConfig& config) const { + const config::QueryConfig& config) const + { validation::checkNonNullOrEmptyStops(departureStops, "Departure"); validation::checkNonNullOrEmptyStops(arrivalStops, "Arrival"); @@ -120,10 +115,26 @@ namespace raptor { auto validatedSourceStops = validation::validateStopsAndGetIndices(departureStops, raptorData.getLookup().stops); auto validatedTargetStops = validation::validateStopsAndGetIndices(arrivalStops, raptorData.getLookup().stops); + // not all compilers support std::ranges::to =( +#ifdef _MSC_VER const auto sourceStopIndices = validatedSourceStops | std::views::keys | std::ranges::to>(); const auto targetStopIndices = validatedTargetStops | std::views::keys | std::ranges::to>(); const auto sourceTimes = validatedSourceStops | std::views::values | std::ranges::to>(); const auto walkingDurationsToTarget = validatedTargetStops | std::views::values | std::ranges::to>(); +#else + std::vector sourceStopIndices; + std::ranges::transform(validatedSourceStops, std::back_inserter(sourceStopIndices), [](const auto& pair) { return pair.first; }); + + std::vector targetStopIndices; + std::ranges::transform(validatedTargetStops, std::back_inserter(targetStopIndices), [](const auto& pair) { return pair.first; }); + + std::vector sourceTimes; + std::ranges::transform(validatedSourceStops, std::back_inserter(sourceTimes), [](const auto& pair) { return pair.second; }); + + std::vector walkingDurationsToTarget; + std::ranges::transform(validatedTargetStops, std::back_inserter(walkingDurationsToTarget), [](const auto& pair) { return pair.second; }); +#endif + const auto queryParams = QueryParams{ .raptorData = raptorData, @@ -142,20 +153,24 @@ namespace raptor { return connection.reconstructParetoOptimalSolutions(bestLabelsPerRound, validatedTargetStops, referenceDate); } - std::vector> RaptorRouter::routeLatestDeparture(const std::map& departureStops, const std::map& arrivalStops, const config::QueryConfig& config) const { + std::vector> RaptorRouter::routeLatestDeparture(const std::map& departureStops, const std::map& arrivalStops, const config::QueryConfig& config) const + { throw std::runtime_error("Not implemented"); } - std::map> RaptorRouter::routeIsolines(const std::map& sourceStops, const config::QueryConfig& config) const { + std::map> RaptorRouter::routeIsolines(const std::map& sourceStops, const config::QueryConfig& config) const + { throw std::runtime_error("Not implemented"); } - const RaptorData& RaptorRouter::getRaptorData() const { + const RaptorData& RaptorRouter::getRaptorData() const + { return raptorData; } - std::vector> RaptorRouter::getConnections(const std::map& sourceStops, const std::map& targetStops, const config::QueryConfig& config) const { + std::vector> RaptorRouter::getConnections(const std::map& sourceStops, const std::map& targetStops, const config::QueryConfig& config) const + { validation::validateSourceStopTimes(sourceStops); // Mocked implementation for processing diff --git a/raptor/src/utils/RaptorDataBuilder.cpp b/raptor/src/utils/RaptorDataBuilder.cpp index fa3e256..97825c8 100644 --- a/raptor/src/utils/RaptorDataBuilder.cpp +++ b/raptor/src/utils/RaptorDataBuilder.cpp @@ -13,15 +13,18 @@ #include #include #include +#include namespace raptor { RaptorRouterBuilder::RaptorRouterBuilder(const int defaultSameStopTransferTime) - : defaultSameStopTransferTime(defaultSameStopTransferTime) {} + : defaultSameStopTransferTime(defaultSameStopTransferTime) + { + } - RaptorRouterBuilder& RaptorRouterBuilder::addStop(const std::string& id) { - if (stops.contains(id)) - { + RaptorRouterBuilder& RaptorRouterBuilder::addStop(const std::string& id) + { + if (stops.contains(id)) { throw std::invalid_argument("Stop " + id + " already exists"); } getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Adding stop: id={}", id)); @@ -31,16 +34,14 @@ namespace raptor { return *this; } - RaptorRouterBuilder& RaptorRouterBuilder::addRoute(const std::string& id, const std::vector& stopIds) { - if (routeBuilders.contains(id)) - { + RaptorRouterBuilder& RaptorRouterBuilder::addRoute(const std::string& id, const std::vector& stopIds) + { + if (routeBuilders.contains(id)) { throw std::invalid_argument("Route " + id + " already exists"); } - for (const auto& stopId : stopIds) - { - if (!stops.contains(stopId)) - { + for (const auto& stopId : stopIds) { + if (!stops.contains(stopId)) { throw std::invalid_argument("Stop " + stopId + " does not exist"); } stopRoutes[stopId].insert(id); @@ -55,33 +56,33 @@ namespace raptor { return *this; } - RaptorRouterBuilder& RaptorRouterBuilder::addTrip(const std::string& tripId, const std::string& routeId) { + RaptorRouterBuilder& RaptorRouterBuilder::addTrip(const std::string& tripId, const std::string& routeId) + { getRouteBuilder(routeId)->addTrip(tripId); return *this; } - RaptorRouterBuilder& RaptorRouterBuilder::addStopTime(const std::string& routeId, const std::string& tripId, const int position, const std::string& stopId, const int arrival, const int departure) { + RaptorRouterBuilder& RaptorRouterBuilder::addStopTime(const std::string& routeId, const std::string& tripId, const int position, const std::string& stopId, const int arrival, const int departure) + { const StopTime stopTime(arrival, departure); getRouteBuilder(routeId)->addStopTime(tripId, position, stopId, stopTime); stopTimeSize++; return *this; } - RaptorRouterBuilder& RaptorRouterBuilder::addTransfer(const std::string& sourceStopId, const std::string& targetStopId, int duration) { + RaptorRouterBuilder& RaptorRouterBuilder::addTransfer(const std::string& sourceStopId, const std::string& targetStopId, int duration) + { getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Adding transfer: sourceStopId={} targetStopId={} duration={}", sourceStopId, targetStopId, duration)); - if (!stops.contains(sourceStopId)) - { + if (!stops.contains(sourceStopId)) { throw std::invalid_argument("Source stop " + sourceStopId + " does not exist"); } - if (!stops.contains(targetStopId)) - { + if (!stops.contains(targetStopId)) { throw std::invalid_argument("Target stop " + targetStopId + " does not exist"); } - if (sourceStopId == targetStopId) - { + if (sourceStopId == targetStopId) { sameStopTransfers[sourceStopId] = duration; return *this; } @@ -91,7 +92,8 @@ namespace raptor { return *this; } - std::shared_ptr RaptorRouterBuilder::build() { + std::shared_ptr RaptorRouterBuilder::build() + { getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Initialize Raptor with {} stops, {} routes, {} route stops, {} stop times, {} transfers", stops.size(), routeBuilders.size(), routeStopSize, stopTimeSize, transferSize)); const auto routeContainers = buildAndSortRouteContainers(); @@ -102,10 +104,10 @@ namespace raptor { return std::make_shared(lookup, stopContext, routeTraversal); } - std::vector RaptorRouterBuilder::buildAndSortRouteContainers() const { + std::vector RaptorRouterBuilder::buildAndSortRouteContainers() const + { std::vector containers; - for (const auto& [id, routeBuilder] : routeBuilders) - { + for (const auto& [id, routeBuilder] : routeBuilders) { containers.push_back(routeBuilder->build()); } std::sort(containers.begin(), containers.end()); @@ -113,17 +115,18 @@ namespace raptor { return containers; } - StopRoutesIndexLookup RaptorRouterBuilder::buildLookup(const std::vector& routeContainers) const { + StopRoutesIndexLookup RaptorRouterBuilder::buildLookup(const std::vector& routeContainers) const + { getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Building lookup with {} stops and {} routes", stops.size(), routeContainers.size())); std::unordered_map routes; - for (auto i = 0; i < routeContainers.size(); ++i) - { + for (auto i = 0; i < routeContainers.size(); ++i) { routes[routeContainers[i].id()] = i; } return StopRoutesIndexLookup(stops, routes); } - StopContext RaptorRouterBuilder::buildStopContext(const StopRoutesIndexLookup& lookup) { + StopContext RaptorRouterBuilder::buildStopContext(const StopRoutesIndexLookup& lookup) + { getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Building stop context with {} stops and {} transfers", stops.size(), transferSize)); std::vector stopArr(stops.size()); auto stopRouteSizeView = stopRoutes @@ -137,14 +140,12 @@ namespace raptor { int transferIdx = 0; int stopRouteIdx = 0; - for (const auto& entry : stops) - { + for (const auto& entry : stops) { const auto& stopId = entry.first; const auto stopIdx = entry.second; const auto& currentStopRoutes = stopRoutes[stopId]; - if (currentStopRoutes.empty()) - { + if (currentStopRoutes.empty()) { throw std::logic_error("Stop " + stopId + " has no routes"); } @@ -155,16 +156,13 @@ namespace raptor { stopArr[stopIdx] = Stop(stopId, stopRouteIdx, static_cast(currentStopRoutes.size()), sameStopTransferTime, static_cast(numberOfTransfers) == 0 ? NO_INDEX : transferIdx, static_cast(numberOfTransfers)); - if (currentTransfers != transfers.end()) - { - for (const auto& transfer : currentTransfers->second) - { + if (currentTransfers != transfers.end()) { + for (const auto& transfer : currentTransfers->second) { transferArr[transferIdx++] = transfer; } } - for (const auto& routeId : currentStopRoutes) - { + for (const auto& routeId : currentStopRoutes) { stopRouteArr[stopRouteIdx++] = lookup.routes.at(routeId); } } @@ -172,7 +170,8 @@ namespace raptor { return StopContext(transferArr, stopArr, stopRouteArr); } - RouteTraversal RaptorRouterBuilder::buildRouteTraversal(const std::vector& routeContainers) const { + RouteTraversal RaptorRouterBuilder::buildRouteTraversal(const std::vector& routeContainers) const + { getConsoleLogger(LoggerName::RAPTOR)->info(std::format("Building route traversal with {} routes, {} route stops, {} stop times", routeContainers.size(), routeStopSize, stopTimeSize)); std::vector routeArr(routeContainers.size()); std::vector routeStopArr(static_cast(routeStopSize)); @@ -180,25 +179,28 @@ namespace raptor { types::raptorIdx routeStopCnt = 0; types::raptorIdx stopTimeCnt = 0; - for (size_t routeIdx = 0; routeIdx < routeContainers.size(); ++routeIdx) - { + for (size_t routeIdx = 0; routeIdx < routeContainers.size(); ++routeIdx) { const auto& routeContainer = routeContainers[routeIdx]; const auto numberOfStops = routeContainer.stopSequence().size(); const auto numberOfTrips = routeContainer.trips().size(); + +#ifdef _MSC_VER const auto tripIdsArray = routeContainer.trips() | std::views::keys | std::ranges::to>(); +#else + const std::vector tripIdsArray; + std::ranges::transform(routeContainer.trips(), std::back_inserter(tripIdsArray), [](const auto& pair) { return pair.first; }); +#endif + routeArr[routeIdx] = Route(routeContainer.id(), routeStopCnt, static_cast(numberOfStops), stopTimeCnt, static_cast(numberOfTrips), tripIdsArray); - for (int position = 0; position < numberOfStops; ++position) - { + for (int position = 0; position < numberOfStops; ++position) { const auto stopIdx = stops.at(routeContainer.stopSequence().at(position)); routeStopArr[routeStopCnt++] = RouteStop(stopIdx, static_cast(routeIdx)); } - for (const auto& tripEntry : routeContainer.trips()) - { - for (const auto& stopTime : tripEntry.second) - { + for (const auto& tripEntry : routeContainer.trips()) { + for (const auto& stopTime : tripEntry.second) { stopTimeArr[stopTimeCnt++] = stopTime; } } @@ -207,10 +209,10 @@ namespace raptor { return {routeStopArr, stopTimeArr, routeArr}; } - RouteBuilder* RaptorRouterBuilder::getRouteBuilder(const std::string& routeId) { + RouteBuilder* RaptorRouterBuilder::getRouteBuilder(const std::string& routeId) + { const auto it = routeBuilders.find(routeId); - if (it == routeBuilders.end()) - { + if (it == routeBuilders.end()) { throw std::invalid_argument("Route " + routeId + " does not exist"); } return it->second.get(); diff --git a/raptor/test/CMakeLists.txt b/raptor/test/CMakeLists.txt index 4d0a1c7..e40f37f 100644 --- a/raptor/test/CMakeLists.txt +++ b/raptor/test/CMakeLists.txt @@ -2,9 +2,6 @@ project("raptor_tests" DESCRIPTION "Raptor test" ) -include(CTest) -enable_testing() - add_executable(${PROJECT_NAME}) target_sources(${PROJECT_NAME} PRIVATE @@ -38,16 +35,5 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/raptor/in target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/raptor/include/config) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/schedule/include) -# if(USE_ZURICH_GTFS) -# -# # add_definitions(-DTEST_DATA_DIR="C:/Users/MichaelBrunner/Downloads/2024_google_transit/") -# # add_definitions(-DTEST_DATA_DIR="${CMAKE_SOURCE_DIR}/raptor/test/sample-feed/") -# else () -# # add_definitions(-DTEST_DATA_DIR="${CMAKE_SOURCE_DIR}/schedule/test/test-data/") -# add_definitions(-DTEST_DATA_DIR="C:/Users/MichaelBrunner/Downloads/gtfs_fp2024_2024-05-27/Verkehrsbetriebe_der_Stadt_St_Gallen/") -# endif() - - - diff --git a/schedule/test/CMakeLists.txt b/schedule/test/CMakeLists.txt index 98d345a..109a730 100644 --- a/schedule/test/CMakeLists.txt +++ b/schedule/test/CMakeLists.txt @@ -6,9 +6,6 @@ if (NOT ${CMAKE_BUILD_TYPE} STREQUAL "Debug") message(STATUS "Tests should be built in Debug mode") endif () -include(CTest) -enable_testing() - add_executable(${PROJECT_NAME}) target_sources(${PROJECT_NAME} PRIVATE