]> git.lizzy.rs Git - rust.git/blobdiff - src/test/bench/sudoku.rs
test: Make manual changes to deal with the fallout from removal of
[rust.git] / src / test / bench / sudoku.rs
index 79eee4006ceed3c80b0ff170cfb81b2eb556aab7..d41a19c60a169ab52f11a5eecbe8a61ceba4c905 100644 (file)
@@ -17,7 +17,6 @@
 use std::io::BufferedReader;
 use std::os;
 use std::intrinsics::cttz16;
-use std::slice;
 
 // Computes a single solution to a given 9x9 sudoku
 //
@@ -48,8 +47,8 @@ pub fn new(g: grid) -> Sudoku {
     }
 
     pub fn from_vec(vec: &[[u8, ..9], ..9]) -> Sudoku {
-        let g = slice::from_fn(9u, |i| {
-            slice::from_fn(9u, |j| { vec[i][j] })
+        let g = Vec::from_fn(9u, |i| {
+            Vec::from_fn(9u, |j| { vec[i][j] })
         });
         return Sudoku::new(g)
     }
@@ -57,7 +56,8 @@ pub fn from_vec(vec: &[[u8, ..9], ..9]) -> Sudoku {
     pub fn equal(&self, other: &Sudoku) -> bool {
         for row in range(0u8, 9u8) {
             for col in range(0u8, 9u8) {
-                if self.grid[row][col] != other.grid[row][col] {
+                if *self.grid.get(row as uint).get(col as uint) !=
+                        *other.grid.get(row as uint).get(col as uint) {
                     return false;
                 }
             }
@@ -68,14 +68,15 @@ pub fn equal(&self, other: &Sudoku) -> bool {
     pub fn read(mut reader: BufferedReader<StdReader>) -> Sudoku {
         assert!(reader.read_line().unwrap() == ~"9,9"); /* assert first line is exactly "9,9" */
 
-        let mut g = slice::from_fn(10u, { |_i| vec!(0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8) });
+        let mut g = Vec::from_fn(10u, { |_i| vec!(0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8) });
         for line in reader.lines() {
             let comps: Vec<&str> = line.trim().split(',').collect();
 
             if comps.len() == 3u {
-                let row     = from_str::<uint>(comps[0]).unwrap() as u8;
-                let col     = from_str::<uint>(comps[1]).unwrap() as u8;
-                g[row][col] = from_str::<uint>(comps[2]).unwrap() as u8;
+                let row     = from_str::<uint>(*comps.get(0)).unwrap() as u8;
+                let col     = from_str::<uint>(*comps.get(1)).unwrap() as u8;
+                *g.get_mut(row as uint).get_mut(col as uint) =
+                    from_str::<uint>(*comps.get(2)).unwrap() as u8;
             }
             else {
                 fail!("Invalid sudoku file");
@@ -86,9 +87,11 @@ pub fn read(mut reader: BufferedReader<StdReader>) -> Sudoku {
 
     pub fn write(&self, writer: &mut io::Writer) {
         for row in range(0u8, 9u8) {
-            write!(writer, "{}", self.grid[row][0]);
+            write!(writer, "{}", *self.grid.get(row as uint).get(0));
             for col in range(1u8, 9u8) {
-                write!(writer, " {}", self.grid[row][col]);
+                write!(writer, " {}", *self.grid
+                                           .get(row as uint)
+                                           .get(col as uint));
             }
             write!(writer, "\n");
          }
@@ -99,7 +102,7 @@ pub fn solve(&mut self) {
         let mut work: Vec<(u8, u8)> = Vec::new(); /* queue of uncolored fields */
         for row in range(0u8, 9u8) {
             for col in range(0u8, 9u8) {
-                let color = self.grid[row][col];
+                let color = *self.grid.get(row as uint).get(col as uint);
                 if color == 0u8 {
                     work.push((row, col));
                 }
@@ -109,9 +112,11 @@ pub fn solve(&mut self) {
         let mut ptr = 0u;
         let end = work.len();
         while ptr < end {
-            let (row, col) = work[ptr];
+            let (row, col) = *work.get(ptr);
             // is there another color to try?
-            if self.next_color(row, col, self.grid[row][col] + (1 as u8)) {
+            let the_color = *self.grid.get(row as uint).get(col as uint) +
+                                (1 as u8);
+            if self.next_color(row, col, the_color) {
                 //  yes: advance work list
                 ptr = ptr + 1u;
             } else {
@@ -132,18 +137,22 @@ fn next_color(&mut self, row: u8, col: u8, start_color: u8) -> bool {
 
             // find first remaining color that is available
             let next = avail.next();
-            self.grid[row][col] = next;
+            *self.grid.get_mut(row as uint).get_mut(col as uint) = next;
             return 0u8 != next;
         }
-        self.grid[row][col] = 0u8;
+        *self.grid.get_mut(row as uint).get_mut(col as uint) = 0u8;
         return false;
     }
 
     // find colors available in neighbourhood of (row, col)
     fn drop_colors(&mut self, avail: &mut Colors, row: u8, col: u8) {
         for idx in range(0u8, 9u8) {
-            avail.remove(self.grid[idx][col]); /* check same column fields */
-            avail.remove(self.grid[row][idx]); /* check same row fields */
+            avail.remove(*self.grid
+                              .get(idx as uint)
+                              .get(col as uint)); /* check same column fields */
+            avail.remove(*self.grid
+                              .get(row as uint)
+                              .get(idx as uint)); /* check same row fields */
         }
 
         // check same block fields
@@ -151,7 +160,9 @@ fn drop_colors(&mut self, avail: &mut Colors, row: u8, col: u8) {
         let col0 = (col / 3u8) * 3u8;
         for alt_row in range(row0, row0 + 3u8) {
             for alt_col in range(col0, col0 + 3u8) {
-                avail.remove(self.grid[alt_row][alt_col]);
+                avail.remove(*self.grid
+                                  .get(alt_row as uint)
+                                  .get(alt_col as uint));
             }
         }
     }