ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Sort.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Sort.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
115/*--------------------------------------------------------------------------*/
116/*----------------------------- DEFINITIONS --------------------------------*/
117/*--------------------------------------------------------------------------*/
118
119#ifndef __ADS_Sort
120 #define __ADS_Sort
121/* self-identification: #endif constant */
122
123/*--------------------------------------------------------------------------*/
124/*------------------------------ INCLUDES ----------------------------------*/
125/*--------------------------------------------------------------------------*/
126
127#include "ADS/Contract.h"
128#include "ADS/memory/Prefetch.h"
130#include "ADS/sort/RadixSort.h"
131#include "ADS/sort/RunSorts.h"
132#include "ADS/sort/SampleSort.h"
133#include "ADS/sort/Sorted.h"
134#include "ADS/sort/StringSort.h"
136
137#include <algorithm>
138#include <array>
139#include <cstddef>
140#include <cstdint>
141#include <cstring>
142#include <functional>
143#include <memory>
144#include <string>
145#include <tuple>
146#include <type_traits>
147#include <utility>
148#include <vector>
149
150/*--------------------------------------------------------------------------*/
151/*--------------------------- NAMESPACE ------------------------------------*/
152/*--------------------------------------------------------------------------*/
153
154namespace ADS
155{
156
158inline constexpr std::size_t kRadix = 512;
159
161inline constexpr std::size_t kStrings = 64;
162
164inline constexpr std::size_t kRuns = 16;
165
208template< SortStore Store ,
209 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
210 class C = std::less<> >
211void sort( Store && A , C less = C() );
212
214template< class T , class C = std::less<> >
215void sort( std::vector< T > & A , C less = C() );
216
244template< SortStore Store ,
245 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
246 class C = std::less<> >
247void stableSort( Store && A , C less = C() );
248
250template< class T , class C = std::less<> >
251void stableSort( std::vector< T > & A , C less = C() );
252
253/*--------------------------------------------------------------------------*/
254/*------------------------- THE SORTS OF A RANGE ---------------------------*/
255/*--------------------------------------------------------------------------*/
256
261template< SortRange R , class... P >
262 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
263 P &&... p )
264{ sort( v , std::forward< P >( p )... ); }
265decltype( auto ) sort( R && A , P &&... p )
266{
267 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
268 return( sort( v , std::forward< P >( p )... ) );
269 };
270 return( detail::sortRange( A , f ) );
271 }
272
277template< SortRange R , class... P >
278 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
279 P &&... p )
280{ stableSort( v , std::forward< P >( p )... ); }
281decltype( auto ) stableSort( R && A , P &&... p )
282{
283 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
284 return( stableSort( v , std::forward< P >( p )... ) );
285 };
286 return( detail::sortRange( A , f ) );
287 }
288
289/*--------------------------------------------------------------------------*/
290
291} // end( namespace ADS )
292
293/*--------------------------------------------------------------------------*/
294/*-------------------------- IMPLEMENTATION --------------------------------*/
295/*--------------------------------------------------------------------------*/
296
297#include "ADS/sort/impl/Sort.tpp"
298
299/*--------------------------------------------------------------------------*/
300
301#endif /* Sort.h included */
302
303/*--------------------------------------------------------------------------*/
304/*---------------------------- End File Sort.h -----------------------------*/
305/*--------------------------------------------------------------------------*/
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 ...
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
The sorts by the digits of the keys, beyond the book (which has Counting Sort and Pigeonhole Sort,...
The natural merge sort that adapts to the runs of the input, beyond the book [see naturalMergeSort() ...
The in-place super scalar samplesort, beyond the book: sampleSort() splits the vector into up to 256 ...
The checks of the contracts of the sorts [see Contract.h]: a copy of the input kept only when the aud...
The sort of strings by integer keys of their prefixes, beyond the book: prefixKeySort() reads each st...
The quicksort in the lanes of the vector registers, beyond the book: vectorQuickSort() sorts integers...
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 sort(E &ex, Store &&A, C less=C())
the vector sorted by less on the threads of ex, not necessarily stable
void stableSort(E &ex, Store &&A, C less=C())
the vector sorted by less on the threads of ex, the equal items in their order
constexpr std::size_t kRadix
the items from which the numbers go to radixSort()
Definition Sort.h:158
constexpr std::size_t kStrings
the items from which the strings go to the sorts by characters
Definition Sort.h:161
constexpr std::size_t kRuns
the runs up to which the numbers and the strings go to powerSort()
Definition Sort.h:164
Definition ADT.h:96