@@ -27,9 +27,12 @@ impl Display for SearchSortedSide {
27
27
}
28
28
29
29
/// Result of performing search_sorted on an Array
30
+ ///
31
+ /// See [`SearchSortedFn`] documentation for interpretation of the results
30
32
#[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
31
33
pub enum SearchResult {
32
- /// Result for a found element was found at the given index in the sorted array
34
+ /// Result for a found element was found in the array and another one could be inserted at the given position
35
+ /// in the sorted order
33
36
Found ( usize ) ,
34
37
35
38
/// Result for an element not found, but that could be inserted at the given position
@@ -61,11 +64,11 @@ impl SearchResult {
61
64
pub fn to_offsets_index ( self , len : usize , side : SearchSortedSide ) -> usize {
62
65
match self {
63
66
SearchResult :: Found ( i) => {
64
- i . saturating_sub ( if side == SearchSortedSide :: Right || i == len {
65
- 1
67
+ if side == SearchSortedSide :: Right || i == len {
68
+ i . saturating_sub ( 1 )
66
69
} else {
67
- 0
68
- } )
70
+ i
71
+ }
69
72
}
70
73
SearchResult :: NotFound ( i) => i. saturating_sub ( 1 ) ,
71
74
}
@@ -80,18 +83,6 @@ impl SearchResult {
80
83
let idx = self . to_index ( ) ;
81
84
if idx == len { idx - 1 } else { idx }
82
85
}
83
-
84
- /// Apply a transformation to the Found or NotFound index.
85
- #[ inline]
86
- pub fn map < F > ( self , f : F ) -> SearchResult
87
- where
88
- F : FnOnce ( usize ) -> usize ,
89
- {
90
- match self {
91
- SearchResult :: Found ( i) => SearchResult :: Found ( f ( i) ) ,
92
- SearchResult :: NotFound ( i) => SearchResult :: NotFound ( f ( i) ) ,
93
- }
94
- }
95
86
}
96
87
97
88
impl Display for SearchResult {
@@ -105,7 +96,12 @@ impl Display for SearchResult {
105
96
106
97
/// Searches for value assuming the array is sorted.
107
98
///
108
- /// For nullable arrays we assume that the nulls are sorted last, i.e. they're the greatest value
99
+ /// Returned indices satisfy following condition if the search for value was to be inserted into the array at found positions
100
+ ///
101
+ /// |side |result satisfies|
102
+ /// |-----|----------------|
103
+ /// |left |array\[i-1\] < value <= array\[i\]|
104
+ /// |right|array\[i-1\] <= value < array\[i\]|
109
105
pub trait SearchSortedFn < A : Copy > {
110
106
fn search_sorted (
111
107
& self ,
0 commit comments