Add HashSet::Reserve
Reserves enough room to insert n elements without expanding.
Motivation is to use this for cases where we know how many elements
will be inserted.
Change-Id: I3c51fc7f4601903411fc90b0f1bf270fe9a30919
diff --git a/runtime/base/hash_set.h b/runtime/base/hash_set.h
index 4819f06..95baa82 100644
--- a/runtime/base/hash_set.h
+++ b/runtime/base/hash_set.h
@@ -420,6 +420,19 @@
Resize(Size() / max_load_factor_);
}
+ // Reserve enough room to insert until Size() == num_elements without requiring to grow the hash
+ // set. No-op if the hash set is already large enough to do this.
+ void Reserve(size_t num_elements) {
+ size_t num_buckets = num_elements / max_load_factor_;
+ // Deal with rounding errors. Add one for rounding.
+ while (static_cast<size_t>(num_buckets * max_load_factor_) <= num_elements + 1u) {
+ ++num_buckets;
+ }
+ if (num_buckets > NumBuckets()) {
+ Resize(num_buckets);
+ }
+ }
+
// To distance that inserted elements were probed. Used for measuring how good hash functions
// are.
size_t TotalProbeDistance() const {
@@ -488,6 +501,15 @@
}
}
+ // The hash set expands when Size() reaches ElementsUntilExpand().
+ size_t ElementsUntilExpand() const {
+ return elements_until_expand_;
+ }
+
+ size_t NumBuckets() const {
+ return num_buckets_;
+ }
+
private:
T& ElementForIndex(size_t index) {
DCHECK_LT(index, NumBuckets());
@@ -543,10 +565,6 @@
return emptyfn_.IsEmpty(ElementForIndex(index));
}
- size_t NumBuckets() const {
- return num_buckets_;
- }
-
// Allocate a number of buckets.
void AllocateStorage(size_t num_buckets) {
num_buckets_ = num_buckets;