소스 검색

Flip uses swap.

Steve Thielemann 1 주 전
부모
커밋
79023a30f0
1개의 변경된 파일22개의 추가작업 그리고 20개의 파일을 삭제
  1. 22 20
      sudoku/src/sudoku.rs

+ 22 - 20
sudoku/src/sudoku.rs

@@ -512,13 +512,14 @@ impl AnyBoard {
         false
     }
 
-    // Mess with your brain:
     // 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
     ///
-    /// 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 {
         self.width - 1 - index
     }
@@ -549,7 +550,7 @@ impl AnyBoard {
         // (x1,y1) = lower case
         // (x2,y2) = upper case
 
-        for idx in 0..self.width-1 {
+        for idx in 0..self.width - 1 {
             // println!("idx: {idx}");
             for pass in 0..2 {
                 x1 = idx;
@@ -573,9 +574,9 @@ impl AnyBoard {
                 // println!("Idx {idx} Pass {pass} Starting ({x1},{y1}), ({x2},{y2})");
                 for _swap in 0..self.width {
                     // 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;
                     }
                     x1 += 1;
@@ -589,7 +590,7 @@ impl AnyBoard {
                 }
             }
         }
-        
+
         /*
         // The memory allocation way of swapping.
         let temp = self.clone();
@@ -625,6 +626,9 @@ impl AnyBoard {
     ///
     /// Which is same as flip + flip_x.
     pub fn rotate_cw(&mut self) {
+        self.flip();
+        self.flip_x();
+        /*
         let temp = self.clone();
         let width = self.width;
         for x in 0..self.width {
@@ -632,12 +636,16 @@ impl AnyBoard {
                 self[(width - 1 - x, y)] = temp[(y, x)]
             }
         }
+        */
     }
 
     /// Rotate board Counter-Clockwise
     ///
     /// Which is flip + flip_y.
     pub fn rotate_ccw(&mut self) {
+        self.flip();
+        self.flip_y();
+        /*
         let temp = self.clone();
         let width = self.width;
         for x in 0..self.width {
@@ -645,18 +653,10 @@ impl AnyBoard {
                 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.
 #[derive(Debug, Clone)]
 pub struct AnyPossible {
@@ -1087,7 +1087,9 @@ impl AnySolver {
 
         const OUTPUT: bool = false;
 
+        if OUTPUT {
         println!("finalize_possible starts");
+        };
 
         // I might want to save the pair information.
 
@@ -1106,13 +1108,11 @@ impl AnySolver {
         }
         updated_here = false;
 
-        /// Do we output things from this function? (Debug, Development infomation)
-        const NOISY: bool = true; // false;
         if OUTPUT {
             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];
@@ -1365,7 +1365,9 @@ impl AnySolver {
             }
         }
 
+        if OUTPUT {
         println!("finalize_possible ends...");
+        }
         return updated;
 
         /*