libhatchet
Loading...
Searching...
No Matches
hxsort.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
11
12#include "libhatchet.h"
13
14// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
15#if HX_USE_MACROS_WITH_MODULE
16#error Header does not provide macros alone.
17#endif
18
19#include "hxkey.hpp"
20#include "hxrange.hpp"
21
22HX_NS_BEGIN_
23
24#include "detail/hxsort_detail.hpp"
25
33template<hxrandom_iterator_concept_ iterator_t_, typename less_t_>
35void hxinsertion_sort(iterator_t_ begin_, iterator_t_ end_, const less_t_& less_) {
36 // Address sanitizer: Avoids adding 1 to null iterators.
37 if(begin_ == end_) { return; }
38 const hxrestrict_t<iterator_t_> begin_r_(begin_);
39 for(iterator_t_ i_ = begin_r_, j_ = begin_r_ + ptrdiff_t{1}; j_ < end_; i_ = j_, ++j_) {
40 if(less_(*j_, *i_)) {
41 auto t_ = hxmove(*j_);
42 *j_ = hxmove(*i_);
43 while(begin_r_ < i_ && less_(t_, *(i_ - ptrdiff_t{1}))) {
44 *i_ = hxmove(*(i_ - ptrdiff_t{1}));
45 --i_;
46 }
47 *i_ = hxmove(t_);
48 }
49 }
50}
51
55template<hxsorted_iterator_concept_ iterator_t_>
57void hxinsertion_sort(iterator_t_ begin_, iterator_t_ end_) {
58 hxinsertion_sort<iterator_t_>(begin_, end_, hxkey_less_t<decltype(*begin_)>{});
59}
60
64template<hxrandom_iterator_concept_ iterator_t_, typename less_t_>
65hxinline hxconstexpr void hxheapsort(iterator_t_ begin_, iterator_t_ end_, const less_t_& less_) {
66 if(begin_ == end_) { return; } // Prevents UB.
67 hxdetail_::hxmake_heap_<iterator_t_>(begin_, end_, less_);
68 const hxrestrict_t<iterator_t_> begin_r_(begin_);
69 for(iterator_t_ it_ = end_ - ptrdiff_t{1}; begin_r_ < it_; --it_) {
70 hxswap(*begin_r_, *it_);
71 hxdetail_::hxheapsort_heapify_<iterator_t_>(begin_r_, begin_r_, it_, less_);
72 }
73}
74
77template<hxsorted_iterator_concept_ iterator_t_>
78hxinline hxconstexpr void hxheapsort(iterator_t_ begin_, iterator_t_ end_) {
79 hxheapsort<iterator_t_>(begin_, end_, hxkey_less_t<decltype(*begin_)>{});
80}
81
87template<hxrandom_iterator_concept_ iterator_t_, typename less_t_>
88hxinline hxconstexpr void hxsort(iterator_t_ begin_, iterator_t_ end_, const less_t_& less_) {
89 const hxsize_t size_ = end_ - begin_;
90 hxdetail_::hxintro_sort_<iterator_t_>(begin_, end_, less_,
91 size_ > 0 ? 2 * hxlog2i(static_cast<uint32_t>(size_)) : 0);
92}
93
94template<hxsorted_iterator_concept_ iterator_t_>
95hxinline hxconstexpr void hxsort(iterator_t_ begin_, iterator_t_ end_) {
96 const hxsize_t size_ = end_ - begin_;
97 hxdetail_::hxintro_sort_<iterator_t_>(begin_, end_, hxkey_less_t<decltype(*begin_)>{},
98 size_ > 0 ? 2 * hxlog2i(static_cast<uint32_t>(size_)) : 0);
99}
100
101HX_NS_END_
hxkey_less_t<T> - A constexpr callable that returns true if the first object is less than the second.
Definition hxkey.hpp:91
User-specializable key-equal, key-less, and key-hash callables.
Read-only range utilities for libhatchet.
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:125
#define hxattr_flatten
hxattr_flatten - Inline every call inside a function's body into it, recursively, regardless of the c...
Definition hxsettings.h:90
#define hxconstexpr
hxconstexpr - Enables C++23 compatable constexpr usage for functions as that has support for destruct...
Definition hxsettings.h:266
constexpr void hxsort(iterator_t_ begin_, iterator_t_ end_, const less_t_ &less_)
hxsort - A general purpose sort routine using T::T(&&), T::~T(), T::operator=(&&),...
Definition hxsort.hpp:88
constexpr void hxheapsort(iterator_t_ begin_, iterator_t_ end_, const less_t_ &less_)
hxheapsort - Sorts the elements in the range [begin, end) using the heapsort algorithm.
Definition hxsort.hpp:65
HX_NS_BEGIN_ constexpr void hxinsertion_sort(iterator_t_ begin_, iterator_t_ end_, const less_t_ &less_)
hxinsertion_sort - Sorts the elements in the range [begin, end) in comparison order using the inserti...
Definition hxsort.hpp:35
typename hxrestrict_t_< T >::type hxrestrict_t
hxrestrict_t - Adds the __restrict keyword to C++ pointers.
Definition hxutility.h:287
constexpr void hxswap(T &x_, T &y_)
hxswap - Exchanges the contents of x and y using a temporary.
Definition hxutility.h:414
constexpr hxremove_reference_t< T > && hxmove(T &&t_)
hxmove - Implements std::move.
Definition hxutility.h:407
int hxlog2i(uint32_t i_)
hxlog2i - Returns log2(n) as an integer which is the power of 2 of the largest bit in n.
Definition hxutility.h:312
Provides core macros, memory management and feature detection.
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:207