nonogram_rs/
layout.rs

1use crate::{Solution, Token};
2
3use crate::algo::collection::Collection;
4use crate::algo::Branch;
5
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8
9/// An item in a number grid.
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11#[derive(Default, Clone)]
12pub struct Item<T> {
13    pub color: T,
14    pub len: usize,
15}
16
17impl<T> Item<T> {
18    /// Creates a new item.
19    pub fn new(color: T, len: usize) -> Self {
20        Self { color, len }
21    }
22}
23
24/// A layout composed of two number grids.
25#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
26#[derive(Default, Clone)]
27pub struct Layout<T> {
28    pub cols: Vec<Vec<Item<T>>>,
29    pub rows: Vec<Vec<Item<T>>>,
30}
31
32impl<T: Copy + PartialEq + Send + Sync> Layout<T> {
33    /// Creates a new layout.
34    pub fn new(cols: Vec<Vec<Item<T>>>, rows: Vec<Vec<Item<T>>>) -> Self {
35        Self { cols, rows }
36    }
37
38    /// Tries to solve a layout.
39    ///
40    /// # Parameters
41    /// * `limit`: The maximum amount of nonograms to include in the solution.
42    /// * `token`: Some cancellation token.
43    pub fn solve(self, limit: usize, token: impl Token) -> Solution<T> {
44        let mut collection = Collection::new(limit, token);
45
46        Branch::build(self.cols, self.rows).solve(&mut collection);
47
48        collection.into()
49    }
50}
51
52#[cfg(test)]
53mod test {
54    use super::*;
55
56    #[test]
57    fn layout_solve() {
58        let cols = vec![vec![Item::new('a', 1)]];
59        let rows = vec![vec![Item::new('a', 1)]];
60        let layout = Layout::new(cols, rows);
61
62        assert_eq!(1, layout.solve(usize::MAX, ()).collection.len());
63    }
64}