-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bef873a
commit 3f996ab
Showing
15 changed files
with
1,098 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2024 Man Group Operations Limited | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <compare> | ||
#include <concepts> | ||
#include "sparrow/config.hpp" | ||
|
||
namespace sparrow | ||
{ | ||
#if COMPILING_WITH_APPLE_CLANG | ||
|
||
template <typename T> | ||
concept OrdCategory = std::same_as<T, std::strong_ordering> || std::same_as<T, std::weak_ordering> | ||
|| std::same_as<T, std::partial_ordering>; | ||
|
||
template <class R1, class R2, class Cmp> | ||
concept LexicographicalComparable = requires(const R1& r1, const R2& r2, Cmp comp) { | ||
{ r1.cbegin() } -> std::input_or_output_iterator; | ||
{ r2.cbegin() } -> std::input_or_output_iterator; | ||
OrdCategory<decltype(comp(*r1.cbegin(), *r2.cbegin()))>; | ||
}; | ||
|
||
template <class R1, class R2, class Cmp> | ||
requires LexicographicalComparable<R1, R2, Cmp> | ||
constexpr auto lexicographical_compare_three_way_non_std(const R1& range1, const R2& range2, Cmp comp) | ||
-> decltype(comp(*range1.cbegin(), *range1.cbegin())) | ||
{ | ||
auto iter_1 = range1.cbegin(); | ||
const auto end_1 = range1.cend(); | ||
auto iter_2 = range2.cbegin(); | ||
const auto end_2 = range2.cend(); | ||
|
||
while (true) | ||
{ | ||
if (iter_1 == end_1) | ||
{ | ||
return iter_2 == end_2 ? std::strong_ordering::equal : std::strong_ordering::less; | ||
} | ||
|
||
if (iter_2 == end_2) | ||
{ | ||
return std::strong_ordering::greater; | ||
} | ||
|
||
if (const auto result = comp(*iter_1, *iter_2); result != 0) | ||
{ | ||
return result; | ||
} | ||
|
||
++iter_1; | ||
++iter_2; | ||
} | ||
} | ||
#endif | ||
|
||
template <class R1, class R2, class Cmp> | ||
constexpr auto lexicographical_compare_three_way(const R1& range1, const R2& range2, Cmp comp) | ||
-> decltype(comp(*range1.cbegin(), *range2.cbegin())) | ||
{ | ||
#if COMPILING_WITH_APPLE_CLANG | ||
return lexicographical_compare_three_way_non_std(range1, range2, comp); | ||
#else | ||
return std::lexicographical_compare_three_way( | ||
range1.cbegin(), | ||
range1.cend(), | ||
range2.cbegin(), | ||
range2.cend(), | ||
comp | ||
); | ||
#endif | ||
} | ||
|
||
#if COMPILING_WITH_APPLE_CLANG | ||
struct compare_three_way | ||
{ | ||
template <class T, class U> | ||
constexpr auto operator()(const T& t, const U& u) const noexcept -> std::partial_ordering | ||
{ | ||
if (t < u) | ||
{ | ||
return std::partial_ordering::less; | ||
} | ||
if (u < t) | ||
{ | ||
return std::partial_ordering::greater; | ||
} | ||
return std::partial_ordering::equivalent; | ||
} | ||
}; | ||
#endif | ||
|
||
template <class R1, class R2> | ||
constexpr auto lexicographical_compare_three_way(const R1& r1, const R2& r2) -> std::partial_ordering | ||
{ | ||
return lexicographical_compare_three_way<R1, R2>( | ||
r1, | ||
r2, | ||
#if COMPILING_WITH_APPLE_CLANG | ||
compare_three_way {} | ||
#else | ||
std::compare_three_way{} | ||
#endif | ||
); | ||
} | ||
|
||
template <class R1, class R2> | ||
constexpr auto lexicographical_compare(const R1& r1, const R2& r2) -> bool | ||
{ | ||
return lexicographical_compare_three_way(r1, r2) == std::strong_ordering::less; | ||
} | ||
|
||
} // namespace sparrow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 Man Group Operations Limited | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#if defined(__apple_build_version__) | ||
#define COMPILING_WITH_APPLE_CLANG 1 | ||
#else | ||
#define COMPILING_WITH_APPLE_CLANG 0 | ||
#endif | ||
|
||
consteval bool is_apple_compiler() | ||
{ | ||
return static_cast<bool>(COMPILING_WITH_APPLE_CLANG); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.