Skip to content

Commit e44cea5

Browse files
authored
[libc][CPP] Add all_of and find_if_not to algorithm.h (#94058)
This is needed for the allocator implementation for malloc.
1 parent 25b037b commit e44cea5

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

libc/src/__support/CPP/algorithm.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,21 @@ template <class T> LIBC_INLINE constexpr const T &min(const T &a, const T &b) {
2525
return (a < b) ? a : b;
2626
}
2727

28+
template <class InputIt, class UnaryPred>
29+
LIBC_INLINE constexpr InputIt find_if_not(InputIt first, InputIt last,
30+
UnaryPred q) {
31+
for (; first != last; ++first)
32+
if (!q(*first))
33+
return first;
34+
35+
return last;
36+
}
37+
38+
template <class InputIt, class UnaryPred>
39+
LIBC_INLINE constexpr bool all_of(InputIt first, InputIt last, UnaryPred p) {
40+
return find_if_not(first, last, p) == last;
41+
}
42+
2843
} // namespace cpp
2944
} // namespace LIBC_NAMESPACE
3045

libc/test/src/__support/CPP/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
add_custom_target(libc-cpp-utils-tests)
22

3+
add_libc_test(
4+
algorithm_test
5+
SUITE
6+
libc-cpp-utils-tests
7+
SRCS
8+
algorithm_test.cpp
9+
DEPENDS
10+
libc.src.__support.CPP.algorithm
11+
)
12+
313
add_libc_test(
414
array_test
515
SUITE
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===-- Unittests for Algorithm -------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/CPP/algorithm.h"
10+
#include "src/__support/CPP/array.h"
11+
#include "test/UnitTest/Test.h"
12+
13+
// TODO(https://github.com/llvm/llvm-project/issues/94066): Add unittests for
14+
// the remaining algorithm functions.
15+
namespace LIBC_NAMESPACE::cpp {
16+
17+
TEST(LlvmLibcAlgorithmTest, FindIfNot) {
18+
array<int, 4> nums{1, 2, 3, 4};
19+
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i == 0; }),
20+
nums.begin());
21+
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i == 1; }),
22+
nums.begin() + 1);
23+
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i < 4; }),
24+
nums.begin() + 3);
25+
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i < 5; }),
26+
nums.end());
27+
28+
EXPECT_EQ(
29+
find_if_not(nums.begin() + 1, nums.end(), [](int i) { return i == 0; }),
30+
nums.begin() + 1);
31+
EXPECT_EQ(
32+
find_if_not(nums.begin(), nums.begin(), [](int i) { return i == 0; }),
33+
nums.begin());
34+
}
35+
36+
TEST(LlvmLibcAlgorithmTest, AllOf) {
37+
array<int, 4> nums{1, 2, 3, 4};
38+
EXPECT_TRUE(all_of(nums.begin(), nums.end(), [](int i) { return i < 5; }));
39+
EXPECT_FALSE(all_of(nums.begin(), nums.end(), [](int i) { return i < 4; }));
40+
EXPECT_TRUE(
41+
all_of(nums.begin(), nums.begin() + 3, [](int i) { return i < 4; }));
42+
EXPECT_TRUE(
43+
all_of(nums.begin() + 1, nums.end(), [](int i) { return i > 1; }));
44+
EXPECT_TRUE(all_of(nums.begin(), nums.begin(), [](int i) { return i < 0; }));
45+
}
46+
47+
} // namespace LIBC_NAMESPACE::cpp

0 commit comments

Comments
 (0)