ParallelSort< SORTCLASS, FLAGS, PARALLELIZATION_THRESHOLD > Class Template Reference Data Structures
#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 会被使用。 |
Example for traditional C-style arrays:
class Sort1 : public ParallelSort<Sort1> { public : static inline Bool LessThan ( Int a, Int b) { return a < b; } };Example for arrays:
class Sort2 : public ParallelSort<Sort2> { public : static inline Bool LessThan ( Int a, Int b) { return a < b; } }; ... BlockArray< Int > array2; ... Sort2 sort2; sort2.Sort(array2.Begin(), array2.End()); // do not write Sort(&array2[0], &array2[array2.GetCount()]) as this will create a debug assert (the second array access is illegal) sort2.Sort(array2); // any arrays derived from BaseArray can be passed directlyExample for arbitrary structures with multiple sort criteria:
class MyStruct { public : String _str; Int _index; Float _weight; }; class Sort3 : public ParallelSort<Sort3> { public : static inline Bool LessThan ( const MyStruct& a, const MyStruct& b) { if (a._weight < b._weight) return true ; if (a._weight > b._weight) return false ; return a._index < b._index; // if weights are identical sort by index } }; ... BlockArray<MyStruct> array3; ... Sort3 sort3; sort3.Sort(array3);Searching Example:
class MyStruct { public : String _str; Int _index; Float _weight; }; class MySearch : public ParallelSort<MySearch> { public : // for sorting: compare array element to array element static inline Bool LessThan ( const MyStruct& a, const MyStruct& b) { if (a._weight < b._weight) return true ; if (a._weight > b._weight) return false ; return a._index < b._index; // if weights are identical sort by index }// for searching: compare search key to array element static inline Bool LessThan ( Float weight, const MyStruct& b) { return weight < b._weight; }
// for searching: compare search key to array element static inline Bool IsEqual ( Float weight, const MyStruct& b) { return weight == b._weight; } }; ... BlockArray<MyStruct> array4; ... MySearch search; search.Sort(array4); ...
// array is now sorted, we can search it MyStruct* result = search.Find(5.0, array4);
Public Types |
|
using | Super = BaseSort < SORTCLASS, FLAGS > |
公共成员函数 |
|
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 |
私有成员函数 |
|
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 |
using Super = BaseSort <SORTCLASS, FLAGS> |
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 |
|
private |
|
private |