microlibrary

Algorithms

Algorithms are defined in the microlibrary static library’s microlibrary/algorithm.h/microlibrary/algorithm.cc header/source file pair. Algorithms automated tests are defined in the test-automated-microlibrary-algorithm automated test executable’s main.cc source file.

Table of Contents

  1. Algorithm Functor Policies
  2. for_each()
  3. fill()
  4. generate()
  5. min()
  6. max()
  7. equal()

Algorithm Functor Policies

The following algorithm functor policy types are used to select algorithm implementations that check for functor reported errors and report functor errors to the caller:

for_each()

To apply a functor to a range, use the ::microlibrary::for_each() algorithm. To report functor errors to the caller, use the ::microlibrary::Functor_Reports_Errors_Return_Functor or ::microlibrary::Functor_Reports_Errors_Discard_Functor algorithm functor policy.

#include <cstdint>

#include "microlibrary/algorithm.h"
#include "microlibrary/result.h"

void foo( std::uint_fast8_t value ) noexcept;

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

auto wibble( std::uint_fast8_t const * begin, std::uint_fast8_t const * end ) noexcept
{
    return ::microlibrary::for_each( begin, end, foo );
}

auto wobble( std::uint_fast8_t const * begin, std::uint_fast8_t const * end ) noexcept
{
    auto result = ::microlibrary::for_each<::microlibrary::Functor_Reports_Errors_Return_Functor>(
        begin, end, bar );
    if ( result.is_error() ) {
        // handle error
    } // if

    return result.value();
}

void wubble( std::uint_fast8_t const * begin, std::uint_fast8_t const * end ) noexcept
{
    auto result = ::microlibrary::for_each<::microlibrary::Functor_Reports_Errors_Discard_Functor>(
        begin, end, bar );
    if ( result.is_error() ) {
        // handle error
    } // if
}

fill()

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

generate()

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

#include <cstdint>

#include "microlibrary/algorithm.h"
#include "microlibrary/result.h"

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

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

void wibble( std::uint_fast8_t * begin, std::uint_fast8_t * end ) noexcept
{
    ::microlibrary::generate( begin, end, foo );
}

void wobble( std::uint_fast8_t * begin, std::uint_fast8_t * end ) noexcept
{
    auto result = ::microlibrary::generate<::microlibrary::Functor_Reports_Errors>(
        begin, end, bar );
    if ( result.is_error() ) {
        // handle error
    } // if
}

min()

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

max()

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

equal()

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