ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
RadixSort.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File RadixSort.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
85/*--------------------------------------------------------------------------*/
86/*----------------------------- DEFINITIONS --------------------------------*/
87/*--------------------------------------------------------------------------*/
88
89#ifndef __ADS_RadixSort
90 #define __ADS_RadixSort
91/* self-identification: #endif constant */
92
93/*--------------------------------------------------------------------------*/
94/*------------------------------ INCLUDES ----------------------------------*/
95/*--------------------------------------------------------------------------*/
96
97#include "ADS/Contract.h"
101#include "ADS/sort/Sorted.h"
103
104#include <algorithm>
105#include <array>
106#include <bit>
107#include <cstddef>
108#include <cstdint>
109#include <cstring>
110#include <functional>
111#include <memory>
112#include <string>
113#include <type_traits>
114#include <utility>
115#include <vector>
116
117/*--------------------------------------------------------------------------*/
118/*--------------------------- NAMESPACE ------------------------------------*/
119/*--------------------------------------------------------------------------*/
120
121namespace ADS
122{
123
125template< class T >
126concept RadixKey =
127 ( std::is_integral_v< T > || std::is_floating_point_v< T > ) &&
128 ( sizeof( T ) <= 8 );
129
134template< RadixKey T >
135[[nodiscard]] auto radixKey( T x );
136
166template< SortStore Store , RadixKey T = std::ranges::range_value_t<
167 std::remove_cvref_t< Store > > >
168void radixSort( Store && A );
169
171template< RadixKey T >
172void radixSort( std::vector< T > & A );
173
180template< SortStore Store ,
181 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
182 class K >
183void radixSortBy( Store && A , K key );
184
186template< class T , class K >
187void radixSortBy( std::vector< T > & A , K key );
188
192inline constexpr std::size_t kFlagSmall = kVectorSort ? 8192 : 256;
193
240template< SortStore Store , RadixKey T = std::ranges::range_value_t<
241 std::remove_cvref_t< Store > > >
242void americanFlagSort( Store && A );
243
245template< RadixKey T >
246void americanFlagSort( std::vector< T > & A );
247
276template< StringStore Store >
277void msdRadixSort( Store && A );
278
280inline void msdRadixSort( std::vector< std::string > & A );
281
282/*--------------------------------------------------------------------------*/
283/*------------------------- THE SORTS OF A RANGE ---------------------------*/
284/*--------------------------------------------------------------------------*/
285
290template< SortRange R , class... P >
291 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
292 P &&... p )
293{ radixSort( v , std::forward< P >( p )... ); }
294decltype( auto ) radixSort( R && A , P &&... p )
295{
296 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
297 return( radixSort( v , std::forward< P >( p )... ) );
298 };
299 return( detail::sortRange( A , f ) );
300 }
301
306template< SortRange R , class... P >
307 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
308 P &&... p )
309{
310 radixSortBy( v , std::forward< P >( p )... );
311 }
312decltype( auto ) radixSortBy( R && A , P &&... p )
313{
314 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
315 return( radixSortBy( v , std::forward< P >( p )... ) );
316 };
317 return( detail::sortRange( A , f ) );
318 }
319
324template< SortRange R , class... P >
325 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
326 P &&... p )
327{
328 americanFlagSort( v , std::forward< P >( p )... );
329 }
330decltype( auto ) americanFlagSort( R && A , P &&... p )
331{
332 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
333 return( americanFlagSort( v , std::forward< P >( p )... ) );
334 };
335 return( detail::sortRange( A , f ) );
336 }
337
342template< SortRange R , class... P >
343 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
344 P &&... p )
345{
346 msdRadixSort( v , std::forward< P >( p )... );
347 }
348decltype( auto ) msdRadixSort( R && A , P &&... p )
349{
350 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
351 return( msdRadixSort( v , std::forward< P >( p )... ) );
352 };
353 return( detail::sortRange( A , f ) );
354 }
355
356/*--------------------------------------------------------------------------*/
357
358} // end( namespace ADS )
359
360/*--------------------------------------------------------------------------*/
361/*-------------------------- IMPLEMENTATION --------------------------------*/
362/*--------------------------------------------------------------------------*/
363
364#include "ADS/sort/impl/RadixSort.tpp"
365
366/*--------------------------------------------------------------------------*/
367
368#endif /* RadixSort.h included */
369
370/*--------------------------------------------------------------------------*/
371/*-------------------------- End File RadixSort.h --------------------------*/
372/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The quicksorts beyond the quicksort of Montresor's book [see QuickSort.h for it and its improvements ...
Insertion Sort (section 2.7.2), as one sorts a hand of cards: is inserted in the sorted prefix ,...
The room of the parallel algorithms, beyond the book: the buffers of items that the parallel sorts a...
The checks of the contracts of the sorts [see Contract.h]: a copy of the input kept only when the aud...
The quicksort in the lanes of the vector registers, beyond the book: vectorQuickSort() sorts integers...
an integer or a floating point number of at most 8 bytes
Definition RadixSort.h:126
a range that the sorts take beside std::vector and std::span: random access, sized,...
Definition Sorted.h:153
the storage on which the sorts work in place, by reference: a std::vector or a std::span of its items...
Definition Sorted.h:178
void msdRadixSort(Store &&A)
the MSD radix sort of strings
constexpr std::size_t kFlagSmall
the groups that americanFlagSort() leaves to a sort by comparisons are those of fewer items than this...
Definition RadixSort.h:192
void radixSortBy(Store &&A, K key)
the LSD radix sort of any items by the unsigned key( x ), stable
void americanFlagSort(Store &&A)
the MSD radix sort of numbers in place
void radixSort(Store &&A)
the LSD radix sort of numbers
auto radixKey(T x)
the unsigned key of , whose order is the order of the numbers: the bits of with the sign bit flipped...
constexpr bool kVectorSort
true if vectorQuickSort() runs in the vectors of the processor, by the quicksort of Highway,...
Definition VectorQuickSort.h:173
Definition ADT.h:96