Skip to content

Commit 1deaedd

Browse files
[ADT] Make DenseMapBase::swap the public entry point (#167650)
Without this patch, DenseMap::swap and SmallDenseMap::swap are inconsistent because DenseMap::swap increments the epoch while SmallDenseMap::swap does not. This patch solves the inconsistency by making DenseMapBase::swap the public entry point and renaming the existing swap to swapImpl. To ease the review process, this patch does not move or group functions according to access specifiers like private: and protected:.
1 parent b6dd511 commit 1deaedd

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

llvm/include/llvm/ADT/DenseMap.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,12 @@ class DenseMapBase : public DebugEpochBase {
360360
return getBuckets();
361361
}
362362

363+
void swap(DerivedT &RHS) {
364+
this->incrementEpoch();
365+
RHS.incrementEpoch();
366+
derived().swapImpl(RHS);
367+
}
368+
363369
protected:
364370
DenseMapBase() = default;
365371

@@ -736,7 +742,7 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
736742

737743
DenseMap(DenseMap &&other) : BaseT() {
738744
init(0);
739-
swap(other);
745+
this->swap(other);
740746
}
741747

742748
template <typename InputIt> DenseMap(const InputIt &I, const InputIt &E) {
@@ -756,15 +762,15 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
756762
deallocateBuckets();
757763
}
758764

759-
void swap(DenseMap &RHS) {
760-
this->incrementEpoch();
761-
RHS.incrementEpoch();
765+
private:
766+
void swapImpl(DenseMap &RHS) {
762767
std::swap(Buckets, RHS.Buckets);
763768
std::swap(NumEntries, RHS.NumEntries);
764769
std::swap(NumTombstones, RHS.NumTombstones);
765770
std::swap(NumBuckets, RHS.NumBuckets);
766771
}
767772

773+
public:
768774
DenseMap &operator=(const DenseMap &other) {
769775
if (&other != this)
770776
this->copyFrom(other);
@@ -775,7 +781,7 @@ class DenseMap : public DenseMapBase<DenseMap<KeyT, ValueT, KeyInfoT, BucketT>,
775781
this->destroyAll();
776782
deallocateBuckets();
777783
init(0);
778-
swap(other);
784+
this->swap(other);
779785
return *this;
780786
}
781787

@@ -895,7 +901,7 @@ class SmallDenseMap
895901

896902
SmallDenseMap(SmallDenseMap &&other) : BaseT() {
897903
init(0);
898-
swap(other);
904+
this->swap(other);
899905
}
900906

901907
template <typename InputIt>
@@ -916,7 +922,8 @@ class SmallDenseMap
916922
deallocateBuckets();
917923
}
918924

919-
void swap(SmallDenseMap &RHS) {
925+
private:
926+
void swapImpl(SmallDenseMap &RHS) {
920927
unsigned TmpNumEntries = RHS.NumEntries;
921928
RHS.NumEntries = NumEntries;
922929
NumEntries = TmpNumEntries;
@@ -987,6 +994,7 @@ class SmallDenseMap
987994
new (SmallSide.getLargeRep()) LargeRep(std::move(TmpRep));
988995
}
989996

997+
public:
990998
SmallDenseMap &operator=(const SmallDenseMap &other) {
991999
if (&other != this)
9921000
this->copyFrom(other);
@@ -997,7 +1005,7 @@ class SmallDenseMap
9971005
this->destroyAll();
9981006
deallocateBuckets();
9991007
init(0);
1000-
swap(other);
1008+
this->swap(other);
10011009
return *this;
10021010
}
10031011

0 commit comments

Comments
 (0)