|
| 1 | +/**************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2025 IMProject Development Team. All rights reserved. |
| 4 | + * Authors: Juraj Ciberlin <[email protected]> |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in |
| 14 | + * the documentation and/or other materials provided with the |
| 15 | + * distribution. |
| 16 | + * 3. Neither the name IMProject nor the names of its contributors may be |
| 17 | + * used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 25 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 26 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 27 | + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 28 | + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 30 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 31 | + * POSSIBILITY OF SUCH DAMAGE. |
| 32 | + * |
| 33 | + ****************************************************************************/ |
| 34 | + |
| 35 | +#include "merge_sort.h" |
| 36 | + |
| 37 | +#include <string.h> |
| 38 | + |
| 39 | +void |
| 40 | +MergeHalves(byte_t* buffer, int32_t element_size, int32_t left, int32_t mid, int32_t right, |
| 41 | + bool (*compareFun)(void* first, void* second)) { |
| 42 | + byte_t* elements = buffer; |
| 43 | + byte_t left_half[MAX_HALVES_SIZE]; |
| 44 | + byte_t right_half[MAX_HALVES_SIZE]; |
| 45 | + int32_t i; |
| 46 | + int32_t j; |
| 47 | + int32_t k; |
| 48 | + int32_t number_of_elements_1 = mid - left + 1; |
| 49 | + int32_t number_of_elements_2 = right - mid; |
| 50 | + |
| 51 | + for (i = 0; i < number_of_elements_1; ++i) { |
| 52 | + memcpy(&left_half[i * element_size], &elements[(left + i) * element_size], (size_t)element_size); |
| 53 | + } |
| 54 | + |
| 55 | + for (j = 0; j < number_of_elements_2; ++j) { |
| 56 | + memcpy(&right_half[j * element_size], &elements[(mid + 1 + j) * element_size], (size_t)element_size); |
| 57 | + } |
| 58 | + |
| 59 | + i = 0; |
| 60 | + j = 0; |
| 61 | + k = left; |
| 62 | + |
| 63 | + while ((i < number_of_elements_1) && (j < number_of_elements_2)) { |
| 64 | + bool compare = compareFun(&right_half[j * element_size], &left_half[i * element_size]); |
| 65 | + if (compare) { |
| 66 | + memcpy(&elements[k * element_size], &left_half[i * element_size], (size_t)element_size); |
| 67 | + ++i; |
| 68 | + } else { |
| 69 | + memcpy(&elements[k * element_size], &right_half[j * element_size], (size_t)element_size); |
| 70 | + ++j; |
| 71 | + } |
| 72 | + ++k; |
| 73 | + } |
| 74 | + |
| 75 | + while (i < number_of_elements_1) { |
| 76 | + memcpy(&elements[k * element_size], &left_half[i * element_size], (size_t)element_size); |
| 77 | + ++i; |
| 78 | + ++k; |
| 79 | + } |
| 80 | + |
| 81 | + while (j < number_of_elements_2) { |
| 82 | + memcpy(&elements[k * element_size], &right_half[j * element_size], (size_t)element_size); |
| 83 | + ++j; |
| 84 | + ++k; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +void |
| 89 | +MergeSort_sort(byte_t* buffer, int32_t number_of_elements, int32_t element_size, |
| 90 | + bool (*compareFun)(void* first, void* second)) { |
| 91 | + byte_t* elements = buffer; |
| 92 | + for (int32_t current_size = 1; current_size <= number_of_elements - 1; current_size *= 2) { |
| 93 | + for (int32_t left = 0; left < number_of_elements - 1; left += (2 * current_size)) { |
| 94 | + int32_t compare_first_1 = left + current_size - 1; |
| 95 | + int32_t compare_second = number_of_elements - 1; |
| 96 | + int32_t mid = (compare_first_1 < compare_second) ? compare_first_1 : compare_second; |
| 97 | + |
| 98 | + int32_t compare_first_2 = left + (2 * current_size) - 1; |
| 99 | + int32_t right = (compare_first_2 < compare_second) ? compare_first_2 : compare_second; |
| 100 | + |
| 101 | + MergeHalves(elements, element_size, left, mid, right, compareFun); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments