[llvm] 9ae059b - [ADT] Refactor MapVector::insert, try_emplace, and operator[] (NFC) (#155205)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 25 09:37:32 PDT 2025
Author: Kazu Hirata
Date: 2025-08-25T09:37:29-07:00
New Revision: 9ae059bc22a49fb233a62837b6dc91004882b918
URL: https://github.com/llvm/llvm-project/commit/9ae059bc22a49fb233a62837b6dc91004882b918
DIFF: https://github.com/llvm/llvm-project/commit/9ae059bc22a49fb233a62837b6dc91004882b918.diff
LOG: [ADT] Refactor MapVector::insert, try_emplace, and operator[] (NFC) (#155205)
The l-value and r-value reference variants of try_emplace contain
nearly identical code. Also, operator[] makes its own call to
Map.try_emplace.
This patch introduces a templated helper function, try_emplace_impl,
and uses it in all of MapVector::insert, try_emplace, and operator[].
The helper function uses perfect forwarding to preserve the exact key
type.
This patch moves the "private:" section to the end of the class so
that the new helper function can use iterator.
Added:
Modified:
llvm/include/llvm/ADT/MapVector.h
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/MapVector.h b/llvm/include/llvm/ADT/MapVector.h
index 24976535716d1..4a50126ff5aad 100644
--- a/llvm/include/llvm/ADT/MapVector.h
+++ b/llvm/include/llvm/ADT/MapVector.h
@@ -34,13 +34,6 @@ template <typename KeyT, typename ValueT,
typename MapType = DenseMap<KeyT, unsigned>,
typename VectorType = SmallVector<std::pair<KeyT, ValueT>, 0>>
class MapVector {
- MapType Map;
- VectorType Vector;
-
- static_assert(
- std::is_integral_v<typename MapType::mapped_type>,
- "The mapped_type of the specified Map must be an integral type");
-
public:
using key_type = KeyT;
using value_type = typename VectorType::value_type;
@@ -99,13 +92,7 @@ class MapVector {
}
ValueT &operator[](const KeyT &Key) {
- std::pair<typename MapType::iterator, bool> Result = Map.try_emplace(Key);
- auto &I = Result.first->second;
- if (Result.second) {
- Vector.push_back(std::make_pair(Key, ValueT()));
- I = Vector.size() - 1;
- }
- return Vector[I].second;
+ return try_emplace_impl(Key).first->second;
}
// Returns a copy of the value. Only allowed if ValueT is copyable.
@@ -118,33 +105,18 @@ class MapVector {
template <typename... Ts>
std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&...Args) {
- auto [It, Inserted] = Map.try_emplace(Key);
- if (Inserted) {
- It->second = Vector.size();
- Vector.emplace_back(std::piecewise_construct, std::forward_as_tuple(Key),
- std::forward_as_tuple(std::forward<Ts>(Args)...));
- return {std::prev(end()), true};
- }
- return {begin() + It->second, false};
+ return try_emplace_impl(Key, std::forward<Ts>(Args)...);
}
template <typename... Ts>
std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
- auto [It, Inserted] = Map.try_emplace(Key);
- if (Inserted) {
- It->second = Vector.size();
- Vector.emplace_back(std::piecewise_construct,
- std::forward_as_tuple(std::move(Key)),
- std::forward_as_tuple(std::forward<Ts>(Args)...));
- return {std::prev(end()), true};
- }
- return {begin() + It->second, false};
+ return try_emplace_impl(std::move(Key), std::forward<Ts>(Args)...);
}
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
- return try_emplace(KV.first, KV.second);
+ return try_emplace_impl(KV.first, KV.second);
}
std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) {
- return try_emplace(std::move(KV.first), std::move(KV.second));
+ return try_emplace_impl(std::move(KV.first), std::move(KV.second));
}
template <typename V>
@@ -224,6 +196,27 @@ class MapVector {
/// Erase all elements that match \c Pred in a single pass. Takes linear
/// time.
template <class Predicate> void remove_if(Predicate Pred);
+
+private:
+ MapType Map;
+ VectorType Vector;
+
+ static_assert(
+ std::is_integral_v<typename MapType::mapped_type>,
+ "The mapped_type of the specified Map must be an integral type");
+
+ template <typename KeyArgT, typename... Ts>
+ std::pair<iterator, bool> try_emplace_impl(KeyArgT &&Key, Ts &&...Args) {
+ auto [It, Inserted] = Map.try_emplace(Key);
+ if (Inserted) {
+ It->second = Vector.size();
+ Vector.emplace_back(std::piecewise_construct,
+ std::forward_as_tuple(std::forward<KeyArgT>(Key)),
+ std::forward_as_tuple(std::forward<Ts>(Args)...));
+ return {std::prev(end()), true};
+ }
+ return {begin() + It->second, false};
+ }
};
template <typename KeyT, typename ValueT, typename MapType, typename VectorType>
More information about the llvm-commits
mailing list