xref: /llvm-project/libc/src/stdlib/qsort_r.cpp (revision a738d81cd2822698539b0482af48d49d91ea5a2e)
1d3074f16SMichael Jones //===-- Implementation of qsort_r -----------------------------------------===//
2d3074f16SMichael Jones //
3d3074f16SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4d3074f16SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5d3074f16SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d3074f16SMichael Jones //
7d3074f16SMichael Jones //===----------------------------------------------------------------------===//
8d3074f16SMichael Jones 
9d3074f16SMichael Jones #include "src/stdlib/qsort_r.h"
10d3074f16SMichael Jones #include "src/__support/common.h"
115ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
12d3074f16SMichael Jones #include "src/stdlib/qsort_util.h"
13d3074f16SMichael Jones 
14d3074f16SMichael Jones #include <stdint.h>
15d3074f16SMichael Jones 
165ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
17d3074f16SMichael Jones 
18d3074f16SMichael Jones LLVM_LIBC_FUNCTION(void, qsort_r,
19d3074f16SMichael Jones                    (void *array, size_t array_size, size_t elem_size,
20d3074f16SMichael Jones                     int (*compare)(const void *, const void *, void *),
21d3074f16SMichael Jones                     void *arg)) {
22a6d2da8bSlntue 
23*a738d81cSLukas Bergdoll   const auto is_less = [compare, arg](const void *a, const void *b) -> bool {
24*a738d81cSLukas Bergdoll     return compare(a, b, arg) < 0;
25*a738d81cSLukas Bergdoll   };
26*a738d81cSLukas Bergdoll 
27*a738d81cSLukas Bergdoll   internal::unstable_sort(array, array_size, elem_size, is_less);
28d3074f16SMichael Jones }
29d3074f16SMichael Jones 
305ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
31