com.alexkasko.unsafe.offheapstruct
class OffHeapStructSorterWithComparator extends Object
alexkasko: borrowed from https://android.googlesource.com/platform/libcore/+/android-4.2.2_r1/luni/src/main/java/java/util/DualPivotQuicksort.java
and adapted to OffHeapStructCollection
with comparator.
This class implements the Dual-Pivot Quicksort algorithm by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. The algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
Modifier and Type | Class and Description |
---|---|
private static class |
OffHeapStructSorterWithComparator.MergeIter
Merge-sort iterator over partly sorted collection
|
private static class |
OffHeapStructSorterWithComparator.Worker
Worker, sorts part of the collection
|
Constructor and Description |
---|
OffHeapStructSorterWithComparator() |
Modifier and Type | Method and Description |
---|---|
(package private) static void |
doSort(OffHeapStructCollection a,
long left,
long right,
OffHeapStructComparator comp,
byte[] pi,
byte[] pj,
byte[] pe1,
byte[] pe2,
byte[] pe3,
byte[] pe4,
byte[] pe5)
Sorts the specified range of the off-heap struct collection into ascending order using unsigned long struct key.
|
private static void |
dualPivotQuicksort(OffHeapStructCollection a,
long left,
long right,
OffHeapStructComparator comp,
byte[] pi,
byte[] pj,
byte[] pe1,
byte[] pe2,
byte[] pe3,
byte[] pe4,
byte[] pe5)
Sorts the specified range of the off-heap struct collection into ascending order by the
Dual-Pivot Quicksort algorithm using unsigned long struct key.
|
private static void |
invokeAndWait(ExecutorService executor,
List<OffHeapStructSorterWithComparator.Worker> workers)
Helper method to invoke and wait
|
(package private) static void |
sort(OffHeapStructCollection a,
Comparator<OffHeapStructAccessor> comparator)
Sorts the specified off-heap struct collection into ascending order using unsigned long struct key.
|
(package private) static void |
sort(OffHeapStructCollection a,
long fromIndex,
long toIndex,
Comparator<OffHeapStructAccessor> comparator)
Sorts the specified range of the off-heap struct collection into ascending order using unsigned long struct key.
|
(package private) static OffHeapDisposableIterator<byte[]> |
sortedIterator(ExecutorService executor,
int threads,
OffHeapStructCollection a,
Comparator<OffHeapStructAccessor> comparator)
Partially sorts collection and returns fully sorted iterator over it
|
(package private) static OffHeapDisposableIterator<byte[]> |
sortedIterator(ExecutorService executor,
int threads,
OffHeapStructCollection a,
long fromIndex,
long toIndex,
Comparator<OffHeapStructAccessor> comparator)
Partially sorts part of the collection and returns fully sorted iterator over it
|
OffHeapStructSorterWithComparator()
static OffHeapDisposableIterator<byte[]> sortedIterator(ExecutorService executor, int threads, OffHeapStructCollection a, Comparator<OffHeapStructAccessor> comparator)
executor
- executor service for parallel sortingthreads
- number of worker threads to usea
- the off-heap struct collection to be sortedcomparator
- structs comparatorIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
RuntimeException
- on worker thread errorstatic OffHeapDisposableIterator<byte[]> sortedIterator(ExecutorService executor, int threads, OffHeapStructCollection a, long fromIndex, long toIndex, Comparator<OffHeapStructAccessor> comparator)
executor
- executor service for parallel sortingthreads
- number of worker threads to usea
- the off-heap struct collection to be sortedfromIndex
- the index of the first element, inclusive, to be sortedtoIndex
- the index of the last element, exclusive, to be sortedcomparator
- structs comparatorIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
RuntimeException
- on worker thread errorstatic void sort(OffHeapStructCollection a, Comparator<OffHeapStructAccessor> comparator)
a
- the off-heap struct collection to be sortedcomparator
- structs comparatorstatic void sort(OffHeapStructCollection a, long fromIndex, long toIndex, Comparator<OffHeapStructAccessor> comparator)
fromIndex
, inclusive, to
the index toIndex
, exclusive. If fromIndex == toIndex
,
the range to be sorted is empty (and the call is a no-op).a
- the off-heap struct collection to be sortedfromIndex
- the index of the first element, inclusive, to be sortedtoIndex
- the index of the last element, exclusive, to be sortedcomparator
- structs comparatorIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
static void doSort(OffHeapStructCollection a, long left, long right, OffHeapStructComparator comp, byte[] pi, byte[] pj, byte[] pe1, byte[] pe2, byte[] pe3, byte[] pe4, byte[] pe5)
sort
method in that the
right
index is inclusive, and it does no range checking on
left
or right
.a
- the off-heap header-payload collection to be sortedleft
- the index of the first element, inclusive, to be sortedright
- the index of the last element, inclusive, to be sorted* @param keyOffsetcomp
- structs comparatorpi
- temporary buffer for structspj
- temporary buffer for structspe1
- temporary buffer for structspe2
- temporary buffer for structspe3
- temporary buffer for structspe4
- temporary buffer for structspe5
- temporary buffer for structsprivate static void dualPivotQuicksort(OffHeapStructCollection a, long left, long right, OffHeapStructComparator comp, byte[] pi, byte[] pj, byte[] pe1, byte[] pe2, byte[] pe3, byte[] pe4, byte[] pe5)
a
- the off-heap header-payload collection to be sortedleft
- the index of the first element, inclusive, to be sortedright
- the index of the last element, inclusive, to be sortedcomp
- structs comparatorpi
- temporary buffer for structspj
- temporary buffer for structspe1
- temporary buffer for structspe2
- temporary buffer for structspe3
- temporary buffer for structspe4
- temporary buffer for structspe5
- temporary buffer for structsprivate static void invokeAndWait(ExecutorService executor, List<OffHeapStructSorterWithComparator.Worker> workers)
executor
- executor instanceworkers
- workers listCopyright © 2014. All Rights Reserved.