diff options
author | Sven Gothel <[email protected]> | 2022-11-13 05:44:30 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-11-13 05:44:30 +0100 |
commit | 5fd6dcec6538ad4bc453410fe41d634f20a1fe15 (patch) | |
tree | cc073e5a61c3a48875e7feafdcb0256002b12248 /include/jau/basic_algos.hpp | |
parent | 2fc95722a583b2272a51049adbb8dc8b7c7370ee (diff) |
basic_algo: Use InputArray::size_type not size_t; Add contains()
Diffstat (limited to 'include/jau/basic_algos.hpp')
-rw-r--r-- | include/jau/basic_algos.hpp | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/include/jau/basic_algos.hpp b/include/jau/basic_algos.hpp index 06546a8..9cebaa8 100644 --- a/include/jau/basic_algos.hpp +++ b/include/jau/basic_algos.hpp @@ -112,6 +112,26 @@ namespace jau { } /** + * Return true if `value` is contained in `array`. + * @tparam InputArray array type + * @tparam T value_type of value + * @param array the array to search + * @param value the value to search for + * @return true if contained, otherwise false + */ + template<class InputArray, class T> + constexpr bool contains(InputArray &array, const T& value) + { + const typename InputArray::size_type size = array.size(); + for (typename InputArray::size_type i = 0; i < size; ++i) { + if( value == array[i] ) { + return true; + } + } + return false; + } + + /** * Like std::find_if() of 'algorithm' * <p> * Only exists here as performance analysis over O(n*n) complexity @@ -297,8 +317,8 @@ namespace jau { template<class InputArray, class UnaryFunction> constexpr UnaryFunction for_each_idx(InputArray &array, UnaryFunction f) { - const size_t size = array.size(); - for (size_t i = 0; i < size; ++i) { + const typename InputArray::size_type size = array.size(); + for (typename InputArray::size_type i = 0; i < size; ++i) { f(array[i]); } return f; // implicit move since C++11 @@ -321,8 +341,8 @@ namespace jau { { const std::lock_guard<Mutex> lock(mtx); // RAII-style acquire and relinquish via destructor - const size_t size = array.size(); - for (size_t i = 0; i < size; ++i) { + const typename InputArray::size_type size = array.size(); + for (typename InputArray::size_type i = 0; i < size; ++i) { f(array[i]); } return f; // implicit move since C++11 |