Commit 831e57a4 by Evan Brown Committed by Copybara-Service

Change find_or_prepare_insert to return std::pair<iterator, bool> to match return type of insert.

PiperOrigin-RevId: 609058024
Change-Id: I2f7cc2daf862e7e2d23acd6dd3fe85cb1945d5f0
parent 92c8575d
...@@ -201,8 +201,8 @@ class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> { ...@@ -201,8 +201,8 @@ class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> {
if (res.second) if (res.second)
this->emplace_at(res.first, std::forward<K>(k), std::forward<V>(v)); this->emplace_at(res.first, std::forward<K>(k), std::forward<V>(v));
else else
Policy::value(&*this->iterator_at(res.first)) = std::forward<V>(v); Policy::value(&*res.first) = std::forward<V>(v);
return {this->iterator_at(res.first), res.second}; return res;
} }
template <class K = key_type, class... Args> template <class K = key_type, class... Args>
...@@ -213,7 +213,7 @@ class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> { ...@@ -213,7 +213,7 @@ class raw_hash_map : public raw_hash_set<Policy, Hash, Eq, Alloc> {
this->emplace_at(res.first, std::piecewise_construct, this->emplace_at(res.first, std::piecewise_construct,
std::forward_as_tuple(std::forward<K>(k)), std::forward_as_tuple(std::forward<K>(k)),
std::forward_as_tuple(std::forward<Args>(args)...)); std::forward_as_tuple(std::forward<Args>(args)...));
return {this->iterator_at(res.first), res.second}; return res;
} }
}; };
......
...@@ -2329,7 +2329,7 @@ class raw_hash_set { ...@@ -2329,7 +2329,7 @@ class raw_hash_set {
PolicyTraits::element(that_slot))) == h2 && PolicyTraits::element(that_slot))) == h2 &&
"hash function value changed unexpectedly during the copy"); "hash function value changed unexpectedly during the copy");
SetCtrl(common(), offset, h2, sizeof(slot_type)); SetCtrl(common(), offset, h2, sizeof(slot_type));
emplace_at(offset, PolicyTraits::element(that_slot)); emplace_at(iterator_at(offset), PolicyTraits::element(that_slot));
common().maybe_increment_generation_on_insert(); common().maybe_increment_generation_on_insert();
}); });
if (shift != 0) { if (shift != 0) {
...@@ -2629,11 +2629,11 @@ class raw_hash_set { ...@@ -2629,11 +2629,11 @@ class raw_hash_set {
F&& f) ABSL_ATTRIBUTE_LIFETIME_BOUND { F&& f) ABSL_ATTRIBUTE_LIFETIME_BOUND {
auto res = find_or_prepare_insert(key); auto res = find_or_prepare_insert(key);
if (res.second) { if (res.second) {
slot_type* slot = slot_array() + res.first; slot_type* slot = res.first.slot();
std::forward<F>(f)(constructor(&alloc_ref(), &slot)); std::forward<F>(f)(constructor(&alloc_ref(), &slot));
assert(!slot); assert(!slot);
} }
return iterator_at(res.first); return res.first;
} }
// Extension API: support for heterogeneous keys. // Extension API: support for heterogeneous keys.
...@@ -2956,7 +2956,7 @@ class raw_hash_set { ...@@ -2956,7 +2956,7 @@ class raw_hash_set {
if (res.second) { if (res.second) {
s.emplace_at(res.first, std::forward<Args>(args)...); s.emplace_at(res.first, std::forward<Args>(args)...);
} }
return {s.iterator_at(res.first), res.second}; return res;
} }
raw_hash_set& s; raw_hash_set& s;
}; };
...@@ -2967,11 +2967,11 @@ class raw_hash_set { ...@@ -2967,11 +2967,11 @@ class raw_hash_set {
std::pair<iterator, bool> operator()(const K& key, Args&&...) && { std::pair<iterator, bool> operator()(const K& key, Args&&...) && {
auto res = s.find_or_prepare_insert(key); auto res = s.find_or_prepare_insert(key);
if (res.second) { if (res.second) {
s.transfer(s.slot_array() + res.first, &slot); s.transfer(res.first.slot(), &slot);
} else if (do_destroy) { } else if (do_destroy) {
s.destroy(&slot); s.destroy(&slot);
} }
return {s.iterator_at(res.first), res.second}; return res;
} }
raw_hash_set& s; raw_hash_set& s;
// Constructed slot. Either moved into place or destroyed. // Constructed slot. Either moved into place or destroyed.
...@@ -3208,11 +3208,11 @@ class raw_hash_set { ...@@ -3208,11 +3208,11 @@ class raw_hash_set {
} }
protected: protected:
// Attempts to find `key` in the table; if it isn't found, returns a slot that // Attempts to find `key` in the table; if it isn't found, returns an iterator
// the value can be inserted into, with the control byte already set to // where the value can be inserted into, with the control byte already set to
// `key`'s H2. // `key`'s H2. Returns a bool indicating whether an insertion can take place.
template <class K> template <class K>
std::pair<size_t, bool> find_or_prepare_insert(const K& key) { std::pair<iterator, bool> find_or_prepare_insert(const K& key) {
prefetch_heap_block(); prefetch_heap_block();
auto hash = hash_ref()(key); auto hash = hash_ref()(key);
auto seq = probe(common(), hash); auto seq = probe(common(), hash);
...@@ -3223,13 +3223,13 @@ class raw_hash_set { ...@@ -3223,13 +3223,13 @@ class raw_hash_set {
if (ABSL_PREDICT_TRUE(PolicyTraits::apply( if (ABSL_PREDICT_TRUE(PolicyTraits::apply(
EqualElement<K>{key, eq_ref()}, EqualElement<K>{key, eq_ref()},
PolicyTraits::element(slot_array() + seq.offset(i))))) PolicyTraits::element(slot_array() + seq.offset(i)))))
return {seq.offset(i), false}; return {iterator_at(seq.offset(i)), false};
} }
if (ABSL_PREDICT_TRUE(g.MaskEmpty())) break; if (ABSL_PREDICT_TRUE(g.MaskEmpty())) break;
seq.next(); seq.next();
assert(seq.index() <= capacity() && "full table!"); assert(seq.index() <= capacity() && "full table!");
} }
return {prepare_insert(hash), true}; return {iterator_at(prepare_insert(hash)), true};
} }
// Given the hash of a value not currently in the table, finds the next // Given the hash of a value not currently in the table, finds the next
...@@ -3272,16 +3272,15 @@ class raw_hash_set { ...@@ -3272,16 +3272,15 @@ class raw_hash_set {
// after an unsuccessful find_or_prepare_insert() and before any other // after an unsuccessful find_or_prepare_insert() and before any other
// modifications happen in the raw_hash_set. // modifications happen in the raw_hash_set.
// //
// PRECONDITION: i is an index returned from find_or_prepare_insert(k), where // PRECONDITION: iter was returned from find_or_prepare_insert(k), where k is
// k is the key decomposed from `forward<Args>(args)...`, and the bool // the key decomposed from `forward<Args>(args)...`, and the bool returned by
// returned by find_or_prepare_insert(k) was true. // find_or_prepare_insert(k) was true.
// POSTCONDITION: *m.iterator_at(i) == value_type(forward<Args>(args)...). // POSTCONDITION: *m.iterator_at(i) == value_type(forward<Args>(args)...).
template <class... Args> template <class... Args>
void emplace_at(size_t i, Args&&... args) { void emplace_at(iterator iter, Args&&... args) {
construct(slot_array() + i, std::forward<Args>(args)...); construct(iter.slot(), std::forward<Args>(args)...);
assert(PolicyTraits::apply(FindElement{*this}, *iterator_at(i)) == assert(PolicyTraits::apply(FindElement{*this}, *iter) == iter &&
iterator_at(i) &&
"constructed value does not match the lookup key"); "constructed value does not match the lookup key");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment