contains.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Johel Guerrero 2019
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_ALGORITHM_CONTAINS_HPP
  14. #define RANGES_V3_ALGORITHM_CONTAINS_HPP
  15. #include <utility>
  16. #include <concepts/concepts.hpp>
  17. #include <range/v3/algorithm/find.hpp>
  18. #include <range/v3/detail/config.hpp>
  19. #include <range/v3/functional/comparisons.hpp>
  20. #include <range/v3/functional/identity.hpp>
  21. #include <range/v3/iterator/concepts.hpp>
  22. #include <range/v3/range/access.hpp>
  23. #include <range/v3/range/concepts.hpp>
  24. #include <range/v3/detail/prologue.hpp>
  25. namespace ranges
  26. {
  27. /// \addtogroup group-algorithms
  28. /// @{
  29. RANGES_FUNC_BEGIN(contains)
  30. /// \brief function template \c contains
  31. template(typename I, typename S, typename T, typename P = identity)(
  32. requires input_iterator<I> AND sentinel_for<S, I> AND
  33. indirect_relation<equal_to, projected<I, P>, const T *>)
  34. constexpr bool RANGES_FUNC(contains)(I first, S last, const T & val, P proj = {})
  35. {
  36. return find(std::move(first), last, val, std::move(proj)) != last;
  37. }
  38. /// \overload
  39. template(typename Rng, typename T, typename P = identity)(
  40. requires input_range<Rng> AND
  41. indirect_relation<equal_to, projected<iterator_t<Rng>, P>, const T *>)
  42. constexpr bool RANGES_FUNC(contains)(Rng && rng, const T & val, P proj = {})
  43. {
  44. return (*this)(begin(rng), end(rng), val, std::move(proj));
  45. }
  46. RANGES_FUNC_END(contains)
  47. /// @}
  48. } // namespace ranges
  49. #include <range/v3/detail/epilogue.hpp>
  50. #endif // RANGES_V3_ALGORITHM_CONTAINS_HPP