Skip to content

Commit a5eb91b

Browse files
Abseil Teamnetkex
authored andcommitted
Make begin() to return end() on empty tables.
PiperOrigin-RevId: 605460827 Change-Id: I57007a7ad18829e7bfed27ba65871afbd227d012
1 parent 8f46006 commit a5eb91b

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

absl/container/internal/raw_hash_set.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2027,6 +2027,7 @@ class raw_hash_set {
20272027
++ctrl_;
20282028
++slot_;
20292029
skip_empty_or_deleted();
2030+
if (ABSL_PREDICT_FALSE(*ctrl_ == ctrl_t::kSentinel)) ctrl_ = nullptr;
20302031
return *this;
20312032
}
20322033
// PRECONDITION: not an end() iterator.
@@ -2061,18 +2062,15 @@ class raw_hash_set {
20612062
explicit iterator(const GenerationType* generation_ptr)
20622063
: HashSetIteratorGenerationInfo(generation_ptr), ctrl_(nullptr) {}
20632064

2064-
// Fixes up `ctrl_` to point to a full by advancing it and `slot_` until
2065-
// they reach one.
2066-
//
2067-
// If a sentinel is reached, we null `ctrl_` out instead.
2065+
// Fixes up `ctrl_` to point to a full or sentinel by advancing `ctrl_` and
2066+
// `slot_` until they reach one.
20682067
void skip_empty_or_deleted() {
20692068
while (IsEmptyOrDeleted(*ctrl_)) {
20702069
uint32_t shift =
20712070
GroupFullEmptyOrDeleted{ctrl_}.CountLeadingEmptyOrDeleted();
20722071
ctrl_ += shift;
20732072
slot_ += shift;
20742073
}
2075-
if (ABSL_PREDICT_FALSE(*ctrl_ == ctrl_t::kSentinel)) ctrl_ = nullptr;
20762074
}
20772075

20782076
ctrl_t* control() const { return ctrl_; }
@@ -2369,8 +2367,11 @@ class raw_hash_set {
23692367
~raw_hash_set() { destructor_impl(); }
23702368

23712369
iterator begin() ABSL_ATTRIBUTE_LIFETIME_BOUND {
2370+
// TODO(b/324478958): Consider reverting if no impact.
2371+
if (ABSL_PREDICT_FALSE(empty())) return end();
23722372
auto it = iterator_at(0);
23732373
it.skip_empty_or_deleted();
2374+
assert(IsFull(*it.control()));
23742375
return it;
23752376
}
23762377
iterator end() ABSL_ATTRIBUTE_LIFETIME_BOUND {

0 commit comments

Comments
 (0)