Browse Source

Call finalize less often. Speed is back.

Steve Thielemann 1 week ago
parent
commit
710bcec944
1 changed files with 11 additions and 25 deletions
  1. 11 25
      sudoku/src/sudoku.rs

+ 11 - 25
sudoku/src/sudoku.rs

@@ -1046,10 +1046,9 @@ impl AnySolver {
     /// Process a move
     /// - Remove value from rows, columns, and cells.
     /// - Clear possibles from (x,y) position, it's filled.
-    /// - Added finalize parameter
     ///   - Use it when doing a single move.
     ///   - Don't do it when setting up the board.  Call finalize_move then.
-    pub fn process_move(&mut self, x: u8, y: u8, value: u8, finalize: bool) {
+    pub fn process_move(&mut self, x: u8, y: u8, value: u8) {
         debug_assert!(
             x <= self.board.width && y <= self.board.width,
             "Expected ({}, {}) <= {}",
@@ -1082,19 +1081,6 @@ impl AnySolver {
         let idx = self.possible.pos(x, y);
         self.possible.possible[idx] = Flags::new(self.board.width as usize);
         self.board.board[idx] = value;
-
-        // Tests are run as debug, and this kills things...
-        // Like the invalid board in validated_board test.
-        /* 
-        let cell_index = self.group.which_cell(x, y);
-
-        self.cell_poss[cell_index as usize].set(value as usize - 1, false);
-        */
-
-        if finalize {
-            self.finalize_possible();
-            // self.finalize_cell(cell_index);
-        }
     }
 
     /*
@@ -1931,7 +1917,7 @@ impl AnySolver {
                         // self.board.display();
                         return false;
                     }
-                    self.process_move(x, y, value, false);
+                    self.process_move(x, y, value);
                 } else {
                     // Below, we check to see if blanks have possible values.
 
@@ -1985,7 +1971,7 @@ impl AnySolver {
             for x in 0..self.board.width {
                 let value = self.board.get(x, y);
                 if value != 0 {
-                    self.process_move(x, y, value, false);
+                    self.process_move(x, y, value);
                 }
             }
         }
@@ -1997,8 +1983,7 @@ impl AnySolver {
     /// - This updates the board.
     /// - This updates all the possibles (row,column,cell).
     /// - Clears the possible for (x,y) [See process_move].
-    /// TO FIX:  I think set also needs finalize ... to pass to process_move.
-    pub fn set(&mut self, x: u8, y: u8, value: u8, finalize: bool) {
+    pub fn set(&mut self, x: u8, y: u8, value: u8) {
         debug_assert!(
             x < self.board.width && y < self.board.width,
             "Expected ({}, {}) < {}",
@@ -2015,7 +2000,7 @@ impl AnySolver {
 
         self.board.set(x, y, value);
         if value != 0 {
-            self.process_move(x, y, value, finalize);
+            self.process_move(x, y, value);
         }
     }
 
@@ -2070,7 +2055,7 @@ impl AnySolver {
                 for value in available.into_iter() {
                     assert!(value != 0);
 
-                    self.set(x, y, value, true);
+                    self.set(x, y, value);
                     // self.board.display();
                     // self.possible.display();
 
@@ -2125,7 +2110,7 @@ impl AnySolver {
             value = self.get(x, y);
         }
 
-        self.set(x, y, 0, true);
+        self.set(x, y, 0);
 
         // clone, and solve by logic.
         let mut puzcopy = self.clone();
@@ -2138,7 +2123,7 @@ impl AnySolver {
 
         // Not solvable, restore and return false.
         // Since this failed, don't bother to finalize.
-        self.set(x, y, value, false);
+        self.set(x, y, value);
         return false;
     }
 
@@ -2168,7 +2153,7 @@ impl AnySolver {
                     let value = self.possible.possible[i].iter().next().unwrap() + 1;
                     let pos = self.board.xy(i);
                     // println!("SET {}({},{})={}", i, pos.0 + 1, pos.1 + 1, value);
-                    self.set(pos.0, pos.1, value as u8, true);
+                    self.set(pos.0, pos.1, value as u8);
                     found_something = true;
                 }
             }
@@ -2179,6 +2164,7 @@ impl AnySolver {
                 // - Record that we did something.
                 pass1 = true;
                 pass1_again = true;
+                self.finalize_possible();
             }
         }
         pass1
@@ -2238,7 +2224,7 @@ impl AnySolver {
                         let xy = this.board.xy(pos);
                         // this.possible.display();
                         // this.board.display();
-                        this.set(xy.0, xy.1, (v + 1) as u8, true);
+                        this.set(xy.0, xy.1, (v + 1) as u8);
                         // println!("SET {} ({},{}) = {}", pos, xy.0 + 1, xy.1 + 1, v+1);
 
                         found_something = true;