1#ifndef CDA_RAIL_DATASTRUCTURE_ROUTE_HPP
2#define CDA_RAIL_DATASTRUCTURE_ROUTE_HPP
13#include <unordered_map>
14#include <unordered_set>
27 std::pair<double, double>
pos1;
28 std::pair<double, double>
pos2;
29 std::unordered_set<size_t>
edges;
66 [[nodiscard]] std::optional<double>
68 const Network& network,
bool first_match)
const;
77 void is_route_index_valid(
size_t route_index)
const;
83 [[nodiscard]]
bool has_edges()
const;
99 [[nodiscard]]
size_t size()
const {
return m_edges.size(); };
114 [[nodiscard]]
bool empty()
const {
return m_edges.empty(); };
153 [[nodiscard]] EdgePosition
167 [[nodiscard]] std::optional<double>
181 [[nodiscard]] std::optional<double>
208 [[nodiscard]]
size_t get_edge_id(
size_t route_index)
const;
254 return std::ranges::contains(m_edges, edgeIndex);
268 const Network& network)
const {
280 return edgeIndex.has_value() &&
contains_edge(edgeIndex.value());
363 const std::vector<std::pair<size_t, cda_rail::index_vector>>& new_edges);
376 std::unordered_map<std::string, Route> m_routes;
413 :
RouteMap(std::filesystem::path(path), network) {};
422 :
RouteMap(std::filesystem::path(path), network) {};
442 [[nodiscard]]
auto begin()
const {
return m_routes.cbegin(); };
444 [[nodiscard]]
auto end()
const {
return m_routes.cend(); };
448 [[nodiscard]]
auto cbegin()
const {
return m_routes.cbegin(); };
450 [[nodiscard]]
auto cend()
const {
return m_routes.cend(); };
460 [[nodiscard]]
size_t size()
const {
return m_routes.size(); };
466 [[nodiscard]]
bool empty()
const {
return m_routes.empty(); };
476 [[nodiscard]]
const Route&
get_route(
const std::string& train_name)
const;
489 [[nodiscard]]
double route_length(
const std::string& train_name,
497 [[nodiscard]]
bool has_route(
const std::string& train_name)
const {
498 return m_routes.contains(train_name);
524 [[nodiscard]] std::vector<ConflictPair>
548 [[nodiscard]] std::vector<ConflictPair>
574 [[nodiscard]] std::vector<ConflictPair>
596 [[nodiscard]] std::vector<ConflictPair>
747 return {path, network};
766 return {path, network};
793 bool every_train_must_have_route =
true)
const;
807 const std::vector<std::pair<size_t, cda_rail::index_vector>>& new_edges);
816 void throw_if_train_has_no_route(std::string
const& train_name)
const;
Directed multigraph representing a railway network.
Definition RailwayNetwork.hpp:163
std::vector< ConflictPair > get_reverse_overlaps(const std::string &train1, const std::string &train2, const Network &network) const
Finds segments where the two trains travel in opposite directions over the same physical track.
Definition Route.cpp:366
void remove_last_edge(const std::string &train_name)
Removes the last edge from the given train's route.
Definition Route.cpp:501
RouteMap(const std::string &path, const Network &network)
Convenience overload accepting a std::string path.
Definition Route.hpp:412
void export_routes(const std::string &path, const Network &network) const
Writes all routes to routes.json in the specified directory.
Definition Route.hpp:712
bool check_consistency(const TrainList &trains, const Network &network, bool every_train_must_have_route=true) const
Checks whether all stored routes are valid with respect to the given train list and network.
Definition Route.cpp:565
void push_front_edge(const std::string &train_name, Network::EdgeInput const &new_edge, const Network &network)
Prepends an edge at the beginning of the given train's route.
Definition Route.cpp:489
void remove_route(const std::string &train_name)
Removes the entire route for the given train from the map.
Definition Route.cpp:506
auto cend() const
Read-only iterator past the last route entry (explicit const).
Definition Route.hpp:450
static RouteMap import_routes(const char *path, const Network &network)
Loads train routes from a file at the given directory path.
Definition Route.hpp:764
auto end() const
Read-only iterator past the last route entry.
Definition Route.hpp:444
static RouteMap import_routes(const std::filesystem::path &p, const Network &network)
Loads routes from a directory into a new RouteMap.
Definition Route.hpp:733
const Route & get_route(const std::string &train_name) const
Returns the route assigned to the given train.
Definition Route.cpp:253
RouteMap(const RouteMap &other)=default
void update_after_discretization(const std::vector< std::pair< size_t, cda_rail::index_vector > > &new_edges)
Updates all routes after a network discretization step.
Definition Route.cpp:578
size_t size() const
Counts the number of stored train routes.
Definition Route.hpp:460
bool empty() const
Determines whether the map contains no routes.
Definition Route.hpp:466
void export_routes(const std::filesystem::path &p, const Network &network) const
Writes all routes to a routes.json file inside directory p.
Definition Route.cpp:511
double route_length(const std::string &train_name, const Network &network) const
Returns the total length of the route assigned to the given train.
Definition Route.cpp:258
void export_routes(const char *path, const Network &network) const
Exports routes to a directory specified by a C-string path.
Definition Route.hpp:722
auto begin() const
Read-only iterator to the first route entry.
Definition Route.hpp:442
RouteMap(RouteMap &&other) noexcept=default
static RouteMap import_routes(const std::string &path, const Network &network)
Static factory: convenience overload accepting a std::string path.
Definition Route.hpp:745
void add_empty_route(const std::string &train_name)
Registers an empty route for the given train name.
Definition Route.cpp:468
std::vector< ConflictPair > get_crossing_overlaps(const std::string &train1, const std::string &train2, const Network &network) const
Finds all segments where the two trains could potentially block each other, regardless of travel dire...
Definition Route.cpp:433
auto cbegin() const
Returns a const iterator to the beginning of the routes.
Definition Route.hpp:448
RouteMap & operator=(RouteMap &&other) noexcept=default
Move-assigns another RouteMap to this one.
std::vector< ConflictPair > get_parallel_overlaps(const std::string &train1, const std::string &train2, const Network &network) const
Finds segments where both trains travel in the same direction over the same track section.
Definition Route.cpp:264
void push_back_edge(const std::string &train_name, Network::EdgeInput const &new_edge, const Network &network)
Appends an edge at the end of the given train's route.
Definition Route.cpp:482
std::vector< ConflictPair > get_ttd_overlaps(const std::string &train1, const std::string &train2, const Network &network) const
Finds segments where both trains share an unbreakable (TTD) track section.
Definition Route.cpp:318
RouteMap & operator=(const RouteMap &other)=default
RouteMap(const char *path, const Network &network)
Convenience overload accepting a C-string path.
Definition Route.hpp:421
void remove_first_edge(const std::string &train_name)
Removes the first edge from the given train's route.
Definition Route.cpp:496
RouteMap()=default
Creates an empty route map with no routes.
bool has_route(const std::string &train_name) const
Checks whether a route for the given train is stored in this map.
Definition Route.hpp:497
Ordered list of network edges that a train follows.
Definition Route.hpp:38
const cda_rail::index_vector & get_edges() const
Provides the edges forming the route in travel order.
Definition Route.hpp:120
size_t get_edge_id_at_pos(double pos, const Network &network) const
Returns the edge that contains a given position on the route.
Definition Route.cpp:111
double length(const Network &network) const
Returns the total route length.
Definition Route.cpp:34
std::optional< double > get_first_pos_on_edges(const cda_rail::index_set &edge_indices, const Network &network) const
Returns where the first matching edge starts.
Definition Route.cpp:99
void remove_last_edge()
Removes the last edge from the route.
Definition Route.cpp:174
size_t get_edge_id(size_t route_index) const
Returns the edge id at a given route position.
Definition Route.cpp:135
const Edge & get_first_edge(const Network &network) const
Returns the first edge in the route.
Definition Route.hpp:233
EdgePosition edge_pos_on_route(Network::EdgeInput const &edge, const Network &network) const
Returns where a specific edge lies on this route.
Definition Route.cpp:43
void push_back_edge(Network::EdgeInput const &new_edge, const Network &network)
Adds an edge at the end of the route.
Definition Route.cpp:146
const Edge & get_last_edge(const Network &network) const
Returns the last edge in the route.
Definition Route.hpp:244
const Edge & get_edge(size_t route_index, const Network &network) const
Returns the full edge object at a given route position.
Definition Route.cpp:140
void remove_first_edge()
Removes the first edge from the route.
Definition Route.cpp:167
void push_front_edge(Network::EdgeInput const &new_edge, const Network &network)
Adds an edge at the beginning of the route.
Definition Route.cpp:156
bool contains_edge(Network::EdgeInput const &edge_input, const Network &network) const
Checks whether a network edge appears in this route.
Definition Route.hpp:267
void update_after_discretization(const std::vector< std::pair< size_t, cda_rail::index_vector > > &new_edges)
Updates the route after edge splitting in the network.
Definition Route.cpp:193
bool contains_edge(size_t const edgeIndex) const
Checks whether an edge id appears in this route.
Definition Route.hpp:253
size_t size() const
Gets the number of edges in the route.
Definition Route.hpp:99
bool check_consistency(const Network &network) const
Checks whether the route is connected step by step.
Definition Route.cpp:181
EdgePosition edge_set_pos_on_route(const cda_rail::index_vector &edges_to_consider, const Network &network) const
Returns one interval that covers all selected edges on the route.
Definition Route.cpp:68
std::optional< double > get_last_pos_on_edges(const cda_rail::index_set &edge_indices, const Network &network) const
Returns where the last matching edge ends.
Definition Route.cpp:105
bool contains_edge(std::optional< size_t > const edgeIndex) const
Determines if an optional edge id is present in this route.
Definition Route.hpp:279
bool empty() const
Checks whether the route is empty.
Definition Route.hpp:114
Container for trains with name-based indexing.
Definition Train.hpp:200
Definition CustomExceptions.hpp:15
std::unordered_set< size_t > index_set
Definition Definitions.hpp:22
std::vector< size_t > index_vector
Definition Definitions.hpp:21
One overlap interval between two routes.
Definition Route.hpp:26
std::pair< double, double > pos2
Definition Route.hpp:28
std::unordered_set< size_t > edges
Definition Route.hpp:29
std::pair< double, double > pos1
Definition Route.hpp:27
Represents a directed edge in the railway network graph.
Definition RailwayNetwork.hpp:79
Start and end position of one edge within the route.
Definition Route.hpp:46
double target
Definition Route.hpp:48
double source
Definition Route.hpp:47