Skip to content

Commit

Permalink
editoast: make intersection as a type
Browse files Browse the repository at this point in the history
Signed-off-by: Jean SIMARD <[email protected]>
  • Loading branch information
woshilapin committed Sep 20, 2024
1 parent 68e0513 commit 8a82a50
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
46 changes: 38 additions & 8 deletions editoast/src/views/path/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<'a> PathProjection<'a> {
/// If there is no common track section range, the returned list is empty.
/// The positions in the intersection list are guaranteed to increase. In other words `list[n].0 < list[n].1 <= list[n+1].0 < list[n+1].1`
/// These positions can then be use in conjunction with [PathProjection::get_location].
pub fn get_intersections(&self, track_ranges: &[TrackRange]) -> Vec<(u64, u64)> {
pub fn get_intersections(&self, track_ranges: &[TrackRange]) -> Vec<Intersection> {
// Handle the length computation in mm
let mut next_pos: u64 = 0;
let mut current_pos: u64;
Expand Down Expand Up @@ -206,10 +206,32 @@ impl<'a> PathProjection<'a> {
}
}

#[cfg_attr(test, derive(Debug, PartialEq))]
pub struct Intersection {
start: u64,
end: u64,
}
impl From<(u64, u64)> for Intersection {
fn from((start, end): (u64, u64)) -> Self {
debug_assert!(
start <= end,
"intersection should have a 'start' ({start}) smaller than 'end' ({end})"
);
Self { start, end }
}
}
impl Intersection {
pub fn start(&self) -> u64 {
self.start
}
pub fn end(&self) -> u64 {
self.end
}
}
struct IntersectionBuilder {
start: Option<u64>,
current: u64,
intersections: Vec<(u64, u64)>,
intersections: Vec<Intersection>,
}

impl IntersectionBuilder {
Expand All @@ -224,7 +246,8 @@ impl IntersectionBuilder {
fn finish(&mut self) {
if let Some(start) = self.start {
assert_ne!(start, self.current);
self.intersections.push((start, self.current));
self.intersections
.push(Intersection::from((start, self.current)));
}
self.start = None;
}
Expand Down Expand Up @@ -396,7 +419,7 @@ mod tests {
];

let boundaries = projection.get_intersections(&track_ranges);
let expected: Vec<(u64, u64)> = vec![(50, 730)];
let expected: Vec<Intersection> = vec![Intersection::from((50, 730))];

assert_eq!(boundaries, expected);
}
Expand All @@ -421,7 +444,10 @@ mod tests {
];

let boundaries = projection.get_intersections(&track_ranges);
let expected: Vec<(u64, u64)> = vec![(100, 350), (350, 420)];
let expected: Vec<Intersection> = vec![
Intersection::from((100, 350)),
Intersection::from((350, 420)),
];

assert_eq!(boundaries, expected);
}
Expand All @@ -447,7 +473,11 @@ mod tests {
];

let boundaries = projection.get_intersections(&track_ranges);
let expected: Vec<(u64, u64)> = vec![(50, 300), (400, 450), (550, 620)];
let expected: Vec<Intersection> = vec![
Intersection::from((50, 300)),
Intersection::from((400, 450)),
Intersection::from((550, 620)),
];

assert_eq!(boundaries, expected);
}
Expand All @@ -461,7 +491,7 @@ mod tests {

let boundaries = projection.get_intersections(&track_ranges);

let expected: Vec<(u64, u64)> = vec![];
let expected: Vec<Intersection> = vec![];
assert_eq!(boundaries, expected);
}

Expand All @@ -474,7 +504,7 @@ mod tests {

let boundaries = projection.get_intersections(&track_ranges);

let expected: Vec<(u64, u64)> = vec![];
let expected: Vec<Intersection> = vec![];
assert_eq!(boundaries, expected);
}
}
3 changes: 2 additions & 1 deletion editoast/src/views/train_schedule/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,8 @@ fn compute_space_time_curves(

let mut space_time_curves = vec![];
for intersection in intersections {
let (start, end) = intersection;
let start = intersection.start();
let end = intersection.end();
let start_index = find_index_upper(positions, start);
let end_index = find_index_upper(positions, end);

Expand Down

0 comments on commit 8a82a50

Please sign in to comment.