Skip to content

Commit e1ca63c

Browse files
authored
Merge pull request swiftlang#15661 from atrick/blotvec
Fix usability issues with BlotVector.
2 parents e4e5c33 + 9923d5d commit e1ca63c

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

include/swift/Basic/BlotMapVector.h

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class BlotMapVector {
5151
iterator_range<iterator> getItems() {
5252
return swift::make_range(begin(), end());
5353
}
54+
iterator_range<const_iterator> getItems() const {
55+
return swift::make_range(begin(), end());
56+
}
5457

5558
ValueT &operator[](const KeyT &Arg) {
5659
auto Pair = Map.insert(std::make_pair(Arg, size_t(0)));
@@ -85,27 +88,34 @@ class BlotMapVector {
8588
}
8689

8790
const_iterator find(const KeyT &Key) const {
88-
return const_cast<BlotMapVector &>(*this)->find(Key);
91+
return const_cast<BlotMapVector &>(*this).find(Key);
8992
}
9093

91-
/// This is similar to erase, but instead of removing the element from the
92-
/// vector, it just zeros out the key in the vector. This leaves iterators
93-
/// intact, but clients must be prepared for zeroed-out keys when iterating.
94-
void erase(const KeyT &Key) { blot(Key); }
95-
96-
/// This is similar to erase, but instead of removing the element from the
97-
/// vector, it just zeros out the key in the vector. This leaves iterators
94+
/// Eliminate the element at `Key`. Instead of removing the element from the
95+
/// vector, just zero out the key in the vector. This leaves iterators
9896
/// intact, but clients must be prepared for zeroed-out keys when iterating.
97+
///
98+
/// Return true if the element was erased.
99+
bool erase(const KeyT &Key) { return blot(Key); }
100+
101+
/// Eliminate the element at the given iterator. Instead of removing the
102+
/// element from the vector, just zero out the key in the vector. This
103+
/// leaves iterators intact, but clients must be prepared for zeroed-out
104+
/// keys when iterating.
99105
void erase(iterator I) { erase((*I)->first); }
100106

101-
/// This is similar to erase, but instead of removing the element from the
107+
/// Eliminate the element at `Key`. Instead of removing the element from the
102108
/// vector, it just zeros out the key in the vector. This leaves iterators
103109
/// intact, but clients must be prepared for zeroed-out keys when iterating.
104-
void blot(const KeyT &Key) {
110+
///
111+
/// Return true if the element was found and erased.
112+
bool blot(const KeyT &Key) {
105113
typename MapT::iterator It = Map.find(Key);
106-
if (It == Map.end()) return;
114+
if (It == Map.end())
115+
return false;
107116
Vector[It->second] = None;
108117
Map.erase(It);
118+
return true;
109119
}
110120

111121
void clear() {

include/swift/Basic/BlotSetVector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class BlotSetVector {
120120
if (Iter == Map.end())
121121
return false;
122122
unsigned Index = Iter->second;
123-
Map.erase(V);
123+
Map.erase(Iter);
124124
Vector[Index] = None;
125125
return true;
126126
}

unittests/Basic/BlotMapVectorTest.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ TYPED_TEST(BlotMapVectorTest, EraseTest) {
446446
// Test erase(value) method
447447
TYPED_TEST(BlotMapVectorTest, EraseTest2) {
448448
this->Map[this->getKey()] = this->getValue();
449-
this->Map.erase(this->getKey());
449+
EXPECT_TRUE(this->Map.erase(this->getKey()));
450450

451451
EXPECT_EQ(0u, this->Map.size());
452452
EXPECT_TRUE(this->Map.empty());
@@ -639,9 +639,11 @@ TEST(BlotMapVectorCustomTest, SmallBlotMapVectorGrowTest) {
639639
for (unsigned i = 0; i < 20; ++i)
640640
map[i] = i + 1;
641641
for (unsigned i = 0; i < 10; ++i)
642-
map.erase(i);
642+
EXPECT_TRUE(map.erase(i));
643643
for (unsigned i = 20; i < 32; ++i)
644644
map[i] = i + 1;
645+
for (unsigned i = 0; i < 10; ++i)
646+
EXPECT_FALSE(map.erase(i));
645647

646648
// Size tests
647649
EXPECT_EQ(22u, map.size());

0 commit comments

Comments
 (0)