Skip to content

Commit

Permalink
feat: replace simpleerror with thiserror
Browse files Browse the repository at this point in the history
  • Loading branch information
sgasse committed Oct 19, 2024
1 parent 9e6fb8c commit 107ecfa
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 143 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ getrandom = { version = "0.2.15", features = ["js"] }
log = "0.4.22"
rand = "0.8.5"
rustc-hash = "2.0.0"
simple-error = "0.3.1"
thiserror = "1.0.64"
wasm-bindgen = "0.2.95"
wasm-logger = "0.2.0"
web-sys = { version = "0.3.72", features = [
Expand Down
7 changes: 6 additions & 1 deletion benches/solver_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ lazy_static::lazy_static! {
};
}

const MAX_NUM_STEPS_OPTIMAL: usize = 2_000_000;

fn criterion_benchmark(c: &mut Criterion) {
let mut group100 = c.benchmark_group("100 samples");
group100.sample_size(100);
Expand All @@ -27,6 +29,7 @@ fn criterion_benchmark(c: &mut Criterion) {
black_box(&PUZZLE_3X3_16.0),
black_box(PUZZLE_3X3_16.1),
black_box(PUZZLE_3X3_16.2),
MAX_NUM_STEPS_OPTIMAL,
)
})
});
Expand All @@ -38,6 +41,7 @@ fn criterion_benchmark(c: &mut Criterion) {
black_box(&PUZZLE_4X4_10.0),
black_box(PUZZLE_4X4_10.1),
black_box(PUZZLE_4X4_10.2),
MAX_NUM_STEPS_OPTIMAL,
)
})
});
Expand Down Expand Up @@ -77,11 +81,12 @@ fn criterion_benchmark(c: &mut Criterion) {
black_box(&PUZZLE_4X4_17.0),
black_box(PUZZLE_4X4_17.1),
black_box(PUZZLE_4X4_17.2),
MAX_NUM_STEPS_OPTIMAL,
)
})
});

group_slow.bench_function("4x4 10 steps divide and conquer", |b| {
group_slow.bench_function("4x4 17 steps divide and conquer", |b| {
b.iter(|| {
let mut solver = DacPuzzleSolver::new(
black_box(&PUZZLE_4X4_17.0),
Expand Down
27 changes: 13 additions & 14 deletions src/board.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use rand::prelude::SliceRandom;
use simple_error::simple_error;

use crate::Error;
use crate::error::LibError;

#[derive(Debug)]
pub(crate) struct Board {
Expand Down Expand Up @@ -111,23 +110,23 @@ where
t_zero <= row && row < height && t_zero <= col && col < width
}

/// Initialize fields as vector.
pub(crate) fn initialize_fields(num_elements: usize) -> Vec<u8> {
let num_elements = usize::min(num_elements, u8::MAX as usize) as u8;
(0..num_elements).collect()
}

/// Get the index of a value in a slice.
///
/// This is a convenience wrapper which should not be used in a hot path.
pub(crate) fn get_idx_of_val(slice: &[u8], value: u8) -> Result<usize, Error> {
pub(crate) fn get_idx_of_val(slice: &[u8], value: u8) -> Result<usize, LibError> {
slice
.iter()
.position(|&v| v == value)
.ok_or_else(|| simple_error!("value not found").into())
}

/// Initialize fields as vector.
pub(crate) fn initialize_fields(num_elements: usize) -> Vec<u8> {
let num_elements = usize::min(num_elements, u8::MAX as usize) as u8;
(0..num_elements).collect()
.ok_or(LibError::ValueNotFound(value))
}

pub(crate) fn get_empty_field_idx(fields: &[u8]) -> Result<usize, Error> {
pub(crate) fn get_empty_field_idx(fields: &[u8]) -> Result<usize, LibError> {
get_idx_of_val(fields, fields.len() as u8 - 1)
}

Expand Down Expand Up @@ -168,7 +167,7 @@ pub(crate) fn get_shuffle_sequence(
size: usize,
mut empty_field_idx: usize,
num_swaps: usize,
) -> Result<Vec<(usize, usize)>, Error> {
) -> Vec<(usize, usize)> {
let mut swaps = Vec::with_capacity(num_swaps);

// We want to avoid swapping fields back and forth like (2, 1), (1, 2)
Expand All @@ -182,11 +181,11 @@ pub(crate) fn get_shuffle_sequence(
.collect();
let chosen_neighbour = swappable_neighbours
.choose(&mut rand::thread_rng())
.ok_or_else(|| simple_error!("no random neighbour to choose"))?;
.expect("should always have a neighbour to swap");
swaps.push((empty_field_idx, *chosen_neighbour));
prev_empty_field_idx = empty_field_idx;
empty_field_idx = *chosen_neighbour;
}

Ok(swaps)
swaps
}
17 changes: 17 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#[derive(thiserror::Error, Debug)]
pub enum LibError {
#[error("value {0} not found")]
ValueNotFound(u8),
#[error("pos (row: {row}, col: {col}) out of bounds")]
PosOutOfBounds { row: i32, col: i32 },
#[error("algorithm terminated without finding a solution")]
TerminatedWithoutSolution,
#[error("maximum number of steps ({0}) reached without finding a solution")]
MaxNumStepsReached(usize),
#[error("board is not square with width {width} and height {height}")]
NotSquare { width: i32, height: i32 },
#[error("fields slice with {len} fields does not match board expecting {expected} fields")]
FieldsBoardMismatch { len: usize, expected: i32 },
#[error("boards below 3x3 are not supported")]
Below3x3,
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use ui::{
use wasm_bindgen::prelude::*;

pub mod board;
pub mod error;
pub mod solver;
pub mod ui;

Expand Down
Loading

0 comments on commit 107ecfa

Please sign in to comment.