#include <parallelsort.h>
Template for sorting arrays and searching in sorted arrays
To use this template derive a class from it and define the member 'LessThan' that implements an element comparison. If you do searching the member 'IsEqual' needs to be implemented that does an element comparison with the search key as the first argument. If the search key is different from the element type a second LessThan routine is needed (comparing SEARCHKEY to an element).
SORTCLASS | The class itself. This has to be a derived class of BaseSort. |
FLAGS | See BASESORTFLAGS. |
PARALLELIZATION_THRESHOLD | The minimum number of elements to start parallel sorting. if less elements are sorted a regular BaseSort will be used. |
Example for traditional C-style arrays:
Example for arrays:
Example for arbitrary structures with multiple sort criteria:
Searching Example:
Public Types | |
using | Super = BaseSort< SORTCLASS, FLAGS > |
Public Member Functions | |
template<typename ITERATOR > | |
void | Sort (ITERATOR start, ITERATOR end, JobQueueInterface *queue=JOBQUEUE_CURRENT) const |
template<typename ITERATOR > | |
void | Sort (ITERATOR start, Int count, JobQueueInterface *queue=JOBQUEUE_CURRENT) const |
template<typename ARRAY > | |
void | Sort (ARRAY &arr, JobQueueInterface *queue=JOBQUEUE_CURRENT) const |
![]() | |
void | Sort (ITERATOR start, ITERATOR end) const |
void | Sort (ITERATOR start, Int count) const |
void | Sort (ARRAY &arr) const |
Int | FindIndex (const SEARCHTYPE &key, ITERATOR arr, Int count) const |
ITERATOR | Find (const SEARCHTYPE &key, ITERATOR arr, Int count) const |
ARRAY::ValueType * | Find (const SEARCHTYPE &key, const ARRAY &arr) const |
ITERATOR | FindInsertionIndex (const SEARCHTYPE &key, ITERATOR arr, Int count, Int &insertionIndex) const |
ARRAY::ValueType * | FindInsertionIndex (const SEARCHTYPE &key, const ARRAY &arr, Int &insertionIndex) const |
Private Member Functions | |
template<typename ITERATOR , typename CONTENT > | |
void | ISort (ITERATOR start, Int count, const CONTENT &valType, JobQueueInterface *queue) const |
template<typename CONTENT > | |
void | ISort (CONTENT *start, Int count, const CONTENT &valType, JobQueueInterface *queue) const |
template<typename ITERATOR , typename CONTENT , typename MOVEHELPERFORWARD , typename MOVEHELPERBACKWARD > | |
void | ParallelSortAlgorithm (ITERATOR start, Int count, const CONTENT &valType, JobQueueInterface *queue, const MOVEHELPERFORWARD &moveHelperForward, const MOVEHELPERBACKWARD &moveHelperBackward) const |
template<typename ITERATOR > | |
void | ReduceRedundantBlocks (Int chunksize, Int &cnt, ITERATOR &arr) const |
Int | PowerOfTwo (Int count) const |
void Sort | ( | ITERATOR | start, |
ITERATOR | end, | ||
JobQueueInterface * | queue = JOBQUEUE_CURRENT |
||
) | const |
Sort elements of an array, so that the smallest element comes first. Note that the sorting is not guaranteed to be stable (elements with the same sort value may change their order). The time for sorting is O(n * log(n)). Depending on the available number of threads and initial element distribution you can get speedups of 1 to 8 times compared to BaseSort. If the number of elements is too small less threads will be used or ultimately non-parallel sorting will be used (<1024 elements). Parallel sorting temporarily needs the same amount of memory as the sorted elements take up. If memory isn't available the algorithm will still succeed, but be slightly slower than the non-Threaded version.
[in] | start | The start iterator of an array. |
[in] | end | The end iterator of an array. Note that this is not the last element! It needs to be the boundary (which is last element + 1). See in the BaseSort class description for examples. |
[in] | queue | Optional queue on which the parallel sort will run. |
void Sort | ( | ITERATOR | start, |
Int | count, | ||
JobQueueInterface * | queue = JOBQUEUE_CURRENT |
||
) | const |
Sort elements of an array, so that the smallest element comes first. Note that the sorting is not guaranteed to be stable (elements with the same sort value may change their order). The time for sorting is O(n * log(n)) Depending on the available number of threads and initial element distribution you can get speedups of 1 to 8 times compared to BaseSort. If the number of elements is too small less threads will be used or ultimately non-parallel sorting will be used (<1024 elements). Parallel sorting temporarily needs the same amount of memory as the sorted elements take up. If memory isn't available the algorithm will still succeed, but be slightly slower than the non-Threaded version.
[in] | start | The start iterator of an array. |
[in] | count | The number of elements to be sorted. |
[in] | queue | Optional queue on which the parallel sort will run. |
void Sort | ( | ARRAY & | arr, |
JobQueueInterface * | queue = JOBQUEUE_CURRENT |
||
) | const |
Sort elements of an array, so that the smallest element comes first. Note that the sorting is not guaranteed to be stable (elements with the same sort value may change their order). The time for sorting is O(n * log(n)) Depending on the available number of threads and initial element distribution you can get speedups of 1 to 8 times compared to BaseSort. If the number of elements is too small less threads will be used or ultimately non-parallel sorting will be used (<1024 elements). Parallel sorting temporarily needs the same amount of memory as the sorted elements take up. If memory isn't available the algorithm will still succeed, but be slightly slower than the non-Threaded version.
[in] | arr | The array to be sorted. |
[in] | queue | Optional queue on which the parallel sort will run. |
|
private |
|
private |
|
private |