Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement callback invocation for each trip #147

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/nigiri/rt/frun.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,12 @@ struct frun : public run {
trip_idx_t trip_idx() const;
clasz get_clasz() const noexcept;

void for_each_trip(
std::function<void(trip_idx_t const, interval<stop_idx_t> const)> const&)
const;
void for_each_shape_point(
shapes_storage const*,
interval<stop_idx_t> const&,
interval<stop_idx_t> const,
std::function<void(geo::latlng const&)> const&) const;

void print(std::ostream&, interval<stop_idx_t>);
Expand Down
46 changes: 28 additions & 18 deletions src/rt/frun.cc
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,31 @@ clasz frun::get_clasz() const noexcept {
}
}

void frun::for_each_trip(
std::function<void(trip_idx_t const, interval<stop_idx_t> const)> const&
callback) const {
auto curr_trip_idx = trip_idx_t::invalid();
auto curr_from = stop_idx_t{0U};
for (auto const [from, to] :
utl::pairwise(interval{stop_idx_t{0U}, stop_range_.to_})) {
auto const trip_idx =
(*this)[static_cast<stop_idx_t>(from - stop_range_.from_)] //
.get_trip_idx(event_type::kDep);
if (trip_idx == curr_trip_idx) {
continue;
}
if (from != 0U) {
callback(curr_trip_idx, interval{curr_from, to});
}
curr_trip_idx = trip_idx;
curr_from = from;
}
callback(curr_trip_idx, interval{curr_from, stop_range_.to_});
}

void frun::for_each_shape_point(
shapes_storage const* shapes_data,
interval<stop_idx_t> const& range,
interval<stop_idx_t> const range,
std::function<void(geo::latlng const&)> const& callback) const {
utl::verify(range.size() >= 2, "Range must contain at least 2 stops. Is {}",
range.size());
Expand Down Expand Up @@ -421,21 +443,9 @@ void frun::for_each_shape_point(
}
last_pos = pos;
};
// Range over all trips using absolute 'trip_details.offset_range_'
auto curr_trip_idx = trip_idx_t::invalid();
auto absolute_trip_start = stop_idx_t{0U};
for (auto const [from, to] :
utl::pairwise(interval{stop_idx_t{0U}, stop_range_.to_})) {
auto const trip_idx =
(*this)[static_cast<stop_idx_t>(from - stop_range_.from_)] //
.get_trip_idx(event_type::kDep);
if (trip_idx != curr_trip_idx) {
curr_trip_idx = trip_idx;
absolute_trip_start = from;
}
auto const common_stops =
interval{from, static_cast<stop_idx_t>(to + 1)}.intersect(
absolute_stop_range);
for_each_trip([&](trip_idx_t const trip_idx,
interval<stop_idx_t> const subrange) {
auto const common_stops = subrange.intersect(absolute_stop_range);
if (common_stops.size() > 1) {
std::visit(utl::overloaded{[&](std::span<geo::latlng const> shape) {
for (auto const& pos : shape) {
Expand All @@ -447,9 +457,9 @@ void frun::for_each_shape_point(
consume_pos((*this)[stop_idx].pos());
}
}},
get_subshape(common_stops, trip_idx, absolute_trip_start));
get_subshape(common_stops, trip_idx, subrange.from_));
}
}
});
consume_pos((*this)[static_cast<stop_idx_t>(range.to_ - 1)].pos(), true);
}

Expand Down
Loading