Use ranges properly

This commit is contained in:
Greg Hellings
2024-10-16 15:59:39 -05:00
parent 0904dce763
commit 37d80eda28
2 changed files with 23 additions and 11 deletions
+17 -11
View File
@@ -66,7 +66,7 @@ impl Board {
pub fn update_square(&mut self, point: &Point, value: CellValue) { pub fn update_square(&mut self, point: &Point, value: CellValue) {
let row_bounds = point.row_bounds(); let row_bounds = point.row_bounds();
let col_bounds = point.col_bounds(); let col_bounds = point.col_bounds();
for c in self.cells.slice_mut(s![row_bounds.0..row_bounds.1, col_bounds.0..col_bounds.1]) { for c in self.cells.slice_mut(s![row_bounds, col_bounds]) {
c.remove_candidate(value); c.remove_candidate(value);
} }
} }
@@ -83,6 +83,12 @@ impl Board {
0..REGION_SIZE 0..REGION_SIZE
} }
pub fn square_iter(&self, point: &Point) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 2]>> {
let row_bounds = point.row_bounds();
let col_bounds = point.col_bounds();
self.cells.slice(s![row_bounds, col_bounds])
}
pub fn row_iter(&self, row: usize) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 1]>> { pub fn row_iter(&self, row: usize) -> ndarray::ArrayBase<ViewRepr<&Cell>, Dim<[usize; 1]>> {
self.cells.slice(s![row, ..]) self.cells.slice(s![row, ..])
} }
@@ -109,17 +115,17 @@ impl Point {
[self.row, self.col] [self.row, self.col]
} }
fn _bounds(val: usize) -> (usize, usize) { fn _bounds(val: usize) -> std::ops::Range<usize> {
let mut start = val / (SUB_REGION_SIZE as usize); let mut start = val / (SUB_REGION_SIZE as usize);
start *= SUB_REGION_SIZE; start *= SUB_REGION_SIZE;
(start, start + SUB_REGION_SIZE) std::ops::Range {start, end: start + SUB_REGION_SIZE}
} }
fn row_bounds(&self) -> (usize, usize) { fn row_bounds(&self) -> std::ops::Range<usize> {
Self::_bounds(self.row) Self::_bounds(self.row)
} }
fn col_bounds(&self) -> (usize, usize) { fn col_bounds(&self) -> std::ops::Range<usize> {
Self::_bounds(self.col) Self::_bounds(self.col)
} }
} }
@@ -133,14 +139,14 @@ mod test {
#[test] #[test]
fn bounds_are_correct() { fn bounds_are_correct() {
let origin = Point{ row: 0, col: 0}; let origin = Point{ row: 0, col: 0};
assert_eq!(origin.row_bounds(), (0, 3)); assert_eq!(origin.row_bounds(), 0..3);
assert_eq!(origin.col_bounds(), (0, 3)); assert_eq!(origin.col_bounds(), 0..3);
let one = Point{ row: 1, col: 1}; let one = Point{ row: 1, col: 1};
assert_eq!(one.row_bounds(), (0, 3)); assert_eq!(one.row_bounds(), 0..3);
assert_eq!(one.col_bounds(), (0, 3)); assert_eq!(one.col_bounds(), 0..3);
let five = Point{ row: 5, col: 7}; let five = Point{ row: 5, col: 7};
assert_eq!(five.row_bounds(), (3, 6)); assert_eq!(five.row_bounds(), 3..6);
assert_eq!(five.col_bounds(), (6, 9)); assert_eq!(five.col_bounds(), 6..9);
} }
#[test] #[test]
+6
View File
@@ -17,9 +17,15 @@ pub fn widget(cell: &Cell) -> iced::Element<Message> {
button(text(display)) button(text(display))
.style(|theme: &Theme, status| { .style(|theme: &Theme, status| {
// Get the current theme's palette
let palette = theme.palette(); let palette = theme.palette();
// Create a default border, since we need to set off the cells
// TODO: Figure out how to get this out of the theme?
let mut border = Border::default().color(LIGHT_GRAY); let mut border = Border::default().color(LIGHT_GRAY);
border.width = 1.0; border.width = 1.0;
// Return a style based on the status
match status { match status {
button::Status::Active => { button::Status::Active => {
button::Style{ button::Style{