Skip to content

Commit

Permalink
no improvement, will come back in the future to see what I can do, co…
Browse files Browse the repository at this point in the history
…mment, and clean up
  • Loading branch information
BAXYCode committed Apr 27, 2023
1 parent 2142432 commit f07ec45
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 53 deletions.
19 changes: 10 additions & 9 deletions src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Matrix {
let cell = self.allocate_column();

// add column in last position
self.horizontal.insert(self.horizontal[CERO].prev, cell);
unsafe{self.horizontal.insert(self.horizontal.links.get_unchecked(CERO.0).prev, cell);}

cell
}
Expand All @@ -76,7 +76,7 @@ impl Matrix {
self.sizes[col] += 1;
let new_cell = self.allocate_cell(col);
//fetch column and add new cell as cols new previous cell
self.vertical.insert(self.vertical[col].prev, new_cell);
unsafe{self.vertical.insert(self.vertical.links.get_unchecked(col.0).prev, new_cell);}
if let Some(prev) = prev {
self.horizontal.insert(prev, new_cell);
}
Expand All @@ -98,7 +98,7 @@ impl Matrix {
let mut curr = self.horizontal.cursor(c_axis_cell);
while let Some(r_axis_cell) = curr.next(&self.horizontal) {
self.vertical.remove(r_axis_cell);
self.sizes[self.access[r_axis_cell]] -= 1;
unsafe{self.sizes[*(self.access.get_unchecked(r_axis_cell.0))] -= 1;}
//self.total_covered+=1;
// println!("size for column {:?} reduced to : {}",self.access[r_axis_cell],self.sizes[self.access[r_axis_cell]]);
}
Expand All @@ -119,7 +119,7 @@ impl Matrix {
let mut r_axis_cell = self.horizontal.cursor(c_axis_cell);
while let Some(current_cell) = r_axis_cell.prev(&self.horizontal) {
//self.total_uncovered +=1;
self.sizes[self.access[current_cell]] += 1;
unsafe{self.sizes[*(self.access.get_unchecked(current_cell.0))] += 1;}
self.vertical.add_back(current_cell);
}
} //self.total_uncovered+=1;
Expand All @@ -129,7 +129,8 @@ impl Matrix {
pub(crate) fn uncover_all_row(&mut self, cell: Cell) {
let mut cursor = self.vertical.cursor(cell);
while let Some(cell) = cursor.prev(&self.horizontal) {
let header = self.access[cell];
let header:Cell;
unsafe{ header = *(self.access.get_unchecked(cell.0));}
self.uncover(header)
}
}
Expand All @@ -138,9 +139,9 @@ impl Matrix {
let mut cols_ind = Vec::new();

let mut curr = self.horizontal.cursor(cell);
cols_ind.push(self.access[cell].0);
unsafe{cols_ind.push(self.access.get_unchecked(cell.0).0);}
while let Some(current) = curr.next(&self.horizontal) {
cols_ind.push(self.access[current].0);
unsafe{ cols_ind.push(self.access.get_unchecked(current.0).0);}
}
cols_ind.sort();
cols_ind
Expand All @@ -151,8 +152,8 @@ impl Matrix {
let mut changed:bool = false;
let mut current_smallest_size = self.sizes.clone().into_iter().max().unwrap()+1;
while let Some(node) = cursor.next(&self.horizontal) {
if self.sizes[self.access[node]] < current_smallest_size {
current_smallest_size = self.sizes[self.access[node]];
if unsafe{self.sizes[*(self.access.get_unchecked(node.0))]} < current_smallest_size {
unsafe{current_smallest_size = self.sizes[*(self.access.get_unchecked(node.0))];}
smallest_column = node;
if current_smallest_size == 1 {
return smallest_column;
Expand Down
81 changes: 37 additions & 44 deletions src/sudoku_tester.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,65 @@
use crate::solver::Solver;
use crate::sudoku::Sudoku;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use std::thread::{JoinHandle, self};
use crate::solver::Solver;
use crate::sudoku::Sudoku;
pub(crate) struct SudokuTester{
pub response: Vec<String>
use std::thread::{self, JoinHandle};
pub(crate) struct SudokuTester {
pub response: Vec<String>,
}

impl SudokuTester {

pub fn get_sudoku(&self,sudokus: &str) -> Vec<String>{

let sudokus_vec: Vec<String> = {
let untreated = sudokus.split("\n");
let mut treated:Vec<String> = Vec::new();
for each in untreated{
treated.push(each.to_owned().trim_end().to_owned());
}
treated
};
sudokus_vec
}
fn treat_file(&mut self)-> String{
pub fn get_sudoku(&self, sudokus: &str) -> Vec<String> {
let sudokus_vec: Vec<String> = {
let untreated = sudokus.split("\n");
let mut treated: Vec<String> = Vec::new();
for each in untreated {
treated.push(each.to_owned().trim_end().to_owned());
}
treated
};
sudokus_vec
}
fn treat_file(&mut self) -> String {
let path = Path::new("data.txt");
let mut file = File::open(path).unwrap();
let mut bufreader = String::new();
let mut bufreader = String::new();
let ok = file.read_to_string(&mut bufreader);
if let Ok(_ok) = ok{
return bufreader;}
if let Ok(_ok) = ok {
return bufreader;
}
"None".to_owned()
}
}

pub(crate)fn test_performance(&mut self){
pub(crate) fn test_performance(&mut self) {
let mut results: Vec<String> = Vec::new();
let treated = self.treat_file();
let unsolved = self.get_sudoku(&treated);
let mut handles: Vec<JoinHandle<_>> = Vec::with_capacity(1000);
for (i ,sud) in unsolved.into_iter().enumerate(){
let mut sudoku = Sudoku::new(sud,3usize);
if (i+1)%5000 == 0{
for handle in handles{
let mut handles: Vec<JoinHandle<_>> = Vec::with_capacity(1000);
for (i, sud) in unsolved.into_iter().enumerate() {
let mut sudoku = Sudoku::new(sud, 3usize);
if (i + 1) % 5000 == 0 {
for handle in handles {
let answer = handle.join().unwrap();
results.push(answer);
}
handles = Vec::new();
}

let handle = thread::spawn(move || -> String{
let handle = thread::spawn(move || -> String {
let answer = sudoku.solver(None);
if let Some(answer) = answer{
if let Some(answer) = answer {
return answer[0].clone();
}
return "".to_owned()
return "".to_owned();
});
handles.push(handle);
}
for handle in handles{
let answer = handle.join().unwrap();
results.push(answer);
}
}
for handle in handles {
let answer = handle.join().unwrap();
results.push(answer);
}

self.response = results;
}






}

0 comments on commit f07ec45

Please sign in to comment.