Skip to content

Commit f9dca5b

Browse files
authored
[ADT] Add convenience function filter_to_vector (#117460)
This materializes a filter range as a small vector. Similar to `map_to_vector`, this new utility function lives in the `SmallVectorExtras.h` header.
1 parent 7800d59 commit f9dca5b

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

llvm/include/llvm/ADT/SmallVectorExtras.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,28 @@
1919

2020
namespace llvm {
2121

22+
/// Filter a range to a SmallVector with the element types deduced.
23+
template <unsigned Size, class ContainerTy, class PredicateFn>
24+
auto filter_to_vector(ContainerTy &&C, PredicateFn &&Pred) {
25+
return to_vector<Size>(make_filter_range(std::forward<ContainerTy>(C),
26+
std::forward<PredicateFn>(Pred)));
27+
}
28+
29+
/// Filter a range to a SmallVector with the element types deduced.
30+
template <class ContainerTy, class PredicateFn>
31+
auto filter_to_vector(ContainerTy &&C, PredicateFn &&Pred) {
32+
return to_vector(make_filter_range(std::forward<ContainerTy>(C),
33+
std::forward<PredicateFn>(Pred)));
34+
}
35+
2236
/// Map a range to a SmallVector with element types deduced from the mapping.
2337
template <unsigned Size, class ContainerTy, class FuncTy>
2438
auto map_to_vector(ContainerTy &&C, FuncTy &&F) {
2539
return to_vector<Size>(
2640
map_range(std::forward<ContainerTy>(C), std::forward<FuncTy>(F)));
2741
}
42+
43+
/// Map a range to a SmallVector with element types deduced from the mapping.
2844
template <class ContainerTy, class FuncTy>
2945
auto map_to_vector(ContainerTy &&C, FuncTy &&F) {
3046
return to_vector(

llvm/unittests/ADT/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ add_llvm_unittest(ADTTests
7575
SmallPtrSetTest.cpp
7676
SmallSetTest.cpp
7777
SmallStringTest.cpp
78+
SmallVectorExtrasTest.cpp
7879
SmallVectorTest.cpp
7980
SparseBitVectorTest.cpp
8081
SparseMultiSetTest.cpp
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===- llvm/unittest/ADT/SmallVectorExtrasTest.cpp ------------------------===//
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+
// SmallVectorExtras unit tests.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/ADT/SmallVectorExtras.h"
14+
#include "gmock/gmock.h"
15+
#include "gtest/gtest.h"
16+
17+
#include <type_traits>
18+
#include <vector>
19+
20+
using testing::ElementsAre;
21+
22+
namespace llvm {
23+
namespace {
24+
25+
TEST(SmallVectorExtrasTest, FilterToVector) {
26+
std::vector<int> Numbers = {0, 1, 2, 3, 4};
27+
auto Odd = filter_to_vector<2>(Numbers, [](int X) { return (X % 2) != 0; });
28+
static_assert(std::is_same_v<decltype(Odd), SmallVector<int, 2>>);
29+
EXPECT_THAT(Odd, ElementsAre(1, 3));
30+
31+
auto Even = filter_to_vector(Numbers, [](int X) { return (X % 2) == 0; });
32+
static_assert(std::is_same_v<decltype(Even), SmallVector<int>>);
33+
EXPECT_THAT(Even, ElementsAre(0, 2, 4));
34+
}
35+
36+
} // end namespace
37+
} // namespace llvm

0 commit comments

Comments
 (0)