ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SortingNetworks.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File SortingNetworks.h -------------------------*/
3/*--------------------------------------------------------------------------*/
40/*--------------------------------------------------------------------------*/
41/*----------------------------- DEFINITIONS --------------------------------*/
42/*--------------------------------------------------------------------------*/
43
44#ifndef __ADS_SortingNetworks
45 #define __ADS_SortingNetworks
46/* self-identification: #endif constant */
47
48/*--------------------------------------------------------------------------*/
49/*------------------------------ INCLUDES ----------------------------------*/
50/*--------------------------------------------------------------------------*/
51
52#include "ADS/Contract.h"
53#include "ADS/sort/Sorted.h"
54
55#include <algorithm>
56#include <bit>
57#include <cstddef>
58#include <functional>
59#include <utility>
60#include <vector>
61
62/*--------------------------------------------------------------------------*/
63/*--------------------------- NAMESPACE ------------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66namespace ADS
67{
68
71using Comparator = std::pair< std::size_t , std::size_t >;
72
95inline std::vector< Comparator > mergeExchangeNetwork( std::size_t n );
96
119inline std::vector< Comparator > bitonicNetwork( std::size_t n );
120
132template< RankStore Store ,
133 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
134 class C = std::less<> >
135void applyNetwork( Store && A , const std::vector< Comparator > & N ,
136 C less = C() );
137
139template< class T , class C = std::less<> >
140void applyNetwork( std::vector< T > & A , const std::vector< Comparator > & N ,
141 C less = C() );
142
143/*--------------------------------------------------------------------------*/
144/*------------------------- THE SORTS OF A RANGE ---------------------------*/
145/*--------------------------------------------------------------------------*/
146
151template< SortRange R , class... P >
152 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
153 P &&... p )
154{
155 applyNetwork( v , std::forward< P >( p )... );
156 }
157decltype( auto ) applyNetwork( R && A , P &&... p )
158{
159 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
160 return( applyNetwork( v , std::forward< P >( p )... ) );
161 };
162 return( detail::sortRange( A , f , f ) );
163 }
164
165/*--------------------------------------------------------------------------*/
166
167} // end( namespace ADS )
168
169/*--------------------------------------------------------------------------*/
170/*-------------------------- IMPLEMENTATION --------------------------------*/
171/*--------------------------------------------------------------------------*/
172
173#include "ADS/sort/impl/SortingNetworks.tpp"
174
175/*--------------------------------------------------------------------------*/
176
177#endif /* SortingNetworks.h included */
178
179/*--------------------------------------------------------------------------*/
180/*----------------------- End File SortingNetworks.h -----------------------*/
181/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The checks of the contracts of the sorts [see Contract.h]: a copy of the input kept only when the aud...
the storage on which the sorts of the book work in place, which read the items by their ranks only: a...
Definition Sorted.h:184
a range that the sorts take beside std::vector and std::span: random access, sized,...
Definition Sorted.h:153
std::vector< Comparator > bitonicNetwork(std::size_t n)
the bitonic sorter for items
std::pair< std::size_t, std::size_t > Comparator
a comparator of a network: the smaller of and goes to
Definition SortingNetworks.h:71
std::vector< Comparator > mergeExchangeNetwork(std::size_t n)
the merge exchange of Batcher for items
void applyNetwork(Store &&A, const std::vector< Comparator > &N, C less=C())
the comparators of applied to in their order
Definition ADT.h:96