#include <radix_sort.h>
getRadixKey() return the unsigned key for this element.
Nevrax France
Definition at line 49 of file radix_sort.h.
Public Member Functions | |
CRadixSort (uint keyDepth=32, uint digitDepth=8) | |
T * | reverse_sort (T *array0, T *array1, uint size) |
T * | sort (T *array0, T *array1, uint size) |
Private Member Functions | |
T * | doSort (T *arraySrc, T *arrayDst, uint size, bool increasingOrder) |
radix sort algorithm | |
Private Attributes | |
uint | _DigitDepth |
uint | _DigitSize |
uint | _KeyDepth |
std::vector< CSortDigit > | _SortDigits |
|
Constructor
Definition at line 62 of file radix_sort.h. References NL3D::CRadixSort< T >::_DigitDepth, NL3D::CRadixSort< T >::_DigitSize, NL3D::CRadixSort< T >::_KeyDepth, NL3D::CRadixSort< T >::_SortDigits, NLMISC::clamp(), min, and uint.
00063 { 00064 // setup 00065 _KeyDepth= keyDepth; 00066 NLMISC::clamp(_KeyDepth, 1U, 32U); 00067 _DigitDepth= digitDepth; 00068 NLMISC::clamp(_DigitDepth, 1U, 16U); 00069 _DigitDepth= std::min(_DigitDepth, _KeyDepth); 00070 00071 // resize the array of digits. 00072 _DigitSize= 1<<_DigitDepth; 00073 _SortDigits.resize(_DigitSize); 00074 } |
|
radix sort algorithm
Definition at line 113 of file radix_sort.h. References NL3D::CRadixSort< T >::_DigitDepth, NL3D::CRadixSort< T >::_DigitSize, NL3D::CRadixSort< T >::_KeyDepth, NL3D::CRadixSort< T >::_SortDigits, sint, size, uint, and uint32. Referenced by NL3D::CRadixSort< T >::reverse_sort(), and NL3D::CRadixSort< T >::sort().
00114 { 00115 if(size==0) 00116 return arraySrc; 00117 00118 // for all digits. 00119 for(uint digit=0; digit< _KeyDepth; digit+=_DigitDepth ) 00120 { 00121 sint i; 00122 // how many bits do we shift? 00123 uint digitShift= digit; 00124 uint digitMask= _DigitSize - 1; 00125 00126 // Init digit count. 00127 for(i=0; i<(sint)_DigitSize; i++) 00128 { 00129 _SortDigits[i].Count= 0; 00130 } 00131 00132 // for all elements in array, count the usage of current digit. 00133 T *srcPtr= arraySrc; 00134 for(i=0; i<(sint)size; i++, srcPtr++) 00135 { 00136 // get the key for this element 00137 uint32 key= srcPtr->getRadixKey(); 00138 // get the actual digit of interest 00139 key>>= digitShift; 00140 key&= digitMask; 00141 // increment the use of this digit. 00142 _SortDigits[key].Count++; 00143 } 00144 00145 // for all digit, init start Ptr. 00146 T *dstPtr= arrayDst; 00147 if(increasingOrder) 00148 { 00149 for(i=0; i<(sint)_DigitSize; i++) 00150 { 00151 // setup the dest ptr for this digit. 00152 _SortDigits[i].Ptr= dstPtr; 00153 // increment the ptr of digit usage 00154 dstPtr+= _SortDigits[i].Count; 00155 } 00156 } 00157 else 00158 { 00159 // reverse order of copy for digits, so the biggest one will 00160 // copy in the beginning of the array 00161 for(i=_DigitSize-1; i>=0; i--) 00162 { 00163 // setup the dest ptr for this digit. 00164 _SortDigits[i].Ptr= dstPtr; 00165 // increment the ptr of digit usage 00166 dstPtr+= _SortDigits[i].Count; 00167 } 00168 } 00169 00170 // for all elements, sort for this digit, by copying from src to dest. 00171 srcPtr= arraySrc; 00172 for(i=0; i<(sint)size; i++, srcPtr++) 00173 { 00174 // get the key for this element 00175 uint32 key= srcPtr->getRadixKey(); 00176 // get the actual digit of interest 00177 key>>= digitShift; 00178 key&= digitMask; 00179 // copy to good digit dst, and increment dest ptr. 00180 *(_SortDigits[key].Ptr++)= *srcPtr; 00181 } 00182 00183 // arraDst has now values of arraySrc sorted for the current digit. 00184 // now, restart with next digit, so swap src / dst 00185 std::swap(arraySrc, arrayDst); 00186 } 00187 00188 // return the array correctly sorted. because of last swap, this is arraySrc 00189 return arraySrc; 00190 } |
|
same as sort(), but elements are reordered in decreasing order
Definition at line 90 of file radix_sort.h. References NL3D::CRadixSort< T >::doSort(), size, and uint.
00090 {return doSort(array0, array1, size, false);} |
|
Sort an array of T, in (keyDepth/digitSize)*O(size). Elements are reordered in increasing order.
Definition at line 84 of file radix_sort.h. References NL3D::CRadixSort< T >::doSort(), size, and uint.
00084 {return doSort(array0, array1, size, true);} |
|
Definition at line 106 of file radix_sort.h. Referenced by NL3D::CRadixSort< T >::CRadixSort(), and NL3D::CRadixSort< T >::doSort(). |
|
Definition at line 107 of file radix_sort.h. Referenced by NL3D::CRadixSort< T >::CRadixSort(), and NL3D::CRadixSort< T >::doSort(). |
|
Definition at line 105 of file radix_sort.h. Referenced by NL3D::CRadixSort< T >::CRadixSort(), and NL3D::CRadixSort< T >::doSort(). |
|
Definition at line 109 of file radix_sort.h. Referenced by NL3D::CRadixSort< T >::CRadixSort(), and NL3D::CRadixSort< T >::doSort(). |