casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Static Public Member Functions | Static Private Member Functions | List of all members
casacore::GenSort< T > Class Template Reference

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...
 

Detailed Description

template<class T>
class casacore::GenSort< T >

General in-place sort functions.

Intended use:

Internal

Review Status

Reviewed By:
Friso Olnon
Date Reviewed:
1995/03/16
Test programs:
tGenSort

Synopsis

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:

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)
is the fastest. It is about 4-6 times faster than the qsort function on the SUN. No worst case has been found, even not for cases where qsort is terribly slow.
Sort::HeapSort
is about twice as slow as quicksort. It has the advantage that the worst case is always o(n*log(n)), while quicksort can have hypothetical inputs with o(n*n).
Sort::InsSort
is o(n*n) for random inputs. It is, however, the only stable sort (i.e. equal values remain in the original order).

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.

Template Type Argument Requirements (T)

Definition at line 114 of file GenSort.h.

Member Function Documentation

template<class T >
static void casacore::GenSort< T >::heapAscSiftDown ( Int  ,
Int  ,
T *   
)
staticprivate

Helper function for ascending heapsort.

template<class T >
static uInt casacore::GenSort< T >::heapSort ( T *  ,
uInt  nr,
Sort::Order  = Sort::Ascending,
int  options = 0 
)
static

Sort C-array using heapsort.

template<class T >
static void casacore::GenSort< T >::heapSortAsc ( T *  ,
Int   
)
staticprivate

Heapsort in ascending order.

template<class T >
static uInt casacore::GenSort< T >::insSort ( T *  ,
uInt  nr,
Sort::Order  = Sort::Ascending,
int  options = 0 
)
static

Sort C-array using insertion sort.

template<class T >
static uInt casacore::GenSort< T >::insSortAsc ( T *  ,
Int  ,
int  option 
)
staticprivate

Insertion sort in ascending order.

template<class T >
static uInt casacore::GenSort< T >::insSortAscDup ( T *  ,
Int   
)
staticprivate

Insertion sort in ascending order allowing duplicates.

This is also used by quicksort for its last steps.

template<class T >
static uInt casacore::GenSort< T >::insSortAscNoDup ( T *  ,
Int   
)
staticprivate

Insertion sort in ascending order allowing no duplicates.

This is also used by the other sort algorithms to skip duplicates.

template<class T >
static T casacore::GenSort< T >::kthLargest ( T *  data,
uInt  nr,
uInt  k 
)
static

Find the k-th largest value.


Note: it does a partial quicksort, thus the data array gets changed.

template<class T >
static T* casacore::GenSort< T >::merge ( T *  data,
T *  tmp,
uInt  nrrec,
uInt index,
uInt  nparts 
)
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).

template<class T >
static uInt casacore::GenSort< T >::parSort ( T *  ,
uInt  nr,
Sort::Order  = Sort::Ascending,
int  options = 0,
int  nthread = 0 
)
static

Sort C-array using parallel merge sort (using OpenMP).

By default OpenMP determines the number of threads that can be used.

template<class T >
static uInt casacore::GenSort< T >::quickSort ( T *  ,
uInt  nr,
Sort::Order  = Sort::Ascending,
int  options = 0 
)
static

Sort C-array using quicksort.

template<class T >
static void casacore::GenSort< T >::quickSortAsc ( T *  ,
Int  ,
Bool  multiThread = False,
Int  rec_lim = 128 
)
staticprivate

Quicksort in ascending order.

template<class T >
static void casacore::GenSort< T >::reverse ( T *  data,
const T *  res,
uInt  nrrec 
)
static

Reverse the elements in res and put them into data.

Care is taken if both pointers reference the same data.

template<class T >
static uInt casacore::GenSort< T >::sort ( T *  ,
uInt  nr,
Sort::Order  = Sort::Ascending,
int  options = 0 
)
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(), casa::ImageStatsCalculator< T >::setAxes(), and casa::CalAnalysis::unique().

template<class T >
static uInt casacore::GenSort< T >::sort ( Array< T > &  ,
Sort::Order  = Sort::Ascending,
int  options = 0 
)
static
template<class T >
static uInt casacore::GenSort< T >::sort ( Block< T > &  ,
uInt  nr,
Sort::Order  = Sort::Ascending,
int  options = 0 
)
static
template<class T >
void casacore::GenSort< T >::swap ( T &  l,
T &  r 
)
inlinestatic

Swap 2 elements in array.

Implement inline member functions.

Definition at line 387 of file GenSort.h.


The documentation for this class was generated from the following file: