Browse Source

Flip uses swap.

Steve Thielemann 1 week ago
parent
commit
79023a30f0
1 changed files with 22 additions and 20 deletions
  1. 22 20
      sudoku/src/sudoku.rs

+ 22 - 20
sudoku/src/sudoku.rs

@@ -512,13 +512,14 @@ impl AnyBoard {
         false
         false
     }
     }
 
 
-    // Mess with your brain:
     // The board manipulations below should be implemented without cloning the board.
     // The board manipulations below should be implemented without cloning the board.
-    // We should be able to just swap values.  FUTURE: TODO: no allocations.
+    // We should be able to just swap values.  Done.
 
 
     /// Invert the index
     /// Invert the index
     ///
     ///
-    /// Convert 0..width to width..=0
+    /// Convert 0..width to (width-1)..=0
+    /// With width=9 it is 0 to 8 inverted becomes 8 to 0.
+    #[inline]
     pub fn invert(&self, index: u8) -> u8 {
     pub fn invert(&self, index: u8) -> u8 {
         self.width - 1 - index
         self.width - 1 - index
     }
     }
@@ -549,7 +550,7 @@ impl AnyBoard {
         // (x1,y1) = lower case
         // (x1,y1) = lower case
         // (x2,y2) = upper case
         // (x2,y2) = upper case
 
 
-        for idx in 0..self.width-1 {
+        for idx in 0..self.width - 1 {
             // println!("idx: {idx}");
             // println!("idx: {idx}");
             for pass in 0..2 {
             for pass in 0..2 {
                 x1 = idx;
                 x1 = idx;
@@ -573,9 +574,9 @@ impl AnyBoard {
                 // println!("Idx {idx} Pass {pass} Starting ({x1},{y1}), ({x2},{y2})");
                 // println!("Idx {idx} Pass {pass} Starting ({x1},{y1}), ({x2},{y2})");
                 for _swap in 0..self.width {
                 for _swap in 0..self.width {
                     // println!("Swap ({x1},{y1}) <=> ({x2},{y2})");
                     // println!("Swap ({x1},{y1}) <=> ({x2},{y2})");
-                    (self[(x1,y1)], self[(x2,y2)]) = (self[(x2,y2)], self[(x1,y1)]);
+                    (self[(x1, y1)], self[(x2, y2)]) = (self[(x2, y2)], self[(x1, y1)]);
 
 
-                    if (y1 == 0) || (x2 == 0)  {
+                    if (y1 == 0) || (x2 == 0) {
                         break;
                         break;
                     }
                     }
                     x1 += 1;
                     x1 += 1;
@@ -589,7 +590,7 @@ impl AnyBoard {
                 }
                 }
             }
             }
         }
         }
-        
+
         /*
         /*
         // The memory allocation way of swapping.
         // The memory allocation way of swapping.
         let temp = self.clone();
         let temp = self.clone();
@@ -625,6 +626,9 @@ impl AnyBoard {
     ///
     ///
     /// Which is same as flip + flip_x.
     /// Which is same as flip + flip_x.
     pub fn rotate_cw(&mut self) {
     pub fn rotate_cw(&mut self) {
+        self.flip();
+        self.flip_x();
+        /*
         let temp = self.clone();
         let temp = self.clone();
         let width = self.width;
         let width = self.width;
         for x in 0..self.width {
         for x in 0..self.width {
@@ -632,12 +636,16 @@ impl AnyBoard {
                 self[(width - 1 - x, y)] = temp[(y, x)]
                 self[(width - 1 - x, y)] = temp[(y, x)]
             }
             }
         }
         }
+        */
     }
     }
 
 
     /// Rotate board Counter-Clockwise
     /// Rotate board Counter-Clockwise
     ///
     ///
     /// Which is flip + flip_y.
     /// Which is flip + flip_y.
     pub fn rotate_ccw(&mut self) {
     pub fn rotate_ccw(&mut self) {
+        self.flip();
+        self.flip_y();
+        /*
         let temp = self.clone();
         let temp = self.clone();
         let width = self.width;
         let width = self.width;
         for x in 0..self.width {
         for x in 0..self.width {
@@ -645,18 +653,10 @@ impl AnyBoard {
                 self[(x, width - 1 - y)] = temp[(y, x)]
                 self[(x, width - 1 - y)] = temp[(y, x)]
             }
             }
         }
         }
+        */
     }
     }
 }
 }
 
 
-//
-// Ok! This is interesting:
-//
-// This allows you to index the board by (u8,u8) or usize.
-//
-
-// Need to use u32, so 5*5=25, 25 bits can be accessed.
-// u16 is fine for 3*3=9.
-
 /// AnyPossible - Track what values can possibly go in what positions.
 /// AnyPossible - Track what values can possibly go in what positions.
 #[derive(Debug, Clone)]
 #[derive(Debug, Clone)]
 pub struct AnyPossible {
 pub struct AnyPossible {
@@ -1087,7 +1087,9 @@ impl AnySolver {
 
 
         const OUTPUT: bool = false;
         const OUTPUT: bool = false;
 
 
+        if OUTPUT {
         println!("finalize_possible starts");
         println!("finalize_possible starts");
+        };
 
 
         // I might want to save the pair information.
         // I might want to save the pair information.
 
 
@@ -1106,13 +1108,11 @@ impl AnySolver {
         }
         }
         updated_here = false;
         updated_here = false;
 
 
-        /// Do we output things from this function? (Debug, Development infomation)
-        const NOISY: bool = true; // false;
         if OUTPUT {
         if OUTPUT {
             self.possible.display();
             self.possible.display();
         }
         }
-        let mut cell_has_value = Flags::new(self.board.max_index);
-        // vec![false; self.board.max_index];
+
+        // let mut cell_has_value = Flags::new(self.board.max_index);
 
 
         /*
         /*
         let mut cell_update = vec![false; width as usize];
         let mut cell_update = vec![false; width as usize];
@@ -1365,7 +1365,9 @@ impl AnySolver {
             }
             }
         }
         }
 
 
+        if OUTPUT {
         println!("finalize_possible ends...");
         println!("finalize_possible ends...");
+        }
         return updated;
         return updated;
 
 
         /*
         /*