Skip to content

Commit 0bddef7

Browse files
author
Michael Pozulp
committedJun 7, 2019
[ADT] Enable set_difference() to be used on StringSet
Subscribers: mgorny, mgrang, dexonsmith, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D62992 llvm-svn: 362766
1 parent c7903b9 commit 0bddef7

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed
 

‎llvm/include/llvm/ADT/StringMap.h

+5
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,11 @@ class StringMap : public StringMapImpl {
359359
return find(Key) == end() ? 0 : 1;
360360
}
361361

362+
template <typename InputTy>
363+
size_type count(const StringMapEntry<InputTy> &MapEntry) const {
364+
return count(MapEntry.getKey());
365+
}
366+
362367
/// insert - Insert the specified key/value pair into the map. If the key
363368
/// already exists in the map, return false and ignore the request, otherwise
364369
/// insert it and return true.

‎llvm/include/llvm/ADT/StringSet.h

+6
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ namespace llvm {
4545
for (auto It = Begin; It != End; ++It)
4646
base::insert(std::make_pair(*It, '\0'));
4747
}
48+
49+
template <typename ValueTy>
50+
std::pair<typename base::iterator, bool>
51+
insert(const StringMapEntry<ValueTy> &MapEntry) {
52+
return insert(MapEntry.getKey());
53+
}
4854
};
4955

5056
} // end namespace llvm

‎llvm/unittests/ADT/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ add_llvm_unittest(ADTTests
6565
StringExtrasTest.cpp
6666
StringMapTest.cpp
6767
StringRefTest.cpp
68+
StringSetTest.cpp
6869
StringSwitchTest.cpp
6970
TinyPtrVectorTest.cpp
7071
TripleTest.cpp

‎llvm/unittests/ADT/StringMapTest.cpp

-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "llvm/ADT/StringMap.h"
10-
#include "llvm/ADT/StringSet.h"
1110
#include "llvm/ADT/Twine.h"
1211
#include "llvm/Support/DataTypes.h"
1312
#include "gtest/gtest.h"
@@ -284,20 +283,6 @@ TEST_F(StringMapTest, IterMapKeys) {
284283
EXPECT_EQ(Expected, Keys);
285284
}
286285

287-
TEST_F(StringMapTest, IterSetKeys) {
288-
StringSet<> Set;
289-
Set.insert("A");
290-
Set.insert("B");
291-
Set.insert("C");
292-
Set.insert("D");
293-
294-
auto Keys = to_vector<4>(Set.keys());
295-
llvm::sort(Keys);
296-
297-
SmallVector<StringRef, 4> Expected = {"A", "B", "C", "D"};
298-
EXPECT_EQ(Expected, Keys);
299-
}
300-
301286
// Create a non-default constructable value
302287
struct StringMapTestStruct {
303288
StringMapTestStruct(int i) : i(i) {}

‎llvm/unittests/ADT/StringSetTest.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===- llvm/unittest/ADT/StringSetTest.cpp - StringSet unit tests ----------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "llvm/ADT/StringSet.h"
10+
#include "gtest/gtest.h"
11+
using namespace llvm;
12+
13+
namespace {
14+
15+
// Test fixture
16+
class StringSetTest : public testing::Test {};
17+
18+
TEST_F(StringSetTest, IterSetKeys) {
19+
StringSet<> Set;
20+
Set.insert("A");
21+
Set.insert("B");
22+
Set.insert("C");
23+
Set.insert("D");
24+
25+
auto Keys = to_vector<4>(Set.keys());
26+
llvm::sort(Keys);
27+
28+
SmallVector<StringRef, 4> Expected = {"A", "B", "C", "D"};
29+
EXPECT_EQ(Expected, Keys);
30+
}
31+
32+
TEST_F(StringSetTest, InsertAndCountStringMapEntry) {
33+
// Test insert(StringMapEntry) and count(StringMapEntry)
34+
// which are required for set_difference(StringSet, StringSet).
35+
StringSet<> Set;
36+
StringMapEntry<StringRef> Element(1, "A");
37+
Set.insert(Element);
38+
size_t Count = Set.count(Element);
39+
size_t Expected = 1;
40+
EXPECT_EQ(Expected, Count);
41+
}
42+
43+
} // end anonymous namespace

0 commit comments

Comments
 (0)
Please sign in to comment.