4#include <include/qlibs_types.hpp>
37 void swap( T& x, T& y )
noexcept
50 template<
typename T,
size_t N>
56 sort_stack() : topIndex( 0 ) {}
57 bool empty(
void )
const noexcept
61 void push(
const T& value )
noexcept
64 dat[ topIndex++ ] = value;
67 void pop(
void )
noexcept
73 T& top(
void )
noexcept
75 return dat[ topIndex - 1 ];
99 template<
typename T,
size_t n>
102 size_t last = n - 1U,
103 bool (*comp)(
const T&,
const T&) =
nullptr ) noexcept
106 algorithm::impl::sort_stack<impl::sort_pair, n> stack;
107 int start =
static_cast<int>( first );
108 int end =
static_cast<int>( last );
110 stack.push( { start, end } );
111 while ( !stack.empty() ) {
112 impl::sort_pair indices = stack.top();
114 start = indices.first;
115 end = indices.second;
116 int pivotIndex = start;
117 T pivotValue = array[ end ];
119 for (
int i = start; i < end; ++i ) {
120 if (
nullptr != comp ) {
121 if ( comp( array[ i ], array[ pivotIndex ] ) ) {
126 else if ( array[ i ] < pivotValue ) {
132 if ( pivotIndex - 1 > start ) {
133 stack.push( { start, pivotIndex - 1 } );
135 if ( pivotIndex + 1 < end ) {
136 stack.push( { pivotIndex + 1, end } );
149 template<
typename T,
size_t n>
151 const size_t first = 0U,
152 const size_t last = n - 1U ) noexcept
154 if ( last > first ) {
155 size_t s = first, e = last;
173 template<
typename T,
size_t n>
175 const int k = 1 ) noexcept
180 r =
static_cast<size_t>( k );
188 r =
static_cast<size_t>( -k );
207 template<
typename T,
size_t n>
208 inline void fill( T ( &array )[ n ],
210 const size_t first = 0U,
211 const size_t last = n - 1U ) noexcept
213 for (
size_t i = first ; i <= last; ++i ) {
232 template<
typename T,
size_t n>
233 inline T*
find( T ( &array )[ n ],
235 const size_t first = 0U,
236 const size_t last = n - 1U ) noexcept
240 for (
size_t i = first; i <= last; ++i ) {
241 if ( array[ i ] == key ) {
262 template<
typename T,
size_t n>
264 bool (*pred)(
const T ),
265 const size_t first = 0U,
266 const size_t last = n - 1U ) noexcept
270 for (
size_t i = first; i <= last; ++i ) {
271 if ( pred( array[ i ] ) ) {
292 template<
typename T,
size_t n>
294 bool (*pred)(
const T ),
295 const size_t first = 0U,
296 const size_t last = n - 1U ) noexcept
300 for (
size_t i = first; i <= last; ++i ) {
301 if ( !pred( array[ i ] ) ) {
320 template<
typename T,
size_t n>
324 const size_t first = 0U,
325 const size_t last = n - 1U ) noexcept
327 for (
size_t i = first; i <= last; ++i ) {
328 if ( old_value == array[ i ] ) {
329 array[ i ] = new_value;
347 template<
typename T,
size_t n>
349 bool (*pred)(
const T& ),
351 const size_t first = 0U,
352 const size_t last = n - 1U ) noexcept
354 for (
size_t i = first; i <= last; ++i ) {
355 if ( pred( array[ i ] ) ) {
356 array[ i ] = new_value;
374 template<
typename T,
size_t n>
376 bool (*pred)(
const T ),
377 const size_t first = 0U,
378 const size_t last = n - 1U ) noexcept
382 for (
size_t i = first; i <= last; ++i ) {
383 if ( pred( array[ i ] ) ) {
405 template<
typename T,
size_t n>
407 bool (*pred)(
const T ),
408 const size_t first = 0U,
409 const size_t last = n - 1U ) noexcept
413 for (
size_t i = first; i <= last; ++i ) {
414 if ( pred( array[ i ] ) ) {
431 template<
typename T,
size_t n>
434 const size_t first = 0U,
435 const size_t last = n - 1U ) noexcept
437 for (
size_t i = first; i <= last; ++i ) {
438 (void)fn( array[ i ] );
459 template<
typename T,
size_t n>
462 const size_t first = 0U,
463 const size_t last = n - 1U ) noexcept
466 int left =
static_cast<int>( first );
467 int right =
static_cast<int>( last );
469 while ( left <= right ) {
470 int mid = left + ( right - left )/2;
472 if ( array[ mid ] == key ) {
473 found = &array[ mid ];
476 if ( array[ mid ] < key ) {
void reverse(T(&array)[n], const size_t first=0U, const size_t last=n - 1U) noexcept
Reverses the order of the elements in the range [first,last).
Definition algorithm.hpp:150
void swap(T &x, T &y) noexcept
Exchanges the values of a and b.
Definition algorithm.hpp:37
void sort(T(&array)[n], size_t first=0U, size_t last=n - 1U, bool(*comp)(const T &, const T &)=nullptr) noexcept
Sorts the given array in the range [first,last) into ascending order.
Definition algorithm.hpp:100
void replace(T(&array)[n], const T &old_value, const T &new_value, const size_t first=0U, const size_t last=n - 1U) noexcept
Replaces all elements satisfying specific criteria with new_value in the range [first,...
Definition algorithm.hpp:321
void rotate(T(&array)[n], const int k=1) noexcept
Rotates k elements of the array. Rotation direction is determined by the sign of k,...
Definition algorithm.hpp:174
T * binary_search(T(&array)[n], const T key, const size_t first=0U, const size_t last=n - 1U) noexcept
Returns a pointer to the first element in the range [first,last) that compares equal to key....
Definition algorithm.hpp:460
bool any_of(T(&array)[n], bool(*pred)(const T), const size_t first=0U, const size_t last=n - 1U) noexcept
Returns true if pred returns true for any of the elements in the range [first,last),...
Definition algorithm.hpp:263
T * find_if(T(&array)[n], bool(*pred)(const T), const size_t first=0U, const size_t last=n - 1U) noexcept
Returns an iterator to the first element in the range [first,last) for which pred returns true....
Definition algorithm.hpp:406
void replace_if(T(&array)[n], bool(*pred)(const T &), const T &new_value, const size_t first=0U, const size_t last=n - 1U) noexcept
Replaces all elements satisfying specific criteria with new_value in the range [first,...
Definition algorithm.hpp:348
void fill(T(&array)[n], const T value, const size_t first=0U, const size_t last=n - 1U) noexcept
Assigns value to all the elements of the array in the range [first,last).
Definition algorithm.hpp:208
void for_each(T(&array)[n], void(*fn)(T &), const size_t first=0U, const size_t last=n - 1U) noexcept
Applies function fn to each of the elements in the range [first,last).
Definition algorithm.hpp:432
bool all_of(T(&array)[n], bool(*pred)(const T), const size_t first=0U, const size_t last=n - 1U) noexcept
Returns true if pred returns true for all the elements in the range [first,last), and false otherwise...
Definition algorithm.hpp:293
T * find(T(&array)[n], const T key, const size_t first=0U, const size_t last=n - 1U) noexcept
Returns a pointer to the first element in the range [first,last) that compares equal to key....
Definition algorithm.hpp:233
size_t count_if(T(&array)[n], bool(*pred)(const T), const size_t first=0U, const size_t last=n - 1U) noexcept
Returns the number of elements in the range [first,last) for which pred is true.
Definition algorithm.hpp:375
The qLibs++ library namespace.
Definition fp16.cpp:4