FastLED 3.9.15
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 399 of file algorithm.h.

399 {
400 // If one of the ranges is empty, nothing to merge
401 if (first == middle || middle == last) {
402 return;
403 }
404
405 // If arrays are small enough, use insertion-based merge
406 auto left_size = middle - first;
407 auto right_size = last - middle;
408 if (left_size + right_size <= 32) {
409 // Simple insertion-based merge for small arrays
410 Iterator left = first;
411 Iterator right = middle;
412
413 while (left < middle && right < last) {
414 if (!comp(*right, *left)) {
415 // left element is in correct position
416 ++left;
417 } else {
418 // right element needs to be inserted into left part
419 auto value = fl::move(*right);
420 Iterator shift_end = right;
421 Iterator shift_start = left;
422
423 // Shift elements to make room
424 while (shift_end > shift_start) {
425 *shift_end = fl::move(*(shift_end - 1));
426 --shift_end;
427 }
428
429 *left = fl::move(value);
430 ++left;
431 ++middle; // middle has shifted right
432 ++right;
433 }
434 }
435 return;
436 }
437
438 // For larger arrays, use rotation-based merge
439 if (left_size == 0 || right_size == 0) {
440 return;
441 }
442
443 if (left_size == 1) {
444 // Find insertion point for the single left element in right array
445 Iterator pos = lower_bound_impl(middle, last, *first, comp);
446 rotate_impl(first, middle, pos);
447 return;
448 }
449
450 if (right_size == 1) {
451 // Find insertion point for the single right element in left array
452 Iterator pos = lower_bound_impl(first, middle, *(last - 1), comp);
453 rotate_impl(pos, middle, last);
454 return;
455 }
456
457 // Divide both arrays and recursively merge
458 Iterator left_mid = first + left_size / 2;
459 Iterator right_mid = lower_bound_impl(middle, last, *left_mid, comp);
460
461 // Rotate to bring the two middle parts together
462 rotate_impl(left_mid, middle, right_mid);
463
464 // Update middle position
465 Iterator new_middle = left_mid + (right_mid - middle);
466
467 // Recursively merge the two parts
468 merge_inplace(first, left_mid, new_middle, comp);
469 merge_inplace(new_middle, right_mid, last, comp);
470}
uint8_t pos
Definition Blur.ino:11
void merge_inplace(Iterator first, Iterator middle, Iterator last, Compare comp)
Definition algorithm.h:399
void rotate_impl(Iterator first, Iterator middle, Iterator last)
Definition algorithm.h:364
Iterator lower_bound_impl(Iterator first, Iterator last, const T &value, Compare comp)
Definition algorithm.h:382
constexpr remove_reference< T >::type && move(T &&t) noexcept
Definition move.h:27

References lower_bound_impl(), merge_inplace(), fl::move(), pos, and rotate_impl().

Referenced by merge_inplace(), and mergesort_impl().

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