Skip to content

Commit

Permalink
Extend ABI to expose journeys, footpaths, cancellations (#139)
Browse files Browse the repository at this point in the history
* abi: expose footpaths, journeys, platform/cancelled updates

* emit correctly lower bounded time events to avoid time travel, correct propagation of lower bounded time for has_time

* expose route count

* rebase fixes, tidy up, tests

* UB fixes

* more fixes

* fix leak

* refactoring rt abi
  • Loading branch information
traines-source authored Jan 1, 2025
1 parent 0acb412 commit 255e31e
Show file tree
Hide file tree
Showing 6 changed files with 412 additions and 56 deletions.
60 changes: 57 additions & 3 deletions include/nigiri/abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ struct nigiri_transport {
};
typedef struct nigiri_transport nigiri_transport_t;

static const uint32_t kTargetBits = 22U;
static const uint32_t kDurationBits = 8 * sizeof(uint32_t) - kTargetBits;
struct nigiri_footpath {
unsigned int target_location_idx : kTargetBits;
unsigned int duration : kDurationBits;
};
typedef struct nigiri_footpath nigiri_footpath_t;

struct nigiri_location {
const char* id;
uint32_t id_len;
Expand All @@ -25,6 +33,8 @@ struct nigiri_location {
double lon;
double lat;
uint16_t transfer_time;
nigiri_footpath_t* footpaths;
uint32_t n_footpaths;
uint32_t parent;
};
typedef struct nigiri_location nigiri_location_t;
Expand All @@ -48,16 +58,49 @@ typedef struct nigiri_route nigiri_route_t;
struct nigiri_event_change {
uint32_t transport_idx;
uint16_t day_idx;
uint32_t stop_idx;
uint16_t stop_idx;
bool is_departure;
int16_t delay;
bool cancelled;
bool stop_change;
uint32_t stop_location_idx; // ignore if UINT_MAX or stop_change == false
bool stop_in_out_allowed; // ignore if stop_change == false
int16_t delay; // ignore if stop_change == true
};
typedef struct nigiri_event_change nigiri_event_change_t;

struct nigiri_leg {
bool is_footpath;
uint32_t transport_idx;
uint16_t day_idx;
uint16_t from_stop_idx;
uint32_t from_location_idx;
uint16_t to_stop_idx;
uint32_t to_location_idx;
uint32_t duration;
};
typedef struct nigiri_leg nigiri_leg_t;

struct nigiri_journey {
uint16_t n_legs;
nigiri_leg_t* legs;
int64_t start_time;
int64_t dest_time;
};
typedef struct nigiri_journey nigiri_journey_t;

struct nigiri_pareto_set {
uint16_t n_journeys;
nigiri_journey_t* journeys;
};
typedef struct nigiri_pareto_set nigiri_pareto_set_t;

nigiri_timetable_t* nigiri_load(const char* path,
int64_t from_ts,
int64_t to_ts);
nigiri_timetable_t* nigiri_load_linking_stops(const char* path,
int64_t from_ts,
int64_t to_ts,
unsigned link_stop_distance);

void nigiri_destroy(const nigiri_timetable_t* t);
int64_t nigiri_get_start_day_ts(const nigiri_timetable_t* t);
uint16_t nigiri_get_day_count(const nigiri_timetable_t* t);
Expand All @@ -68,11 +111,14 @@ void nigiri_destroy_transport(const nigiri_transport_t* transport);
bool nigiri_is_transport_active(const nigiri_timetable_t* t,
const uint32_t transport_idx,
uint16_t day_idx);
uint32_t nigiri_get_route_count(const nigiri_timetable_t* t);
nigiri_route_t* nigiri_get_route(const nigiri_timetable_t* t, uint32_t idx);
void nigiri_destroy_route(const nigiri_route_t* route);
uint32_t nigiri_get_location_count(const nigiri_timetable_t* t);
nigiri_location_t* nigiri_get_location(const nigiri_timetable_t* t,
uint32_t idx);
nigiri_location_t* nigiri_get_location_with_footpaths(
const nigiri_timetable_t* t, uint32_t idx, bool incoming_footpaths);
void nigiri_destroy_location(const nigiri_location_t* location);

void nigiri_update_with_rt(const nigiri_timetable_t* t,
Expand All @@ -81,6 +127,14 @@ void nigiri_update_with_rt(const nigiri_timetable_t* t,
void* context),
void* context);

nigiri_pareto_set_t* nigiri_get_journeys(const nigiri_timetable_t* t,
uint32_t start_location_idx,
uint32_t destination_location_idx,
int64_t time,
bool backward_search);

void nigiri_destroy_journeys(const nigiri_pareto_set_t* journeys);

#ifdef __cplusplus
}
#endif
47 changes: 35 additions & 12 deletions include/nigiri/rt/rt_timetable.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

#include "nigiri/common/delta_t.h"
#include "nigiri/common/interval.h"
#include "nigiri/rt/run.h"
#include "nigiri/stop.h"
#include "nigiri/timetable.h"
#include "nigiri/types.h"

namespace nigiri {

using change_callback_t = std::function<void(transport const transport,
stop_idx_t const stop_idx,
event_type const ev_type,
duration_t const delay,
bool const cancelled)>;
using change_callback_t =
std::function<void(transport const transport,
stop_idx_t const stop_idx,
event_type const ev_type,
std::optional<location_idx_t> const location_idx,
std::optional<bool> const in_out_allowed,
std::optional<duration_t> const delay)>;

// General note:
// - The real-time timetable does not use bitfields. It requires an initial copy
Expand Down Expand Up @@ -63,16 +66,36 @@ struct rt_timetable {

void reset_change_callback() { change_callback_ = nullptr; }

void dispatch_event_change(transport const t,
stop_idx_t const stop_idx,
event_type const ev_type,
duration_t const delay,
bool const cancelled) {
if (change_callback_) {
change_callback_(t, stop_idx, ev_type, delay, cancelled);
void dispatch_event(rt::run const& r,
stop_idx_t const stop_idx,
event_type const ev_type,
std::optional<location_idx_t> const location_idx,
std::optional<bool> const in_out_allowed,
std::optional<duration_t> const delay) {
if (change_callback_ &&
((ev_type == event_type::kArr && stop_idx != r.stop_range_.from_) ||
(ev_type == event_type::kDep && stop_idx != r.stop_range_.to_ - 1))) {
change_callback_(r.t_, stop_idx, ev_type, location_idx, in_out_allowed,
delay);
}
}

void dispatch_delay(rt::run const& r,
stop_idx_t const stop_idx,
event_type const ev_type,
duration_t const delay) {
dispatch_event(r, stop_idx, ev_type, std::nullopt, std::nullopt, delay);
}

void dispatch_stop_change(rt::run const& r,
stop_idx_t const stop_idx,
event_type const ev_type,
std::optional<location_idx_t> const location_idx,
std::optional<bool> const in_out_allowed) {
dispatch_event(r, stop_idx, ev_type, location_idx, in_out_allowed,
std::nullopt);
}

unixtime_t unix_event_time(rt_transport_idx_t const rt_t,
stop_idx_t const stop_idx,
event_type const ev_type) const {
Expand Down
Loading

0 comments on commit 255e31e

Please sign in to comment.