|
| 1 | +// Copyright (c) Darrell Wright |
| 2 | +// |
| 3 | +// Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | +// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | +// |
| 6 | +// Official repository: https://github.com/beached/header_libraries |
| 7 | +// |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include "daw/daw_attributes.h" |
| 12 | +#include "daw/daw_ordering_to_integer.h" |
| 13 | +#include "daw/daw_traits.h" |
| 14 | +#include "daw/traits/daw_remove_array_ref.h" |
| 15 | + |
| 16 | +#include <functional> |
| 17 | +#include <type_traits> |
| 18 | + |
| 19 | +namespace daw { |
| 20 | + namespace array_cmp_details { |
| 21 | +#if defined( DAW_HAS_CPP20_3WAY ) |
| 22 | + template<typename Order> |
| 23 | + inline constexpr Order equal_or_equivalent_v = |
| 24 | + daw::equal_or_equivalent_v<Order>; |
| 25 | +#else |
| 26 | + template<typename> |
| 27 | + inline constexpr bool equal_or_equivalent_v = true; |
| 28 | +#endif |
| 29 | + } // namespace array_cmp_details |
| 30 | + |
| 31 | + template<typename T, typename Compare = std::equal_to<>, |
| 32 | + typename U = std::enable_if_t<std::is_array_v<remove_array_ref_t<T>>, |
| 33 | + remove_array_ref_t<T>>> |
| 34 | + DAW_ATTRIB_FLATTEN constexpr auto |
| 35 | + array_cmp( T &&lhs, T &&rhs, Compare const &cmp = Compare{ } ) { |
| 36 | + using compare_result_t = |
| 37 | + std::invoke_result_t<Compare, std::remove_all_extents_t<U>, |
| 38 | + std::remove_all_extents_t<U>>; |
| 39 | + for( std::size_t n = 0; n < std::extent_v<U>; ++n ) { |
| 40 | + auto r = [&] { |
| 41 | + if constexpr( std::rank_v<U> > 1 ) { |
| 42 | + return array_cmp( lhs[n], rhs[n], cmp ); |
| 43 | + } else { |
| 44 | + return cmp( lhs[n], rhs[n] ); |
| 45 | + } |
| 46 | + }( ); |
| 47 | + if( r != array_cmp_details::equal_or_equivalent_v<compare_result_t> ) { |
| 48 | + return r; |
| 49 | + } |
| 50 | + } |
| 51 | + return array_cmp_details::equal_or_equivalent_v<compare_result_t>; |
| 52 | + } |
| 53 | +} // namespace daw |
0 commit comments