Skip to content

Commit

Permalink
ref: remove unneed check for empty matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
sozelfist committed Oct 24, 2024
1 parent 4436d2c commit 9b15298
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions src/searching/saddleback_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ pub enum MatrixError {

/// Checks if the given matrix (vector of vectors) is sorted row-wise and column-wise.
///
/// A matrix is considered sorted if
///
/// * Each row is sorted in non-decreasing order.
/// * Each column is sorted in non-decreasing order.
///
/// # Arguments
///
/// * `matrix` - A vector of vectors representing the matrix to check.
Expand All @@ -22,10 +17,6 @@ pub enum MatrixError {
///
/// Returns `true` if the matrix is sorted both row-wise and column-wise. Otherwise, returns `false`.
fn is_sorted(matrix: &[Vec<isize>]) -> bool {
if matrix.is_empty() || matrix.iter().all(|row| row.is_empty()) {
return true;
}

let rows = matrix.len();
let cols = matrix[0].len();

Expand Down Expand Up @@ -72,10 +63,6 @@ pub fn saddleback_search(
element: isize,
check_sorted: bool,
) -> Result<Option<(usize, usize)>, MatrixError> {
if check_sorted && !is_sorted(matrix) {
return Err(MatrixError::NotSorted);
}

if matrix.is_empty() || matrix.iter().all(|row| row.is_empty()) {
return Ok(None);
}
Expand All @@ -84,6 +71,10 @@ pub fn saddleback_search(
return Err(MatrixError::NonRectangularInput);
}

if check_sorted && !is_sorted(matrix) {
return Err(MatrixError::NotSorted);
}

let mut left_index = 0;
let mut right_index = matrix[0].len() - 1;

Expand Down

0 comments on commit 9b15298

Please sign in to comment.