xref: /freebsd/contrib/llvm-project/libcxx/include/__algorithm/includes.h (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___ALGORITHM_INCLUDES_H
10 #define _LIBCPP___ALGORITHM_INCLUDES_H
11 
12 #include <__algorithm/comp.h>
13 #include <__algorithm/comp_ref_type.h>
14 #include <__config>
15 #include <__functional/identity.h>
16 #include <__functional/invoke.h>
17 #include <__iterator/iterator_traits.h>
18 #include <__type_traits/is_callable.h>
19 #include <__utility/move.h>
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #  pragma GCC system_header
23 #endif
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 template <class _Iter1, class _Sent1, class _Iter2, class _Sent2, class _Comp, class _Proj1, class _Proj2>
28 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
29 __includes(_Iter1 __first1, _Sent1 __last1, _Iter2 __first2, _Sent2 __last2,
30            _Comp&& __comp, _Proj1&& __proj1, _Proj2&& __proj2) {
31   for (; __first2 != __last2; ++__first1) {
32     if (__first1 == __last1 || std::__invoke(
33           __comp, std::__invoke(__proj2, *__first2), std::__invoke(__proj1, *__first1)))
34       return false;
35     if (!std::__invoke(__comp, std::__invoke(__proj1, *__first1), std::__invoke(__proj2, *__first2)))
36       ++__first2;
37   }
38   return true;
39 }
40 
41 template <class _InputIterator1, class _InputIterator2, class _Compare>
42 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool includes(
43     _InputIterator1 __first1,
44     _InputIterator1 __last1,
45     _InputIterator2 __first2,
46     _InputIterator2 __last2,
47     _Compare __comp) {
48   static_assert(__is_callable<_Compare, decltype(*__first1), decltype(*__first2)>::value,
49       "Comparator has to be callable");
50 
51   typedef typename __comp_ref_type<_Compare>::type _Comp_ref;
52   return std::__includes(
53       std::move(__first1), std::move(__last1), std::move(__first2), std::move(__last2),
54       static_cast<_Comp_ref>(__comp), __identity(), __identity());
55 }
56 
57 template <class _InputIterator1, class _InputIterator2>
58 _LIBCPP_NODISCARD_EXT inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
59 includes(_InputIterator1 __first1, _InputIterator1 __last1, _InputIterator2 __first2, _InputIterator2 __last2) {
60   return std::includes(
61       std::move(__first1),
62       std::move(__last1),
63       std::move(__first2),
64       std::move(__last2),
65       __less<typename iterator_traits<_InputIterator1>::value_type,
66              typename iterator_traits<_InputIterator2>::value_type>());
67 }
68 
69 _LIBCPP_END_NAMESPACE_STD
70 
71 #endif // _LIBCPP___ALGORITHM_INCLUDES_H
72