Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ADT]Add helper function to return a ArrayRef of MapVector's underlying vector #138726

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

Merged
merged 1 commit into from
May 6, 2025

Conversation

mingmingl-llvm
Copy link
Contributor

SetVector currently has a similar method, and #138170 has a use case to get an ArrayRef of MapVector's underlying vector.

@llvmbot
Copy link
Member

llvmbot commented May 6, 2025

@llvm/pr-subscribers-llvm-adt

Author: Mingming Liu (mingmingl-llvm)

Changes

SetVector currently has a similar method, and #138170 has a use case to get an ArrayRef of MapVector's underlying vector.


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

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/MapVector.h (+3)
  • (modified) llvm/unittests/ADT/MapVectorTest.cpp (+25)
diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h
index c11617a81c97d..adbcda116dd56 100644
--- a/llvm/include/llvm/ADT/MapVector.h
+++ b/llvm/include/llvm/ADT/MapVector.h
@@ -57,6 +57,9 @@ class MapVector {
     return std::move(Vector);
   }
 
+  /// Returns an array reference of the underlying vector.
+  ArrayRef<value_type> getArrayRef() const { return Vector; }
+
   size_type size() const { return Vector.size(); }
 
   /// Grow the MapVector so that it can contain at least \p NumEntries items
diff --git a/llvm/unittests/ADT/MapVectorTest.cpp b/llvm/unittests/ADT/MapVectorTest.cpp
index e0f11b60a0223..639e1a14e8178 100644
--- a/llvm/unittests/ADT/MapVectorTest.cpp
+++ b/llvm/unittests/ADT/MapVectorTest.cpp
@@ -7,7 +7,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/ADT/MapVector.h"
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/iterator_range.h"
+#include "gmock/gmock.h"
 #include "gtest/gtest.h"
 #include <memory>
 #include <utility>
@@ -267,6 +269,29 @@ TEST(MapVectorTest, NonCopyable) {
   ASSERT_EQ(*MV.find(2)->second, 2);
 }
 
+TEST(MapVectorTest, GetArrayRef) {
+  MapVector<int, int> MV;
+
+  // The underlying vector is empty to begin with.
+  EXPECT_TRUE(MV.getArrayRef().empty());
+
+  // Test inserted element.
+  MV.insert(std::make_pair(100, 99));
+  EXPECT_TRUE(MV.getArrayRef().equals({std::pair(100, 99)}));
+
+  // Inserting a different element for an existing key won't change the
+  // underlying vector.
+  auto [Iter, Inserted] = MV.try_emplace(100, 98);
+  EXPECT_FALSE(Inserted);
+  EXPECT_EQ(Iter->second, 99);
+  EXPECT_TRUE(MV.getArrayRef().equals({std::pair(100, 99)}));
+
+  // Inserting a new element. Tests that elements are in order in the underlying
+  // array.
+  MV.insert(std::make_pair(99, 98));
+  EXPECT_TRUE(MV.getArrayRef().equals({std::pair(100, 99), std::pair(99, 98)}));
+}
+
 template <class IntType> struct MapVectorMappedTypeTest : ::testing::Test {
   using int_type = IntType;
 };

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!

@mingmingl-llvm mingmingl-llvm merged commit ad5b3e0 into main May 6, 2025
8 of 11 checks passed
@mingmingl-llvm mingmingl-llvm deleted the users/mingmingl-llvm/mapvector branch May 6, 2025 18:26
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
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.

4 participants