impl AtomicBits<'_> { /// Set bit `idx`; true when this call is the one that set it. A plain load /// first, so re-marking an already-set bit (most refs: a node is shared by /// every way through it) never takes the cache line exclusive. #[inline] pub(crate) fn set(&self, idx: u64) -> bool { use std::sync::atomic::Ordering::Relaxed; let byte = &self.0[(idx / 8) as usize]; let mask = 1u8 << (idx % 8); if byte.load(Relaxed) & mask != 0 { return false; } byte.fetch_or(mask, Relaxed) & mask == 0 } } /// Dense addressing for the graph's nodes: which OSM ids are in the graph, and /// which of those the file actually supplied coordinates for. /// /// Both tests matter, and for different reasons. The mask is what makes /// [`Bitset::dense`] meaningful. `present` is what keeps *dangling references* /// out: pass 1 marks every ref of every routable way, including refs to nodes no /// blob in the file defines — an extract clipped at a boundary is full of them — /// and those slots never receive coordinates. Left in, each would read as /// `(0, 0)`, a point in the Atlantic that corrupts distances, spatial keys and /// polylines while leaving every count looking plausible. pub(crate) struct NodeIndex { pub(crate) mask: Bitset, present: Bitset, } impl NodeIndex { /// Dense id of a node that is both in the graph and really in the file. #[inline] pub(crate) fn dense(&self, osm_id: i64) -> Option { if osm_id < 0 || osm_id as u64 >= BITSET_SIZE || !self.mask.get(osm_id as u64) { return None; } let d = self.mask.dense(osm_id as u64); self.present.get(u64::from(d)).then_some(d) } } #[derive(Default)] struct Pass1 { /// Ids this chunk was the first to mark. Refs and stops are marked straight /// into the shared mask from the decode workers (a set union, so order /// cannot matter); only these two scalars reach the sink. Nothing about a /// way is retained, because the ways pass re-reads them. marked: u64, /// Largest id this chunk marked or found already marked. max_id: u64, } /// A region bbox filter, or `None` for a world build. /// /// Passed by value into the block visitors (it is `Copy`): every routable way /// whose nodes all fall outside is dropped, and a stop outside never enters /// the graph. Ways are kept whole when any node touches — the same /// `complete_ways` rule [`crate::bbox`] documents — so a road straddling the /// border keeps its boundary stub rather than ending mid-segment. pub type RegionFilter = Option; include!("graph_build_part1.rs"); include!("graph_build_part2.rs"); include!("graph_build_part3.rs"); include!("graph_build_part4.rs"); include!("graph_build_part5.rs"); include!("graph_build_part11.rs"); #[cfg(test)] mod tests { include!("graph_build_part6.rs"); include!("graph_build_part7.rs"); include!("graph_build_part8.rs"); include!("graph_build_part9.rs"); include!("graph_build_part10.rs"); }