From 19661fe98144f34deb6092eba158450368af91eb Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 14 Feb 2022 12:32:34 +1100 Subject: [PATCH] AP_Common: added uint16_t sorting code also added test suite --- libraries/AP_Common/sorting.cpp | 109 ++++++++++++++ libraries/AP_Common/sorting.h | 44 ++++++ libraries/AP_Common/tests/test_sorting.cpp | 156 +++++++++++++++++++++ 3 files changed, 309 insertions(+) create mode 100644 libraries/AP_Common/sorting.cpp create mode 100644 libraries/AP_Common/sorting.h create mode 100644 libraries/AP_Common/tests/test_sorting.cpp diff --git a/libraries/AP_Common/sorting.cpp b/libraries/AP_Common/sorting.cpp new file mode 100644 index 0000000000..cc68563696 --- /dev/null +++ b/libraries/AP_Common/sorting.cpp @@ -0,0 +1,109 @@ +/* + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +#include +#include "sorting.h" + +/* + in-place insertion sort for small arrays of data. This is O(n) if + already sorted and O(n^2) for worst case (elements are reversed) + sort order is smallest first + */ +void insertion_sort_uint16(uint16_t *data, uint16_t n) +{ + for (uint16_t i=1; i= 0 && data[j] > temp) { + data[j+1] = data[j]; + j--; + } + data[j+1] = temp; + } +} + +/* + remove duplicates from a sorted uint16_t array, returning the new + count + */ +uint16_t remove_duplicates_uint16(uint16_t *data, uint16_t n) +{ + uint16_t removed = 0; + for (uint16_t i=1; i data[mid]) { + low = mid+1; + continue; + } + return true; + } + return data[low] == value; +} + +/* + remove elements in a 2nd sorted array from a sorted uint16_t array + return the number of remaining elements + */ +uint16_t remove_list_uint16(uint16_t *data, uint16_t n, const uint16_t *rem, uint16_t n2) +{ + uint16_t removed = 0; + for (uint16_t i=0; i. + */ + +/* + in-place insertion sort for small arrays of data. This is O(n) if + already sorted and O(n^2) for worst case (elements are reversed) + sort order is smallest first + */ +void insertion_sort_uint16(uint16_t *data, uint16_t n); + +/* + remove duplicates from a sorted uint16_t array, returning the new + count + */ +uint16_t remove_duplicates_uint16(uint16_t *data, uint16_t n); + +/* + bisection search on a sorted uint16_t array to find an element + return true if found +*/ +bool bisect_search_uint16(const uint16_t *data, uint16_t n, uint16_t value); + +/* + remove elements in a 2nd sorted array from a sorted uint16_t array + return the number of remaining elements + */ +uint16_t remove_list_uint16(uint16_t *data, uint16_t n, const uint16_t *rem, uint16_t n2); + +/* + return number of common elements between two sorted uint16_t lists + */ +uint16_t common_list_uint16(uint16_t *data, uint16_t n, const uint16_t *rem, uint16_t n2); diff --git a/libraries/AP_Common/tests/test_sorting.cpp b/libraries/AP_Common/tests/test_sorting.cpp new file mode 100644 index 0000000000..d44f3b14dc --- /dev/null +++ b/libraries/AP_Common/tests/test_sorting.cpp @@ -0,0 +1,156 @@ +#include + +/* + tests for AP_Common/sorting.cpp + */ + +#include +#include +#include + +#if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX + +static int comp16(const uint16_t *v1, const uint16_t *v2) { + return int32_t(*v1) - int32_t(*v2); +} + +static void check_equal(const uint16_t *a1, const uint16_t *a2, uint16_t n) +{ + for (uint8_t j=0; j