summaryrefslogtreecommitdiff
path: root/src/solvers/gpusolver.rs
blob: 2b9eb4add69d070c91bf7d7e8053eaa4db4862d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::solver::{wall_stats, Solver, IteratorSolver};
use crate::structs::StoneWall;
use crate::permutations::PermutationGenerator;

#[derive(Debug)]
pub struct GpuSolver {
    n: u32, h: u32, w: u32,
    permutations: Vec<Vec<u32>>,
    masks: Vec<u64>,
}

impl GpuSolver {
    fn solve_to_vec(&mut self) -> Vec<StoneWall> {
        vec![]
    }
}

fn generate_permutations(n: u32) -> Vec<Vec<u32>> {
    crate::permutations::HeapsPermutations::permutations(n)
}

fn generate_masks(permutations: &[Vec<u32>]) -> Vec<u64> {
    let mut masks = Vec::with_capacity(permutations.len());
    for p in permutations {
        let mut v = 0;
        let mut x = 0u64;
        for i in p.iter().take(p.len() - 1).map(|i| {
                    v += i;
                    v
                }) {
            x |= 1 << i
        }
        masks.push(x)
    }
    masks
}

impl Solver for GpuSolver {
    fn new(n: u32) -> Self {
        let (h, w) = wall_stats(n);
        let permutations = generate_permutations(n);
        let masks = generate_masks(&permutations);
        Self {
            n, h, w,
            permutations,
            masks,
        }
    }
    fn n(&self) -> u32 {
        self.n
    }
    fn h(&self) -> u32 {
        self.h
    }
    fn w(&self) -> u32 {
        self.w
    }
}

impl IteratorSolver for GpuSolver {
    type IntoIter = std::vec::IntoIter<StoneWall>;
    fn solve(mut self) -> Self::IntoIter {
        self.solve_to_vec().into_iter()
    }
}