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 RoomCoordinate and RoomXY, new RoomOffset for optimized arithmetic #543

Merged
merged 27 commits into from
Oct 29, 2024
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9902299
feat: add indexing impls for `RoomCoordinate`
khoover Oct 9, 2024
9293f72
feat: add `ROOM_USIZE` constant
khoover Oct 9, 2024
3cf8c14
feat: create x/y-major wrappers for `RoomXY` indexing
khoover Oct 9, 2024
e7cad72
feat: allow taking `LocalCostMatrix` as `XMajor<u8>` ref
khoover Oct 9, 2024
3271f97
refactor: use `RoomCoordinate` indexing for `LocalRoomTerrain`
khoover Oct 9, 2024
1f0ff51
refactor: use ROOM_USIZE instead of ROOM_SIZE as usize
khoover Oct 9, 2024
2324310
style: import ROOM_AREA from constants too
khoover Oct 9, 2024
2e69bd9
refactor: Replaced match with ?
khoover Oct 13, 2024
1257f7c
feat: Added size assumption function, removed unsafe
khoover Oct 13, 2024
f149aa7
fix: Fix clippy lints
khoover Oct 13, 2024
934be55
perf: better wasm emission for RoomCoordinate arithmetic
khoover Oct 13, 2024
469cdc4
perf: Added RoomCoord tests, improved checked add
khoover Oct 14, 2024
494f7fd
cleanup: Use copied and all in tests
khoover Oct 14, 2024
619c188
test: refine the room area test
khoover Oct 14, 2024
ada73d8
test: replace .min().max() with clamp
khoover Oct 14, 2024
b27b803
perf: faster saturating add for RoomCoordinate
khoover Oct 14, 2024
fb107cd
fix: eliminate all ROOM_SIZE-related literals
khoover Oct 14, 2024
bd2cfde
fix: fmt
khoover Oct 14, 2024
8f7ef85
fix: missing parens
khoover Oct 14, 2024
34b23db
feat: Add RoomOffset and more arithmetic
khoover Oct 14, 2024
7dba3a5
refactor: rename assume_size_constraint
khoover Oct 14, 2024
5bff038
More docs, `RoomOffset::abs`
khoover Oct 15, 2024
7701804
refactor: Add doc comments, return Result over Option
khoover Oct 29, 2024
9568dc7
fix: added correct import for doctest
khoover Oct 29, 2024
394a303
fix: fmt
khoover Oct 29, 2024
2f10f6f
fix: Fix wrapping arithmetic for overflowing_add.
khoover Oct 29, 2024
63ca176
Changelog
shanemadden Oct 29, 2024
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
refactor: rename assume_size_constraint
  • Loading branch information
khoover committed Oct 14, 2024
commit 7dba3a53a59ffe10187aaf385cb2666a18ce4dc9
26 changes: 13 additions & 13 deletions src/local/room_coordinate.rs
khoover marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ impl RoomCoordinate {
/// Provides a hint to the compiler that the contained `u8` is smaller than
/// `ROOM_SIZE`. Allows for better optimized safe code that uses this
/// property.
pub fn assume_size_constraint(self) {
pub fn assume_bounds_constraint(self) {
debug_assert!(self.0 < ROOM_SIZE);
// SAFETY: It is only safe to construct `RoomCoordinate` when self.0 <
// ROOM_SIZE.
@@ -100,7 +100,7 @@ impl RoomCoordinate {
/// assert_eq!(forty_nine.checked_add(1), None);
/// ```
pub fn checked_add(self, rhs: i8) -> Option<RoomCoordinate> {
self.assume_size_constraint();
self.assume_bounds_constraint();
// Why this works, assuming ROOM_SIZE < i8::MAX + 1 == 128 and ignoring the
// test:
// - if rhs < 0: the smallest value this can produce is -128, which casted to
@@ -112,7 +112,7 @@ impl RoomCoordinate {
}

pub fn checked_add_offset(self, rhs: RoomOffset) -> Option<RoomCoordinate> {
self.assume_size_constraint();
self.assume_bounds_constraint();
rhs.assume_bounds_constraint();
RoomCoordinate::new(self.0.wrapping_add_signed(rhs.0)).ok()
}
@@ -135,7 +135,7 @@ impl RoomCoordinate {
/// assert_eq!(forty_nine.saturating_add(i8::MIN), zero);
/// ```
pub fn saturating_add(self, rhs: i8) -> RoomCoordinate {
self.assume_size_constraint();
self.assume_bounds_constraint();
let (res, overflow) = self.0.overflowing_add_signed(rhs);
if overflow {
RoomCoordinate::MIN
@@ -146,14 +146,14 @@ impl RoomCoordinate {
}

pub fn saturating_add_offset(self, rhs: RoomOffset) -> Self {
self.assume_size_constraint();
self.assume_bounds_constraint();
rhs.assume_bounds_constraint();
let result = (self.0 as i8 + rhs.0).clamp(0, ROOM_SIZE_I8 - 1);
RoomCoordinate::new(result as u8).unwrap_throw()
}

pub fn overflowing_add(self, rhs: i8) -> (RoomCoordinate, bool) {
self.assume_size_constraint();
self.assume_bounds_constraint();
let raw = self.0 as i16 + rhs as i16;
if raw >= ROOM_SIZE as i16 {
(
@@ -171,7 +171,7 @@ impl RoomCoordinate {
}

pub fn overflowing_add_offset(self, rhs: RoomOffset) -> (RoomCoordinate, bool) {
self.assume_size_constraint();
self.assume_bounds_constraint();
rhs.assume_bounds_constraint();
let raw = self.0 as i8 + rhs.0;
if raw >= ROOM_SIZE_I8 {
@@ -198,12 +198,12 @@ impl RoomCoordinate {
}

pub unsafe fn unchecked_add(self, rhs: i8) -> Self {
self.assume_size_constraint();
self.assume_bounds_constraint();
Self::unchecked_new((self.0 as i8).unchecked_add(rhs) as u8)
}

pub unsafe fn unchecked_add_offset(self, rhs: RoomOffset) -> Self {
self.assume_size_constraint();
self.assume_bounds_constraint();
rhs.assume_bounds_constraint();
Self::unchecked_new((self.0 as i8).unchecked_add(rhs.0) as u8)
}
@@ -239,14 +239,14 @@ impl<T> Index<RoomCoordinate> for [T; ROOM_USIZE] {
type Output = T;

fn index(&self, index: RoomCoordinate) -> &Self::Output {
index.assume_size_constraint();
index.assume_bounds_constraint();
&self[index.0 as usize]
}
}

impl<T> IndexMut<RoomCoordinate> for [T; ROOM_USIZE] {
fn index_mut(&mut self, index: RoomCoordinate) -> &mut Self::Output {
index.assume_size_constraint();
index.assume_bounds_constraint();
&mut self[index.0 as usize]
}
}
@@ -277,8 +277,8 @@ impl Sub for RoomCoordinate {
type Output = RoomOffset;

fn sub(self, rhs: Self) -> Self::Output {
self.assume_size_constraint();
rhs.assume_size_constraint();
self.assume_bounds_constraint();
rhs.assume_bounds_constraint();
RoomOffset::new(self.0 as i8 - rhs.0 as i8).unwrap_throw()
}
}
Loading