Skip to content

Commit 2b32499

Browse files
authored
Add more docs to SearchSortedFn (#2602)
1 parent beedbd7 commit 2b32499

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

vortex-array/src/compute/search_sorted.rs

+14-18
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ impl Display for SearchSortedSide {
2727
}
2828

2929
/// Result of performing search_sorted on an Array
30+
///
31+
/// See [`SearchSortedFn`] documentation for interpretation of the results
3032
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
3133
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
3336
Found(usize),
3437

3538
/// Result for an element not found, but that could be inserted at the given position
@@ -61,11 +64,11 @@ impl SearchResult {
6164
pub fn to_offsets_index(self, len: usize, side: SearchSortedSide) -> usize {
6265
match self {
6366
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)
6669
} else {
67-
0
68-
})
70+
i
71+
}
6972
}
7073
SearchResult::NotFound(i) => i.saturating_sub(1),
7174
}
@@ -80,18 +83,6 @@ impl SearchResult {
8083
let idx = self.to_index();
8184
if idx == len { idx - 1 } else { idx }
8285
}
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-
}
9586
}
9687

9788
impl Display for SearchResult {
@@ -105,7 +96,12 @@ impl Display for SearchResult {
10596

10697
/// Searches for value assuming the array is sorted.
10798
///
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\]|
109105
pub trait SearchSortedFn<A: Copy> {
110106
fn search_sorted(
111107
&self,

vortex-array/src/patches.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,11 @@ impl Patches {
234234
Ok(match sr {
235235
// If we reached the end of patched values when searching then the result is one after the last patch index
236236
SearchResult::Found(i) => SearchResult::Found(
237-
index
238-
+ if i == self.indices().len() || side == SearchSortedSide::Right {
239-
1
240-
} else {
241-
0
242-
},
237+
if i == self.indices().len() || side == SearchSortedSide::Right {
238+
index + 1
239+
} else {
240+
index
241+
},
243242
),
244243
// If the result is NotFound we should return index that's one after the nearest not found index for the corresponding value
245244
SearchResult::NotFound(i) => {

0 commit comments

Comments
 (0)