xref: /freebsd-src/contrib/llvm-project/libcxx/include/__algorithm/includes.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric 
9fe6060f1SDimitry Andric #ifndef _LIBCPP___ALGORITHM_INCLUDES_H
10fe6060f1SDimitry Andric #define _LIBCPP___ALGORITHM_INCLUDES_H
11fe6060f1SDimitry Andric 
12fe6060f1SDimitry Andric #include <__algorithm/comp.h>
13fe6060f1SDimitry Andric #include <__algorithm/comp_ref_type.h>
1404eeddc0SDimitry Andric #include <__config>
15972a253aSDimitry Andric #include <__functional/identity.h>
16972a253aSDimitry Andric #include <__functional/invoke.h>
17fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
18972a253aSDimitry Andric #include <__type_traits/is_callable.h>
19fcaf7f86SDimitry Andric #include <__utility/move.h>
20fe6060f1SDimitry Andric 
21fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22fe6060f1SDimitry Andric #  pragma GCC system_header
23fe6060f1SDimitry Andric #endif
24fe6060f1SDimitry Andric 
25b3edf446SDimitry Andric _LIBCPP_PUSH_MACROS
26b3edf446SDimitry Andric #include <__undef_macros>
27b3edf446SDimitry Andric 
28fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
29fe6060f1SDimitry Andric 
30972a253aSDimitry Andric template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Comp, class _Proj1, class _Proj2>
31cb14a3feSDimitry Andric _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __includes(
32cb14a3feSDimitry Andric     _Iter1 __first1,
33cb14a3feSDimitry Andric     _Sent1 __last1,
34cb14a3feSDimitry Andric     _Iter2 __first2,
35cb14a3feSDimitry Andric     _Sent2 __last2,
36cb14a3feSDimitry Andric     _Comp&& __comp,
37cb14a3feSDimitry Andric     _Proj1&& __proj1,
38cb14a3feSDimitry Andric     _Proj2&& __proj2) {
39fcaf7f86SDimitry Andric   for (; __first2 != __last2; ++__first1) {
40cb14a3feSDimitry Andric     if (__first1 == __last1 ||
41cb14a3feSDimitry Andric         std::__invoke(__comp, std::__invoke(__proj2, *__first2), std::__invoke(__proj1, *__first1)))
42fe6060f1SDimitry Andric       return false;
43972a253aSDimitry Andric     if (!std::__invoke(__comp, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
44fe6060f1SDimitry Andric       ++__first2;
45fe6060f1SDimitry Andric   }
46fe6060f1SDimitry Andric   return true;
47fe6060f1SDimitry Andric }
48fe6060f1SDimitry Andric 
49fe6060f1SDimitry Andric template <class _InputIterator1, class _InputIterator2, class _Compare>
50*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
51cb14a3feSDimitry Andric includes(_InputIterator1 __first1,
52fcaf7f86SDimitry Andric          _InputIterator1 __last1,
53fcaf7f86SDimitry Andric          _InputIterator2 __first2,
54fcaf7f86SDimitry Andric          _InputIterator2 __last2,
55fcaf7f86SDimitry Andric          _Compare __comp) {
56cb14a3feSDimitry Andric   static_assert(
57cb14a3feSDimitry Andric       __is_callable<_Compare, decltype(*__first1), decltype(*__first2)>::value, "Comparator has to be callable");
58972a253aSDimitry Andric 
59fcaf7f86SDimitry Andric   return std::__includes(
60bdd1243dSDimitry Andric       std::move(__first1),
61bdd1243dSDimitry Andric       std::move(__last1),
62bdd1243dSDimitry Andric       std::move(__first2),
63bdd1243dSDimitry Andric       std::move(__last2),
64bdd1243dSDimitry Andric       static_cast<__comp_ref_type<_Compare> >(__comp),
65bdd1243dSDimitry Andric       __identity(),
66bdd1243dSDimitry Andric       __identity());
67fe6060f1SDimitry Andric }
68fe6060f1SDimitry Andric 
69fe6060f1SDimitry Andric template <class _InputIterator1, class _InputIterator2>
70*0fca6ea1SDimitry Andric _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool
71fcaf7f86SDimitry Andric includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
7206c3fb27SDimitry Andric   return std::includes(std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2), __less<>());
73fe6060f1SDimitry Andric }
74fe6060f1SDimitry Andric 
75fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
76fe6060f1SDimitry Andric 
77b3edf446SDimitry Andric _LIBCPP_POP_MACROS
78b3edf446SDimitry Andric 
79fe6060f1SDimitry Andric #endif // _LIBCPP___ALGORITHM_INCLUDES_H
80