jaulib v1.3.0
Jau Support Library (C++, Java, ..)
Classes | Functions
Basic Algorithms

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...
 

Functions

template<class InputArray , class T >
constexpr bool jau::contains (InputArray &array, const T &value)
 Return true if value is contained in array. More...
 
template<class InputIt , class T >
constexpr InputIt jau::find (InputIt first, InputIt last, const T &value)
 Like std::find() of 'algorithm'. More...
 
template<class T >
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 T >
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)
 Like std::find_if() of 'algorithm'. More...
 
template<class InputIt , class UnaryPredicate >
constexpr InputIt jau::find_if_not (InputIt first, InputIt last, UnaryPredicate q)
 Like std::find_if_not() of 'algorithm'. More...
 
template<class InputIt , class UnaryFunction >
constexpr UnaryFunction jau::for_each (InputIt first, InputIt last, UnaryFunction f)
 Like std::for_each() of 'algorithm'. More...
 
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. More...
 
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() More...
 
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() More...
 
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'. More...
 
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. More...
 
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. More...
 
template<class ForwardIt , class UnaryPredicate >
ForwardIt jau::remove_if (ForwardIt first, ForwardIt last, UnaryPredicate p)
 Identical to C++20 std::remove_if() of algorithm More...
 

Detailed Description

Basic algorithms.

Function Documentation

◆ find()

template<class InputIt , class T >
constexpr InputIt jau::find ( InputIt  first,
InputIt  last,
const T &  value 
)
constexpr

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
InputItthe iterator type
Tthe data type
Parameters
firstrange start of elements to examine
lastrange end of elements to examine, exclusive
valuereference 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.

Here is the caller graph for this function:

◆ contains()

template<class InputArray , class T >
constexpr bool jau::contains ( InputArray &  array,
const T &  value 
)
constexpr

Return true if value is contained in array.

Template Parameters
InputArrayarray type
Tvalue_type of value
Parameters
arraythe array to search
valuethe value to search for
Returns
true if contained, otherwise false

Definition at line 123 of file basic_algos.hpp.

◆ find_if()

template<class InputIt , class UnaryPredicate >
constexpr InputIt jau::find_if ( InputIt  first,
InputIt  last,
UnaryPredicate  p 
)
constexpr

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
InputItthe iterator type
UnaryPredicate
Parameters
firstrange start of elements to examine
lastrange end of elements to examine, exclusive
punary 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 149 of file basic_algos.hpp.

Here is the caller graph for this function:

◆ find_if_not()

template<class InputIt , class UnaryPredicate >
constexpr InputIt jau::find_if_not ( InputIt  first,
InputIt  last,
UnaryPredicate  q 
)
constexpr

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
InputItthe iterator type
UnaryPredicate
Parameters
firstrange start of elements to examine
lastrange end of elements to examine, exclusive
qunary 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 174 of file basic_algos.hpp.

Here is the caller graph for this function:

◆ 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
ForwardItthe iterator type
UnaryPredicate
Parameters
firstrange start of elements to examine
lastrange end of elements to examine, exclusive
punary 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 195 of file basic_algos.hpp.

Here is the caller graph for this function:

◆ for_each()

template<class InputIt , class UnaryFunction >
constexpr 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
InputItthe iterator type
UnaryFunction
Parameters
firstrange start of elements to apply the function
lastrange end of elements to apply the function
fthe function object, like void fun(const Type &a)
Returns
the function

Definition at line 223 of file basic_algos.hpp.

Here is the caller graph for this function:

◆ for_each_fidelity() [1/3]

template<class InputIt , class UnaryFunction >
constexpr 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
InputItthe iterator type, which might be a 'const_iterator' for non const types.
UnaryFunction
Parameters
firstrange start of elements to apply the function
lastrange end of elements to apply the function
fthe 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 275 of file basic_algos.hpp.

◆ for_each_mtx()

template<class Mutex , class InputIt , class UnaryFunction >
constexpr 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 299 of file basic_algos.hpp.

◆ for_each_idx()

template<class InputArray , class UnaryFunction >
constexpr 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 318 of file basic_algos.hpp.

◆ for_each_idx_mtx()

template<class Mutex , class InputArray , class UnaryFunction >
constexpr 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 340 of file basic_algos.hpp.

◆ find_const() [1/2]

template<class T >
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

Definition at line 355 of file basic_algos.hpp.

◆ find_const() [2/2]

template<class T >
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

Definition at line 366 of file basic_algos.hpp.

◆ for_each_const() [1/2]

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 
)
constexprnoexcept
Examples
test_cow_iterator_01.cpp.

Definition at line 383 of file basic_algos.hpp.

Here is the caller graph for this function:

◆ for_each_const() [2/2]

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 
)
constexprnoexcept

Definition at line 392 of file basic_algos.hpp.

◆ for_each_fidelity() [2/3]

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 
)
constexprnoexcept

See jau::for_each_fidelity()

Definition at line 410 of file basic_algos.hpp.

◆ for_each_fidelity() [3/3]

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 
)
constexprnoexcept

See jau::for_each_fidelity()

Definition at line 422 of file basic_algos.hpp.