26#ifndef JAU_BASIC_ALGOS_HPP_
27#define JAU_BASIC_ALGOS_HPP_
72 : f(release_func), released(
false) {}
74 if( !released ) { f(); }
103 template<
class InputIt,
class T>
104 constexpr InputIt
find(InputIt first, InputIt last,
const T& value)
106 for (; first != last; ++first) {
107 if (*first == value) {
122 template<
class InputArray,
class T>
123 constexpr bool contains(InputArray &array,
const T& value)
125 const typename InputArray::size_type size = array.size();
126 for (
typename InputArray::size_type i = 0; i < size; ++i) {
127 if( value == array[i] ) {
148 template<
class InputIt,
class UnaryPredicate>
149 constexpr InputIt
find_if(InputIt first, InputIt last, UnaryPredicate p)
151 for (; first != last; ++first) {
173 template<
class InputIt,
class UnaryPredicate>
174 constexpr InputIt
find_if_not(InputIt first, InputIt last, UnaryPredicate q)
176 for (; first != last; ++first) {
194 template<
class ForwardIt,
class UnaryPredicate>
195 ForwardIt
remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p)
199 for(ForwardIt i = first; ++i != last; ) {
201 *first++ = std::move(*i);
222 template<
class InputIt,
class UnaryFunction>
223 constexpr UnaryFunction
for_each(InputIt first, InputIt last, UnaryFunction f)
225 for (; first != last; ++first) {
274 template<
class InputIt,
class UnaryFunction>
277 typedef typename InputIt::value_type value_type;
279 for (; first != last; ++first) {
280 f( *
const_cast<value_type*
>( & (*first) ) );
298 template<
class Mutex,
class InputIt,
class UnaryFunction>
299 constexpr UnaryFunction
for_each_mtx(Mutex &mtx, InputIt first, InputIt last, UnaryFunction f)
301 const std::lock_guard<Mutex> lock(mtx);
303 for (; first != last; ++first) {
317 template<
class InputArray,
class UnaryFunction>
318 constexpr UnaryFunction
for_each_idx(InputArray &array, UnaryFunction f)
320 const typename InputArray::size_type size = array.size();
321 for (
typename InputArray::size_type i = 0; i < size; ++i) {
339 template<
class Mutex,
class InputArray,
class UnaryFunction>
342 const std::lock_guard<Mutex> lock(mtx);
344 const typename InputArray::size_type size = array.size();
345 for (
typename InputArray::size_type i = 0; i < size; ++i) {
355 const typename T::value_type *
find_const(T& data,
typename T::value_type
const & elem,
358 for (
typename T::const_iterator first = data.cbegin(); !first.is_end(); ++first) {
359 if (*first == elem) {
366 const typename T::value_type *
find_const(T& data,
typename T::value_type
const & elem,
369 typename T::const_iterator first = data.cbegin();
370 typename T::const_iterator last = data.cend();
371 for (; first != last; ++first) {
372 if (*first == elem) {
382 template<
class T,
class UnaryFunction>
386 for (
typename T::const_iterator first = data.cbegin(); !first.is_end(); ++first) {
391 template<
class T,
class UnaryFunction>
395 typename T::const_iterator first = data.cbegin();
396 typename T::const_iterator last = data.cend();
397 for (; first != last; ++first) {
409 template<
class T,
class UnaryFunction>
413 for (
typename T::const_iterator first = data.cbegin(); !first.is_end(); ++first) {
414 f( *
const_cast<typename T::value_type*
>( & (*first) ) );
421 template<
class T,
class UnaryFunction>
425 typename T::const_iterator first = data.cbegin();
426 typename T::const_iterator last = data.cend();
427 for (; first != last; ++first) {
428 f( *
const_cast<typename T::value_type*
>( & (*first) ) );
Call on release allows the user to pass a function to be called at destruction of this instance.
~call_on_release() noexcept
call_on_release(UnaryFunction release_func) noexcept
call_on_release & operator=(const call_on_release &)=delete
call_on_release & operator=(const call_on_release &) volatile=delete
call_on_release(const call_on_release &)=delete
void set_released() noexcept
Mark the resource being orderly released, release_func() will not be called and use after free avoide...
bool is_released() const noexcept
Query whethr the resource has been orderly released.
ForwardIt remove_if(ForwardIt first, ForwardIt last, UnaryPredicate p)
Identical to C++20 std::remove_if() of algorithm
constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPredicate q)
Like std::find_if_not() of 'algorithm'.
constexpr bool contains(InputArray &array, const T &value)
Return true if value is contained in array.
const T::value_type * find_const(T &data, typename T::value_type const &elem, std::enable_if_t< is_cow_type< T >::value, bool >=true) noexcept
constexpr UnaryFunction 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.
constexpr UnaryFunction for_each(InputIt first, InputIt last, UnaryFunction f)
Like std::for_each() of 'algorithm'.
constexpr UnaryFunction for_each_mtx(Mutex &mtx, InputIt first, InputIt last, UnaryFunction f)
Custom for_each template, same as jau::for_each but using a mutex.
constexpr InputIt find(InputIt first, InputIt last, const T &value)
Like std::find() of 'algorithm'.
constexpr UnaryFunction for_each_fidelity(InputIt first, InputIt last, UnaryFunction f)
Like jau::for_each(), see above.
constexpr InputIt find_if(InputIt first, InputIt last, UnaryPredicate p)
Like std::find_if() of 'algorithm'.
constexpr UnaryFunction for_each_const(T &data, UnaryFunction f, std::enable_if_t< is_cow_type< T >::value, bool >=true) noexcept
constexpr UnaryFunction for_each_idx(InputArray &array, UnaryFunction f)
Custom for_each template, using indices instead of iterators, allowing container to be modified withi...
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.
template< class T > is_cow_type<T>::value compile-time Type Trait, determining whether the given temp...