FastLED 3.9.15
Loading...
Searching...
No Matches

◆ qsort_partition()

static size_t fl::qsort_partition ( char * base,
size_t nmemb,
size_t size,
qsort_compare_fn compar )
static

Definition at line 376 of file cstdlib.cpp.hpp.

376 {
377 // Use median-of-three for pivot selection
378 size_t mid = nmemb / 2;
379 size_t last = nmemb - 1;
380
381 char* first_elem = base;
382 char* mid_elem = base + mid * size;
383 char* last_elem = base + last * size;
384
385 // Sort first, middle, last to find median
386 if (compar(mid_elem, first_elem) < 0) {
387 detail::qsort_swap(first_elem, mid_elem, size);
388 }
389 if (compar(last_elem, first_elem) < 0) {
390 detail::qsort_swap(first_elem, last_elem, size);
391 }
392 if (compar(last_elem, mid_elem) < 0) {
393 detail::qsort_swap(mid_elem, last_elem, size);
394 }
395
396 // Now mid_elem contains the median, swap it to end-1 position
397 detail::qsort_swap(mid_elem, base + (last - 1) * size, size);
398 char* pivot = base + (last - 1) * size;
399
400 // Partition
401 size_t i = 0;
402 for (size_t j = 0; j < last - 1; ++j) {
403 if (compar(base + j * size, pivot) < 0) {
404 if (i != j) {
405 detail::qsort_swap(base + i * size, base + j * size, size);
406 }
407 ++i;
408 }
409 }
410
411 // Move pivot to its final position
412 detail::qsort_swap(base + i * size, pivot, size);
413 return i;
414}
void qsort_swap(char *a, char *b, size_t size)

References fl::detail::qsort_swap().

Referenced by fl::detail::qsort_impl().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: