Skip to content

Commit b54a02b

Browse files
committed
chore: return Option<(usize, usize)>
1 parent 326bded commit b54a02b

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/searching/saddleback_search.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ use std::cmp::Ordering;
1313
///
1414
/// # Returns
1515
///
16-
/// Returns a tuple (row, column) where both indices are 1-based. If the element is not found, returns (0, 0).
17-
pub fn saddleback_search(matrix: &[Vec<isize>], element: isize) -> (usize, usize) {
16+
/// Returns `Some((row, column))` where both indices are 1-based. If the element is not found, returns `None`.
17+
pub fn saddleback_search(matrix: &[Vec<isize>], element: isize) -> Option<(usize, usize)> {
1818
let mut left_index = 0;
1919
let mut right_index = matrix[0].len() - 1;
2020

2121
while left_index < matrix.len() {
2222
match element.cmp(&matrix[left_index][right_index]) {
23-
Ordering::Equal => return (left_index + 1, right_index + 1),
23+
Ordering::Equal => return Some((left_index + 1, right_index + 1)),
2424
Ordering::Greater => {
2525
left_index += 1;
2626
}
@@ -34,7 +34,7 @@ pub fn saddleback_search(matrix: &[Vec<isize>], element: isize) -> (usize, usize
3434
}
3535
}
3636

37-
(0, 0)
37+
None
3838
}
3939

4040
#[cfg(test)]
@@ -61,7 +61,7 @@ mod tests {
6161
vec![3, 30, 300]
6262
],
6363
123,
64-
(0, 0),
64+
None,
6565
),
6666
test_element_at_top_left: (
6767
vec![
@@ -70,7 +70,7 @@ mod tests {
7070
vec![3, 30, 300]
7171
],
7272
1,
73-
(1, 1),
73+
Some((1, 1)),
7474
),
7575
test_element_at_bottom_right: (
7676
vec![
@@ -79,7 +79,7 @@ mod tests {
7979
vec![3, 30, 300]
8080
],
8181
300,
82-
(3, 3),
82+
Some((3, 3)),
8383
),
8484
test_element_at_top_right: (
8585
vec![
@@ -88,7 +88,7 @@ mod tests {
8888
vec![3, 30, 300]
8989
],
9090
100,
91-
(1, 3),
91+
Some((1, 3)),
9292
),
9393
test_element_at_bottom_left: (
9494
vec![
@@ -97,7 +97,7 @@ mod tests {
9797
vec![3, 30, 300]
9898
],
9999
3,
100-
(3, 1),
100+
Some((3, 1)),
101101
),
102102
test_element_in_middle: (
103103
vec![
@@ -106,7 +106,7 @@ mod tests {
106106
vec![3, 30, 300, 3000],
107107
],
108108
200,
109-
(2, 3),
109+
Some((2, 3)),
110110
),
111111
test_element_smaller_than_min: (
112112
vec![
@@ -115,7 +115,7 @@ mod tests {
115115
vec![3, 30, 300],
116116
],
117117
0,
118-
(0, 0),
118+
None,
119119
),
120120
}
121121
}

0 commit comments

Comments
 (0)