Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::sort_utils_detail Namespace Reference

Typedefs

template<typename IntT >
using counting_unsigned_t = std::make_unsigned_t< std::remove_cv_t< IntT > >
 Unsigned counterpart used to measure counting-sort key ranges.
 
template<typename IntT >
using radix_unsigned_t = std::make_unsigned_t< std::remove_cv_t< IntT > >
 Unsigned key type used by radix sort passes.
 

Functions

template<template< typename > class C, typename T , class Compare >
void median_of_three_to_last (C< T > &a, const long l, const long r, const Compare &cmp)
 Put in order a(l), a(m) and a(r), with m the middle of [l, r], and move their median to a(r), where partition() takes its pivot.
 
template<typename IntT >
size_t counting_bucket_count (const IntT min_key, const IntT max_key)
 Compute the number of buckets required for a counting sort range.
 
template<typename IntT >
size_t counting_bucket (const IntT value, const IntT min_key) noexcept
 Map a value to its bucket index in counting sort.
 
template<typename IntT >
IntT counting_value_from_bucket (const size_t bucket, const IntT min_key) noexcept
 Retrieve the original value from a bucket index in counting sort.
 
template<template< typename > class C, typename IntT >
requires IntegerSortableValue<IntT>
void counting_sort_impl (C< IntT > &a)
 Internal implementation of counting sort for containers.
 
template<typename IntT >
requires IntegerSortableValue<IntT>
void counting_sort_impl (IntT *a, const size_t n)
 Internal implementation of counting sort for raw pointer ranges.
 
template<typename IntT >
requires IntegerSortableValue<IntT>
void counting_sort_impl (DynList< IntT > &list)
 Internal counting-sort implementation for DynList.
 
template<typename IntT >
requires IntegerSortableValue<IntT>
void counting_sort_impl (DynDlist< IntT > &list)
 Internal counting-sort implementation for DynDlist.
 
template<typename IntT >
constexpr radix_unsigned_t< IntT > radix_key (const IntT value) noexcept
 Map an integer value to an unsigned radix key.
 
template<template< typename > class C, typename IntT >
requires IntegerSortableValue<IntT>
void radix_sort_impl (C< IntT > &a)
 Internal radix-sort implementation for array-like containers.
 
template<typename IntT >
requires IntegerSortableValue<IntT>
void radix_sort_impl (IntT *a, const size_t n)
 Core radix sort implementation for raw pointer ranges.
 
template<typename IntT >
requires IntegerSortableValue<IntT>
void radix_sort_impl (DynList< IntT > &list)
 LSD radix sort implementation for DynList.
 
template<typename IntT >
requires IntegerSortableValue<IntT>
void radix_sort_impl (DynDlist< IntT > &list)
 LSD radix sort implementation for DynDlist.
 

Variables

constexpr long Median_Of_Three_Min_Size = 7
 Ranges with fewer items than this one keep their last item as the pivot of partition(): there the median of three costs more than what it saves.
 

Typedef Documentation

◆ counting_unsigned_t

template<typename IntT >
using Aleph::sort_utils_detail::counting_unsigned_t = typedef std::make_unsigned_t<std::remove_cv_t<IntT> >

Unsigned counterpart used to measure counting-sort key ranges.

The cv-qualifiers are removed before mapping to the corresponding unsigned integer type. This lets the range arithmetic work uniformly for signed and unsigned integral keys.

Template Parameters
IntTIntegral key type.

Definition at line 5307 of file tpl_sort_utils.H.

◆ radix_unsigned_t

template<typename IntT >
using Aleph::sort_utils_detail::radix_unsigned_t = typedef std::make_unsigned_t<std::remove_cv_t<IntT> >

Unsigned key type used by radix sort passes.

Radix sort works on unsigned byte digits. Signed values are first mapped to this unsigned type with radix_key() so their natural signed order is preserved.

Template Parameters
IntTIntegral value type.

Definition at line 5318 of file tpl_sort_utils.H.

Function Documentation

◆ counting_bucket()

template<typename IntT >
size_t Aleph::sort_utils_detail::counting_bucket ( const IntT  value,
const IntT  min_key 
)
inlinenoexcept

Map a value to its bucket index in counting sort.

Template Parameters
IntTInteger type.
Parameters
valueValue to map.
min_keyMinimum key in the range.
Returns
Bucket index in [0, bucket_count).

Definition at line 5355 of file tpl_sort_utils.H.

References value.

Referenced by counting_sort_impl(), counting_sort_impl(), counting_sort_impl(), and counting_sort_impl().

◆ counting_bucket_count()

template<typename IntT >
size_t Aleph::sort_utils_detail::counting_bucket_count ( const IntT  min_key,
const IntT  max_key 
)

Compute the number of buckets required for a counting sort range.

Template Parameters
IntTInteger type.
Parameters
min_keyMinimum value in the range.
max_keyMaximum value in the range.
Returns
Total number of buckets needed.
Exceptions
ah_domain_errorif max_key < min_key.
ah_runtime_errorif the range exceeds size_t capacity.

Definition at line 5330 of file tpl_sort_utils.H.

References ah_domain_error_if, and ah_runtime_error_unless.

Referenced by counting_sort_impl(), counting_sort_impl(), counting_sort_impl(), and counting_sort_impl().

◆ counting_sort_impl() [1/4]

template<template< typename > class C, typename IntT >
requires IntegerSortableValue<IntT>
void Aleph::sort_utils_detail::counting_sort_impl ( C< IntT > &  a)

Internal implementation of counting sort for containers.

Template Parameters
CContainer template type.
IntTInteger type.
Parameters
[in,out]aContainer to sort.

Definition at line 5383 of file tpl_sort_utils.H.

References Aleph::count(), counting_bucket(), counting_bucket_count(), Aleph::Array< T >::create(), and k.

Referenced by Aleph::counting_sort(), Aleph::counting_sort(), Aleph::counting_sort(), Aleph::counting_sort(), and Aleph::counting_sort().

◆ counting_sort_impl() [2/4]

template<typename IntT >
requires IntegerSortableValue<IntT>
void Aleph::sort_utils_detail::counting_sort_impl ( DynDlist< IntT > &  list)

Internal counting-sort implementation for DynDlist.

Rewrites node values in sorted order without reallocating or relinking the doubly-linked list nodes.

Template Parameters
IntTInteger type accepted by CountingSortable.
Parameters
[in,out]listList whose values are sorted in place.

Definition at line 5529 of file tpl_sort_utils.H.

References Aleph::count(), counting_bucket(), counting_bucket_count(), counting_value_from_bucket(), Aleph::Array< T >::create(), Aleph::DynDlist< T >::Iterator::get_curr_ne(), Aleph::Dlink::Iterator::has_curr(), k, Aleph::DynDlist< T >::Iterator::next_ne(), out, Aleph::DynDlist< T >::size(), and value.

◆ counting_sort_impl() [3/4]

template<typename IntT >
requires IntegerSortableValue<IntT>
void Aleph::sort_utils_detail::counting_sort_impl ( DynList< IntT > &  list)

Internal counting-sort implementation for DynList.

Rewrites node values in sorted order without reallocating or relinking the list nodes.

Template Parameters
IntTInteger type accepted by CountingSortable.
Parameters
[in,out]listList whose values are sorted in place.

Definition at line 5477 of file tpl_sort_utils.H.

References Aleph::count(), counting_bucket(), counting_bucket_count(), counting_value_from_bucket(), Aleph::Array< T >::create(), Aleph::DynList< T >::Iterator::get_curr_ne(), Aleph::HTList::Iterator::has_curr(), k, Aleph::HTList::Iterator::next_ne(), out, Aleph::DynList< T >::size(), and value.

◆ counting_sort_impl() [4/4]

template<typename IntT >
requires IntegerSortableValue<IntT>
void Aleph::sort_utils_detail::counting_sort_impl ( IntT *  a,
const size_t  n 
)

Internal implementation of counting sort for raw pointer ranges.

Sorts a[0..n) in nondecreasing order using a stable counting-sort pass over the observed value range.

Template Parameters
IntTInteger type accepted by CountingSortable.
Parameters
[in,out]aPointer to the first element in the range.
[in]nNumber of elements in the range.

Definition at line 5431 of file tpl_sort_utils.H.

References Aleph::count(), counting_bucket(), counting_bucket_count(), Aleph::Array< T >::create(), and k.

◆ counting_value_from_bucket()

template<typename IntT >
IntT Aleph::sort_utils_detail::counting_value_from_bucket ( const size_t  bucket,
const IntT  min_key 
)
inlinenoexcept

Retrieve the original value from a bucket index in counting sort.

Template Parameters
IntTInteger type.
Parameters
bucketBucket index.
min_keyMinimum key in the range.
Returns
The original value corresponding to the bucket.

Definition at line 5369 of file tpl_sort_utils.H.

Referenced by counting_sort_impl(), and counting_sort_impl().

◆ median_of_three_to_last()

template<template< typename > class C, typename T , class Compare >
void Aleph::sort_utils_detail::median_of_three_to_last ( C< T > &  a,
const long  l,
const long  r,
const Compare &  cmp 
)
inline

Put in order a(l), a(m) and a(r), with m the middle of [l, r], and move their median to a(r), where partition() takes its pivot.

The least of the three stays at a(l) and the greatest goes to a(m), so that a sorted or a reversed range splits in halves; with the last item alone as the pivot, they split one item at a time, and moving the median alone to a(r) left the least in the middle, so that a reversed range did. A range of fewer than Median_Of_Three_Min_Size items is left as it is.

Parameters
aThe container.
lLeft index.
rRight index.
cmpComparison functor.

Definition at line 3085 of file tpl_sort_utils.H.

References cmp(), l, m, Median_Of_Three_Min_Size, and r.

Referenced by Aleph::introsort_loop(), Aleph::partition_op_impl(), Aleph::quicksort(), and Aleph::quicksort_rec().

◆ radix_key()

template<typename IntT >
constexpr radix_unsigned_t< IntT > Aleph::sort_utils_detail::radix_key ( const IntT  value)
constexprnoexcept

Map an integer value to an unsigned radix key.

Unsigned values are returned unchanged. Signed values are biased by flipping the sign bit, which makes their unsigned byte order match the natural signed order required by radix sort.

Template Parameters
IntTInteger type.
Parameters
valueValue to map.
Returns
Unsigned key used by radix passes.

Definition at line 5582 of file tpl_sort_utils.H.

References value.

◆ radix_sort_impl() [1/4]

template<template< typename > class C, typename IntT >
requires IntegerSortableValue<IntT>
void Aleph::sort_utils_detail::radix_sort_impl ( C< IntT > &  a)

Internal radix-sort implementation for array-like containers.

Performs a stable least-significant-byte radix sort using a fixed 256-bucket counting pass for each byte of the unsigned radix key.

Template Parameters
CAleph array-like container template.
IntTInteger type accepted by RadixSortable.
Parameters
[in,out]aContainer sorted in place.

Definition at line 5602 of file tpl_sort_utils.H.

References Aleph::count(), and Aleph::Array< T >::create().

Referenced by Aleph::radix_sort(), Aleph::radix_sort(), Aleph::radix_sort(), Aleph::radix_sort(), and Aleph::radix_sort().

◆ radix_sort_impl() [2/4]

template<typename IntT >
requires IntegerSortableValue<IntT>
void Aleph::sort_utils_detail::radix_sort_impl ( DynDlist< IntT > &  list)

LSD radix sort implementation for DynDlist.

Performs a stable radix sort on a doubly-linked list.

Template Parameters
IntTInteger type.
Parameters
[in,out]listThe doubly-linked list to sort.

Definition at line 5738 of file tpl_sort_utils.H.

References Aleph::Array< T >::append(), Aleph::Dnode< T >::get_data(), Aleph::Dnode< T >::remove_first_ne(), Aleph::Array< T >::reserve(), and Aleph::DynDlist< T >::size().

◆ radix_sort_impl() [3/4]

template<typename IntT >
requires IntegerSortableValue<IntT>
void Aleph::sort_utils_detail::radix_sort_impl ( DynList< IntT > &  list)

LSD radix sort implementation for DynList.

Performs a stable radix sort on a linked list. This implementation is memory efficient as it reuses the original list nodes.

Template Parameters
IntTInteger type.
Parameters
[in,out]listThe list to sort.

Definition at line 5699 of file tpl_sort_utils.H.

References Aleph::Array< T >::append(), Aleph::HTList::is_empty(), Aleph::Array< T >::reserve(), and Aleph::DynList< T >::size().

◆ radix_sort_impl() [4/4]

template<typename IntT >
requires IntegerSortableValue<IntT>
void Aleph::sort_utils_detail::radix_sort_impl ( IntT *  a,
const size_t  n 
)

Core radix sort implementation for raw pointer ranges.

Uses counting sort as a stable subroutine to sort by each byte digit.

Template Parameters
IntTInteger type.
Parameters
[in,out]aPointer to the array.
[in]nNumber of elements.

Definition at line 5651 of file tpl_sort_utils.H.

References Aleph::count(), and Aleph::Array< T >::create().

Variable Documentation

◆ Median_Of_Three_Min_Size

constexpr long Aleph::sort_utils_detail::Median_Of_Three_Min_Size = 7
inlineconstexpr

Ranges with fewer items than this one keep their last item as the pivot of partition(): there the median of three costs more than what it saves.

Definition at line 3066 of file tpl_sort_utils.H.

Referenced by median_of_three_to_last().