/// Cut `path` into runs that each fit one edge's geometry. /// /// Returns inclusive index ranges. A run of two nodes is always allowed even if /// its single segment is too long to delta-encode: the caller stores no geometry /// for it, and the reader's straight-chord fallback is then exactly right, /// because a two-node run *is* a straight chord. fn split_runs(path: &[u32], coords: &[geom::Pt]) -> Vec<(usize, usize)> { let point = |n: u32| coords[n as usize]; let mut runs = Vec::new(); let mut start = 0usize; let mut cost = 1u64; let mut i = 1usize; while i < path.len() { let step = geom::segment_points(point(path[i - 1]), point(path[i])); if cost + step <= u64::from(geom::MAX_POINTS) { cost += step; i += 1; continue; } if i - 1 > start { // Close the run before this segment and retry it with a fresh budget // starting at the shared node. runs.push((start, i - 1)); start = i - 1; cost = 1; } else { // One segment that cannot be encoded at all, even alone. runs.push((start, i)); start = i; cost = 1; i += 1; } } if start < path.len() - 1 { runs.push((start, path.len() - 1)); } runs } /// Does dense node `n` survive as a graph node? /// /// A chain endpoint always does. Anything of degree 1 or 3+ is a junction, a /// dead end or an anchor the walks must address. Degree 2 is the only /// collapsible case and is handled by the walk, not here. /// /// Degree 0 is dropped unless it carries a transit stop: a node with no /// incident segment is a way the region filter removed (every road node outside /// the region reads degree 0 once its ways are skipped), dead weight in /// `nodes.bin` with no edge ever addressing it. An isolated stop is the one /// exception — the reconnect pass addresses stop nodes directly. #[inline] pub(crate) fn survives(endpoints: &Bitset, degree: &[u8], stop: &Bitset, n: u32) -> bool { if endpoints.get(u64::from(n)) { return true; } let d = degree[n as usize]; d != 2 && (d != 0 || stop.get(u64::from(n))) } // ---- the spill ----------------------------------------------------------- // // Chains are the last structure in the build that scales with the road network // rather than with the node set, and every stage after them is a sequential scan. // So they go to two flat files and the stages read them back: the CSR build, the // reconnect BFS and each write round all stream, and none of them needs the chain // set resident. // // Two properties are worth the redundancy in the record: // // * **The header is fixed size**, so the chain count is the file's length divided // by the record size, and any stage can seek straight to chain *k* and process a // contiguous slice of chains without reading the ones before it. // * **`chains.pts` holds coordinates, not node ids**, so `coords` — the largest // array in the build, and the only one indexed by *marked* rather than surviving // node — can be freed the moment the spill is written. The cost is that the two // endpoint node ids have to be repeated in the header, which is eight bytes a // chain against gigabytes of coordinates. /// Bytes per record in `chains.hdr`. The trailing padding is reserved and written /// as zero, so the record can grow a field without changing the stride. pub(crate) const CHAIN_REC_BYTES: u64 = 48; /// Bytes per point in `chains.pts`: `i32 lat_e7`, `i32 lon_e7`. const CHAIN_PT_BYTES: u64 = 8; /// One chain, as `chains.hdr` stores it. #[derive(Clone, Copy)] pub(crate) struct ChainRec { /// First point's index in `chains.pts`. pub pts_start: u64, pub pts_len: u32, /// Dense id of the first point. Kept even though the point itself is stored, /// because the writer needs the *node* to map to a final id and the /// coordinates cannot be mapped back to one. pub first: u32, /// Dense id of the last point. pub last: u32, pub dist_mm: u32, pub name_offset: u32, pub fwd_lane_off: u32, pub bwd_lane_off: u32, pub fwd_lane_count: u16, pub bwd_lane_count: u16, pub type_: u8, pub speed_limit: u8, pub oneway: bool, }