Provides utility methods for functionality related to primitive types. This currently includes sorting and searching based on custom comparators.
To include the library in your project (Java 6+ supported), add the following to your POM:
<project>
...
<dependencies>
...
<dependency>
<groupId>net.mintern</groupId>
<artifactId>primitive</artifactId>
<version>1.3</version>
</dependency>
...
</dependencies>
...
</project>
- Sort arrays using primitive comparators
- Binary search using primitive comparators
- Sample usage
- Contributing
- Related projects
The Primitive.sort(...)
methods allow primitive arrays to be sorted using
custom comparators (defined in net.mintern.primitive.comparators
). This
library provides both stable and unstable sorting algorithms. When the stable
algorithm is used, it keeps two values in the same relative position when the
comparator returns 0
for those two values.
The stable algorithm is based on
TimSort, the sorting algorithm
originally developed for use in Python. The implementation in this library is
a shameless copy-paste-edit of the JDK 8 version of Joshua Bloch's
java.util.TimSort
implementation.
The unstable algorithm is based on Java's default primitives Arrays.sort
implementation, a Dual-Pivot
Quicksort. For some inputs,
it may be up to twice as fast as stable sorting.
Although this library is built for Java 6+, the comparators are more convenient when used with Java 8 (as the sample usage illustrates).
After sorting an array using a provided comparator, binary search can be performed using that same comparator. The implementation is another copy-paste-edit of the JDK 8 implementation.
int[] arr = ...;
// Sort by absolute values
Primitive.sort(arr, (i1, i2) -> Math.abs(i1) - Math.abs(i2));
// Sort in reverse order
IntComparator revCmp = (i1, i2) -> Integer.compare(i2, i1);
Primitive.sort(arr, revCmp);
// Binary search in reverse order (now that it's sorted)
int n = ...;
Primitive.binarySearch(arr, n, revCmp);
I will happily accept Pull Requests. If you have any questions, ask away. Please keep changes to a minimum; do not make gratuitous style changes.
In the root directory, run mvn install
. That will build everything.
For this project, I used an ancient-but-solid (by modern standards) template language called FreeMarker to provide templates that generate the source. A handy FreeMarker PreProcessor (FMPP)—in conjunction with an FMPP Maven plugin that I came across on Stack Overflow—turns those templates into the Java source files.
The template configuration and source is in
src/main/fmpp
.
mvn clean deploy -P release
All of my Java libraries are available in the mintern-java organization.