Skip to content

Commit

Permalink
(Improvement) Add builder function LocalCostMatrix::new_with_value (#522
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jciskey authored Jun 25, 2024
1 parent eac5f8a commit bb74f6d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Unreleased
- Add function `Direction::iter` which returns an iterator over all the `Direction` enum values
- Add function `RoomXY::neighbors` which returns an iterator over all the valid neighbors of
a given `RoomXY` position
- Add static function `LocalCostMatrix::new_with_value` which returns a `LocalCostMatrix` with
every position set to a given `u8` value

0.21.0 (2024-05-14)
===================
Expand Down
29 changes: 28 additions & 1 deletion src/local/cost_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,37 @@ impl Default for LocalCostMatrix {
}

impl LocalCostMatrix {
/// Create a `LocalCostMatrix` with a default value of 0 for all positions.
///
/// # Example
///
/// ```rust
/// use screeps::local::{LocalCostMatrix, RoomXY};
///
/// let lcm = LocalCostMatrix::new();
/// let pos = unsafe { RoomXY::unchecked_new(10, 10) };
/// assert_eq!(lcm.get(pos), 0);
/// ```
#[inline]
pub const fn new() -> Self {
LocalCostMatrix::new_with_value(0)
}

/// Create a `LocalCostMatrix` with a default value for all positions.
///
/// # Example
///
/// ```rust
/// use screeps::local::{LocalCostMatrix, RoomXY};
///
/// let lcm = LocalCostMatrix::new_with_value(u8::MAX);
/// let pos = unsafe { RoomXY::unchecked_new(10, 10) };
/// assert_eq!(lcm.get(pos), u8::MAX);
/// ```
#[inline]
pub const fn new_with_value(value: u8) -> Self {
LocalCostMatrix {
bits: [0; ROOM_AREA],
bits: [value; ROOM_AREA],
}
}

Expand Down

0 comments on commit bb74f6d

Please sign in to comment.