/// Footpath transfers, services/exceptions and the spatial grid for /// [`IndexBuilder::finish_to`]: pure section builders extracted verbatim /// from that method (same statement order, same debug assertions). fn build_transfers(stop_lat: &[i32], stop_lon: &[i32]) -> (Vec, Vec, usize) { let stop_count = stop_lat.len(); // --- Footpath transfers via a coarse spatial grid (cross-feed for free) --- // A flat `(cell, stop)` vector sorted once, not a `HashMap` of buckets: on a // world pack the map is millions of cells each holding a tiny `Vec`, whose // headers and allocations cost far more than the stop ids in them. Sorting // by `(cell, stop)` keeps each cell's stops ascending, which is the order the // old bucket pushes produced and which the distance sort below tie-breaks on. let xcell = |lat_e7: i32, lon_e7: i32| -> (i32, i32) { ( (lat_e7 as f64 * 1e-7 / CELL_DEG).floor() as i32, (lon_e7 as f64 * 1e-7 / CELL_DEG).floor() as i32, ) }; let mut xgrid: Vec<((i32, i32), u32)> = (0..stop_count) .map(|i| (xcell(stop_lat[i], stop_lon[i]), i as u32)) .collect(); xgrid.sort_unstable(); let xbucket = |key: (i32, i32)| -> &[((i32, i32), u32)] { let lo = xgrid.partition_point(|&(k, _)| k < key); let hi = xgrid.partition_point(|&(k, _)| k <= key); &xgrid[lo..hi] }; let mut transfer_total = 0usize; let mut sec_transfers = Vec::new(); let mut sec_transfers_idx = Vec::new(); let mut tacc: u32 = 0; for i in 0..stop_count { append_u32(&mut sec_transfers_idx, tacc); let (cx, cy) = xcell(stop_lat[i], stop_lon[i]); let mut cand: Vec<(u32, f64)> = Vec::new(); for dx in -1..=1 { for dy in -1..=1 { for &(_, j) in xbucket((cx + dx, cy + dy)) { if j as usize == i { continue; } let d = dist_m(stop_lat[i], stop_lon[i], stop_lat[j as usize], stop_lon[j as usize]); if d <= MAX_TRANSFER_M { cand.push((j, d)); } } } } // Stable, so equidistant candidates keep the dx/dy/stop order above and // `truncate` drops a deterministic tail. cand.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal)); cand.truncate(MAX_TRANSFERS_PER_STOP); for (j, d) in cand { let secs = (d / WALK_SPEED_M_S).ceil() as u32; append_u32(&mut sec_transfers, j); append_u32(&mut sec_transfers, secs); tacc += 1; transfer_total += 1; } } append_u32(&mut sec_transfers_idx, tacc); (sec_transfers, sec_transfers_idx, transfer_total) } fn build_services_and_exceptions( svc_mask: &[u8], svc_start: &[u32], svc_end: &[u32], exceptions: Vec<(u32, u32, u32)>, ) -> (Vec, Vec, Vec) { // --- Services + exceptions --- let mut sec_services = Vec::new(); for i in 0..svc_mask.len() { sec_services.push(svc_mask[i]); sec_services.extend_from_slice(&[0u8, 0, 0]); append_u32(&mut sec_services, svc_start[i]); append_u32(&mut sec_services, svc_end[i]); } // --- Exceptions: sorted by (service_idx, date), one row per pair, with a // CSR index so the device does a range lookup + binary search instead of a // full scan per trip. `add_feed` already stable-sorted each feed's slice // and a feed's services are a contiguous index range, so the whole array // is sorted; for duplicate (service, date) rows the last in CSV order // wins — matching the old reader, which scanned every exception without // breaking out. --- debug_assert!( exceptions.windows(2).all(|w| (w[0].0, w[0].1) <= (w[1].0, w[1].1)), "per-feed exception slices did not concatenate into a sorted array" ); let mut exc_unique: Vec<(u32, u32, u32)> = Vec::with_capacity(exceptions.len()); for e in exceptions { match exc_unique.last_mut() { Some(last) if last.0 == e.0 && last.1 == e.1 => *last = e, _ => exc_unique.push(e), } } let mut sec_exceptions = Vec::new(); let mut sec_exceptions_idx = Vec::new(); let mut eacc: u32 = 0; let mut ei = 0usize; for s in 0..svc_mask.len() as u32 { append_u32(&mut sec_exceptions_idx, eacc); while let Some(&(sidx, date, added)) = exc_unique.get(ei) { if sidx != s { break; } append_u32(&mut sec_exceptions, sidx); append_u32(&mut sec_exceptions, date); append_u32(&mut sec_exceptions, added); eacc += 1; ei += 1; } } append_u32(&mut sec_exceptions_idx, eacc); (sec_services, sec_exceptions, sec_exceptions_idx) } fn build_spatial_grid( stop_lat: &[i32], stop_lon: &[i32], grid_lat0: i32, grid_lon0: i32, max_lat: i32, max_lon: i32, ) -> (Vec, Vec, Vec, u32, u32, u32, usize) { let stop_count = stop_lat.len(); let grid_cell_e7 = (GRID_CELL_DEG * 1e7) as u32; let cell_col = |lon_e7: i32| -> i64 { ((lon_e7 as i64 - grid_lon0 as i64) / grid_cell_e7 as i64).max(0) }; let cell_row = |lat_e7: i32| -> i64 { ((lat_e7 as i64 - grid_lat0 as i64) / grid_cell_e7 as i64).max(0) }; let grid_cols = (cell_col(max_lon) + 1) as u32; let grid_rows = (cell_row(max_lat) + 1) as u32; // Sorted by `(cell_id, stop)`, which is exactly the two orderings the device // requires: GRID_CELL_IDS ascending, because the reader binary-searches it, // and each cell's GRID_STOPS run stop-ascending. let mut grid: Vec<(u32, u32)> = (0..stop_count) .map(|i| { let row = cell_row(stop_lat[i]) as u32; let col = cell_col(stop_lon[i]) as u32; (row * grid_cols + col, i as u32) }) .collect(); grid.sort_unstable(); let mut sec_grid_cell_ids = Vec::new(); let mut sec_grid_cell_off = Vec::new(); let mut sec_grid_stops = Vec::with_capacity(stop_count * 4); let mut grid_cell_count = 0usize; for (k, &(cid, stop)) in grid.iter().enumerate() { if k == 0 || cid != grid[k - 1].0 { append_u32(&mut sec_grid_cell_ids, cid); append_u32(&mut sec_grid_cell_off, k as u32); grid_cell_count += 1; } append_u32(&mut sec_grid_stops, stop); } append_u32(&mut sec_grid_cell_off, grid.len() as u32); ( sec_grid_cell_ids, sec_grid_cell_off, sec_grid_stops, grid_cols, grid_rows, grid_cell_e7, grid_cell_count, ) }