ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Sorted.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*----------------------------- File Sorted.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
38/*--------------------------------------------------------------------------*/
39/*----------------------------- DEFINITIONS --------------------------------*/
40/*--------------------------------------------------------------------------*/
41
42#ifndef __ADS_Sorted
43 #define __ADS_Sorted
44/* self-identification: #endif constant */
45
46/*--------------------------------------------------------------------------*/
47/*------------------------------ INCLUDES ----------------------------------*/
48/*--------------------------------------------------------------------------*/
49
50#include "ADS/Contract.h"
51#include "ADS/Portable.h"
52
53#include <algorithm>
54#include <cstddef>
55#include <functional>
56#include <iterator>
57#include <ranges>
58#include <span>
59#include <string>
60#include <type_traits>
61#include <utility>
62#include <vector>
63
64// the overloads of the sorts on a vector, each one call of the sort on its
65// storage, inlined into the caller, so that a call on a vector runs the code
66// it ran before the sorts took spans and views
67#define ADS_SORT_FORWARD ADS_ALWAYS_INLINE
68
69/*--------------------------------------------------------------------------*/
70/*--------------------------- NAMESPACE ------------------------------------*/
71/*--------------------------------------------------------------------------*/
72
73namespace ADS
74{
75
78[[nodiscard]] bool checkingContract( void );
79
80namespace detail
81{
82
84template< class F >
85bool contract( F && f );
86
89template< std::ranges::random_access_range S >
90std::vector< std::ranges::range_value_t< S > >
91auditCopy( const S & A , std::size_t lo = 0 ,
92 std::size_t hi = std::size_t( -1 ) );
93
94
98template< std::ranges::random_access_range S , class C >
99bool samePermutation( const S & A ,
100 std::vector< std::ranges::range_value_t< S > > before ,
101 C less , std::size_t lo = 0 ,
102 std::size_t hi = std::size_t( -1 ) );
103
104
108template< std::ranges::random_access_range S , class C >
109bool sortedPermutation( const S & A ,
110 std::vector< std::ranges::range_value_t< S > > before ,
111 C less , std::size_t lo = 0 ,
112 std::size_t hi = std::size_t( -1 ) );
113
114
117template< class S , class C >
118bool sequenceSorted( const S & L , C less );
119
122template< class T , class C >
123inline constexpr bool naturalOrder =
124 std::is_same_v< C , std::less<> > || std::is_same_v< C , std::less< T > >;
125
128template< class R >
129inline constexpr bool ownRange = false;
130
131template< class T >
132inline constexpr bool ownRange< std::vector< T > > = true;
133
134template< class T , std::size_t N >
135inline constexpr bool ownRange< std::span< T , N > > = true;
136
137template< class R >
138inline constexpr bool ownRange< std::ranges::ref_view< R > > = true;
139
145template< class R , class F , class G = void * >
146auto sortRange( R & r , F && f , G && g = nullptr );
147
148} // end( namespace detail )
149
152template< class R >
153concept SortRange =
154 std::ranges::random_access_range< R > && std::ranges::sized_range< R > &&
155 std::permutable< std::ranges::iterator_t< R > > &&
156 std::movable< std::ranges::range_value_t< R > > &&
157 ! detail::ownRange< std::remove_cvref_t< R > >;
158
160namespace detail
161{
162
163template< class S >
164inline constexpr bool spanOrVector = false;
165
166template< class T >
167inline constexpr bool spanOrVector< std::vector< T > > = true;
168
169template< class T >
170inline constexpr bool spanOrVector< std::span< T > > = true;
171
172} // end( namespace detail )
174
177template< class S >
178concept SortStore = detail::spanOrVector< std::remove_cvref_t< S > >;
179
183template< class S >
184concept RankStore =
186 ( std::ranges::random_access_range< std::remove_cvref_t< S > > &&
187 std::ranges::sized_range< std::remove_cvref_t< S > > &&
188 std::ranges::view< std::remove_cvref_t< S > > );
189
191template< class S >
192concept StringStore =
194 std::same_as< std::ranges::range_value_t< std::remove_cvref_t< S > > ,
195 std::string >;
196
197/*--------------------------------------------------------------------------*/
198
199} // end( namespace ADS )
200
201/*--------------------------------------------------------------------------*/
202/*-------------------------- IMPLEMENTATION --------------------------------*/
203/*--------------------------------------------------------------------------*/
204
205#include "ADS/sort/impl/Sorted.tpp"
206
207/*--------------------------------------------------------------------------*/
208
209#endif /* Sorted.h included */
210
211/*--------------------------------------------------------------------------*/
212/*--------------------------- End File Sorted.h ----------------------------*/
213/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
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
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
a SortStore of strings
Definition Sorted.h:192
bool checkingContract(void)
true while a contract of ADS is evaluated: a comparator that counts its calls skips those made by the...
Definition ADT.h:96