diff options
Diffstat (limited to 'src/VBox/Runtime/common/sort')
-rw-r--r-- | src/VBox/Runtime/common/sort/Makefile.kup | 0 | ||||
-rw-r--r-- | src/VBox/Runtime/common/sort/RTSortApvIsSorted.cpp | 58 | ||||
-rw-r--r-- | src/VBox/Runtime/common/sort/RTSortIsSorted.cpp | 60 | ||||
-rw-r--r-- | src/VBox/Runtime/common/sort/nocrt-bsearch.cpp | 80 | ||||
-rw-r--r-- | src/VBox/Runtime/common/sort/nocrt-qsort.cpp | 70 | ||||
-rw-r--r-- | src/VBox/Runtime/common/sort/nocrt-qsort_r.cpp | 56 | ||||
-rw-r--r-- | src/VBox/Runtime/common/sort/shellsort.cpp | 111 |
7 files changed, 435 insertions, 0 deletions
diff --git a/src/VBox/Runtime/common/sort/Makefile.kup b/src/VBox/Runtime/common/sort/Makefile.kup new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/src/VBox/Runtime/common/sort/Makefile.kup diff --git a/src/VBox/Runtime/common/sort/RTSortApvIsSorted.cpp b/src/VBox/Runtime/common/sort/RTSortApvIsSorted.cpp new file mode 100644 index 00000000..e20044c2 --- /dev/null +++ b/src/VBox/Runtime/common/sort/RTSortApvIsSorted.cpp @@ -0,0 +1,58 @@ +/* $Id: RTSortApvIsSorted.cpp $ */ +/** @file + * IPRT - RTSortApvIsSorted. + */ + +/* + * Copyright (C) 2010-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * 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, in version 3 of the + * License. + * + * 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 <https://www.gnu.org/licenses>. + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#include <iprt/sort.h> +#include "internal/iprt.h" + + +RTDECL(bool) RTSortApvIsSorted(void const * const *papvArray, size_t cElements, PFNRTSORTCMP pfnCmp, void *pvUser) +{ + if (cElements >= 2) + { + for (size_t i = 0; i < cElements - 1; i++) + { + if (pfnCmp(papvArray[i], papvArray[i + 1], pvUser) > 0) + return false; + } + } + return true; +} +RT_EXPORT_SYMBOL(RTSortIsSorted); + diff --git a/src/VBox/Runtime/common/sort/RTSortIsSorted.cpp b/src/VBox/Runtime/common/sort/RTSortIsSorted.cpp new file mode 100644 index 00000000..84ea7319 --- /dev/null +++ b/src/VBox/Runtime/common/sort/RTSortIsSorted.cpp @@ -0,0 +1,60 @@ +/* $Id: RTSortIsSorted.cpp $ */ +/** @file + * IPRT - RTSortIsSorted. + */ + +/* + * Copyright (C) 2010-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * 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, in version 3 of the + * License. + * + * 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 <https://www.gnu.org/licenses>. + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#include <iprt/sort.h> +#include "internal/iprt.h" + + +RTDECL(bool) RTSortIsSorted(void const *pvArray, size_t cElements, size_t cbElement, PFNRTSORTCMP pfnCmp, void *pvUser) +{ + if (cElements >= 2) + { + const char *pbArray = (const char *)pvArray; + for (size_t i = 0; i < cElements - 1; i++) + { + const char *pbElement1 = pbArray + i * cbElement; + if (pfnCmp(pbElement1, pbElement1 + cbElement, pvUser) > 0) + return false; + } + } + return true; +} +RT_EXPORT_SYMBOL(RTSortIsSorted); + diff --git a/src/VBox/Runtime/common/sort/nocrt-bsearch.cpp b/src/VBox/Runtime/common/sort/nocrt-bsearch.cpp new file mode 100644 index 00000000..746764d1 --- /dev/null +++ b/src/VBox/Runtime/common/sort/nocrt-bsearch.cpp @@ -0,0 +1,80 @@ +/* $Id: nocrt-bsearch.cpp $ */ +/** @file + * IPRT - No-CRT - bsearch(). + */ + +/* + * Copyright (C) 2022-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * 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, in version 3 of the + * License. + * + * 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 <https://www.gnu.org/licenses>. + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#define IPRT_NO_CRT_FOR_3RD_PARTY +#include "internal/nocrt.h" +#include <iprt/nocrt/stdlib.h> + + +void *RT_NOCRT(bsearch)(const void *pvKey, const void *pvBase, size_t cEntries, size_t cbEntry, + int (*pfnCompare)(const void *pvKey, const void *pvEntry)) +{ + if (cEntries > 0) + { /* likely */ } + else + return NULL; + + size_t iStart = 0; + size_t iEnd = cEntries; + for (;;) + { + size_t const i = (iEnd - iStart) / 2 + iStart; + const void * const pvEntry = (const char *)pvBase + cbEntry * i; + int const iDiff = pfnCompare(pvKey, pvEntry); + if (iDiff > 0) /* target is before i */ + { + if (i > iStart) + iEnd = i; + else + return NULL; + } + else if (iDiff) /* target is after i */ + { + if (i + 1 < iEnd) + iStart = i + 1; + else + return NULL; + } + else /* match */ + return (void *)pvEntry; + } +} +RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(bsearch); + diff --git a/src/VBox/Runtime/common/sort/nocrt-qsort.cpp b/src/VBox/Runtime/common/sort/nocrt-qsort.cpp new file mode 100644 index 00000000..cfe66fa0 --- /dev/null +++ b/src/VBox/Runtime/common/sort/nocrt-qsort.cpp @@ -0,0 +1,70 @@ +/* $Id: nocrt-qsort.cpp $ */ +/** @file + * IPRT - No-CRT - qsort(). + */ + +/* + * Copyright (C) 2022-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * 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, in version 3 of the + * License. + * + * 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 <https://www.gnu.org/licenses>. + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#define IPRT_NO_CRT_FOR_3RD_PARTY +#include "internal/nocrt.h" +#include <iprt/nocrt/stdlib.h> +#include <iprt/sort.h> + + +#if !defined(RT_ARCH_AMD64) /* ASSUMING the regular calling convention */ \ + && !defined(RT_ARCH_X86) /* ASSUMING __cdecl, won't work for __stdcall or __fastcall. */ +# define NEED_COMPARE_WRAPPER +/** @callback_method_impl{FNRTSORTCMP} */ +static int DECLCALLBACK(int) CompareWrapper(void const *pvElement1, void const *pvElement2, void *pvUser)) +{ + int (*pfnCompare)(const void *pv1, const void *pv2) = (int (*)(const void *pv1, const void *pv2))(uintptr_t)pvUser; + return pfnCompare(pvElement1, pvElement2); +} +#endif + +#undef qsort +void RT_NOCRT(qsort)(void *pvBase, size_t cEntries, size_t cbEntry, + int (*pfnCompare)(const void *pv1, const void *pv2)) +{ + /** @todo Implement and use RTSortQuick! */ +#ifdef NEED_COMPARE_WRAPPER + RTSortShell(pvBase, cEntries, cbEntry, CompareWrapper, (void *)(uintptr_t)pfnCompare); +#else + RTSortShell(pvBase, cEntries, cbEntry, (PFNRTSORTCMP)pfnCompare, NULL); +#endif +} +RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(qsort); + diff --git a/src/VBox/Runtime/common/sort/nocrt-qsort_r.cpp b/src/VBox/Runtime/common/sort/nocrt-qsort_r.cpp new file mode 100644 index 00000000..5d40e960 --- /dev/null +++ b/src/VBox/Runtime/common/sort/nocrt-qsort_r.cpp @@ -0,0 +1,56 @@ +/* $Id: nocrt-qsort_r.cpp $ */ +/** @file + * IPRT - No-CRT - qsort_r(). + */ + +/* + * Copyright (C) 2022-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * 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, in version 3 of the + * License. + * + * 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 <https://www.gnu.org/licenses>. + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#define IPRT_NO_CRT_FOR_3RD_PARTY +#include "internal/nocrt.h" +#include <iprt/nocrt/stdlib.h> +#include <iprt/sort.h> + + +#undef qsort_r +void RT_NOCRT(qsort_r)(void *pvBase, size_t cEntries, size_t cbEntry, + int (*pfnCompare)(const void *pv1, const void *pv2, void *pvUser), void *pvUser) +{ + /** @todo Implement and use RTSortQuick! */ + /* ASSUMES that we're using compatible calling conventions (x86: __cdecl, amd64: default). */ + RTSortShell(pvBase, cEntries, cbEntry, (PFNRTSORTCMP)pfnCompare, pvUser); +} +RT_ALIAS_AND_EXPORT_NOCRT_SYMBOL(qsort_r); + diff --git a/src/VBox/Runtime/common/sort/shellsort.cpp b/src/VBox/Runtime/common/sort/shellsort.cpp new file mode 100644 index 00000000..f0b067de --- /dev/null +++ b/src/VBox/Runtime/common/sort/shellsort.cpp @@ -0,0 +1,111 @@ +/* $Id: shellsort.cpp $ */ +/** @file + * IPRT - RTSortShell and RTSortApvShell. + */ + +/* + * Copyright (C) 2010-2023 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * 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, in version 3 of the + * License. + * + * 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 <https://www.gnu.org/licenses>. + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + + +/********************************************************************************************************************************* +* Header Files * +*********************************************************************************************************************************/ +#include <iprt/sort.h> + +#include <iprt/alloca.h> +#include <iprt/assert.h> +#include <iprt/string.h> + + + +RTDECL(void) RTSortShell(void *pvArray, size_t cElements, size_t cbElement, PFNRTSORTCMP pfnCmp, void *pvUser) +{ + Assert(cbElement <= 128); + + /* Anything worth sorting? */ + if (cElements < 2) + return; + + uint8_t *pbArray = (uint8_t *)pvArray; + void *pvTmp = alloca(cbElement); + size_t cGap = (cElements + 1) / 2; + while (cGap > 0) + { + size_t i; + for (i = cGap; i < cElements; i++) + { + memcpy(pvTmp, &pbArray[i * cbElement], cbElement); + size_t j = i; + while ( j >= cGap + && pfnCmp(&pbArray[(j - cGap) * cbElement], pvTmp, pvUser) > 0) + { + memmove(&pbArray[j * cbElement], &pbArray[(j - cGap) * cbElement], cbElement); + j -= cGap; + } + memcpy(&pbArray[j * cbElement], pvTmp, cbElement); + } + + /* This does not generate the most optimal gap sequence, but it has the + advantage of being simple and avoid floating point. */ + cGap /= 2; + } +} + + +RTDECL(void) RTSortApvShell(void **papvArray, size_t cElements, PFNRTSORTCMP pfnCmp, void *pvUser) +{ + /* Anything worth sorting? */ + if (cElements < 2) + return; + + size_t cGap = (cElements + 1) / 2; + while (cGap > 0) + { + size_t i; + for (i = cGap; i < cElements; i++) + { + void *pvTmp = papvArray[i]; + size_t j = i; + while ( j >= cGap + && pfnCmp(papvArray[j - cGap], pvTmp, pvUser) > 0) + { + papvArray[j] = papvArray[j - cGap]; + j -= cGap; + } + papvArray[j] = pvTmp; + } + + /* This does not generate the most optimal gap sequence, but it has the + advantage of being simple and avoid floating point. */ + cGap /= 2; + } +} + |