mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-23 12:51:06 +00:00
Bug 1677092: add constexpr
AnyOf
. r=sg
The STL supports it only with C++20. Differential Revision: https://phabricator.services.mozilla.com/D97123
This commit is contained in:
parent
f23f8b8c50
commit
e0d88600e7
@ -28,6 +28,18 @@ constexpr bool AllOf(Iter aFirst, Iter aLast, Pred aPred) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Like C++20's `std::any_of`.
|
||||
template <typename Iter, typename Pred>
|
||||
constexpr bool AnyOf(Iter aFirst, Iter aLast, Pred aPred) {
|
||||
for (; aFirst != aLast; ++aFirst) {
|
||||
if (aPred(*aFirst)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
template <typename Transform, typename SrcIter>
|
||||
using ArrayElementTransformType = typename std::invoke_result_t<
|
||||
|
@ -7,13 +7,15 @@
|
||||
#include "mozilla/Algorithm.h"
|
||||
#include "mozilla/ArrayUtils.h"
|
||||
#include "mozilla/Assertions.h"
|
||||
|
||||
#include <iterator>
|
||||
|
||||
static constexpr bool even(int32_t n) { return !(n & 1); }
|
||||
static constexpr bool odd(int32_t n) { return (n & 1); }
|
||||
|
||||
using namespace mozilla;
|
||||
|
||||
void TestAllOf() {
|
||||
using namespace mozilla;
|
||||
using std::begin;
|
||||
using std::end;
|
||||
|
||||
@ -36,7 +38,31 @@ void TestAllOf() {
|
||||
static_assert(!AllOf(arr3, arr3 + ArrayLength(arr3), odd), "3-2");
|
||||
}
|
||||
|
||||
void TestAnyOf() {
|
||||
using std::begin;
|
||||
using std::end;
|
||||
|
||||
// The Android NDK's STL doesn't support `constexpr` `std::array::begin`, see
|
||||
// bug 1677484. Hence using a raw array here.
|
||||
constexpr int32_t arr1[1] = {0};
|
||||
static_assert(!AnyOf(arr1, arr1, even));
|
||||
static_assert(!AnyOf(arr1, arr1, odd));
|
||||
|
||||
constexpr int32_t arr2[] = {1};
|
||||
static_assert(!AnyOf(begin(arr2), end(arr2), even));
|
||||
static_assert(AnyOf(begin(arr2), end(arr2), odd));
|
||||
|
||||
constexpr int32_t arr3[] = {2};
|
||||
static_assert(AnyOf(begin(arr3), end(arr3), even));
|
||||
static_assert(!AnyOf(begin(arr3), end(arr3), odd));
|
||||
|
||||
constexpr int32_t arr4[] = {1, 2};
|
||||
static_assert(AnyOf(begin(arr4), end(arr4), even));
|
||||
static_assert(AnyOf(begin(arr4), end(arr4), odd));
|
||||
}
|
||||
|
||||
int main() {
|
||||
TestAllOf();
|
||||
TestAnyOf();
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user