Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADT] Add convenience function filter_to_vector #117460

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

kuhar
Copy link
Member

@kuhar kuhar commented Nov 24, 2024

This materializes a filter range as a small vector.

Similar to map_to_vector, this new utility function lives in the SmallVectorExtras.h header.

This materializes a filter range as a small vector.

Similar to `map_to_vector`, this new utility function lives in
the `SmallVectorExtras.h` header.
@llvmbot
Copy link
Member

llvmbot commented Nov 24, 2024

@llvm/pr-subscribers-llvm-adt

Author: Jakub Kuderski (kuhar)

Changes

This materializes a filter range as a small vector.

Similar to map_to_vector, this new utility function lives in the SmallVectorExtras.h header.


Full diff: https://github.com/llvm/llvm-project/pull/117460.diff

3 Files Affected:

  • (modified) llvm/include/llvm/ADT/SmallVectorExtras.h (+16)
  • (modified) llvm/unittests/ADT/CMakeLists.txt (+1)
  • (added) llvm/unittests/ADT/SmallVectorExtrasTest.cpp (+37)
diff --git a/llvm/include/llvm/ADT/SmallVectorExtras.h b/llvm/include/llvm/ADT/SmallVectorExtras.h
index eea91ca81ca612..061293fae08307 100644
--- a/llvm/include/llvm/ADT/SmallVectorExtras.h
+++ b/llvm/include/llvm/ADT/SmallVectorExtras.h
@@ -19,12 +19,28 @@
 
 namespace llvm {
 
+/// Filter a range to a SmallVector with the element types deduced.
+template <unsigned Size, class ContainerTy, class PredicateFn>
+auto filter_to_vector(ContainerTy &&C, PredicateFn &&Pred) {
+  return to_vector<Size>(make_filter_range(std::forward<ContainerTy>(C),
+                                           std::forward<PredicateFn>(Pred)));
+}
+
+/// Filter a range to a SmallVector with the element types deduced.
+template <class ContainerTy, class PredicateFn>
+auto filter_to_vector(ContainerTy &&C, PredicateFn &&Pred) {
+  return to_vector(make_filter_range(std::forward<ContainerTy>(C),
+                                     std::forward<PredicateFn>(Pred)));
+}
+
 /// Map a range to a SmallVector with element types deduced from the mapping.
 template <unsigned Size, class ContainerTy, class FuncTy>
 auto map_to_vector(ContainerTy &&C, FuncTy &&F) {
   return to_vector<Size>(
       map_range(std::forward<ContainerTy>(C), std::forward<FuncTy>(F)));
 }
+
+/// Map a range to a SmallVector with element types deduced from the mapping.
 template <class ContainerTy, class FuncTy>
 auto map_to_vector(ContainerTy &&C, FuncTy &&F) {
   return to_vector(
diff --git a/llvm/unittests/ADT/CMakeLists.txt b/llvm/unittests/ADT/CMakeLists.txt
index b0077d5b54a3ee..c9bc58f45f08cf 100644
--- a/llvm/unittests/ADT/CMakeLists.txt
+++ b/llvm/unittests/ADT/CMakeLists.txt
@@ -75,6 +75,7 @@ add_llvm_unittest(ADTTests
   SmallPtrSetTest.cpp
   SmallSetTest.cpp
   SmallStringTest.cpp
+  SmallVectorExtrasTest.cpp
   SmallVectorTest.cpp
   SparseBitVectorTest.cpp
   SparseMultiSetTest.cpp
diff --git a/llvm/unittests/ADT/SmallVectorExtrasTest.cpp b/llvm/unittests/ADT/SmallVectorExtrasTest.cpp
new file mode 100644
index 00000000000000..43a51d22b485aa
--- /dev/null
+++ b/llvm/unittests/ADT/SmallVectorExtrasTest.cpp
@@ -0,0 +1,37 @@
+//===- llvm/unittest/ADT/SmallVectorExtrasTest.cpp ------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// SmallVectorExtras unit tests.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/SmallVectorExtras.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+
+#include <type_traits>
+#include <vector>
+
+using testing::ElementsAre;
+
+namespace llvm {
+namespace {
+
+TEST(SmallVectorExtrasTest, FilterToVector) {
+  std::vector<int> Numbers = {0, 1, 2, 3, 4};
+  auto Odd = filter_to_vector<2>(Numbers, [](int X) { return (X % 2) != 0; });
+  static_assert(std::is_same_v<decltype(Odd), SmallVector<int, 2>>);
+  EXPECT_THAT(Odd, ElementsAre(1, 3));
+
+  auto Even = filter_to_vector(Numbers, [](int X) { return (X % 2) == 0; });
+  static_assert(std::is_same_v<decltype(Even), SmallVector<int>>);
+  EXPECT_THAT(Even, ElementsAre(0, 2, 4));
+}
+
+} // end namespace
+} // namespace llvm

Copy link
Contributor

@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks!

Copy link

github-actions bot commented Nov 24, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants