com.alexkasko.unsafe.offheapstruct
public class OffHeapStructSorter 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
.
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 | Field and Description |
---|---|
(package private) static int |
INSERTION_SORT_THRESHOLD
If the length of an collection to be sorted is less than this
constant, insertion sort is used in preference to Quicksort.
|
Constructor and Description |
---|
OffHeapStructSorter() |
Modifier and Type | Method and Description |
---|---|
static void |
sort(OffHeapStructCollection collection,
Comparator<OffHeapStructAccessor> comparator)
Sorts collection using comparator.
|
static void |
sort(OffHeapStructCollection collection,
Comparator<OffHeapStructAccessor> comparator,
long fromIndex,
long toIndex)
Sorts collection using comparator.
|
static void |
sortByIntKey(OffHeapStructCollection a,
int keyOffset)
Sorts the specified off-heap struct collection into ascending order using int struct key.
|
static void |
sortByIntKey(OffHeapStructCollection a,
long fromIndex,
long toIndex,
int keyOffset)
Sorts the specified range of the off-heap struct collection into ascending order using int struct key.
|
static void |
sortByLongKey(OffHeapStructCollection a,
int keyOffset)
Sorts the specified off-heap struct collection into ascending order using long struct key.
|
static void |
sortByLongKey(OffHeapStructCollection a,
long fromIndex,
long toIndex,
int keyOffset)
Sorts the specified range of the off-heap struct collection into ascending order using long struct key.
|
static void |
sortByLongKeysParallel(Executor executor,
int threads,
OffHeapStructCollection a,
int key1Offset,
int key2Offset)
Sorts collection by two long keys.
|
static void |
sortByUnsignedIntKey(OffHeapStructCollection a,
int keyOffset)
Sorts the specified off-heap struct collection into ascending order using unsigned int struct key.
|
static void |
sortByUnsignedIntKey(OffHeapStructCollection a,
long fromIndex,
long toIndex,
int keyOffset)
Sorts the specified range of the off-heap struct collection into ascending order using unsigned int struct key.
|
static void |
sortByUnsignedLongKey(OffHeapStructCollection a,
int keyOffset)
Sorts the specified off-heap struct collection into ascending order using unsigned long struct key.
|
static void |
sortByUnsignedLongKey(OffHeapStructCollection a,
long fromIndex,
long toIndex,
int keyOffset)
Sorts the specified range of the off-heap struct collection into ascending order using unsigned long struct key.
|
static OffHeapDisposableIterable<byte[]> |
sortedByRefIterable(OffHeapStructCollection a,
Comparator<OffHeapStructAccessor> comparator)
Sorts collection using additional
OffHeapLongArray with the same size
as collection itself as an array of references (indices) of the collection |
static OffHeapDisposableIterable<byte[]> |
sortedByRefIterable(OffHeapStructCollection a,
long fromIndex,
long toIndex,
Comparator<OffHeapStructAccessor> comparator)
Sorts collection using additional
OffHeapLongArray with the same size
as collection itself as an array of references (indices) of the collection |
static OffHeapDisposableIterator<byte[]> |
sortedIterator(ExecutorService executor,
int threadsCount,
OffHeapStructCollection collection,
Comparator<OffHeapStructAccessor> comparator)
Partially sorts collection and returns fully sorted iterator over it
|
static OffHeapDisposableIterator<byte[]> |
sortedIterator(ExecutorService executor,
int threadsCount,
OffHeapStructCollection collection,
Comparator<OffHeapStructAccessor> comparator,
long fromIndex,
long toIndex)
Partially sorts part of the collection and returns fully sorted iterator over it
|
static OffHeapDisposableIterator<byte[]> |
sortedIteratorByIntKey(ExecutorService executor,
int threadsCount,
OffHeapStructCollection collection,
int keyOffset)
Partially sorts collection and returns fully sorted iterator over it
|
static OffHeapDisposableIterator<byte[]> |
sortedIteratorByIntKey(ExecutorService executor,
int threadsCount,
OffHeapStructCollection collection,
int keyOffset,
long fromIndex,
long toIndex)
Partially sorts part of the collection and returns fully sorted iterator over it
|
static OffHeapDisposableIterator<byte[]> |
sortedIteratorByLongKey(ExecutorService executor,
int threadsCount,
OffHeapStructCollection collection,
int keyOffset)
Partially sorts collection and returns fully sorted iterator over it
|
static OffHeapDisposableIterator<byte[]> |
sortedIteratorByLongKey(ExecutorService executor,
int threadsCount,
OffHeapStructCollection collection,
int keyOffset,
long fromIndex,
long toIndex)
Partially sorts part of the collection and returns fully sorted iterator over it
|
static final int INSERTION_SORT_THRESHOLD
public OffHeapStructSorter()
public static void sort(OffHeapStructCollection collection, Comparator<OffHeapStructAccessor> comparator)
collection
- off-heap struct collectioncomparator
- structs comparatorpublic static void sort(OffHeapStructCollection collection, Comparator<OffHeapStructAccessor> comparator, long fromIndex, long toIndex)
collection
- off-heap struct collectioncomparator
- structs comparatorfromIndex
- start sort collection indextoIndex
- end sort collection indexpublic static OffHeapDisposableIterator<byte[]> sortedIterator(ExecutorService executor, int threadsCount, OffHeapStructCollection collection, Comparator<OffHeapStructAccessor> comparator)
executor
- executor service for parallel sortingthreadsCount
- number of worker threads to usecollection
- the off-heap struct collection to be sortedcomparator
- structs comparatorIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
RuntimeException
- on worker thread errorpublic static OffHeapDisposableIterator<byte[]> sortedIterator(ExecutorService executor, int threadsCount, OffHeapStructCollection collection, Comparator<OffHeapStructAccessor> comparator, long fromIndex, long toIndex)
executor
- executor service for parallel sortingthreadsCount
- number of worker threads to usecollection
- 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 errorpublic static OffHeapDisposableIterator<byte[]> sortedIteratorByLongKey(ExecutorService executor, int threadsCount, OffHeapStructCollection collection, int keyOffset)
executor
- executor service for parallel sortingthreadsCount
- number of worker threads to usecollection
- the off-heap struct collection to be sortedkeyOffset
- key offsetIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
RuntimeException
- on worker thread errorpublic static OffHeapDisposableIterator<byte[]> sortedIteratorByLongKey(ExecutorService executor, int threadsCount, OffHeapStructCollection collection, int keyOffset, long fromIndex, long toIndex)
executor
- executor service for parallel sortingthreadsCount
- number of worker threads to usecollection
- 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 sortedkeyOffset
- key offsetIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
RuntimeException
- on worker thread errorpublic static OffHeapDisposableIterator<byte[]> sortedIteratorByIntKey(ExecutorService executor, int threadsCount, OffHeapStructCollection collection, int keyOffset)
executor
- executor service for parallel sortingthreadsCount
- number of worker threads to usecollection
- the off-heap struct collection to be sortedkeyOffset
- key offsetIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
RuntimeException
- on worker thread errorpublic static OffHeapDisposableIterator<byte[]> sortedIteratorByIntKey(ExecutorService executor, int threadsCount, OffHeapStructCollection collection, int keyOffset, long fromIndex, long toIndex)
executor
- executor service for parallel sortingthreadsCount
- number of worker threads to usecollection
- 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 sortedkeyOffset
- key offsetIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
RuntimeException
- on worker thread errorpublic static void sortByLongKey(OffHeapStructCollection a, int keyOffset)
a
- the off-heap struct collection to be sortedkeyOffset
- long key field offset within stuct boundspublic static void sortByLongKey(OffHeapStructCollection a, long fromIndex, long toIndex, int keyOffset)
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 sortedkeyOffset
- long sort key field offset within stuct boundsIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
public static void sortByLongKeysParallel(Executor executor, int threads, OffHeapStructCollection a, int key1Offset, int key2Offset)
executor
- executor for parallel sortingthreads
- number of worker threads to usea
- the off-heap struct collection to be sortedkey1Offset
- first key offsetkey2Offset
- second key offsetpublic static void sortByUnsignedLongKey(OffHeapStructCollection a, int keyOffset)
a
- the off-heap struct collection to be sortedkeyOffset
- long key field offset within stuct boundspublic static void sortByUnsignedLongKey(OffHeapStructCollection a, long fromIndex, long toIndex, int keyOffset)
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 sortedkeyOffset
- long sort key field offset within stuct boundsIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
public static void sortByIntKey(OffHeapStructCollection a, int keyOffset)
a
- the off-heap struct collection to be sortedkeyOffset
- int key field offset within stuct boundspublic static void sortByIntKey(OffHeapStructCollection a, long fromIndex, long toIndex, int keyOffset)
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 sortedkeyOffset
- int sort key field offset within stuct boundsIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
public static void sortByUnsignedIntKey(OffHeapStructCollection a, int keyOffset)
a
- the off-heap struct collection to be sortedkeyOffset
- int key field offset within stuct boundspublic static void sortByUnsignedIntKey(OffHeapStructCollection a, long fromIndex, long toIndex, int keyOffset)
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 sortedkeyOffset
- int sort key field offset within stuct boundsIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
public static OffHeapDisposableIterable<byte[]> sortedByRefIterable(OffHeapStructCollection a, Comparator<OffHeapStructAccessor> comparator)
OffHeapLongArray
with the same size
as collection itself as an array of references (indices) of the collectiona
- the off-heap struct collection to be sortedcomparator
- structs comparatorIllegalArgumentException
- if (fromIndex < 0 || fromIndex > toIndex || toIndex > a.size())
public static OffHeapDisposableIterable<byte[]> sortedByRefIterable(OffHeapStructCollection a, long fromIndex, long toIndex, Comparator<OffHeapStructAccessor> comparator)
OffHeapLongArray
with the same size
as collection itself as an array of references (indices) of the collectiona
- 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())
Copyright © 2014. All Rights Reserved.