Skip to content

Commit fd65df6

Browse files
committed
Expose insert_no_grow
1 parent 1393471 commit fd65df6

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "griddle"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
authors = ["Jon Gjengset <jon@thesquareplanet.com>"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"

src/raw/mod.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,20 +245,21 @@ impl<T> RawTable<T> {
245245
return self.insert(hash, value, hasher);
246246
}
247247

248-
let bucket = if cfg!(debug_assertions) {
249-
let buckets = self.table.buckets();
250-
let b = self.table.insert(hash, value, &hasher);
251-
252-
// make sure table didn't resize
253-
assert_eq!(
254-
buckets,
255-
self.table.buckets(),
256-
"resize while elements are still left over"
257-
);
258-
b
259-
} else {
260-
self.table.insert(hash, value, &hasher)
261-
};
248+
self.insert_no_grow(hash, value, hasher)
249+
}
250+
251+
/// Inserts a new element into the table, without growing the table.
252+
///
253+
/// There must be enough space in the table to insert the new element.
254+
///
255+
/// This does not check if the given element already exists in the table.
256+
///
257+
/// Note that unlike `hashbrown::RawTable::insert_no_grow`, this _does_ take a `hasher`.
258+
/// This is because while the insert won't grow the table, it may need to carry over some
259+
/// elements from the pre-resize table to the current table, which requires re-hashing.
260+
#[cfg_attr(feature = "inline-more", inline)]
261+
pub fn insert_no_grow(&mut self, hash: u64, value: T, hasher: impl Fn(&T) -> u64) -> Bucket<T> {
262+
let bucket = self.table.insert_no_grow(hash, value);
262263

263264
if self.leftovers.is_some() {
264265
// Also carry some items over.
@@ -477,7 +478,7 @@ impl<T> RawTable<T> {
477478
// to the resized map, without shrinking the old map.
478479
let value = unsafe { lo.table.remove(e) };
479480
let hash = hasher(&value);
480-
self.table.insert(hash, value, &hasher);
481+
self.table.insert_no_grow(hash, value);
481482
} else {
482483
// The resize is finally fully complete.
483484
let _ = self.leftovers.take();

0 commit comments

Comments
 (0)