Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved rings performance #165

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Wedge/Corner wedge
  • Loading branch information
ManevilleF committed Apr 12, 2024
commit 5a2e317fe8cc8339b91be8ce9352ce79c3b88998
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Update `field_of_movement` to use `HexagonalMap` (#163)
* Added `shapes::rombus` (#163)
* Improved performance `Hex` ring compute methods (#165)
* Improved performance `Hex` edge/wedge compute methods (#165)

## 0.16.1

Expand Down
2 changes: 1 addition & 1 deletion benches/wedges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use hexx::*;
pub fn wedge_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("Hex Wedge");
group.significance_level(0.1).sample_size(100);
let dist = 1000;
let dist = 500;

group.bench_with_input(BenchmarkId::new("Wedge", dist), &dist, |b, dist| {
b.iter(|| {
Expand Down
26 changes: 15 additions & 11 deletions src/hex/rings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Hex {
clockwise: bool,
) -> impl ExactSizeIterator<Item = Self> {
let [start_dir, end_dir] = Self::__vertex_dir_to_edge_dir(direction, clockwise);
self.__ring_edge(radius, start_dir, end_dir)
self.__ring_edge(radius, radius, start_dir, end_dir)
}

#[inline]
Expand All @@ -131,17 +131,20 @@ impl Hex {
}
}

/// Computes an `origin` as `self + start_dir * dist`
/// and computes a line between `origin` and `origin + len * end_dir`
#[allow(clippy::cast_possible_wrap)]
fn __ring_edge(
self,
radius: u32,
dist: u32,
len: u32,
start_dir: EdgeDirection,
end_dir: EdgeDirection,
) -> impl ExactSizeIterator<Item = Self> {
let p = self + start_dir * radius as i32;
let p = self + start_dir * dist as i32;
ExactSizeHexIterator {
iter: (0..=radius).map(move |i| p + end_dir * i as i32),
count: radius as usize + 1,
iter: (0..=len).map(move |i| p + end_dir * i as i32),
count: dist as usize + 1,
}
}

Expand Down Expand Up @@ -185,7 +188,7 @@ impl Hex {
direction: VertexDirection,
) -> impl Iterator<Item = impl ExactSizeIterator<Item = Self>> {
let [start_dir, end_dir] = Self::__vertex_dir_to_edge_dir(direction, false);
ranges.map(move |r| self.__ring_edge(r, start_dir, end_dir))
ranges.map(move |r| self.__ring_edge(r, r, start_dir, end_dir))
}

/// Retrieves all successive [`Hex`] ring edges around `self` in given
Expand All @@ -212,7 +215,7 @@ impl Hex {
clockwise: bool,
) -> impl Iterator<Item = impl ExactSizeIterator<Item = Self>> {
let [start_dir, end_dir] = Self::__vertex_dir_to_edge_dir(direction, clockwise);
ranges.map(move |r| self.__ring_edge(r, start_dir, end_dir))
ranges.map(move |r| self.__ring_edge(r, r, start_dir, end_dir))
}

/// Retrieves all successive [`Hex`] ring edges around `self` in given
Expand Down Expand Up @@ -329,10 +332,11 @@ impl Hex {
range: impl Iterator<Item = u32> + Clone,
direction: EdgeDirection,
) -> impl Iterator<Item = Self> {
let left = self.wedge(range.clone(), direction.diagonal_ccw());
let right = self.wedge(range, direction.diagonal_cw());
left.chain(right)
.filter(move |h| self.way_to(*h) == direction)
let [left, right] = [direction << 2, direction >> 2];
range.flat_map(move |r| {
self.__ring_edge(r, r / 2, direction, left)
.chain(self.__ring_edge(r, r / 2, direction, right))
})
}

/// Retrieves all successive [`Hex`] half ring edges from `self` to `rhs`
Expand Down