picolibrary

Algorithms

Algorithms are defined in the include/picolibrary/algorithm.h/source/picolibrary/algorithm.cc header/source file pair. Algorithm automated tests are defined in the test/automated/picolibrary/algorithm/main.cc source file.

Table of Contents

  1. for_each()
  2. fill()
  3. generate()
  4. min()
  5. max()
  6. equal()

for_each()

To apply a unary functor to a range, use the ::picolibrary::for_each() algorithm. To report functor errors to the caller, use the ::picolibrary::Functor_Can_Fail_Return_Fuctor or ::picolibrary::Functor_Can_Fail_Discard_Functor policy.

#include <cstdint>

#include "picolibrary/algorithm.h"
#include "picolibrary/array.h"
#include "picolibrary/result.h"

void foo( std::uint_fast8_t i ) noexcept;

auto bar( std::uint_fast8_t i ) noexcept -> Result<void>;

auto wibble( ::picolibrary::Array<std::uint_fast8_t, 8> const & array ) noexcept
{
    return ::picolibrary::for_each( array.begin(), array.end(), foo );
}

auto wobble( ::picolibrary::Array<std::uint_fast8_t, 8> const & array ) noexcept
{
    auto result = ::picolibrary::for_each<::picolibrary::Functor_Can_Fail_Return_Functor>(
        array.begin(),
        array.end(),
        bar );
    if ( result.is_error() ) {
        // handle error
    } // if

    return result.value();
}

void wubble( ::picolibrary::Array<std::uint_fast8_t, 8> const & array ) noexcept
{
    auto result = ::picolibrary::for_each<::picolibrary::Functor_Can_Fail_Discard_Functor>(
        array.begin(),
        array.end(),
        bar );
    if ( result.is_error() ) {
        // handle error
    } // if
}

fill()

To fill a range with a value, use the ::picolibrary::fill() algorithm.

generate()

To fill a range with values generated by a nullary functor, use the ::picolibrary::generate() algorithm. To report functor errors to the caller, use the ::picolibrary::Functor_Can_Fail policy.

#include <cstdint>

#include "picolibrary/algorithm.h"
#include "picolibrary/array.h"
#include "picolibrary/result.h"

auto foo() noexcept -> std::uint_fast8_t;

auto bar() noexcept -> Result<std::uint_fast8_t>;

auto wibble() noexcept -> Array<std::uint_fast8_t, 8>
{
    Array<std::uint_fast8_t, 8> array;

    ::picolibrary::generate( array.begin(), array.end(), foo );

    return array;
}

auto wobble() noexcept -> Array<std::uint_fast8_t, 8>
{
    Array<std::uint_fast8_t, 8> array;

    auto result = ::picolibrary::generate<::picolibrary::Functor_Can_Fail>(
        array.begin(),
        array.end(),
        bar );
    if ( result.is_error() ) {
        // handle error
    } // if

    return array;
}

min()

To get the minimum of two values, use the ::picolibrary::min() algorithm.

max()

To get the maximum of two values, use the ::picolibrary::max() algorithm.

equal()

To check if two ranges are equal, use the ::picolibrary::equal() algorithm. If the elements in the ranges do not support direct comparison, a binary predicate can be used to compare elements.