Skip to content

Commit

Permalink
DH-18056: Remove NullNanAwareComparator in favor of built-in Comparat…
Browse files Browse the repository at this point in the history
…ors.
  • Loading branch information
cpwright committed Dec 30, 2024
1 parent 0ae8509 commit c768622
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 167 deletions.

This file was deleted.

17 changes: 8 additions & 9 deletions engine/function/src/templates/Sort.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package io.deephaven.function;

import io.deephaven.vector.*;
import io.deephaven.function.comparators.NullNaNAwareComparator;
import org.apache.commons.lang3.ArrayUtils;

import java.util.Arrays;
Expand Down Expand Up @@ -51,7 +50,7 @@ public class Sort {
* @return sorted values.
*/
static public <T extends Comparable<? super T>> T[] sortObj(final ObjectVector<T> values) {
return sortObj(values, new NullNaNAwareComparator<>());
return sortObj(values, Comparator.nullsFirst(Comparator.naturalOrder()));
}

/**
Expand Down Expand Up @@ -83,7 +82,7 @@ public class Sort {
*/
@SafeVarargs
static public <T extends Comparable<? super T>> T[] sortObj(final T... values) {
return sortObj(values, new NullNaNAwareComparator<>());
return sortObj(values, Comparator.nullsFirst(Comparator.naturalOrder()));
}

/**
Expand Down Expand Up @@ -113,7 +112,7 @@ public class Sort {
* @return sorted indices.
*/
static public <T extends Comparable<? super T>> int[] rankObj(final ObjectVector<T> values) {
return rankObj(values, new NullNaNAwareComparator<>());
return rankObj(values, Comparator.nullsFirst(Comparator.naturalOrder()));
}

/**
Expand Down Expand Up @@ -141,7 +140,7 @@ public class Sort {
*/
@SafeVarargs
static public <T extends Comparable<? super T>> int[] rankObj(final T... values) {
return rankObj(values, new NullNaNAwareComparator<>());
return rankObj(values, Comparator.nullsFirst(Comparator.naturalOrder()));
}

/**
Expand Down Expand Up @@ -172,7 +171,7 @@ public class Sort {
* @return sorted values.
*/
static public <T extends Comparable<? super T>> T[] sortDescendingObj(final ObjectVector<T> values) {
return sortDescendingObj(values, new NullNaNAwareComparator<>());
return sortDescendingObj(values, Comparator.nullsFirst(Comparator.naturalOrder()));
}

/**
Expand All @@ -198,7 +197,7 @@ public class Sort {
*/
@SafeVarargs
static public <T extends Comparable<? super T>> T[] sortDescendingObj(final T... values) {
return sortDescendingObj(values, new NullNaNAwareComparator<>());
return sortDescendingObj(values, Comparator.nullsFirst(Comparator.naturalOrder()));
}

/**
Expand Down Expand Up @@ -229,7 +228,7 @@ public class Sort {
* @return sorted indices.
*/
static public <T extends Comparable<? super T>> int[] rankDescendingObj(final ObjectVector<T> values) {
return rankDescendingObj(values, new NullNaNAwareComparator<>());
return rankDescendingObj(values, Comparator.nullsFirst(Comparator.naturalOrder()));
}

/**
Expand All @@ -255,7 +254,7 @@ public class Sort {
*/
@SafeVarargs
static public <T extends Comparable<? super T>> int[] rankDescendingObj(final T... values) {
return rankDescendingObj(values, new NullNaNAwareComparator<>());
return rankDescendingObj(values, Comparator.nullsFirst(Comparator.naturalOrder()));
}

<#list primitiveTypes as pt>
Expand Down
36 changes: 36 additions & 0 deletions engine/function/src/templates/TestSort.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.deephaven.vector.*;
import org.apache.commons.lang3.ArrayUtils;

import java.math.BigDecimal;
import java.util.Arrays;

import static io.deephaven.util.QueryConstants.*;
import static io.deephaven.function.Sort.*;
Expand Down Expand Up @@ -94,6 +95,41 @@ public class TestSort extends BaseArrayTestCase {
assertTrue(ArrayUtils.isEmpty(sortedNumbers));
}

public void testObjSortDoubles() {
final Comparable [] a = new Comparable[7];
a[0] = 7.0;
a[1] = Double.POSITIVE_INFINITY + Double.NEGATIVE_INFINITY;
a[2] = null;
a[3] = Double.POSITIVE_INFINITY;
a[4] = -Math.PI;
a[5] = Double.NEGATIVE_INFINITY;
a[6] = Math.E;
final ObjectVectorDirect<Comparable> values = new ObjectVectorDirect<>(a);
final Comparable[] res = sortObj(values);
assertTrue(Double.isNaN((double)res[6]));
assertEquals(new Comparable[]{null, Double.NEGATIVE_INFINITY, -Math.PI, Math.E, 7.0, Double.POSITIVE_INFINITY}, new ObjectVectorDirect<>(res).subVector(0, 6).toArray());

final Comparable[] resd = sortDescendingObj(values);
assertTrue(Double.isNaN((double)resd[0]));
assertEquals(new Comparable[]{Double.POSITIVE_INFINITY, 7.0, Math.E, -Math.PI, Double.NEGATIVE_INFINITY, null}, new ObjectVectorDirect<>(resd).subVector(1, 7).toArray());
}

public void testObjRankDoubles() {
final Comparable [] a = new Comparable[7];
a[0] = 7.0;
a[1] = Double.POSITIVE_INFINITY + Double.NEGATIVE_INFINITY;
a[2] = null;
a[3] = Double.POSITIVE_INFINITY;
a[4] = -Math.PI;
a[5] = Double.NEGATIVE_INFINITY;
a[6] = Math.E;
final ObjectVectorDirect<Comparable> values = new ObjectVectorDirect<>(a);
final int[] res = rankObj(values);
assertEquals(new int[]{2, 5, 4, 6, 0, 3, 1}, res);

final int[] resd = rankDescendingObj(values);
assertEquals(new int[]{1, 3, 0, 6, 4, 5, 2}, resd);
}

<#list primitiveTypes as pt>
<#if pt.valueType.isNumber >
Expand Down

This file was deleted.

0 comments on commit c768622

Please sign in to comment.