[llvm] ad5b3e0 - [ADT]Add helper function to return a ArrayRef of MapVector's underlying vector (#138726)

via llvm-commits llvm-commits at lists.llvm.org
Tue May 6 11:26:59 PDT 2025


Author: Mingming Liu
Date: 2025-05-06T11:26:53-07:00
New Revision: ad5b3e01fbc81337ad8d91663fc6d0624a251e14

URL: https://github.com/llvm/llvm-project/commit/ad5b3e01fbc81337ad8d91663fc6d0624a251e14
DIFF: https://github.com/llvm/llvm-project/commit/ad5b3e01fbc81337ad8d91663fc6d0624a251e14.diff

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

SetVector currently has a [similar
method](https://github.com/llvm/llvm-project/blob/c956ed06dc1c1b340d0c589c472c438b9220b36d/llvm/include/llvm/ADT/SetVector.h#L90),
and https://github.com/llvm/llvm-project/pull/138170 has a use case to
get an ArrayRef of MapVector's underlying vector.

Added: 
    

Modified: 
    llvm/include/llvm/ADT/MapVector.h
    llvm/unittests/ADT/MapVectorTest.cpp

Removed: 
    


################################################################################
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 
diff erent 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;
 };


        


More information about the llvm-commits mailing list