4#include <include/qlibs_types.hpp>
36 void swap( T& x, T& y )
noexcept
49 template<
typename T,
size_t N>
55 sort_stack() : topIndex( 0 ) {}
56 bool empty(
void )
const noexcept
60 void push(
const T& value )
noexcept
63 dat[ topIndex++ ] = value;
66 void pop(
void )
noexcept
72 T& top(
void )
noexcept
74 return dat[ topIndex - 1 ];
97 template<
typename T,
size_t n>
98 void sort( T ( &array )[ n ],
100 size_t last = n - 1U,
101 bool (*comp)(
const T&,
const T&) =
nullptr ) noexcept
104 algorithm::impl::sort_stack<impl::sort_pair, n> stack;
105 int start =
static_cast<int>( first );
106 int end =
static_cast<int>( last );
108 stack.push( { start, end } );
109 while ( !stack.empty() ) {
110 impl::sort_pair indices = stack.top();
112 start = indices.first;
113 end = indices.second;
114 int pivotIndex = start;
115 T pivotValue = array[ end ];
117 for (
int i = start; i < end; ++i ) {
118 if (
nullptr != comp ) {
119 if ( comp( array[ i ], array[ pivotIndex ] ) ) {
124 else if ( array[ i ] < pivotValue ) {
130 if ( pivotIndex - 1 > start ) {
131 stack.push( { start, pivotIndex - 1 } );
133 if ( pivotIndex + 1 < end ) {
134 stack.push( { pivotIndex + 1, end } );
146 template<
typename T,
size_t n>
148 const size_t first = 0U,
149 const size_t last = n - 1U ) noexcept
151 if ( last > first ) {
152 size_t s = first, e = last;
169 template<
typename T,
size_t n>
171 const int k = 1 ) noexcept
176 r =
static_cast<size_t>( k );
184 r =
static_cast<size_t>( -k );
202 template<
typename T,
size_t n>
203 inline void fill( T ( &array )[ n ],
205 const size_t first = 0U,
206 const size_t last = n - 1U ) noexcept
208 for (
size_t i = first ; i <= last; ++i ) {
227 template<
typename T,
size_t n>
228 inline T*
find( T ( &array )[ n ],
230 const size_t first = 0U,
231 const size_t last = n - 1U ) noexcept
235 for (
size_t i = first; i <= last; ++i ) {
236 if ( array[ i ] == key ) {
257 template<
typename T,
size_t n>
259 bool (*pred)(
const T ),
260 const size_t first = 0U,
261 const size_t last = n - 1U ) noexcept
265 for (
size_t i = first; i <= last; ++i ) {
266 if ( pred( array[ i ] ) ) {
287 template<
typename T,
size_t n>
289 bool (*pred)(
const T ),
290 const size_t first = 0U,
291 const size_t last = n - 1U ) noexcept
295 for (
size_t i = first; i <= last; ++i ) {
296 if ( !pred( array[ i ] ) ) {
314 template<
typename T,
size_t n>
318 const size_t first = 0U,
319 const size_t last = n - 1U ) noexcept
321 for (
size_t i = first; i <= last; ++i ) {
322 if ( old_value == array[ i ] ) {
323 array[ i ] = new_value;
340 template<
typename T,
size_t n>
342 bool (*pred)(
const T& ),
344 const size_t first = 0U,
345 const size_t last = n - 1U ) noexcept
347 for (
size_t i = first; i <= last; ++i ) {
348 if ( pred( array[ i ] ) ) {
349 array[ i ] = new_value;
367 template<
typename T,
size_t n>
369 bool (*pred)(
const T ),
370 const size_t first = 0U,
371 const size_t last = n - 1U ) noexcept
375 for (
size_t i = first; i <= last; ++i ) {
376 if ( pred( array[ i ] ) ) {
398 template<
typename T,
size_t n>
400 bool (*pred)(
const T ),
401 const size_t first = 0U,
402 const size_t last = n - 1U ) noexcept
406 for (
size_t i = first; i <= last; ++i ) {
407 if ( pred( array[ i ] ) ) {
423 template<
typename T,
size_t n>
426 const size_t first = 0U,
427 const size_t last = n - 1U ) noexcept
429 for (
size_t i = first; i <= last; ++i ) {
430 (void)fn( array[ i ] );
451 template<
typename T,
size_t n>
454 const size_t first = 0U,
455 const size_t last = n - 1U ) noexcept
458 int left =
static_cast<int>( first );
459 int right =
static_cast<int>( last );
461 while ( left <= right ) {
462 int mid = left + ( right - left )/2;
464 if ( array[ mid ] == key ) {
465 found = &array[ mid ];
468 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:147
void swap(T &x, T &y) noexcept
Exchanges the values of a and b.
Definition algorithm.hpp:36
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:98
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:315
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:170
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:452
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:258
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:399
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:341
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:203
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:424
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:288
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:228
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:368
The generic namespace.
Definition algorithm.hpp:23
The qLibs++ library namespace.
Definition mat.hpp:18