FastLED 3.10.10
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 496 of file algorithm.h.

496 {
497 // If one of the ranges is empty, nothing to merge
498 if (first == middle || middle == last) {
499 return;
500 }
501
502 // If arrays are small enough, use insertion-based merge
503 auto left_size = middle - first;
504 auto right_size = last - middle;
505 if (left_size + right_size <= 32) {
506 // Simple insertion-based merge for small arrays
507 Iterator left = first;
508 Iterator right = middle;
509
510 while (left < middle && right < last) {
511 if (!comp(*right, *left)) {
512 // left element is in correct position
513 ++left;
514 } else {
515 // right element needs to be inserted into left part
516 auto value = fl::move(*right);
517 Iterator shift_end = right;
518 Iterator shift_start = left;
519
520 // Shift elements to make room
521 while (shift_end > shift_start) {
522 *shift_end = fl::move(*(shift_end - 1));
523 --shift_end;
524 }
525
526 *left = fl::move(value);
527 ++left;
528 ++middle; // middle has shifted right
529 ++right;
530 }
531 }
532 return;
533 }
534
535 // For larger arrays, use rotation-based merge
536 if (left_size == 0 || right_size == 0) {
537 return;
538 }
539
540 if (left_size == 1) {
541 // Find insertion point for the single left element in right array
542 Iterator pos = lower_bound_impl(middle, last, *first, comp);
543 rotate_impl(first, middle, pos);
544 return;
545 }
546
547 if (right_size == 1) {
548 // Find insertion point for the single right element in left array
549 Iterator pos = lower_bound_impl(first, middle, *(last - 1), comp);
550 rotate_impl(pos, middle, last);
551 return;
552 }
553
554 // Divide both arrays and recursively merge
555 Iterator left_mid = first + left_size / 2;
556 Iterator right_mid = lower_bound_impl(middle, last, *left_mid, comp);
557
558 // Rotate to bring the two middle parts together
559 rotate_impl(left_mid, middle, right_mid);
560
561 // Update middle position
562 Iterator new_middle = left_mid + (right_mid - middle);
563
564 // Recursively merge the two parts
565 merge_inplace(first, left_mid, new_middle, comp);
566 merge_inplace(new_middle, right_mid, last, comp);
567}
uint8_t pos
Definition Blur.ino:11
void rotate_impl(Iterator first, Iterator middle, Iterator last) FL_NO_EXCEPT
Definition algorithm.h:461
Iterator lower_bound_impl(Iterator first, Iterator last, const T &value, Compare comp) FL_NO_EXCEPT
Definition algorithm.h:479
void merge_inplace(Iterator first, Iterator middle, Iterator last, Compare comp) FL_NO_EXCEPT
Definition algorithm.h:496
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: