dune-common  2.11
algorithm.hh
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifndef DUNE_COMMON_STD_ALGORITHM_HH
6 #define DUNE_COMMON_STD_ALGORITHM_HH
7 
8 #include <version>
9 #if !(__cpp_impl_three_way_comparison >= 201907L && __cpp_lib_concepts && __has_include(<compare>))
10  #error "Three-way comparison requires language support!"
11 #endif
12 
13 #include <algorithm>
14 #include <compare>
15 #include <type_traits>
16 
18 
19 namespace Dune::Std {
20 
30 #if __cpp_lib_three_way_comparison >= 201907L
31 
33 
34 #else // __cpp_lib_three_way_comparison
35 
36 template <class I1, class I2, class Cmp = Std::compare_three_way>
37 constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2, Cmp comp = {})
38  -> decltype(comp(*f1, *f2))
39 {
40  using ret_t = decltype(comp(*f1, *f2));
41  static_assert(std::disjunction_v<
42  std::is_same<ret_t, std::strong_ordering>,
43  std::is_same<ret_t, std::weak_ordering>,
44  std::is_same<ret_t, std::partial_ordering>>,
45  "The return type must be a comparison category type.");
46 
47  bool exhaust1 = (f1 == l1);
48  bool exhaust2 = (f2 == l2);
49  for (; !exhaust1 && !exhaust2; exhaust1 = (++f1 == l1), exhaust2 = (++f2 == l2))
50  if (auto c = comp(*f1, *f2); c != 0)
51  return c;
52 
53  return !exhaust1 ? std::strong_ordering::greater:
54  !exhaust2 ? std::strong_ordering::less:
56 }
57 
58 #endif // __cpp_lib_three_way_comparison
59 
60 } // end namespace Dune::Std
61 
62 #endif // DUNE_COMMON_STD_ALGORITHM_HH
Namespace for features backported from new C++ standards.
Definition: algorithm.hh:19
constexpr std::is_same< std::integer_sequence< bool, true,(ST(II)==ST(JJ))... >, std::integer_sequence< bool,(ST(II)==ST(JJ))..., true > > equal(std::integer_sequence< S, II... >, std::integer_sequence< T, JJ... >)
Checks whether two sequences are identical.
Definition: integersequence.hh:170
This file provides some concepts introduced in the C++20 standard library <compare> and <concepts> no...
constexpr auto lexicographical_compare_three_way(I1 f1, I1 l1, I2 f2, I2 l2, Cmp comp={}) -> decltype(comp(*f1, *f2))
Lexicographically compares two ranges [first1, last1) and [first2, last2) using three-way comparison ...
Definition: algorithm.hh:37