[llvm-branch-commits] [llvm] [ADT] Use perfect forwarding in SmallSet::insert() (PR #108590)

Jakub Kuderski via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Sep 13 09:22:14 PDT 2024


================
@@ -231,6 +215,31 @@ class SmallSet {
 
 private:
   bool isSmall() const { return Set.empty(); }
+
+  template <typename ArgType>
+  std::pair<const_iterator, bool> insertImpl(ArgType &&V) {
+    static_assert(std::is_convertible_v<ArgType, T>,
+                  "ArgType must be convertible to T!");
+    if (!isSmall()) {
+      auto [I, Inserted] = Set.insert(std::forward<ArgType>(V));
+      return std::make_pair(const_iterator(I), Inserted);
+    }
+
+    auto I = llvm::find(Vector, V);
+    if (I != Vector.end()) // Don't reinsert if it already exists.
+      return std::make_pair(const_iterator(I), false);
+    if (Vector.size() < N) {
+      Vector.push_back(std::forward<ArgType>(V));
+      return std::make_pair(const_iterator(std::prev(Vector.end())), true);
+    }
+
+    // Otherwise, grow from vector to set.
+    Set.insert(std::make_move_iterator(Vector.begin()),
+               std::make_move_iterator(Vector.end()));
+    Vector.clear();
+    return std::make_pair(
+        const_iterator(Set.insert(std::forward<ArgType>(V)).first), true);
----------------
kuhar wrote:

```suggestion
    return {const_iterator(Set.insert(std::forward<ArgType>(V)).first), true};
```

https://github.com/llvm/llvm-project/pull/108590


More information about the llvm-branch-commits mailing list