casacore
|
General in-place sort functions. More...
#include <GenSort.h>
Static Public Member Functions | |
static uInt | sort (T *, uInt nr, Sort::Order=Sort::Ascending, int options=0) |
Sort a C-array containing nr T -type objects. More... | |
static uInt | sort (Array< T > &, Sort::Order=Sort::Ascending, int options=0) |
static uInt | sort (Block< T > &, uInt nr, Sort::Order=Sort::Ascending, int options=0) |
static T | kthLargest (T *data, uInt nr, uInt k) |
Find the k-th largest value. More... | |
static uInt | quickSort (T *, uInt nr, Sort::Order=Sort::Ascending, int options=0) |
Sort C-array using quicksort. More... | |
static uInt | heapSort (T *, uInt nr, Sort::Order=Sort::Ascending, int options=0) |
Sort C-array using heapsort. More... | |
static uInt | insSort (T *, uInt nr, Sort::Order=Sort::Ascending, int options=0) |
Sort C-array using insertion sort. More... | |
static uInt | parSort (T *, uInt nr, Sort::Order=Sort::Ascending, int options=0, int nthread=0) |
Sort C-array using parallel merge sort (using OpenMP). More... | |
static void | swap (T &, T &) |
Swap 2 elements in array. More... | |
static void | reverse (T *data, const T *res, uInt nrrec) |
Reverse the elements in res and put them into data . More... | |
Static Private Member Functions | |
static T * | merge (T *data, T *tmp, uInt nrrec, uInt *index, uInt nparts) |
Thedata buffer is divided in nparts parts. More... | |
static void | quickSortAsc (T *, Int, Bool multiThread=False, Int rec_lim=128) |
Quicksort in ascending order. More... | |
static void | heapSortAsc (T *, Int) |
Heapsort in ascending order. More... | |
static void | heapAscSiftDown (Int, Int, T *) |
Helper function for ascending heapsort. More... | |
static uInt | insSortAsc (T *, Int, int option) |
Insertion sort in ascending order. More... | |
static uInt | insSortAscDup (T *, Int) |
Insertion sort in ascending order allowing duplicates. More... | |
static uInt | insSortAscNoDup (T *, Int) |
Insertion sort in ascending order allowing no duplicates. More... | |
General in-place sort functions.
Internal
The static member functions of this templated class are highly optimized sort functions. They do an in-place sort of an array of values. The functions are templated, so they can in principle be used with any data type. However, if used with non-builtin data types, their class must provide certain functions (see Template Type Argument Requirements).
If it is impossible or too expensive to define these functions, the Sort class can be used instead. This sorts indirectly using an index array. Instead of the functions mentioned above it requires a comparison routine.
The GenSort
functions can sort:
Array
s of values – the array can have any shape and the increment can be >1; Block
s of values – there is a special function to sort less elements than the size of the Block
. The sort order can be specified in the order field:
Sort::Ascending
(default), Sort::Descending
. Previously the sort algorithm to use could be given in the options field.
Sort::QuickSort
(default) Sort::HeapSort
Sort::InsSort
However, these options are not used anymore because the sort now always uses a merge sort that is equally fast for random input and much faster for degenerated cases like an already ordered or reversely ordered array. Furthermore, merge sort is always stable and will be parallelized if OpenMP support is enabled giving a 6-fold speedup on 8 cores.
Sort::NoDuplicates
in the options field indicates that duplicate values will be removed (only the first occurrance is kept).
The previous sort functionality is still available through the functions quickSort, heapSort, and insSort.
All the sort functions return the number of values sorted as their function value; when duplicate values have been removed, the number of unique valuess will be returned.
The class also provides a function to find the k-th largest value in an array of values. This uses a stripped-down version of quicksort and is at least 6 times faster than a full quicksort.
operator=
to assign when swapping elements operator<
, operator>
and operator==
to compare elements
|
staticprivate |
Helper function for ascending heapsort.
|
static |
Sort C-array using heapsort.
|
staticprivate |
Heapsort in ascending order.
|
static |
Sort C-array using insertion sort.
|
staticprivate |
Insertion sort in ascending order.
|
staticprivate |
Insertion sort in ascending order allowing duplicates.
This is also used by quicksort for its last steps.
|
staticprivate |
Insertion sort in ascending order allowing no duplicates.
This is also used by the other sort algorithms to skip duplicates.
|
static |
Find the k-th largest value.
Note: it does a partial quicksort, thus the data array gets changed.
|
staticprivate |
Thedata
buffer is divided in nparts
parts.
In each part the values are in ascending order. The index tells the nr of elements in each part. Recursively each two subsequent parts are merged until only part is left (giving the sorted array). Alternately data
and tmp
are used for the merge result. The pointer containing the final result is returned.
If possible, merging the parts is done in parallel (using OpenMP).
|
static |
Sort C-array using parallel merge sort (using OpenMP).
By default OpenMP determines the number of threads that can be used.
|
static |
Sort C-array using quicksort.
|
staticprivate |
Quicksort in ascending order.
|
static |
Reverse the elements in res
and put them into data
.
Care is taken if both pointers reference the same data.
|
static |
Sort a C-array containing nr
T
-type objects.
The sort is done in place and is always stable (thus equal keys keep their original order). It returns the number of values, which can be different if a NoDuplicates sort is done.
Insertion sort is used for short arrays (<50 elements). Otherwise, a merge sort is used which will be parallelized if casacore is built with OpenMP support.
Referenced by casacore::genSort().
|
static |
|
static |
|
inlinestatic |