DUNE PDELab (git)

algorithm.hh
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
17#include <dune/common/std/compare.hh>
18
19namespace Dune::Std {
20
30#if __cpp_lib_three_way_comparison >= 201907L
31
32using std::lexicographical_compare_three_way;
33
34#else // __cpp_lib_three_way_comparison
35
36template <class I1, class I2, class Cmp = Std::compare_three_way>
37constexpr 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:
55 std::strong_ordering::equal;
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 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
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jan 8, 23:30, 2025)