Skip to content

Commit

Permalink
Less allocation (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ManevilleF authored Feb 20, 2024
1 parent 7191549 commit 3b81ffd
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 21 deletions.
10 changes: 5 additions & 5 deletions src/components/cell/hexagon_2d_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ impl Cell for HexagonCell2d {
}

#[inline]
fn neighbor_coordinates(&self) -> Vec<Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec()
fn neighbor_coordinates(&self) -> impl IntoIterator<Item = Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords())
}
}

Expand All @@ -80,7 +80,7 @@ mod tests {
let cell = HexagonCell2d {
coords: IVec3::new(10, 10, 10),
};
let neighbors = cell.neighbor_coordinates();
let neighbors = cell.neighbor_coordinates().into_iter().collect::<Vec<_>>();
assert_eq!(
neighbors,
vec![
Expand All @@ -99,7 +99,7 @@ mod tests {
let cell = HexagonCell2d {
coords: IVec3::new(-10, 8, 5),
};
let neighbors = cell.neighbor_coordinates();
let neighbors = cell.neighbor_coordinates().into_iter().collect::<Vec<_>>();
assert_eq!(
neighbors,
vec![
Expand All @@ -118,7 +118,7 @@ mod tests {
let cell = HexagonCell2d {
coords: IVec3::new(0, 0, 0),
};
let neighbors = cell.neighbor_coordinates();
let neighbors = cell.neighbor_coordinates().into_iter().collect::<Vec<_>>();
assert_eq!(
neighbors,
vec![
Expand Down
2 changes: 1 addition & 1 deletion src/components/cell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ pub trait Cell: Clone + Component {

/// Retrieves the coordinates of the neighbor cells
#[must_use]
fn neighbor_coordinates(&self) -> Vec<Self::Coordinates>;
fn neighbor_coordinates(&self) -> impl IntoIterator<Item = Self::Coordinates>;
}
10 changes: 5 additions & 5 deletions src/components/cell/moore_2d_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ impl Cell for MooreCell2d {
}

#[inline]
fn neighbor_coordinates(&self) -> Vec<Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec()
fn neighbor_coordinates(&self) -> impl IntoIterator<Item = Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords())
}
}

Expand All @@ -86,7 +86,7 @@ mod tests {
let cell = MooreCell2d {
coords: IVec2::new(10, 10),
};
let neighbors = cell.neighbor_coordinates();
let neighbors = cell.neighbor_coordinates().into_iter().collect::<Vec<_>>();
assert_eq!(
neighbors,
vec![
Expand Down Expand Up @@ -115,7 +115,7 @@ mod tests {
let cell = MooreCell2d {
coords: IVec2::new(-10, 10),
};
let neighbors = cell.neighbor_coordinates();
let neighbors = cell.neighbor_coordinates().into_iter().collect::<Vec<_>>();
assert_eq!(
neighbors,
vec![
Expand Down Expand Up @@ -144,7 +144,7 @@ mod tests {
let cell = MooreCell2d {
coords: IVec2::new(0, 0),
};
let neighbors = cell.neighbor_coordinates();
let neighbors = cell.neighbor_coordinates().into_iter().collect::<Vec<_>>();
assert_eq!(
neighbors,
vec![
Expand Down
4 changes: 2 additions & 2 deletions src/components/cell/moore_3d_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ impl Cell for MooreCell3d {
}

#[inline]
fn neighbor_coordinates(&self) -> Vec<Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec()
fn neighbor_coordinates(&self) -> impl IntoIterator<Item = Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords())
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/components/cell/neumann_2d_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ impl Cell for NeumannCell2d {
}

#[inline]
fn neighbor_coordinates(&self) -> Vec<Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec()
fn neighbor_coordinates(&self) -> impl IntoIterator<Item = Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords())
}
}

Expand All @@ -77,7 +77,7 @@ mod tests {
let cell = NeumannCell2d {
coords: IVec2::new(10, 10),
};
let neighbors = cell.neighbor_coordinates();
let neighbors = cell.neighbor_coordinates().into_iter().collect::<Vec<_>>();
assert_eq!(
neighbors,
vec![
Expand All @@ -98,7 +98,7 @@ mod tests {
let cell = NeumannCell2d {
coords: IVec2::new(-10, 10),
};
let neighbors = cell.neighbor_coordinates();
let neighbors = cell.neighbor_coordinates().into_iter().collect::<Vec<_>>();
assert_eq!(
neighbors,
vec![
Expand All @@ -119,7 +119,7 @@ mod tests {
let cell = NeumannCell2d {
coords: IVec2::new(0, 0),
};
let neighbors = cell.neighbor_coordinates();
let neighbors = cell.neighbor_coordinates().into_iter().collect::<Vec<_>>();
assert_eq!(
neighbors,
vec![
Expand Down
4 changes: 2 additions & 2 deletions src/components/cell/neumann_3d_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl Cell for NeumannCell3d {
}

#[inline]
fn neighbor_coordinates(&self) -> Vec<Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords()).to_vec()
fn neighbor_coordinates(&self) -> impl IntoIterator<Item = Self::Coordinates> {
NEIGHBOR_COORDINATES.map(|c| c + *self.coords())
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/systems/cells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ where
S: CellState,
{
let neighbor_coords = cell.neighbor_coordinates();
let neighbor_states = neighbor_coords.iter().filter_map(|c| map.get(c));
let neighbor_states = neighbor_coords.into_iter().filter_map(|c| map.get(&c));
let new_state = state.new_cell_state(neighbor_states);
if &new_state == state {
None
Expand Down

0 comments on commit 3b81ffd

Please sign in to comment.