MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
Station.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_DATASTRUCTURE_STATION_HPP
2#define CDA_RAIL_DATASTRUCTURE_STATION_HPP
3
5#include "Definitions.hpp"
7
8#include <cstddef>
9#include <filesystem>
10#include <memory>
11#include <string>
12#include <unordered_map>
13#include <unordered_set>
14#include <utility>
15#include <vector>
16
17namespace cda_rail {
24struct Station {
28 std::string name{};
29
34
49 [[nodiscard]] std::vector<
50 std::pair<size_t, std::vector<cda_rail::index_vector>>>
51 get_stop_tracks(double tr_len, Network const& network,
52 cda_rail::index_set const& edges_to_consider = {}) const;
53
61 [[nodiscard]] bool
62 is_fully_in_station(cda_rail::index_set const& edges) const;
63
70 bool operator==(const Station& other) const {
71 return name == other.name && tracks == other.tracks;
72 }
73};
74
82private:
83 std::unordered_map<std::string, std::shared_ptr<Station>> stations;
84
85 friend class Timetable;
93 [[nodiscard]] std::shared_ptr<Station>
94 get_station_ptr(const std::string& name);
95
96public:
97 // Constructors
101 StationList() = default;
102
112 StationList(std::filesystem::path const& p, Network const& network);
113
125 StationList(std::string const& path, Network const& network)
126 : StationList(std::filesystem::path(path), network) {};
127
134 StationList(char const* const path, Network const& network)
135 : StationList(std::filesystem::path(path), network) {};
136
137 // Custom copy constructor and assignment operator for deep copying
143 StationList(const StationList& other) {
144 for (const auto& [name, station_ptr] : other.stations) {
145 stations[name] = std::make_shared<Station>(*station_ptr);
146 }
147 }
148
156 if (this != &other) {
157 stations.clear();
158 for (const auto& [name, station_ptr] : other.stations) {
159 stations[name] = std::make_shared<Station>(*station_ptr);
160 }
161 }
162 return *this;
163 }
164
165 // Default move constructor and move assignment operator
167 StationList(StationList&&) noexcept = default;
169 StationList& operator=(StationList&&) noexcept = default;
170
171 // Iterators (for range-based for loops) that do not allow modification of the
172 // underlying data
174 [[nodiscard]] auto begin() const { return stations.cbegin(); };
176 [[nodiscard]] auto end() const { return stations.cend(); };
182 [[nodiscard]] auto cbegin() const { return stations.cbegin(); };
184 [[nodiscard]] auto cend() const { return stations.cend(); };
190 [[nodiscard]] size_t size() const { return stations.size(); };
191
192 /*
193 * GETTER
194 */
201 [[nodiscard]] bool has_station(const std::string& name) const {
202 return stations.contains(name);
203 };
204
213 [[nodiscard]] Station const& get_station(const std::string& name) const;
214
220 [[nodiscard]] std::unordered_set<std::string> get_station_names() const;
221
231 [[nodiscard]] bool
232 is_fully_in_station(std::string const& station_name,
233 cda_rail::index_set const& edges) const;
234
235 /*
236 * SETTER / EDITING
237 */
238
246 void add_empty_station(std::string const& name);
247
257 void add_track_to_station(const std::string& name, size_t track);
258
259private:
270 void add_track_to_station_helper(const std::string& name, size_t const track,
271 const Network& network) {
272 if (!network.has_edge(track)) {
274 }
275 add_track_to_station(name, track);
276 };
277
278public:
291 void add_track_to_station(const std::string& name,
293 const Network& network) {
294 add_track_to_station_helper(name, edge.resolve(&network), network);
295 };
296
297 /*
298 * EXPORT/IMPORT
299 */
300
311 void export_stations(const std::filesystem::path& p,
312 const Network& network) const;
313
320 void export_stations(std::string const& path, const Network& network) const {
321 std::filesystem::path const p(path);
322 export_stations(p, network);
323 };
324
332 void export_stations(char const* const path, const Network& network) const {
333 export_stations(std::filesystem::path(path), network);
334 };
335
343 [[nodiscard]] static StationList import_stations(const std::string& path,
344 const Network& network) {
345 return {path, network};
346 };
347
358 [[nodiscard]] static StationList import_stations(const char* path,
359 const Network& network) {
360 return {path, network};
361 };
362
372 [[nodiscard]] static StationList
373 import_stations(const std::filesystem::path& p, const Network& network) {
374 return {p, network};
375 };
376
377 /*
378 * HELPER
379 */
380
390 const std::vector<std::pair<size_t, cda_rail::index_set>>& new_edges);
391
402 const std::vector<std::pair<size_t, cda_rail::index_vector>>& new_edges);
403
416 [[nodiscard]] std::vector<
417 std::pair<size_t, std::vector<cda_rail::index_vector>>>
418 get_stop_tracks(const std::string& name, double tr_len,
419 const Network& network,
420 const cda_rail::index_set& edges_to_consider = {}) const {
421 return get_station(name).get_stop_tracks(tr_len, network,
422 edges_to_consider);
423 }
424};
425} // namespace cda_rail
426
427#endif // CDA_RAIL_DATASTRUCTURE_STATION_HPP
Directed multigraph representing a railway network.
Definition RailwayNetwork.hpp:163
bool has_edge(size_t const index) const
Checks whether a numeric edge index is valid.
Definition RailwayNetwork.hpp:449
Container and persistence API for named stations.
Definition Station.hpp:81
StationList & operator=(const StationList &other)
Copy assignment operator that performs a deep copy of all stations.
Definition Station.hpp:155
StationList(StationList &&) noexcept=default
Move constructor.
void export_stations(const std::filesystem::path &p, const Network &network) const
Exports all stations to stations.json in the target directory.
Definition Station.cpp:129
auto cbegin() const
Obtains a const iterator to the first station.
Definition Station.hpp:182
StationList(char const *const path, Network const &network)
Convenience overload forwarding to the filesystem path constructor.
Definition Station.hpp:134
std::unordered_set< std::string > get_station_names() const
Returns the set of all station names.
Definition Station.cpp:102
void add_track_to_station(const std::string &name, size_t track)
Adds a track index to an existing station.
Definition Station.cpp:119
void export_stations(char const *const path, const Network &network) const
Writes all stations to the specified directory.
Definition Station.hpp:332
auto cend() const
Read-only iterator past the last station entry (explicit const).
Definition Station.hpp:184
StationList()=default
Creates an empty station list.
bool is_fully_in_station(std::string const &station_name, cda_rail::index_set const &edges) const
Checks whether all given edges belong to a named station.
Definition Station.cpp:192
void add_track_to_station(const std::string &name, cda_rail::Network::EdgeInput const &edge, const Network &network)
Adds a track to a station by resolving an edge specification.
Definition Station.hpp:291
static StationList import_stations(const std::filesystem::path &p, const Network &network)
Imports stations from a filesystem path.
Definition Station.hpp:373
bool has_station(const std::string &name) const
Checks whether a station with the given name exists.
Definition Station.hpp:201
StationList(std::string const &path, Network const &network)
Constructs a station list from a string path.
Definition Station.hpp:125
void update_after_discretization(const std::vector< std::pair< size_t, cda_rail::index_set > > &new_edges)
Updates station tracks after network discretization.
Definition Station.cpp:182
auto end() const
Read-only iterator past the last station entry.
Definition Station.hpp:176
size_t size() const
Retrieves the number of stations.
Definition Station.hpp:190
friend class Timetable
Definition Station.hpp:85
Station const & get_station(const std::string &name) const
Retrieves a station by name.
Definition Station.cpp:94
static StationList import_stations(const char *path, const Network &network)
Imports stations from a C-string path.
Definition Station.hpp:358
void add_empty_station(std::string const &name)
Adds a new station without tracks.
Definition Station.cpp:111
std::vector< std::pair< size_t, std::vector< cda_rail::index_vector > > > get_stop_tracks(const std::string &name, double tr_len, const Network &network, const cda_rail::index_set &edges_to_consider={}) const
Computes possible stop tracks for a named station.
Definition Station.hpp:418
void export_stations(std::string const &path, const Network &network) const
Convenience overload forwarding to the filesystem path export.
Definition Station.hpp:320
static StationList import_stations(const std::string &path, const Network &network)
Imports stations from a directory path string.
Definition Station.hpp:343
StationList(const StationList &other)
Copy constructor that performs a deep copy of all stations.
Definition Station.hpp:143
auto begin() const
Read-only iterator to the first station entry.
Definition Station.hpp:174
Thrown when a referenced edge does not exist.
Definition CustomExceptions.hpp:160
Definition CustomExceptions.hpp:15
std::unordered_set< size_t > index_set
Definition Definitions.hpp:22
Unified edge descriptor accepted by all public API functions.
Definition RailwayNetwork.hpp:207
Represents a station as a named set of network tracks (edge indices).
Definition Station.hpp:24
cda_rail::index_set tracks
Set of track indices (network edges) that belong to this station.
Definition Station.hpp:33
std::vector< std::pair< size_t, std::vector< cda_rail::index_vector > > > get_stop_tracks(double tr_len, Network const &network, cda_rail::index_set const &edges_to_consider={}) const
Computes station tracks that can host a stop for a given train length.
Definition Station.cpp:23
bool operator==(const Station &other) const
Compares two stations for equality.
Definition Station.hpp:70
std::string name
Station identifier.
Definition Station.hpp:28
bool is_fully_in_station(cda_rail::index_set const &edges) const
Checks whether all provided edges belong to this station.
Definition Station.cpp:46