Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Cache board color-coordinate lookups to avoid repeated full-board scans.
- Make `--file-type=auto` use recognized image extensions to select image parsing.

### Fixed
Expand Down
16 changes: 9 additions & 7 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use anyhow::{Result, ensure};
use itertools::{Itertools, Position, iproduct};

use crate::{
datastructure::{Coord, CoordSet},
datastructure::{Coord, CoordSet, MAX_SET_SIZE},
squarecolor::SquareColor,
};

Expand Down Expand Up @@ -69,6 +69,7 @@ pub struct Board {
size: usize,
colors: Vec<SquareColor>,
coords: CoordSet,
color_coords: [CoordSet; MAX_SET_SIZE],
queen_borders: Vec<CoordSet>,
}

Expand All @@ -92,10 +93,15 @@ impl Board {
"Colors must be equal to size*size"
);
let coords = iproduct!(0..size, 0..size).collect::<CoordSet>();
let mut color_coords = [CoordSet::default(); MAX_SET_SIZE];
for (idx, color) in colors.iter().enumerate() {
color_coords[*color as usize].add((idx / size, idx % size));
}
let mut board = Board {
size,
colors,
coords,
color_coords,
queen_borders: vec![],
};
board.compute_queen_borders();
Expand Down Expand Up @@ -210,10 +216,7 @@ impl Board {
/// # }
/// ```
pub fn coords_for_color(&self, color: &SquareColor) -> CoordSet {
self.all_coords()
.iter()
.filter(|&coord| self.color(&coord) == *color)
.collect()
self.color_coords[*color as usize]
}

/// Returns a list of all [Coord]s in the grid.
Expand Down Expand Up @@ -302,9 +305,8 @@ impl Board {
.filter(|coord| coord != queen),
);
hs.extend(
self.all_coords()
self.coords_for_color(&self.color(queen))
.iter()
.filter(|coord| self.color(coord) == self.color(queen))
.filter(|coord| coord != queen),
);
if queen.0 > 0 && queen.1 > 0 {
Expand Down
Loading