Documentation
Tools for embedded systems
Loading...
Searching...
No Matches
Basic-algorithms for raw-arrays

A basic implementation of basic algorithms for raw-arrays without recursion and dynamic memory allocation. More...

Functions

template<typename T >
void qlibs::algorithm::swap (T &x, T &y) noexcept
 Exchanges the values of a and b.
 
template<typename T , size_t n>
void qlibs::algorithm::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.
 
template<typename T , size_t n>
void qlibs::algorithm::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).
 
template<typename T , size_t n>
void qlibs::algorithm::rotate (T(&array)[n], const int k=1) noexcept
 Rotates k elements of the array. Rotation direction is determined by the sign of k, the means a positive value performs a right-rotation and a negative value a left-rotation.
 
template<typename T , size_t n>
void qlibs::algorithm::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).
 
template<typename T , size_t n>
T * qlibs::algorithm::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. If no such element is found, the function returns nullptr.
 
template<typename T , size_t n>
bool qlibs::algorithm::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), and false otherwise.
 
template<typename T , size_t n>
bool qlibs::algorithm::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.
 
template<typename T , size_t n>
void qlibs::algorithm::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, last). Replaces all elements that are equal to old_value (using operator==)
 
template<typename T , size_t n>
void qlibs::algorithm::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, last). Replaces all elements for which predicate pred returns true.
 
template<typename T , size_t n>
size_t qlibs::algorithm::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.
 
template<typename T , size_t n>
T * qlibs::algorithm::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. If no such element is found, the function returns nullptr.
 
template<typename T , size_t n>
void qlibs::algorithm::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).
 
template<typename T , size_t n>
T * qlibs::algorithm::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. If no such element is found, the function returns nullptr.
 

Detailed Description

A basic implementation of basic algorithms for raw-arrays without recursion and dynamic memory allocation.

Function Documentation

◆ all_of()

template<typename T , size_t n>
bool qlibs::algorithm::all_of ( T(&) array[n],
bool(* pred )(const T),
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Returns true if pred returns true for all the elements in the range [first,last), and false otherwise.

Parameters
[in]arrayThe array where the check is performed
[in]predUnary function that accepts an element in the range as argument and returns a value convertible to bool. The value returned indicates whether the element fulfills the condition checked by this function.
[in]firstInitial position of the portion to check
[in]lastFinal position of the portion to check
Returns
true if pred returns true for all the elements in the range [first,last), and false otherwise.

◆ any_of()

template<typename T , size_t n>
bool qlibs::algorithm::any_of ( T(&) array[n],
bool(* pred )(const T),
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Returns true if pred returns true for any of the elements in the range [first,last), and false otherwise.

Parameters
[in]arrayThe array where the check is performed
[in]predUnary function that accepts an element in the range as argument and returns a value convertible to bool. The value returned indicates whether the element fulfills the condition checked by this function.
[in]firstInitial position of the portion to check
[in]lastFinal position of the portion to check
Returns
true if pred returns true for any of the elements in the range [first,last), and false otherwise.

◆ binary_search()

template<typename T , size_t n>
T * qlibs::algorithm::binary_search ( T(&) array[n],
const T key,
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Returns a pointer to the first element in the range [first,last) that compares equal to key. If no such element is found, the function returns nullptr.

Note
The elements in the range shall already be sorted according to this same criterion (operator< or operator==), or at least partitioned with respect to key.
The elements are compared using operator '=='
Parameters
[in]arrayThe array where the search is performed
[in]keyValue to search for in the range. T shall be a type supporting comparisons using operator== and operator<.
[in]firstInitial position of the portion to search
[in]lastFinal position of the portion to search
Returns
This function returns a pointer to an entry in the array that matches the search key. If key is not found, a nullptr pointer is returned.

◆ count_if()

template<typename T , size_t n>
size_t qlibs::algorithm::count_if ( T(&) array[n],
bool(* pred )(const T),
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Returns the number of elements in the range [first,last) for which pred is true.

Parameters
[in]arrayThe array where the count will be performed
[in]predUnary function that accepts an element in the range as argument, and returns a value convertible to bool. The value returned indicates whether the element is counted by this function.
[in]firstInitial position of the portion to check
[in]lastFinal position of the portion to check
Returns
The number of elements in the range [first,last) for which pred does not return false.

◆ fill()

template<typename T , size_t n>
void qlibs::algorithm::fill ( T(&) array[n],
const T value,
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Assigns value to all the elements of the array in the range [first,last).

Parameters
[in,out]arrayThe array to fill.
[in]valueThe value to set all elements to.
[in]firstInitial position of the portion to fill
[in]lastFinal position of the portion to fill
Returns
none.

◆ find()

template<typename T , size_t n>
T * qlibs::algorithm::find ( T(&) array[n],
const T key,
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Returns a pointer to the first element in the range [first,last) that compares equal to key. If no such element is found, the function returns nullptr.

Note
The elements are compared using operator '=='
Parameters
[in]arrayThe array where the search is performed
[in]keyValue to search for in the range. T shall be a type supporting comparisons using operator==.
[in]firstInitial position of the portion to search
[in]lastFinal position of the portion to search
Returns
This function returns a pointer to an entry in the array that matches the search key. If key is not found, a nullptr pointer is returned.

◆ find_if()

template<typename T , size_t n>
T * qlibs::algorithm::find_if ( T(&) array[n],
bool(* pred )(const T),
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Returns an iterator to the first element in the range [first,last) for which pred returns true. If no such element is found, the function returns nullptr.

Parameters
[in]arrayThe array where the search is performed
[in]predUnary function that accepts an element in the range as argument and returns a value convertible to bool. The value returned indicates whether the element is considered a match in the context of this function.
[in]firstInitial position of the portion to check
[in]lastFinal position of the portion to check
Returns
A pointer to the first element in the range for which pred does not return false. If pred is false for all elements, the function returns nullptr.

◆ for_each()

template<typename T , size_t n>
void qlibs::algorithm::for_each ( T(&) array[n],
void(* fn )(T &),
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Applies function fn to each of the elements in the range [first,last).

Parameters
[in]arrayThe array
[in]fnUnary function that accepts an element in the range as argument.
[in]firstInitial position of the portion to check
[in]lastFinal position of the portion to check
Returns
none

◆ replace()

template<typename T , size_t n>
void qlibs::algorithm::replace ( T(&) array[n],
const T & old_value,
const T & new_value,
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Replaces all elements satisfying specific criteria with new_value in the range [first, last). Replaces all elements that are equal to old_value (using operator==)

Parameters
[in]arrayThe array where the check is performed
[in]old_valueThe value of elements to replace
[in]new_valueThe value to use as replacement
[in]firstInitial position of the portion to check
[in]lastFinal position of the portion to check
Returns
none.

◆ replace_if()

template<typename T , size_t n>
void qlibs::algorithm::replace_if ( T(&) array[n],
bool(* pred )(const T &),
const T & new_value,
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Replaces all elements satisfying specific criteria with new_value in the range [first, last). Replaces all elements for which predicate pred returns true.

Parameters
[in]arrayThe array where the check is performed
[in]predUnary predicate which returns true if the element value should be replaced.
[in]new_valueThe value to use as replacement
[in]firstInitial position of the portion to check
[in]lastFinal position of the portion to check
Returns
none.

◆ reverse()

template<typename T , size_t n>
void qlibs::algorithm::reverse ( T(&) array[n],
const size_t first = 0U,
const size_t last = n - 1U )
inlinenoexcept

Reverses the order of the elements in the range [first,last).

Parameters
[in,out]arrayThe array to reverse.
[in]firstInitial position of the portion to reverse
[in]lastFinal position of the portion to reverse
Returns
none.

◆ rotate()

template<typename T , size_t n>
void qlibs::algorithm::rotate ( T(&) array[n],
const int k = 1 )
noexcept

Rotates k elements of the array. Rotation direction is determined by the sign of k, the means a positive value performs a right-rotation and a negative value a left-rotation.

Parameters
[in,out]arrayThe array to rotate.
[in]kPositions to rotate. Sign determines the rotate direction.
Returns
none.

◆ sort()

template<typename T , size_t n>
void qlibs::algorithm::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.

Note
The elements are compared using operator<
Remarks
This algorithm uses a non-recursive variant of the Quick Sort algorithm.
Parameters
[in,out]arrayThe array to be sorted.
[in]firstInitial position of the portion to be sorted
[in]lastFinal position of the portion to be sorted
[in]compComparison function which returns ​true if the first argument is less than (i.e. is ordered before) the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1& a, const Type2& b);
bool cmp( const T& a, const T& b );
Returns
none.

◆ swap()

template<typename T >
void qlibs::algorithm::swap ( T & x,
T & y )
noexcept

Exchanges the values of a and b.

Parameters
[in,out]xObject to be swapped.
[in,out]yObject to be swapped.
Returns
none.