-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][stdlib] Implement heap sort. #98582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//===-- Implementation of heap sort -----------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_STDLIB_HEAP_SORT_H | ||
#define LLVM_LIBC_SRC_STDLIB_HEAP_SORT_H | ||
|
||
#include "src/__support/CPP/cstddef.h" | ||
#include "src/stdlib/qsort_data.h" | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace internal { | ||
|
||
// A simple in-place heapsort implementation. | ||
// Follow the implementation in https://en.wikipedia.org/wiki/Heapsort. | ||
|
||
LIBC_INLINE void heap_sort(const Array &array) { | ||
size_t end = array.size(); | ||
size_t start = end / 2; | ||
|
||
auto left_child = [](size_t i) -> size_t { return 2 * i + 1; }; | ||
|
||
while (end > 1) { | ||
if (start > 0) { | ||
// Select the next unheapified element to sift down. | ||
--start; | ||
} else { | ||
// Extract the max element of the heap, moving a leaf to root to be sifted | ||
// down. | ||
--end; | ||
lntue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
array.swap(0, end); | ||
} | ||
|
||
// Sift start down the heap. | ||
size_t root = start; | ||
lntue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
while (left_child(root) < end) { | ||
size_t child = left_child(root); | ||
// If there are two children, set child to the greater. | ||
if (child + 1 < end && | ||
lntue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
array.elem_compare(child, array.get(child + 1)) < 0) | ||
++child; | ||
|
||
// If the root is less than the greater child | ||
if (array.elem_compare(root, array.get(child)) >= 0) | ||
lntue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
|
||
// Swap the root with the greater child and continue sifting down. | ||
array.swap(root, child); | ||
lntue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
root = child; | ||
} | ||
} | ||
} | ||
|
||
} // namespace internal | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_STDLIB_HEAP_SORT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
//===-- Data structures for sorting routines --------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_STDLIB_QSORT_DATA_H | ||
#define LLVM_LIBC_SRC_STDLIB_QSORT_DATA_H | ||
|
||
#include "src/__support/CPP/cstddef.h" | ||
#include "src/__support/macros/config.h" | ||
|
||
#include <stdint.h> | ||
|
||
namespace LIBC_NAMESPACE_DECL { | ||
namespace internal { | ||
|
||
using Compare = int(const void *, const void *); | ||
using CompareWithState = int(const void *, const void *, void *); | ||
|
||
enum class CompType { COMPARE, COMPARE_WITH_STATE }; | ||
|
||
struct Comparator { | ||
union { | ||
Compare *comp_func; | ||
CompareWithState *comp_func_r; | ||
}; | ||
const CompType comp_type; | ||
|
||
void *arg; | ||
|
||
Comparator(Compare *func) | ||
: comp_func(func), comp_type(CompType::COMPARE), arg(nullptr) {} | ||
|
||
Comparator(CompareWithState *func, void *arg_val) | ||
: comp_func_r(func), comp_type(CompType::COMPARE_WITH_STATE), | ||
arg(arg_val) {} | ||
|
||
#if defined(__clang__) | ||
// Recent upstream changes to -fsanitize=function find more instances of | ||
// function type mismatches. One case is with the comparator passed to this | ||
// class. Libraries will tend to pass comparators that take pointers to | ||
// varying types while this comparator expects to accept const void pointers. | ||
// Ideally those tools would pass a function that strictly accepts const | ||
// void*s to avoid UB, or would use qsort_r to pass their own comparator. | ||
[[clang::no_sanitize("function")]] | ||
#endif | ||
int comp_vals(const void *a, const void *b) const { | ||
if (comp_type == CompType::COMPARE) { | ||
return comp_func(a, b); | ||
} else { | ||
return comp_func_r(a, b, arg); | ||
} | ||
} | ||
}; | ||
|
||
class Array { | ||
uint8_t *array; | ||
size_t array_size; | ||
size_t elem_size; | ||
Comparator compare; | ||
|
||
public: | ||
Array(uint8_t *a, size_t s, size_t e, Comparator c) | ||
: array(a), array_size(s), elem_size(e), compare(c) {} | ||
|
||
uint8_t *get(size_t i) const { return array + i * elem_size; } | ||
|
||
void swap(size_t i, size_t j) const { | ||
uint8_t *elem_i = get(i); | ||
uint8_t *elem_j = get(j); | ||
for (size_t b = 0; b < elem_size; ++b) { | ||
uint8_t temp = elem_i[b]; | ||
elem_i[b] = elem_j[b]; | ||
elem_j[b] = temp; | ||
} | ||
} | ||
|
||
int elem_compare(size_t i, const uint8_t *other) const { | ||
// An element must compare equal to itself so we don't need to consult the | ||
// user provided comparator. | ||
if (get(i) == other) | ||
return 0; | ||
return compare.comp_vals(get(i), other); | ||
} | ||
|
||
size_t size() const { return array_size; } | ||
|
||
// Make an Array starting at index |i| and size |s|. | ||
Array make_array(size_t i, size_t s) const { | ||
return Array(get(i), s, elem_size, compare); | ||
} | ||
}; | ||
|
||
using SortingRoutine = void(const Array &); | ||
|
||
} // namespace internal | ||
} // namespace LIBC_NAMESPACE_DECL | ||
|
||
#endif // LLVM_LIBC_SRC_STDLIB_QSORT_DATA_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.