removed reference to pointer parameter - a bit better indirections
This commit is contained in:
parent
298edba5d2
commit
3b413fcba0
23
magyarsort.h
23
magyarsort.h
@ -242,8 +242,15 @@ namespace MagyarSort {
|
||||
/** Recursive Functor: no class should be generated I think (compiler should be smart) */
|
||||
template<int DIGIT, typename COUNTER_TYP>
|
||||
struct RadixMagic : public RadixMagic<DIGIT - 1, COUNTER_TYP> {
|
||||
inline __attribute__((always_inline)) RadixMagic(COUNTER_TYP *radics, uint32_t *&from, uint32_t *&to, COUNTER_TYP size) noexcept // BEWARE: "*&" needed to swap pointers..
|
||||
inline __attribute__((always_inline)) RadixMagic(COUNTER_TYP *radics, uint32_t *from, uint32_t *to, COUNTER_TYP size) noexcept
|
||||
: RadixMagic<DIGIT - 1, COUNTER_TYP>(radics, from, to, size) {
|
||||
//
|
||||
// Compile time - only happens on bottom level
|
||||
if(DIGIT == 0) {
|
||||
this->ourFrom = from;
|
||||
this->ourTo = to;
|
||||
}
|
||||
|
||||
// DEBUG
|
||||
//printf("%d before: ", DIGIT);
|
||||
//debugArr(from, size);
|
||||
@ -256,12 +263,12 @@ namespace MagyarSort {
|
||||
if(i >= 64) { __builtin_prefetch(&from[i - 64]); } // TODO: manually unroll?
|
||||
*/
|
||||
// Get num and its new offset / location
|
||||
auto num = from[i - 1];
|
||||
auto num = this->ourFrom[i - 1];
|
||||
auto digVal = getDigit<DIGIT>(num);
|
||||
auto offset = (--rGet<DIGIT>(radics, digVal));
|
||||
|
||||
// Add to the proper target location
|
||||
to[offset] = num;
|
||||
this->ourTo[offset] = num;
|
||||
}
|
||||
|
||||
// DEBUG
|
||||
@ -269,12 +276,14 @@ namespace MagyarSort {
|
||||
//debugArr(to, size);
|
||||
|
||||
// Only swaps pointers :-)
|
||||
std::swap(from, to);
|
||||
std::swap(this->ourFrom, this->ourTo);
|
||||
}
|
||||
};
|
||||
/** Ends template recursion */
|
||||
template<typename COUNTER_TYP>
|
||||
struct RadixMagic<-1, COUNTER_TYP> {
|
||||
uint32_t *ourFrom = nullptr;
|
||||
uint32_t *ourTo = nullptr;
|
||||
inline RadixMagic(COUNTER_TYP *radics, uint32_t *&from, uint32_t *&to, COUNTER_TYP size) noexcept { }
|
||||
};
|
||||
|
||||
@ -356,13 +365,13 @@ namespace MagyarSort {
|
||||
uint32_t *from = arr;
|
||||
uint32_t *to = &arc[0];
|
||||
|
||||
RadixMagic<DIGITS - 1, COUNTER_TYP>(radics, from, to, size);
|
||||
RadixMagic<DIGITS - 1, COUNTER_TYP> r(radics, from, to, size);
|
||||
|
||||
// With an other API we could spare this copy if we can delete original arr and return ptr or something...
|
||||
// I am fine with this... this is not my main idea anyways, just little ILP tweak to regular radix sort
|
||||
//if(to != arr) // <- logically, but bad they are already swapped here!!! BEWARE
|
||||
if(from != arr) { // <- in reality this is what we want because of last swap happened anyways!
|
||||
memcpy(arr, from, size);
|
||||
if(r.ourFrom != arr) { // <- in reality this is what we want because of last swap happened anyways!
|
||||
memcpy(arr, r.ourFrom, size);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user