ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
InsertionSort.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File InsertionSort.h --------------------------*/
3/*--------------------------------------------------------------------------*/
26/*--------------------------------------------------------------------------*/
27/*----------------------------- DEFINITIONS --------------------------------*/
28/*--------------------------------------------------------------------------*/
29
30#ifndef __ADS_InsertionSort
31 #define __ADS_InsertionSort
32/* self-identification: #endif constant */
33
34/*--------------------------------------------------------------------------*/
35/*------------------------------ INCLUDES ----------------------------------*/
36/*--------------------------------------------------------------------------*/
37
38#include "ADS/Contract.h"
39#include "ADS/sort/Sorted.h"
40
41#include <algorithm>
42#include <cstddef>
43#include <functional>
44#include <utility>
45#include <vector>
46
47/*--------------------------------------------------------------------------*/
48/*--------------------------- NAMESPACE ------------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51namespace ADS
52{
53
75template< RankStore Store ,
76 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
77 class C = std::less<> >
78void insertionSort( Store && A , std::size_t lo , std::size_t hi ,
79 C less = C() );
80
82template< class T , class C = std::less<> >
83void insertionSort( std::vector< T > & A , std::size_t lo , std::size_t hi ,
84 C less = C() );
85
106template< RankStore Store ,
107 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
108 class C = std::less<> >
109void insertionSortSentinel( Store && A , C less = C() );
110
112template< class T , class C = std::less<> >
113void insertionSortSentinel( std::vector< T > & A , C less = C() );
114
123template< RankStore Store ,
124 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
125 class C = std::less<> >
126void insertionSort( Store && A , C less = C() );
127
129template< class T , class C = std::less<> >
130void insertionSort( std::vector< T > & A , C less = C() );
131
132/*--------------------------------------------------------------------------*/
133/*------------------------- THE SORTS OF A RANGE ---------------------------*/
134/*--------------------------------------------------------------------------*/
135
140template< SortRange R , class... P >
141 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
142 P &&... p )
143{
144 insertionSort( v , std::forward< P >( p )... );
145 }
146decltype( auto ) insertionSort( R && A , P &&... p )
147{
148 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
149 return( insertionSort( v , std::forward< P >( p )... ) );
150 };
151 return( detail::sortRange( A , f , f ) );
152 }
153
158template< SortRange R , class... P >
159 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
160 P &&... p )
161{
162 insertionSortSentinel( v , std::forward< P >( p )... );
163 }
164decltype( auto ) insertionSortSentinel( R && A , P &&... p )
165{
166 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
167 return( insertionSortSentinel( v , std::forward< P >( p )... ) );
168 };
169 return( detail::sortRange( A , f , f ) );
170 }
171
172/*--------------------------------------------------------------------------*/
173
174} // end( namespace ADS )
175
176/*--------------------------------------------------------------------------*/
177/*-------------------------- IMPLEMENTATION --------------------------------*/
178/*--------------------------------------------------------------------------*/
179
180#include "ADS/sort/impl/InsertionSort.tpp"
181
182/*--------------------------------------------------------------------------*/
183
184#endif /* InsertionSort.h included */
185
186/*--------------------------------------------------------------------------*/
187/*------------------------ End File InsertionSort.h ------------------------*/
188/*--------------------------------------------------------------------------*/
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
void insertionSort(Store &&A, std::size_t lo, std::size_t hi, C less=C())
the insertion sort of
void insertionSortSentinel(Store &&A, C less=C())
the insertion sort with a sentinel: the minimum first goes to , with comparisons and one swap,...
Definition ADT.h:96