diff --git a/magyarsort.h b/magyarsort.h index 66d6aee..8394fe9 100644 --- a/magyarsort.h +++ b/magyarsort.h @@ -242,10 +242,10 @@ namespace MagyarSort { /** Recursive Functor: no class should be generated I think (compiler should be smart) */ template struct RadixMagic : public RadixMagic { - inline __attribute__((always_inline)) RadixMagic(COUNTER_TYP *radics, uint32_t *from, uint32_t *to, COUNTER_TYP size) noexcept - : RadixMagic(radics, from, to, size) { + inline __attribute__((always_inline)) RadixMagic(bool &swapped, COUNTER_TYP *radics, uint32_t *from, uint32_t *to, COUNTER_TYP size) noexcept + : RadixMagic(swapped, radics, from, to, size) { // Tricky: see (**) - if(this->swapped) { // never true for DIGIT 0 + if(swapped) { // never true for DIGIT 0, see (***) std::swap(from, to); } @@ -274,14 +274,13 @@ namespace MagyarSort { //debugArr(to, size); // (**) Only swaps pointers above in the child class constructor IF NEEDED :-) - this->swapped = !this->swapped; + swapped = !swapped; } }; /** Ends template recursion */ template struct RadixMagic<-1, COUNTER_TYP> { - bool swapped = false; - inline RadixMagic(COUNTER_TYP *radics, uint32_t *&from, uint32_t *&to, COUNTER_TYP size) noexcept { } + inline RadixMagic(bool swapped, COUNTER_TYP *radics, uint32_t *&from, uint32_t *&to, COUNTER_TYP size) noexcept {} }; /* SORT */ @@ -361,13 +360,15 @@ namespace MagyarSort { uint32_t *from = arr; uint32_t *to = &arc[0]; + static thread_local bool swapped; + swapped = false; // must be separate line - RadixMagic r(radics, from, to, size); + RadixMagic r(swapped, 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(r.swapped) { // <- in reality this is what we want because of last swap happened anyways! + if(swapped) { // <- in reality this is what we want because of last swap happened anyways! memcpy(arr, to, size); } }