Skip to content

Commit

Permalink
Make LexicographicalComparator public
Browse files Browse the repository at this point in the history
  • Loading branch information
tustvold committed Dec 8, 2022
1 parent d71f8eb commit e844da8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
4 changes: 2 additions & 2 deletions arrow-ord/src/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn exponential_search_next_partition_point(
let target = start;
let mut bound = 1;
while bound + start < end
&& comparator.compare(&(bound + start), &target) != Ordering::Greater
&& comparator.compare(bound + start, target) != Ordering::Greater
{
bound *= 2;
}
Expand All @@ -101,7 +101,7 @@ fn exponential_search_next_partition_point(
// note here we have right = min(end, start + bound + 1) because (start + bound) might
// actually be considered and must be included.
partition_point(start + bound / 2, end.min(start + bound + 1), |idx| {
comparator.compare(&idx, &target) != Ordering::Greater
comparator.compare(idx, target) != Ordering::Greater
})
}

Expand Down
16 changes: 6 additions & 10 deletions arrow-ord/src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ pub fn lexsort_to_indices(
let lexicographical_comparator = LexicographicalComparator::try_new(columns)?;
// uint32 can be sorted unstably
sort_unstable_by(&mut value_indices, len, |a, b| {
lexicographical_comparator.compare(a, b)
lexicographical_comparator.compare(*a, *b)
});

Ok(UInt32Array::from_iter_values(
Expand All @@ -966,21 +966,17 @@ type LexicographicalCompareItem<'a> = (

/// A lexicographical comparator that wraps given array data (columns) and can lexicographically compare data
/// at given two indices. The lifetime is the same at the data wrapped.
pub(crate) struct LexicographicalComparator<'a> {
pub struct LexicographicalComparator<'a> {
compare_items: Vec<LexicographicalCompareItem<'a>>,
}

impl LexicographicalComparator<'_> {
/// lexicographically compare values at the wrapped columns with given indices.
pub(crate) fn compare<'a, 'b>(
&'a self,
a_idx: &'b usize,
b_idx: &'b usize,
) -> Ordering {
pub fn compare(&self, a_idx: usize, b_idx: usize) -> Ordering {
for (data, comparator, sort_option) in &self.compare_items {
match (data.is_valid(*a_idx), data.is_valid(*b_idx)) {
match (data.is_valid(a_idx), data.is_valid(b_idx)) {
(true, true) => {
match (comparator)(*a_idx, *b_idx) {
match (comparator)(a_idx, b_idx) {
// equal, move on to next column
Ordering::Equal => continue,
order => {
Expand Down Expand Up @@ -1016,7 +1012,7 @@ impl LexicographicalComparator<'_> {

/// Create a new lex comparator that will wrap the given sort columns and give comparison
/// results with two indices.
pub(crate) fn try_new(
pub fn try_new(
columns: &[SortColumn],
) -> Result<LexicographicalComparator<'_>, ArrowError> {
let compare_items = columns
Expand Down
4 changes: 2 additions & 2 deletions arrow/src/row/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1264,13 +1264,13 @@ mod tests {

use arrow_array::NullArray;
use arrow_buffer::Buffer;
use arrow_ord::sort::{LexicographicalComparator, SortColumn, SortOptions};

use crate::array::{
BinaryArray, BooleanArray, DictionaryArray, Float32Array, GenericStringArray,
Int16Array, Int32Array, OffsetSizeTrait, PrimitiveArray,
PrimitiveDictionaryBuilder, StringArray,
};
use crate::compute::{LexicographicalComparator, SortColumn};
use crate::util::display::array_value_to_string;

use super::*;
Expand Down Expand Up @@ -2154,7 +2154,7 @@ mod tests {
let row_i = rows.row(i);
let row_j = rows.row(j);
let row_cmp = row_i.cmp(&row_j);
let lex_cmp = comparator.compare(&i, &j);
let lex_cmp = comparator.compare(i, j);
assert_eq!(
row_cmp,
lex_cmp,
Expand Down

0 comments on commit e844da8

Please sign in to comment.