summaryrefslogtreecommitdiff
path: root/src/solvers.rs
diff options
context:
space:
mode:
authorDennis Kobert <dennis@kobert.dev>2019-12-20 09:30:19 +0100
committerDennis Kobert <dennis@kobert.dev>2019-12-20 09:30:19 +0100
commit7dcf1fe29e7983409027a69f0fa046f76874859b (patch)
tree16f46a23ec9845a10403898869d221754542f327 /src/solvers.rs
parent9b6de93eb16841f2b974398b7773abfa6b5c07e8 (diff)
Introduce helper functions and factor out state struct
Diffstat (limited to 'src/solvers.rs')
-rw-r--r--src/solvers.rs48
1 files changed, 36 insertions, 12 deletions
diff --git a/src/solvers.rs b/src/solvers.rs
index 52ad4c1..6df4b4a 100644
--- a/src/solvers.rs
+++ b/src/solvers.rs
@@ -62,7 +62,40 @@ pub struct Solver<T: num::PrimInt> {
/// width
pub w: u32,
/// Use to store already used blocks as a bitmask
- solve_stack: Vec<T>,
+ solve_stack: Vec<SaveState<T>>,
+}
+
+#[derive(Clone)]
+pub struct SaveState<T: num::PrimInt> {
+ pub bitmask: T,
+ pub index: u32,
+}
+
+impl<T: num::PrimInt> SaveState<T> {
+ pub fn new() -> Self {
+ Self {
+ bitmask: T::zero(),
+ index: 0,
+ }
+ }
+
+ pub fn set_bit(&mut self, stone: u32) {
+ self.bitmask =
+ self.bitmask | T::from(1 << stone).expect("Stone placing index out of bounds");
+ }
+
+ pub fn get_bit(&self, stone: u32) -> T {
+ self.bitmask & T::from(1 << stone).expect("Requested stone index out of bounds")
+ }
+
+ pub fn bridges_gap(&self, index: u32) -> bool {
+ self.get_bit(index - self.index) == T::zero()
+ }
+
+ pub fn bridge_gap(&mut self, index: u32) {
+ self.set_bit(index - self.index);
+ self.index = index
+ }
}
impl<T: num::PrimInt> Solver<T> {
@@ -73,7 +106,7 @@ impl<T: num::PrimInt> Solver<T> {
n: (n as u32),
h: (h as u32),
w: (w as u32),
- solve_stack: vec![T::zero(); h],
+ solve_stack: vec![SaveState::new(); h],
}
}
@@ -83,16 +116,7 @@ impl<T: num::PrimInt> Solver<T> {
.iter()
.take(2)
.zip(0..1)
- .for_each(|(x, i)| self.set_stone(*x as usize, i));
+ .for_each(|(x, i)| self.solve_stack[*x as usize].set_bit(i));
wall
}
-
- fn set_stone(&mut self, row: usize, stone: u32) {
- self.solve_stack[row] =
- self.solve_stack[row] | T::from(1 << stone).expect("Stone placing index out of bounds");
- }
-
- fn get_stone(&self, row: usize, stone: u32) {
- self.solve_stack[row] & T::from(1 << stone).expect("Requested stone index out of bounds");
- }
}