From 06243918fcf4bb731d0c206b465ac7079ca6256e Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 7 Jan 2023 17:47:35 +0100 Subject: [PATCH] vectors: return value instead of reference There was no use-case for returning reference rather than a value and in majority of case the dereference was needed. When using 2D/3D vectors, they are mostly initialized with the numbers and it is contraproductive to pass around references to numbers as they are more expensive than the copies. Signed-off-by: Matej Focko --- src/bin/day14.rs | 8 ++++---- src/bin/day15.rs | 22 +++++++++++----------- src/bin/day18.rs | 26 +++++++++++++------------- src/bin/day23.rs | 16 ++++++++-------- src/vectors/vec2d.rs | 26 +++++++++++++------------- src/vectors/vec3d.rs | 14 ++++++++------ 6 files changed, 57 insertions(+), 55 deletions(-) diff --git a/src/bin/day14.rs b/src/bin/day14.rs index c03c5f7..b11ab8f 100644 --- a/src/bin/day14.rs +++ b/src/bin/day14.rs @@ -116,13 +116,13 @@ impl Solution for Day14 { } fn part_1(input: &Input) -> Output { - let max_y = *input + let max_y = input .iter() .map(|l| max(l.from.y(), l.to.y())) .max() .unwrap(); - let terminate = |grain: &Coord, _is_set: bool| *grain.y() >= max_y; + let terminate = |grain: &Coord, _is_set: bool| grain.y() >= max_y; simulate_falling(input, terminate) } @@ -140,8 +140,8 @@ impl Solution for Day14 { let mut lines = input.clone(); lines.push(Line { - from: Coord::new(500 - (*y + 2), *y + 2), - to: Coord::new(500 + (*y + 2), *y + 2), + from: Coord::new(500 - (y + 2), y + 2), + to: Coord::new(500 + (y + 2), y + 2), }); simulate_falling(&lines, terminate) diff --git a/src/bin/day15.rs b/src/bin/day15.rs index 138ee06..e430080 100644 --- a/src/bin/day15.rs +++ b/src/bin/day15.rs @@ -15,7 +15,7 @@ type Coord = Vector2D; fn manhattan(u: &Coord, v: &Coord) -> i32 { let direction = *v - *u; - (*direction.x()).abs() + (*direction.y()).abs() + (direction.x()).abs() + (direction.y()).abs() } #[derive(Debug, PartialEq, Eq, Clone)] @@ -27,12 +27,12 @@ struct Sensor { impl Sensor { fn bounds_at_y(&self, y: i32) -> Option<(Coord, Coord)> { let m = manhattan(&self.position, &self.beacon); - if y < *self.position.y() - m || y > *self.position.y() + m { + if y < self.position.y() - m || y > self.position.y() + m { return None; } - let x = *self.position.x(); - let side_d = m - (*self.position.y() - y).abs(); + let x = self.position.x(); + let side_d = m - (self.position.y() - y).abs(); Some((Coord::new(x - side_d, y), Coord::new(x + side_d, y))) } @@ -79,7 +79,7 @@ fn cannot_contain(sensors: &Input, watched_y: i32) -> Output { .iter() .filter_map(|s| s.bounds_at_y(watched_y)) .for_each(|(l, r)| { - (*l.x()..=*r.x()).for_each(|x| { + (l.x()..=r.x()).for_each(|x| { positions.insert(x, Status::Cannot); }) }); @@ -87,14 +87,14 @@ fn cannot_contain(sensors: &Input, watched_y: i32) -> Output { // rewrite beacons and sensors if needed sensors .iter() - .filter(|s| *s.position.y() == watched_y || *s.beacon.y() == watched_y) + .filter(|s| s.position.y() == watched_y || s.beacon.y() == watched_y) .for_each(|s| { - if *s.beacon.y() == watched_y { - positions.insert(*s.beacon.x(), Status::Beacon); + if s.beacon.y() == watched_y { + positions.insert(s.beacon.x(), Status::Beacon); } - if *s.position.y() == watched_y { - positions.insert(*s.beacon.x(), Status::Sensor); + if s.position.y() == watched_y { + positions.insert(s.beacon.x(), Status::Sensor); } }); @@ -113,7 +113,7 @@ fn check_row(sensors: &Input, watched_y: i32, upper_bound: i32) -> Vec<(i32, i32 sensors .iter() .filter_map(|s| s.bounds_at_y(watched_y)) - .map(|(l, r)| (max(0, *l.x()), min(*r.x(), upper_bound))), + .map(|(l, r)| (max(0, l.x()), min(r.x(), upper_bound))), ); positions.sort(); diff --git a/src/bin/day18.rs b/src/bin/day18.rs index f813393..4924681 100644 --- a/src/bin/day18.rs +++ b/src/bin/day18.rs @@ -25,9 +25,9 @@ fn get_bounding_box(droplets: &[Coord]) -> (Coord, Coord) { .iter() .fold(Coord::new(i32::MAX, i32::MAX, i32::MAX), |acc, &droplet| { Coord::new( - min(*acc.x(), *droplet.x()), - min(*acc.y(), *droplet.y()), - min(*acc.z(), *droplet.z()), + min(acc.x(), droplet.x()), + min(acc.y(), droplet.y()), + min(acc.z(), droplet.z()), ) }); let max_coord = @@ -35,9 +35,9 @@ fn get_bounding_box(droplets: &[Coord]) -> (Coord, Coord) { .iter() .fold(Coord::new(i32::MIN, i32::MIN, i32::MIN), |acc, &droplet| { Coord::new( - max(*acc.x(), *droplet.x()), - max(*acc.y(), *droplet.y()), - max(*acc.z(), *droplet.z()), + max(acc.x(), droplet.x()), + max(acc.y(), droplet.y()), + max(acc.z(), droplet.z()), ) }); @@ -50,9 +50,9 @@ fn _check_bounding_box(droplets: &[Coord]) { debug!("Bounding box: {:?} <-> {:?}", min_coord, max_coord); debug!( "Cubes in a bounding box: {:?}", - (*max_coord.x() - *min_coord.x() + 1) - * (*max_coord.y() - *min_coord.y() + 1) - * (*max_coord.z() - *min_coord.z() + 1) + (max_coord.x() - min_coord.x() + 1) + * (max_coord.y() - min_coord.y() + 1) + * (max_coord.z() - min_coord.z() + 1) ); } @@ -123,9 +123,9 @@ impl Solution for Day18 { // lava everywhere for cube in [ - *min_coords.x()..=*max_coords.x(), - *min_coords.y()..=*max_coords.y(), - *min_coords.z()..=*max_coords.z(), + min_coords.x()..=max_coords.x(), + min_coords.y()..=max_coords.y(), + min_coords.z()..=max_coords.z(), ] .into_iter() .multi_cartesian_product() @@ -148,7 +148,7 @@ impl Solution for Day18 { // update the outside with the air using a BFS aerate( &mut space, - Coord::new(*min_coords.x(), *min_coords.y(), *min_coords.z()), + Coord::new(min_coords.x(), min_coords.y(), min_coords.z()), ); // count the faces touching the air diff --git a/src/bin/day23.rs b/src/bin/day23.rs index a945cb5..0fb420b 100644 --- a/src/bin/day23.rs +++ b/src/bin/day23.rs @@ -146,20 +146,20 @@ fn _show_ground(positions: &Input) { let min_pos = positions .iter() .fold(Vector2D::new(isize::MAX, isize::MAX), |acc, elf| { - Vector2D::new(min(*acc.x(), *elf.x()), min(*acc.y(), *elf.y())) + Vector2D::new(min(acc.x(), elf.x()), min(acc.y(), elf.y())) }); let max_pos = positions .iter() .fold(Vector2D::new(isize::MIN, isize::MIN), |acc, elf| { - Vector2D::new(max(*acc.x(), *elf.x()), max(*acc.y(), *elf.y())) + Vector2D::new(max(acc.x(), elf.x()), max(acc.y(), elf.y())) }); _show_ground_with_dimensions( positions, - *min_pos.x(), - *max_pos.x(), - *min_pos.y(), - *max_pos.y(), + min_pos.x(), + max_pos.x(), + min_pos.y(), + max_pos.y(), ) } @@ -198,12 +198,12 @@ impl Solution for Day23 { let min_pos = positions .iter() .fold(Vector2D::new(isize::MAX, isize::MAX), |acc, elf| { - Vector2D::new(min(*acc.x(), *elf.x()), min(*acc.y(), *elf.y())) + Vector2D::new(min(acc.x(), elf.x()), min(acc.y(), elf.y())) }); let max_pos = positions .iter() .fold(Vector2D::new(isize::MIN, isize::MIN), |acc, elf| { - Vector2D::new(max(*acc.x(), *elf.x()), max(*acc.y(), *elf.y())) + Vector2D::new(max(acc.x(), elf.x()), max(acc.y(), elf.y())) }); (max_pos.x() - min_pos.x() + 1) * (max_pos.y() - min_pos.y() + 1) diff --git a/src/vectors/vec2d.rs b/src/vectors/vec2d.rs index 4e87e5c..7700688 100644 --- a/src/vectors/vec2d.rs +++ b/src/vectors/vec2d.rs @@ -14,13 +14,22 @@ impl Vector2D { pub fn new(x: T, y: T) -> Vector2D { Vector2D { x, y } } +} - pub fn x(&self) -> &T { - &self.x +impl Vector2D { + pub fn swap(&self) -> Self { + Self { + x: self.y, + y: self.x, + } } - pub fn y(&self) -> &T { - &self.y + pub fn x(&self) -> T { + self.x + } + + pub fn y(&self) -> T { + self.y } } @@ -71,15 +80,6 @@ where .unwrap_or(false) } -impl Vector2D { - pub fn swap(&self) -> Self { - Self { - x: self.y, - y: self.x, - } - } -} - // See: https://github.com/rust-lang/rust/issues/102731 // impl, T> From> for Vector2D { // fn from(value: Vector2D) -> Self { diff --git a/src/vectors/vec3d.rs b/src/vectors/vec3d.rs index 72124ad..55cea8f 100644 --- a/src/vectors/vec3d.rs +++ b/src/vectors/vec3d.rs @@ -15,17 +15,19 @@ impl Vector3D { pub fn new(x: T, y: T, z: T) -> Vector3D { Vector3D { x, y, z } } +} - pub fn x(&self) -> &T { - &self.x +impl Vector3D { + pub fn x(&self) -> T { + self.x } - pub fn y(&self) -> &T { - &self.y + pub fn y(&self) -> T { + self.y } - pub fn z(&self) -> &T { - &self.z + pub fn z(&self) -> T { + self.z } }