Jelajahi Sumber

Mostly working, but finalize is slow.

and call too often.
Steve Thielemann 1 bulan lalu
induk
melakukan
986907072e
3 mengubah file dengan 816 tambahan dan 125 penghapusan
  1. 3 47
      sudoku/src/bits.rs
  2. 7 0
      sudoku/src/group.rs
  3. 806 78
      sudoku/src/sudoku.rs

+ 3 - 47
sudoku/src/bits.rs

@@ -1,49 +1,3 @@
-/*
-Or MAYBE, just use Vec<bool> ?!
-While that would use more memory, it would certainly be faster!
-Is saving a few bytes (these days) worth it, really?
-
-The iterator would ... already be written for Vec<T>. :P
-(Buuut, not quite. I only want true values...)
-
-fn main() {
-    let vector = vec![true, false, false, true];
-
-    for z in vector.iter().enumerate().filter(|x| *x.1) {
-        println!("{:?}", z);
-    }
-    for z in vector.iter().enumerate().filter(|x| *x.1) {
-        println!("{:?}", z);
-    }
-}
-
-This returns (0, true), (3, true)
-
-fn main() {
-    let vector = vec![true, false, false, true];
-
-    let d : Vec<usize> = vector.iter().enumerate().filter(|x| *x.1).map(|x| x.0).collect();
-    println!("{:?}", &d);
-}
-
-This returns (0,3) !  This is what I was looking for!
-I'd rather have an iterator (like I have below), but I might
-have to compromise here.
-
-    let d = vector.iter().enumerate().filter(|x| *x.1).map(|x| x.0);
-    for dv in d {
-        println!(" {}", dv);
-    }
-
-There we go, map returns iterator. :D
-Enumerate creates (index, value),
-filter finds "true" items,
-map returns iterator (with index)!
-
-And we could handle any crazy size as well... :O
-
- */
-
 use std::ops::RangeBounds;
 use std::fmt;
 // use std::iter::{Map, Filter, Enumerate};
@@ -94,17 +48,19 @@ impl GenBits {
         self.0.iter().enumerate().filter(|x| *x.1).map(|x| x.0)
     }
 
-    /// The "default" iterator for GenBits returns this.
+    /// Iterate over all of the indexes that are set true.
     pub fn iter(&self) -> impl Iterator<Item = usize> + '_ {
         self.0.iter().enumerate().filter(|x| *x.1).map(|x| x.0)
     }
 
+    // Nope.  This isn't how the original iterator for GenBits worked.
     /*
     pub fn iter(&self) -> std::slice::Iter<'_, bool> {
         self.0.iter()
     }
     */
 
+    #[must_use]
     /// Display bits that are set.
     /// +1 is added to the display, so it matches the cell values (1..N)
     pub fn display(&self) -> String {

+ 7 - 0
sudoku/src/group.rs

@@ -51,6 +51,7 @@ impl AnyGroup {
     /// Called only by new().
     fn calculate(&mut self) {
         let size = self.size;
+        
         for y in 0..self.width {
             self.row_cell[y as usize] = y as usize / size as usize;
             self.col_cell[y as usize] = (y as usize/ size as usize) * size as usize;
@@ -132,6 +133,12 @@ impl AnyGroup {
         (x / self.size) + (y / self.size) * self.size
     }
   
+    /// Which cell contain this index?
+    pub fn which_cell_idx(&self, index:usize) -> u8 {
+        let (x,y) = self.xy(index);
+        self.which_cell(x,y)
+    }
+
     #[must_use]
     /// Which index for given cell and (x,y)?
     pub fn which_cell_index(&self, cell_index:u8, x:u8, y:u8) -> usize {

File diff ditekan karena terlalu besar
+ 806 - 78
sudoku/src/sudoku.rs


Beberapa file tidak ditampilkan karena terlalu banyak file yang berubah dalam diff ini