FastLED 3.10.8
Loading...
Searching...
No Matches

◆ merge_inplace()

template<typename Iterator, typename Compare>
void fl::detail::merge_inplace ( Iterator first,
Iterator middle,
Iterator last,
Compare comp )

Definition at line 464 of file algorithm.h.

464 {
465 // If one of the ranges is empty, nothing to merge
466 if (first == middle || middle == last) {
467 return;
468 }
469
470 // If arrays are small enough, use insertion-based merge
471 auto left_size = middle - first;
472 auto right_size = last - middle;
473 if (left_size + right_size <= 32) {
474 // Simple insertion-based merge for small arrays
475 Iterator left = first;
476 Iterator right = middle;
477
478 while (left < middle && right < last) {
479 if (!comp(*right, *left)) {
480 // left element is in correct position
481 ++left;
482 } else {
483 // right element needs to be inserted into left part
484 auto value = fl::move(*right);
485 Iterator shift_end = right;
486 Iterator shift_start = left;
487
488 // Shift elements to make room
489 while (shift_end > shift_start) {
490 *shift_end = fl::move(*(shift_end - 1));
491 --shift_end;
492 }
493
494 *left = fl::move(value);
495 ++left;
496 ++middle; // middle has shifted right
497 ++right;
498 }
499 }
500 return;
501 }
502
503 // For larger arrays, use rotation-based merge
504 if (left_size == 0 || right_size == 0) {
505 return;
506 }
507
508 if (left_size == 1) {
509 // Find insertion point for the single left element in right array
510 Iterator pos = lower_bound_impl(middle, last, *first, comp);
511 rotate_impl(first, middle, pos);
512 return;
513 }
514
515 if (right_size == 1) {
516 // Find insertion point for the single right element in left array
517 Iterator pos = lower_bound_impl(first, middle, *(last - 1), comp);
518 rotate_impl(pos, middle, last);
519 return;
520 }
521
522 // Divide both arrays and recursively merge
523 Iterator left_mid = first + left_size / 2;
524 Iterator right_mid = lower_bound_impl(middle, last, *left_mid, comp);
525
526 // Rotate to bring the two middle parts together
527 rotate_impl(left_mid, middle, right_mid);
528
529 // Update middle position
530 Iterator new_middle = left_mid + (right_mid - middle);
531
532 // Recursively merge the two parts
533 merge_inplace(first, left_mid, new_middle, comp);
534 merge_inplace(new_middle, right_mid, last, comp);
535}
uint8_t pos
Definition Blur.ino:11
void rotate_impl(Iterator first, Iterator middle, Iterator last) FL_NO_EXCEPT
Definition algorithm.h:429
Iterator lower_bound_impl(Iterator first, Iterator last, const T &value, Compare comp) FL_NO_EXCEPT
Definition algorithm.h:447
void merge_inplace(Iterator first, Iterator middle, Iterator last, Compare comp) FL_NO_EXCEPT
Definition algorithm.h:464
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition s16x16x4.h:30
constexpr int type_rank< T >::value

References fl::FL_NO_EXCEPT, lower_bound_impl(), merge_inplace(), fl::fl::adl_barrier::move(), pos, rotate_impl(), and fl::type_rank< T >::value.

Referenced by merge_inplace(), and mergesort_impl().

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