Skip to content

Commit

Permalink
Don't redundantly sort Rect inputs
Browse files Browse the repository at this point in the history
Since ec69add, `Rect::new` already
sorts it's input by min/max, and so there's no longer a need to manually
sort min/max points.
  • Loading branch information
michaelkirk committed Mar 10, 2021
1 parent e322624 commit 2650eba
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 59 deletions.
10 changes: 1 addition & 9 deletions geo-types/src/private_utils.rs
Expand Up @@ -16,15 +16,7 @@ pub fn line_bounding_rect<T>(line: Line<T>) -> Rect<T>
where
T: CoordNum,
{
let a = line.start;
let b = line.end;
let (xmin, xmax) = if a.x <= b.x { (a.x, b.x) } else { (b.x, a.x) };
let (ymin, ymax) = if a.y <= b.y { (a.y, b.y) } else { (b.y, a.y) };

Rect::new(
Coordinate { x: xmin, y: ymin },
Coordinate { x: xmax, y: ymax },
)
Rect::new(line.start, line.end)
}

pub fn get_bounding_rect<I, T>(collection: I) -> Option<Rect<T>>
Expand Down
9 changes: 1 addition & 8 deletions geo/src/algorithm/bounding_rect.rs
Expand Up @@ -66,14 +66,7 @@ where
type Output = Rect<T>;

fn bounding_rect(&self) -> Self::Output {
let a = self.start;
let b = self.end;
let (xmin, xmax) = if a.x <= b.x { (a.x, b.x) } else { (b.x, a.x) };
let (ymin, ymax) = if a.y <= b.y { (a.y, b.y) } else { (b.y, a.y) };
Rect::new(
Coordinate { x: xmin, y: ymin },
Coordinate { x: xmax, y: ymax },
)
Rect::new(self.start, self.end)
}
}

Expand Down
46 changes: 4 additions & 42 deletions geo/src/algorithm/map_coords.rs
Expand Up @@ -497,34 +497,11 @@ impl<T: CoordNum> MapCoordsInplace<T> for GeometryCollection<T> {
}
}

fn normalize_rect_bounds<T: PartialOrd>(min: &mut (T, T), max: &mut (T, T)) {
use std::mem::swap;
if min.0 > max.0 {
swap(&mut min.0, &mut max.0);
}
if min.1 > max.1 {
swap(&mut min.1, &mut max.1);
}
}

impl<T: CoordNum, NT: CoordNum> MapCoords<T, NT> for Rect<T> {
type Output = Rect<NT>;

fn map_coords(&self, func: impl Fn(&(T, T)) -> (NT, NT) + Copy) -> Self::Output {
let mut new_min = func(&self.min().x_y());
let mut new_max = func(&self.max().x_y());
normalize_rect_bounds(&mut new_min, &mut new_max);

Rect::new(
Coordinate {
x: new_min.0,
y: new_min.1,
},
Coordinate {
x: new_max.0,
y: new_max.1,
},
)
Rect::new(func(&self.min().x_y()), func(&self.max().x_y()))
}
}

Expand All @@ -535,31 +512,16 @@ impl<T: CoordNum, NT: CoordNum> TryMapCoords<T, NT> for Rect<T> {
&self,
func: impl Fn(&(T, T)) -> Result<(NT, NT), Box<dyn Error + Send + Sync>>,
) -> Result<Self::Output, Box<dyn Error + Send + Sync>> {
let mut new_min = func(&(self.min().x, self.min().y))?;
let mut new_max = func(&(self.max().x, self.max().y))?;
normalize_rect_bounds(&mut new_min, &mut new_max);

Ok(Rect::new(
Coordinate {
x: new_min.0,
y: new_min.1,
},
Coordinate {
x: new_max.0,
y: new_max.1,
},
func(&self.min().x_y())?,
func(&self.max().x_y())?,
))
}
}

impl<T: CoordNum> MapCoordsInplace<T> for Rect<T> {
fn map_coords_inplace(&mut self, func: impl Fn(&(T, T)) -> (T, T)) {
let mut new_min = func(&self.min().x_y());
let mut new_max = func(&self.max().x_y());

normalize_rect_bounds(&mut new_min, &mut new_max);
let mut new_rect = Rect::new(new_min, new_max);

let mut new_rect = Rect::new(func(&self.min().x_y()), func(&self.max().x_y()));
::std::mem::swap(self, &mut new_rect);
}
}
Expand Down

0 comments on commit 2650eba

Please sign in to comment.