Skip to content

Commit 31d19da

Browse files
Add function to get a location's round times (#329)
* Add function to get a location's round times * Add tests
1 parent 2ddf927 commit 31d19da

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

include/nigiri/routing/one_to_all.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,13 @@ fastest_offset get_fastest_one_to_all_offsets(timetable const& tt,
3131
unixtime_t start_time,
3232
std::uint8_t transfers);
3333

34+
void for_each_one_to_all_round_time(
35+
timetable const&,
36+
raptor_state const&,
37+
direction,
38+
location_idx_t,
39+
unixtime_t start_time,
40+
std::uint8_t transfers,
41+
std::function<void(std::uint8_t, duration_t)> const&);
42+
3443
} // namespace nigiri::routing

src/routing/one_to_all.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,30 @@ fastest_offset get_fastest_one_to_all_offsets(timetable const& tt,
128128
return {};
129129
};
130130

131+
void for_each_one_to_all_round_time(
132+
timetable const& tt,
133+
raptor_state const& state,
134+
direction const search_dir,
135+
location_idx_t const l,
136+
unixtime_t const start_time,
137+
std::uint8_t const transfers,
138+
std::function<void(std::uint8_t, duration_t)> const& cb) {
139+
auto const invalid_delta = search_dir == direction::kForward
140+
? kInvalidDelta<direction::kForward>
141+
: kInvalidDelta<direction::kBackward>;
142+
auto const& round_times = state.get_round_times<kVias>();
143+
auto const base =
144+
tt.internal_interval_days().from_ +
145+
static_cast<int>(to_idx(make_base(tt, start_time))) * date::days{1};
146+
for (auto const k : std::views::iota(std::uint8_t{0U}, transfers + 2U)) {
147+
if (round_times[k][to_idx(l)][kVias] != invalid_delta) {
148+
auto end_time = delta_to_unix(base, round_times[k][to_idx(l)][kVias]);
149+
cb(k, search_dir == direction::kForward ? end_time - start_time
150+
: start_time - end_time);
151+
}
152+
}
153+
}
154+
131155
template raptor_state one_to_all<direction::kForward>(timetable const&,
132156
rt_timetable const*,
133157
query const&);

test/rt/frun_shape_test.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,12 @@ TEST(
245245
leg_shape.push_back(point);
246246
};
247247

248+
using round_times_t = std::vector<std::pair<std::uint8_t, duration_t>>;
249+
auto round_times = round_times_t{};
250+
auto const add_round_time = [&](std::uint8_t const k, duration_t const d) {
251+
round_times.emplace_back(k, d);
252+
};
253+
248254
auto const to_location_idx = [&](std::string_view x) {
249255
auto const src = source_idx_t{0};
250256
return tt.locations_.location_id_to_idx_.at({x, src});
@@ -894,6 +900,24 @@ TEST(
894900
delta_t{185 + kDefaultFootpathDuration.count()});
895901
ASSERT_EQ(stats_s_direct.k_, 1U);
896902
}
903+
// Test round times
904+
{
905+
round_times.clear();
906+
routing::for_each_one_to_all_round_time(
907+
tt, state, kSearchDir, to_location_idx("S"), start_time,
908+
q.max_transfers_, add_round_time);
909+
EXPECT_EQ(
910+
(round_times_t{{1, duration_t{185} + kDefaultFootpathDuration},
911+
{2, duration_t{140} + kDefaultFootpathDuration}}),
912+
round_times);
913+
round_times.clear();
914+
routing::for_each_one_to_all_round_time(
915+
tt, state, kSearchDir, to_location_idx("W"), start_time,
916+
q.max_transfers_, add_round_time);
917+
EXPECT_EQ(
918+
(round_times_t{{2, duration_t{200} + kDefaultFootpathDuration}}),
919+
round_times);
920+
}
897921
}
898922
}
899923
// One-to-All backwards search for time point
@@ -971,6 +995,13 @@ TEST(
971995
q.max_transfers_);
972996
EXPECT_EQ(stats_f.duration_,
973997
delta_t{-110 - kDefaultFootpathDuration.count()});
998+
// Test round times
999+
round_times.clear();
1000+
routing::for_each_one_to_all_round_time(tt, state, kSearchDir,
1001+
to_location_idx("F"), start_time,
1002+
q.max_transfers_, add_round_time);
1003+
EXPECT_EQ((round_times_t{{1, duration_t{110} + kDefaultFootpathDuration}}),
1004+
round_times);
9741005
}
9751006
// Loading statistics
9761007
{

0 commit comments

Comments
 (0)