Skip to content

Commit 8b57bb5

Browse files
committed
[libc][CPP] Add all_of and find_if_not to algorithm.h
This is needed for the allocator implementation for malloc.
1 parent 5bec47c commit 8b57bb5

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
namespace LIBC_NAMESPACE::cpp {
14+
15+
TEST(LlvmLibcAlgorithmTest, FindIfNot) {
16+
array<int, 4> nums{1, 2, 3, 4};
17+
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i == 0; }),
18+
nums.begin());
19+
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i == 1; }),
20+
nums.begin() + 1);
21+
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i < 4; }),
22+
nums.begin() + 3);
23+
EXPECT_EQ(find_if_not(nums.begin(), nums.end(), [](int i) { return i < 5; }),
24+
nums.end());
25+
26+
EXPECT_EQ(
27+
find_if_not(nums.begin() + 1, nums.end(), [](int i) { return i == 0; }),
28+
nums.begin() + 1);
29+
EXPECT_EQ(
30+
find_if_not(nums.begin(), nums.begin(), [](int i) { return i == 0; }),
31+
nums.begin());
32+
}
33+
34+
TEST(LlvmLibcAlgorithmTest, AllOf) {
35+
array<int, 4> nums{1, 2, 3, 4};
36+
EXPECT_TRUE(all_of(nums.begin(), nums.end(), [](int i) { return i < 5; }));
37+
EXPECT_FALSE(all_of(nums.begin(), nums.end(), [](int i) { return i < 4; }));
38+
EXPECT_TRUE(
39+
all_of(nums.begin(), nums.begin() + 3, [](int i) { return i < 4; }));
40+
EXPECT_TRUE(
41+
all_of(nums.begin() + 1, nums.end(), [](int i) { return i > 1; }));
42+
EXPECT_TRUE(all_of(nums.begin(), nums.begin(), [](int i) { return i < 0; }));
43+
}
44+
45+
} // namespace LIBC_NAMESPACE::cpp

0 commit comments

Comments
 (0)