jaulib: Basic Algorithms (original) (raw)

Basic algorithms. More...

Classes
class jau::call_on_release< UnaryFunction >
Call on release allows the user to pass a function to be called at destruction of this instance. More...
class jau::OptDeleter< T >
Functions
template<class InputArray, class T>
constexpr bool jau::contains (InputArray &array, const T &value) noexcept
Return true if value is contained in array.
template<class InputArray, class T>
constexpr bool jau::eraseFirst (InputArray &array, const T &value)
template<class InputIt, class T>
constexpr InputIt jau::find (InputIt first, InputIt last, const T &value) noexcept
Like std::find() of 'algorithm'.
template
const T::value_type * jau::find_const (T &data, typename T::value_type const &elem, std::enable_if_t< is\_cow\_type< T >::value, bool >=true) noexcept
template
const T::value_type * jau::find_const (T &data, typename T::value_type const &elem, std::enable_if_t< is_cow_type< T >::value, bool >=true) noexcept
template<class InputIt, class UnaryPredicate>
constexpr InputIt jau::find_if (InputIt first, InputIt last, UnaryPredicate p) noexcept
Like std::find_if() of 'algorithm'.
template<class InputIt, class UnaryPredicate>
constexpr InputIt jau::find_if_not (InputIt first, InputIt last, UnaryPredicate q) noexcept
Like std::find_if_not() of 'algorithm'.
template<class InputIt, class UnaryFunction>
constexpr UnaryFunction jau::for_each (InputIt first, InputIt last, UnaryFunction f)
Like std::for_each() of 'algorithm'.
template<class T, class UnaryFunction>
constexpr UnaryFunction jau::for_each_const (T &data, UnaryFunction f, std::enable_if_t< is\_cow\_type< T >::value, bool >=true) noexcept
template<class T, class UnaryFunction>
constexpr UnaryFunction jau::for_each_const (T &data, UnaryFunction f, std::enable_if_t< is_cow_type< T >::value, bool >=true) noexcept
template<class InputIt, class UnaryFunction>
constexpr UnaryFunction jau::for_each_fidelity (InputIt first, InputIt last, UnaryFunction f)
Like jau::for_each(), see above.
template<class T, class UnaryFunction>
constexpr UnaryFunction jau::for_each_fidelity (T &data, UnaryFunction f, std::enable_if_t< is\_cow\_type< T >::value, bool >=true) noexcept
See jau::for_each_fidelity()
template<class T, class UnaryFunction>
constexpr UnaryFunction jau::for_each_fidelity (T &data, UnaryFunction f, std::enable_if_t< is_cow_type< T >::value, bool >=true) noexcept
See jau::for_each_fidelity()
template<class InputArray, class UnaryFunction>
constexpr UnaryFunction jau::for_each_idx (InputArray &array, UnaryFunction f)
Custom for_each template, using indices instead of iterators, allowing container to be modified within lambda 'callback'.
template<class Mutex, class InputArray, class UnaryFunction>
constexpr UnaryFunction jau::for_each_idx_mtx (Mutex &mtx, InputArray &array, UnaryFunction f)
Custom for_each template, same as jau::for_each but using indices instead of iterators and a mutex.
template<class Mutex, class InputIt, class UnaryFunction>
constexpr UnaryFunction jau::for_each_mtx (Mutex &mtx, InputIt first, InputIt last, UnaryFunction f)
Custom for_each template, same as jau::for_each but using a mutex.
template<class ForwardIt, class T>
ForwardIt jau::remove (ForwardIt first, ForwardIt last, const T &value)
Identical to C++20 std::remove() of algorithm
template<class ForwardIt, class UnaryPredicate>
ForwardIt jau::remove_if (ForwardIt first, ForwardIt last, UnaryPredicate p)
Identical to C++20 std::remove_if() of algorithm

Basic algorithms.

find()

template<class InputIt, class T>

InputIt jau::find ( InputIt first, InputIt last, const T & value ) constexprnoexcept

Like std::find() of 'algorithm'.

Only exists here as performance analysis over O(n*n) complexity exposes std::find() to be approximately 3x slower.
See test/test_cow_darray_perf01.cpp

Template Parameters

InputIt the iterator type
T the data type

Parameters

first range start of elements to examine
last range end of elements to examine, exclusive
value reference value for comparison

Returns

Iterator to the first element satisfying the condition or last if no such element is found.

Definition at line 104 of file basic_algos.hpp.

contains()

template<class InputArray, class T>

bool jau::contains ( InputArray & array, const T & value ) constexprnoexcept

Return true if value is contained in array.

Template Parameters

InputArray array type
T value_type of value

Parameters

array the array to search
value the value to search for

Returns

true if contained, otherwise false

Definition at line 123 of file basic_algos.hpp.

eraseFirst()

template<class InputArray, class T>

bool jau::eraseFirst ( InputArray & array, const T & value ) constexpr

find_if()

template<class InputIt, class UnaryPredicate>

InputIt jau::find_if ( InputIt first, InputIt last, UnaryPredicate p ) constexprnoexcept

Like std::find_if() of 'algorithm'.

Only exists here as performance analysis over O(n*n) complexity exposes std::find_if() to be approximately 3x slower for 1000 x 1000.
See test/test_cow_darray_perf01.cpp

Template Parameters

InputIt the iterator type
UnaryPredicate

Parameters

first range start of elements to examine
last range end of elements to examine, exclusive
p unary predicate which returns ​true for the desired element.

Returns

Iterator to the first element satisfying the condition or last if no such element is found.

Definition at line 162 of file basic_algos.hpp.

find_if_not()

template<class InputIt, class UnaryPredicate>

InputIt jau::find_if_not ( InputIt first, InputIt last, UnaryPredicate q ) constexprnoexcept

Like std::find_if_not() of 'algorithm'.

Only exists here as performance analysis over O(n*n) complexity exposes std::find_if_not() to be approximately 3x slower for 1000 x 1000.
See test/test_cow_darray_perf01.cpp

Template Parameters

InputIt the iterator type
UnaryPredicate

Parameters

first range start of elements to examine
last range end of elements to examine, exclusive
q unary predicate which returns ​false for the desired element.

Returns

Iterator to the first element satisfying the condition or last if no such element is found.

Definition at line 187 of file basic_algos.hpp.

remove()

template<class ForwardIt, class T>

ForwardIt jau::remove ( ForwardIt first,
ForwardIt last,
const T & value )

Identical to C++20 std::remove() of algorithm

Template Parameters

ForwardIt the iterator type
UnaryPredicate

Parameters

first range start of elements to examine
last range end of elements to examine, exclusive
value the value to remove

Returns

past-the end iterator for the new range of values.

Definition at line 208 of file basic_algos.hpp.

remove_if()

template<class ForwardIt, class UnaryPredicate>

ForwardIt jau::remove_if ( ForwardIt first,
ForwardIt last,
UnaryPredicate p )

Identical to C++20 std::remove_if() of algorithm

Template Parameters

ForwardIt the iterator type
UnaryPredicate

Parameters

first range start of elements to examine
last range end of elements to examine, exclusive
p unary predicate which returns true for the desired element to be removed

Returns

past-the end iterator for the new range of values.

Definition at line 232 of file basic_algos.hpp.

for_each()

template<class InputIt, class UnaryFunction>

UnaryFunction jau::for_each ( InputIt first, InputIt last, UnaryFunction f ) constexpr

Like std::for_each() of 'algorithm'.

Only exists here as performance analysis over O(n*n) complexity exposes std::for_each() to be 'a little' slower for 1000 x 1000.
See test/test_cow_darray_perf01.cpp

Template Parameters

InputIt the iterator type
UnaryFunction

Parameters

first range start of elements to apply the function
last range end of elements to apply the function
f the function object, like void fun(const Type &a)

Returns

the function

Definition at line 260 of file basic_algos.hpp.

for_each_fidelity() [1/3]

template<class InputIt, class UnaryFunction>

UnaryFunction jau::for_each_fidelity ( InputIt first, InputIt last, UnaryFunction f ) constexpr

Like jau::for_each(), see above.

Additionally this template function removes the const qualifier of the UnaryFunction sole argument.
The latter is retrieved by dereferencing the iterator, which might expose the const qualifier if the iterator is a const_iterator.

Implementation casts argument in the following fashion const_cast<value_type*>(&arg), allowing to use const_iterator and subsequent non-const features of the argument, see below.

Such situations may occur when preferring to use the const_iterator over non-const.
jau::cow_darray is such a scenario, where one might not mutate the elements of the container itself but needs to invoke non-const functions in good faith.
Here we can avoid costly side-effects of copying the CoW storage for later replacement.
See jau::cow_ro_iterator and jau::cow_rw_iterator in conjunction with jau::cow_darray.

Requirements for the given IteratorIt type are to have typename InputIt::value_type available.

Template Parameters

InputIt the iterator type, which might be a 'const_iterator' for non const types.
UnaryFunction

Parameters

first range start of elements to apply the function
last range end of elements to apply the function
f the function object, like void fun(const Type &a)

Returns

the function

See also

jau::cow_darray

jau::cow_ro_iterator

jau::cow_rw_iterator

Definition at line 312 of file basic_algos.hpp.

for_each_mtx()

template<class Mutex, class InputIt, class UnaryFunction>

UnaryFunction jau::for_each_mtx ( Mutex & mtx, InputIt first, InputIt last, UnaryFunction f ) constexpr

Custom for_each template, same as jau::for_each but using a mutex.

Method performs UnaryFunction on all elements [first..last).

This method also utilizes a given mutex to ensure thread-safety, by operating within an RAII-style std::lock_guard block.

Definition at line 336 of file basic_algos.hpp.

for_each_idx()

template<class InputArray, class UnaryFunction>

UnaryFunction jau::for_each_idx ( InputArray & array, UnaryFunction f ) constexpr

Custom for_each template, using indices instead of iterators, allowing container to be modified within lambda 'callback'.

Method performs UnaryFunction on all elements [0..n-1], where n is being retrieved once before the loop!

Definition at line 355 of file basic_algos.hpp.

for_each_idx_mtx()

template<class Mutex, class InputArray, class UnaryFunction>

UnaryFunction jau::for_each_idx_mtx ( Mutex & mtx, InputArray & array, UnaryFunction f ) constexpr

Custom for_each template, same as jau::for_each but using indices instead of iterators and a mutex.

Method performs UnaryFunction on all elements [0..n-1], where n is being retrieved once before the loop!

This method also utilizes a given mutex to ensure thread-safety, by operating within an RAII-style std::lock_guard block.

Definition at line 377 of file basic_algos.hpp.

find_const() [1/2]

template

const T::value_type * jau::find_const ( T & data, typename T::value_type const & elem, std::enable_if_t< is_cow_type< T >::value, bool > = true ) noexcept

find_const() [2/2]

template

const T::value_type * jau::find_const ( T & data, typename T::value_type const & elem, std::enable_if_t< is\_cow\_type< T >::value, bool > = true ) noexcept

for_each_const() [1/2]

template<class T, class UnaryFunction>

UnaryFunction jau::for_each_const ( T & data, UnaryFunction f, std::enable_if_t< is_cow_type< T >::value, bool > = true ) constexprnoexcept

for_each_const() [2/2]

template<class T, class UnaryFunction>

UnaryFunction jau::for_each_const ( T & data, UnaryFunction f, std::enable_if_t< is\_cow\_type< T >::value, bool > = true ) constexprnoexcept

for_each_fidelity() [2/3]

template<class T, class UnaryFunction>

UnaryFunction jau::for_each_fidelity ( T & data, UnaryFunction f, std::enable_if_t< is_cow_type< T >::value, bool > = true ) constexprnoexcept

for_each_fidelity() [3/3]

template<class T, class UnaryFunction>

UnaryFunction jau::for_each_fidelity ( T & data, UnaryFunction f, std::enable_if_t< is\_cow\_type< T >::value, bool > = true ) constexprnoexcept