summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authornatrixaeria <upezu@student.kit.edu>2020-01-05 21:47:19 +0100
committernatrixaeria <upezu@student.kit.edu>2020-01-05 21:47:19 +0100
commit85627bc39db56d1ca3dca747535afcf6fd9cdcdd (patch)
tree80dcd562180573d0a81456ca6cc49ec6faa09d88 /src
parent920a6729577d14ba9190abcb3a2c4087652228a4 (diff)
Create GpuSolver
Diffstat (limited to 'src')
-rwxr-xr-xsrc/main.rs1
-rw-r--r--src/permutations.rs45
-rwxr-xr-xsrc/solver.rs12
-rw-r--r--src/solvers/gpusolver.rs41
-rwxr-xr-xsrc/solvers/intuitive.rs2
-rwxr-xr-xsrc/solvers/mod.rs1
6 files changed, 98 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index 30acc60..a59f553 100755
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,4 @@
+mod permutations;
mod solver;
mod solvers;
mod structs;
diff --git a/src/permutations.rs b/src/permutations.rs
new file mode 100644
index 0000000..7b8e4d3
--- /dev/null
+++ b/src/permutations.rs
@@ -0,0 +1,45 @@
+use permutohedron::{Heap, LexicalPermutation};
+
+pub trait PermutationGenerator {
+ fn permutations(n: u32) -> Vec<Vec<u32>>;
+}
+
+pub struct HeapsPermutations;
+
+impl PermutationGenerator for HeapsPermutations {
+ fn permutations(n: u32) -> Vec<Vec<u32>> {
+ Heap::<'_, Vec<u32>, _>::new(&mut (1..=n).collect()).collect()
+ }
+}
+
+pub struct LexicalPermutations {
+ state: Vec<u32>,
+ is_ended: bool,
+}
+
+impl Iterator for LexicalPermutations {
+ type Item = Vec<u32>;
+ fn next(&mut self) -> Option<Self::Item> {
+ if self.is_ended {
+ None
+ } else {
+ self.is_ended = self.state.next_permutation();
+ Some(self.state.clone())
+ }
+ }
+}
+
+impl LexicalPermutations {
+ fn new(n: u32) -> Self {
+ Self {
+ state: (1..=n).collect(),
+ is_ended: false
+ }
+ }
+}
+
+impl PermutationGenerator for LexicalPermutations {
+ fn permutations(n: u32) -> Vec<Vec<u32>> {
+ Self::new(n).collect()
+ }
+}
diff --git a/src/solver.rs b/src/solver.rs
index 809ddb4..db4e732 100755
--- a/src/solver.rs
+++ b/src/solver.rs
@@ -8,10 +8,16 @@ pub fn wall_stats(n: u32) -> (u32, u32) {
pub trait Solver {
fn new(n: u32) -> Self;
- fn solve(&mut self) -> StoneWall;
fn n(&self) -> u32;
-
fn h(&self) -> u32;
-
fn w(&self) -> u32;
}
+
+pub trait FirstSolver {
+ fn solve(self) -> StoneWall;
+}
+
+pub trait IteratorSolver: Solver {
+ type IntoIter: Iterator<Item=StoneWall>;
+ fn solve(self) -> Self::IntoIter;
+}
diff --git a/src/solvers/gpusolver.rs b/src/solvers/gpusolver.rs
new file mode 100644
index 0000000..4c58251
--- /dev/null
+++ b/src/solvers/gpusolver.rs
@@ -0,0 +1,41 @@
+use crate::solver::{wall_stats, Solver, IteratorSolver};
+use crate::structs::StoneWall;
+
+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![]
+ }
+}
+
+impl Solver for GpuSolver {
+ fn new(n: u32) -> Self {
+ let (h, w) = wall_stats(n);
+ Self {
+ n, h, w,
+ permutations: vec![],
+ masks: vec![]
+ }
+ }
+ 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()
+ }
+}
diff --git a/src/solvers/intuitive.rs b/src/solvers/intuitive.rs
index 3caac73..b8d375a 100755
--- a/src/solvers/intuitive.rs
+++ b/src/solvers/intuitive.rs
@@ -22,7 +22,7 @@ impl NormalSolver {
pub fn new(n: u32) -> Self {
let h = n / 2 + 1;
let w = h * (n - 1);
- let mut heap = (1..=n).collect::<Vec<u32>>();
+ let mut heap: Vec<_> = (1..=n).collect();
let heap = permutohedron::Heap::new(&mut heap);
let n_f = permutohedron::factorial(n as usize);
let chunk = n_f as u32 / n;
diff --git a/src/solvers/mod.rs b/src/solvers/mod.rs
index 0615eab..6d26266 100755
--- a/src/solvers/mod.rs
+++ b/src/solvers/mod.rs
@@ -2,6 +2,7 @@
pub mod intuitive;
#[cfg(feature = "gpu")]
pub mod opencl;
+pub mod gpusolver;
lazy_static! {
pub static ref PERMUTATIONS: (Vec<Vec<u32>>, Vec<u64>) = {