Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / graph_build_s1.rs
graph_build_s1.rs77 lines · 3.0 KiBRaw
| 1 | impl AtomicBits<'_> { |
| 2 | /// Set bit `idx`; true when this call is the one that set it. A plain load |
| 3 | /// first, so re-marking an already-set bit (most refs: a node is shared by |
| 4 | /// every way through it) never takes the cache line exclusive. |
| 5 | #[inline] |
| 6 | pub(crate) fn set(&self, idx: u64) -> bool { |
| 7 | use std::sync::atomic::Ordering::Relaxed; |
| 8 | let byte = &self.0[(idx / 8) as usize]; |
| 9 | let mask = 1u8 << (idx % 8); |
| 10 | if byte.load(Relaxed) & mask != 0 { |
| 11 | return false; |
| 12 | } |
| 13 | byte.fetch_or(mask, Relaxed) & mask == 0 |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | /// Dense addressing for the graph's nodes: which OSM ids are in the graph, and |
| 18 | /// which of those the file actually supplied coordinates for. |
| 19 | /// |
| 20 | /// Both tests matter, and for different reasons. The mask is what makes |
| 21 | /// [`Bitset::dense`] meaningful. `present` is what keeps *dangling references* |
| 22 | /// out: pass 1 marks every ref of every routable way, including refs to nodes no |
| 23 | /// blob in the file defines — an extract clipped at a boundary is full of them — |
| 24 | /// and those slots never receive coordinates. Left in, each would read as |
| 25 | /// `(0, 0)`, a point in the Atlantic that corrupts distances, spatial keys and |
| 26 | /// polylines while leaving every count looking plausible. |
| 27 | pub(crate) struct NodeIndex { |
| 28 | pub(crate) mask: Bitset, |
| 29 | present: Bitset, |
| 30 | } |
| 31 | |
| 32 | impl NodeIndex { |
| 33 | /// Dense id of a node that is both in the graph and really in the file. |
| 34 | #[inline] |
| 35 | pub(crate) fn dense(&self, osm_id: i64) -> Option<u32> { |
| 36 | if osm_id < 0 || osm_id as u64 >= BITSET_SIZE || !self.mask.get(osm_id as u64) { |
| 37 | return None; |
| 38 | } |
| 39 | let d = self.mask.dense(osm_id as u64); |
| 40 | self.present.get(u64::from(d)).then_some(d) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | #[derive(Default)] |
| 45 | struct Pass1 { |
| 46 | /// Ids this chunk was the first to mark. Refs and stops are marked straight |
| 47 | /// into the shared mask from the decode workers (a set union, so order |
| 48 | /// cannot matter); only these two scalars reach the sink. Nothing about a |
| 49 | /// way is retained, because the ways pass re-reads them. |
| 50 | marked: u64, |
| 51 | /// Largest id this chunk marked or found already marked. |
| 52 | max_id: u64, |
| 53 | } |
| 54 | |
| 55 | /// A region bbox filter, or `None` for a world build. |
| 56 | /// |
| 57 | /// Passed by value into the block visitors (it is `Copy`): every routable way |
| 58 | /// whose nodes all fall outside is dropped, and a stop outside never enters |
| 59 | /// the graph. Ways are kept whole when any node touches — the same |
| 60 | /// `complete_ways` rule [`crate::bbox`] documents — so a road straddling the |
| 61 | /// border keeps its boundary stub rather than ending mid-segment. |
| 62 | pub type RegionFilter = Option<crate::bbox::BBox>; |
| 63 | |
| 64 | include!("graph_build_part1.rs"); |
| 65 | include!("graph_build_part2.rs"); |
| 66 | include!("graph_build_part3.rs"); |
| 67 | include!("graph_build_part4.rs"); |
| 68 | include!("graph_build_part5.rs"); |
| 69 | include!("graph_build_part11.rs"); |
| 70 | #[cfg(test)] |
| 71 | mod tests { |
| 72 | include!("graph_build_part6.rs"); |
| 73 | include!("graph_build_part7.rs"); |
| 74 | include!("graph_build_part8.rs"); |
| 75 | include!("graph_build_part9.rs"); |
| 76 | include!("graph_build_part10.rs"); |
| 77 | } |