[PATCH] D62369: [ADT] Enable set_difference() to be used on StringSet

Mike Pozulp via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 24 00:34:27 PDT 2019


mmpozulp created this revision.
mmpozulp added a reviewer: jhenderson.
Herald added subscribers: llvm-commits, kristina, dexonsmith, mgorny.
Herald added a project: LLVM.

Repository:
  rL LLVM

https://reviews.llvm.org/D62369

Files:
  llvm/include/llvm/ADT/StringMap.h
  llvm/include/llvm/ADT/StringSet.h
  llvm/unittests/ADT/CMakeLists.txt
  llvm/unittests/ADT/SetOperationsTest.cpp


Index: llvm/unittests/ADT/SetOperationsTest.cpp
===================================================================
--- /dev/null
+++ llvm/unittests/ADT/SetOperationsTest.cpp
@@ -0,0 +1,29 @@
+//===- llvm/unittest/ADT/StringMapMap.cpp - StringMap unit tests ----------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/SetOperations.h"
+#include "llvm/ADT/StringSet.h"
+#include "gtest/gtest.h"
+using namespace llvm;
+
+namespace {
+
+// Test fixture
+class SetOperationsTest : public testing::Test {};
+TEST_F(SetOperationsTest, StringSetDiffTest) {
+  StringSet<> SS1{"a", "b"};
+  StringSet<> SS2{"a"};
+
+  auto Diff = set_difference(SS1, SS2);
+  auto DiffVec = to_vector<1>(Diff.keys());
+
+  SmallVector<StringRef, 1> Expected{"b"};
+  EXPECT_EQ(Expected, DiffVec);
+}
+
+} // end anonymous namespace
Index: llvm/unittests/ADT/CMakeLists.txt
===================================================================
--- llvm/unittests/ADT/CMakeLists.txt
+++ llvm/unittests/ADT/CMakeLists.txt
@@ -52,6 +52,7 @@
   STLExtrasTest.cpp
   ScopeExitTest.cpp
   SequenceTest.cpp
+  SetOperationsTest.cpp
   SetVectorTest.cpp
   SimpleIListTest.cpp
   SmallPtrSetTest.cpp
Index: llvm/include/llvm/ADT/StringSet.h
===================================================================
--- llvm/include/llvm/ADT/StringSet.h
+++ llvm/include/llvm/ADT/StringSet.h
@@ -45,6 +45,12 @@
       for (auto It = Begin; It != End; ++It)
         base::insert(std::make_pair(*It, '\0'));
     }
+
+    template <typename InputTy>
+    std::pair<typename base::iterator, bool>
+    insert(const StringMapEntry<InputTy> &MapEntry) {
+      return insert(MapEntry.getKey());
+    }
   };
 
 } // end namespace llvm
Index: llvm/include/llvm/ADT/StringMap.h
===================================================================
--- llvm/include/llvm/ADT/StringMap.h
+++ llvm/include/llvm/ADT/StringMap.h
@@ -359,6 +359,11 @@
     return find(Key) == end() ? 0 : 1;
   }
 
+  template <typename InputTy>
+  size_type count(const StringMapEntry<InputTy> &MapEntry) const {
+    return count(MapEntry.getKey());
+  }
+
   /// insert - Insert the specified key/value pair into the map.  If the key
   /// already exists in the map, return false and ignore the request, otherwise
   /// insert it and return true.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D62369.201132.patch
Type: text/x-patch
Size: 2551 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190524/c5f3a3af/attachment.bin>


More information about the llvm-commits mailing list