Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ iddqd = { path = "crates/iddqd", default-features = false }
iddqd-test-utils = { path = "crates/iddqd-test-utils" }
proptest = { version = "1.7.0", default-features = false, features = ["std"] }
ref-cast = "1.0.24"
rustc-hash = { version = "2.1.1", default-features = false }
schemars = "0.8.22"
serde = "1.0.223"
serde_core = "1.0.223"
Expand Down
3 changes: 1 addition & 2 deletions crates/iddqd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ equivalent.workspace = true
foldhash = { workspace = true, optional = true }
hashbrown.workspace = true
ref-cast = { workspace = true, optional = true }
rustc-hash.workspace = true
schemars = { workspace = true, optional = true }
serde_core = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
Expand All @@ -50,7 +49,7 @@ default-hasher = ["dep:foldhash", "iddqd-test-utils/default-hasher"]
proptest = ["dep:proptest"]
schemars08 = ["dep:schemars", "dep:serde_json", "serde"]
serde = ["dep:serde_core", "iddqd-test-utils/serde"]
std = ["dep:foldhash", "iddqd-test-utils/std", "rustc-hash/std"]
std = ["dep:foldhash", "iddqd-test-utils/std"]

# Internal-only feature for testing that schemars/preserve_order works.
internal-schemars08-preserve-order = ["schemars08", "schemars/preserve_order"]
Expand Down
38 changes: 28 additions & 10 deletions crates/iddqd/src/bi_hash_map/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,9 +812,7 @@ impl<T: BiHashItem, S: Clone + BuildHasher, A: Allocator> BiHashMap<T, S, A> {
&mut self,
additional: usize,
) -> Result<(), crate::errors::TryReserveError> {
self.items
.try_reserve(additional)
.map_err(crate::errors::TryReserveError::from_hashbrown)?;
self.items.try_reserve(additional)?;
self.tables
.k1_to_item
.try_reserve(additional)
Expand Down Expand Up @@ -863,9 +861,19 @@ impl<T: BiHashItem, S: Clone + BuildHasher, A: Allocator> BiHashMap<T, S, A> {
/// # }
/// ```
pub fn shrink_to_fit(&mut self) {
self.items.shrink_to_fit();
self.tables.k1_to_item.shrink_to_fit();
self.tables.k2_to_item.shrink_to_fit();
let remap = self.items.shrink_to_fit();
if !remap.is_empty() {
self.tables.k1_to_item.remap_indexes(&remap);
self.tables.k2_to_item.remap_indexes(&remap);
}
let items = &self.items;
let state = &self.tables.state;
self.tables
.k1_to_item
.shrink_to_fit(|ix| state.hash_one(items[*ix].key1()));
self.tables
.k2_to_item
.shrink_to_fit(|ix| state.hash_one(items[*ix].key2()));
}

/// Shrinks the capacity of the map with a lower limit. It will drop
Expand Down Expand Up @@ -910,9 +918,19 @@ impl<T: BiHashItem, S: Clone + BuildHasher, A: Allocator> BiHashMap<T, S, A> {
/// # }
/// ```
pub fn shrink_to(&mut self, min_capacity: usize) {
self.items.shrink_to(min_capacity);
self.tables.k1_to_item.shrink_to(min_capacity);
self.tables.k2_to_item.shrink_to(min_capacity);
let remap = self.items.shrink_to(min_capacity);
if !remap.is_empty() {
self.tables.k1_to_item.remap_indexes(&remap);
self.tables.k2_to_item.remap_indexes(&remap);
}
let items = &self.items;
let state = &self.tables.state;
self.tables
.k1_to_item
.shrink_to(min_capacity, |ix| state.hash_one(items[*ix].key1()));
self.tables
.k2_to_item
.shrink_to(min_capacity, |ix| state.hash_one(items[*ix].key2()));
}

/// Returns an iterator over all items in the map.
Expand Down Expand Up @@ -1031,7 +1049,7 @@ impl<T: BiHashItem, S: Clone + BuildHasher, A: Allocator> BiHashMap<T, S, A> {
self.tables.validate(self.len(), compactness)?;

// Check that the indexes are all correct.
for (&ix, item) in self.items.iter() {
for (ix, item) in self.items.iter() {
let key1 = item.key1();
let key2 = item.key2();

Expand Down
14 changes: 5 additions & 9 deletions crates/iddqd/src/bi_hash_map/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use super::{RefMut, tables::BiHashMapTables};
use crate::{
BiHashItem, DefaultHashBuilder,
support::{
alloc::{AllocWrapper, Allocator, Global},
item_set::ItemSet,
alloc::{Allocator, Global},
item_set::{self, ItemSet},
},
};
use core::{hash::BuildHasher, iter::FusedIterator};
use hashbrown::hash_map;

/// An iterator over the elements of a [`BiHashMap`] by shared reference.
/// Created by [`BiHashMap::iter`].
Expand All @@ -20,7 +19,7 @@ use hashbrown::hash_map;
/// [`HashMap`]: std::collections::HashMap
#[derive(Clone, Debug, Default)]
pub struct Iter<'a, T: BiHashItem> {
inner: hash_map::Values<'a, usize, T>,
inner: item_set::Values<'a, T>,
}

impl<'a, T: BiHashItem> Iter<'a, T> {
Expand All @@ -45,7 +44,6 @@ impl<T: BiHashItem> ExactSizeIterator for Iter<'_, T> {
}
}

// hash_map::Iter is a FusedIterator, so Iter is as well.
impl<T: BiHashItem> FusedIterator for Iter<'_, T> {}

/// An iterator over the elements of a [`BiHashMap`] by mutable reference.
Expand All @@ -67,7 +65,7 @@ pub struct IterMut<
A: Allocator = Global,
> {
tables: &'a BiHashMapTables<S, A>,
inner: hash_map::ValuesMut<'a, usize, T>,
inner: item_set::ValuesMut<'a, T>,
}

impl<'a, T: BiHashItem, S: Clone + BuildHasher, A: Allocator>
Expand Down Expand Up @@ -103,7 +101,6 @@ impl<T: BiHashItem, S: Clone + BuildHasher, A: Allocator> ExactSizeIterator
}
}

// hash_map::IterMut is a FusedIterator, so IterMut is as well.
impl<T: BiHashItem, S: Clone + BuildHasher, A: Allocator> FusedIterator
for IterMut<'_, T, S, A>
{
Expand All @@ -120,7 +117,7 @@ impl<T: BiHashItem, S: Clone + BuildHasher, A: Allocator> FusedIterator
/// [`HashMap`]: std::collections::HashMap
#[derive(Debug)]
pub struct IntoIter<T: BiHashItem, A: Allocator = Global> {
inner: hash_map::IntoValues<usize, T, AllocWrapper<A>>,
inner: item_set::IntoValues<T, A>,
}

impl<T: BiHashItem, A: Allocator> IntoIter<T, A> {
Expand All @@ -145,5 +142,4 @@ impl<T: BiHashItem, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
}
}

// hash_map::IterMut is a FusedIterator, so IterMut is as well.
impl<T: BiHashItem, A: Allocator> FusedIterator for IntoIter<T, A> {}
25 changes: 24 additions & 1 deletion crates/iddqd/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub struct TryReserveError {
}

#[derive(Clone, PartialEq, Eq, Debug)]
enum TryReserveErrorKind {
pub(crate) enum TryReserveErrorKind {
/// Error due to the computed capacity exceeding the collection's maximum
/// (usually `isize::MAX` bytes).
CapacityOverflow,
Expand All @@ -97,6 +97,29 @@ impl TryReserveError {
};
Self { kind }
}

/// Converts from an `allocator_api2` `TryReserveError`.
pub(crate) fn from_allocator_api2(
error: allocator_api2::collections::TryReserveError,
) -> Self {
use allocator_api2::collections::TryReserveErrorKind as Kind;
let kind = match error.kind() {
Kind::CapacityOverflow => TryReserveErrorKind::CapacityOverflow,
Kind::AllocError { layout, .. } => {
TryReserveErrorKind::AllocError { layout }
}
};
Self { kind }
}

#[doc(hidden)]
pub(crate) fn __from_kind(kind: TryReserveErrorKind) -> Self {
Self { kind }
}

pub(crate) fn kind(&self) -> &TryReserveErrorKind {
&self.kind
}
}

impl fmt::Display for TryReserveError {
Expand Down
28 changes: 20 additions & 8 deletions crates/iddqd/src/id_hash_map/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,7 @@ impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IdHashMap<T, S, A> {
&mut self,
additional: usize,
) -> Result<(), crate::errors::TryReserveError> {
self.items
.try_reserve(additional)
.map_err(crate::errors::TryReserveError::from_hashbrown)?;
self.items.try_reserve(additional)?;
self.tables
.key_to_item
.try_reserve(additional)
Expand Down Expand Up @@ -733,8 +731,15 @@ impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IdHashMap<T, S, A> {
/// # }
/// ```
pub fn shrink_to_fit(&mut self) {
self.items.shrink_to_fit();
self.tables.key_to_item.shrink_to_fit();
let remap = self.items.shrink_to_fit();
if !remap.is_empty() {
self.tables.key_to_item.remap_indexes(&remap);
}
let items = &self.items;
let state = &self.tables.state;
self.tables
.key_to_item
.shrink_to_fit(|ix| state.hash_one(items[*ix].key()));
}

/// Shrinks the capacity of the map with a lower limit. It will drop
Expand Down Expand Up @@ -775,8 +780,15 @@ impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IdHashMap<T, S, A> {
/// # }
/// ```
pub fn shrink_to(&mut self, min_capacity: usize) {
self.items.shrink_to(min_capacity);
self.tables.key_to_item.shrink_to(min_capacity);
let remap = self.items.shrink_to(min_capacity);
if !remap.is_empty() {
self.tables.key_to_item.remap_indexes(&remap);
}
let items = &self.items;
let state = &self.tables.state;
self.tables
.key_to_item
.shrink_to(min_capacity, |ix| state.hash_one(items[*ix].key()));
}

/// Iterates over the items in the map.
Expand Down Expand Up @@ -880,7 +892,7 @@ impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IdHashMap<T, S, A> {
self.tables.validate(self.len(), compactness)?;

// Check that the indexes are all correct.
for (&ix, item) in self.items.iter() {
for (ix, item) in self.items.iter() {
let key = item.key();
let Some(ix1) = self.find_index(&key) else {
return Err(ValidationError::general(format!(
Expand Down
14 changes: 5 additions & 9 deletions crates/iddqd/src/id_hash_map/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use super::{RefMut, tables::IdHashMapTables};
use crate::{
DefaultHashBuilder, IdHashItem,
support::{
alloc::{AllocWrapper, Allocator, Global},
item_set::ItemSet,
alloc::{Allocator, Global},
item_set::{self, ItemSet},
},
};
use core::{hash::BuildHasher, iter::FusedIterator};
use hashbrown::hash_map;

/// An iterator over the elements of a [`IdHashMap`] by shared reference.
/// Created by [`IdHashMap::iter`].
Expand All @@ -20,7 +19,7 @@ use hashbrown::hash_map;
/// [`HashMap`]: std::collections::HashMap
#[derive(Clone, Debug, Default)]
pub struct Iter<'a, T: IdHashItem> {
inner: hash_map::Values<'a, usize, T>,
inner: item_set::Values<'a, T>,
}

impl<'a, T: IdHashItem> Iter<'a, T> {
Expand All @@ -45,7 +44,6 @@ impl<T: IdHashItem> ExactSizeIterator for Iter<'_, T> {
}
}

// hash_map::Iter is a FusedIterator, so Iter is as well.
impl<T: IdHashItem> FusedIterator for Iter<'_, T> {}

/// An iterator over the elements of a [`IdHashMap`] by mutable reference.
Expand All @@ -67,7 +65,7 @@ pub struct IterMut<
A: Allocator = Global,
> {
tables: &'a IdHashMapTables<S, A>,
inner: hash_map::ValuesMut<'a, usize, T>,
inner: item_set::ValuesMut<'a, T>,
}

impl<'a, T: IdHashItem, S: BuildHasher, A: Allocator> IterMut<'a, T, S, A> {
Expand Down Expand Up @@ -101,7 +99,6 @@ impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> ExactSizeIterator
}
}

// hash_map::IterMut is a FusedIterator, so IterMut is as well.
impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> FusedIterator
for IterMut<'_, T, S, A>
{
Expand All @@ -118,7 +115,7 @@ impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> FusedIterator
/// [`HashMap`]: std::collections::HashMap
#[derive(Debug)]
pub struct IntoIter<T: IdHashItem, A: Allocator = Global> {
inner: hash_map::IntoValues<usize, T, AllocWrapper<A>>,
inner: item_set::IntoValues<T, A>,
}

impl<T: IdHashItem, A: Allocator> IntoIter<T, A> {
Expand All @@ -143,5 +140,4 @@ impl<T: IdHashItem, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
}
}

// hash_map::IterMut is a FusedIterator, so IterMut is as well.
impl<T: IdHashItem, A: Allocator> FusedIterator for IntoIter<T, A> {}
16 changes: 12 additions & 4 deletions crates/iddqd/src/id_ord_map/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,11 @@ impl<T: IdOrdItem> IdOrdMap<T> {
/// assert!(map.capacity() >= 2);
/// ```
pub fn shrink_to_fit(&mut self) {
self.items.shrink_to_fit();
let remap = self.items.shrink_to_fit();
if !remap.is_empty() {
let items = &self.items;
self.tables.key_to_item.remap_indexes(&remap, |ix| items[ix].key());
}
}

/// Shrinks the capacity of the map with a lower limit. It will drop
Expand Down Expand Up @@ -474,7 +478,11 @@ impl<T: IdOrdItem> IdOrdMap<T> {
/// assert!(map.capacity() >= 2);
/// ```
pub fn shrink_to(&mut self, min_capacity: usize) {
self.items.shrink_to(min_capacity);
let remap = self.items.shrink_to(min_capacity);
if !remap.is_empty() {
let items = &self.items;
self.tables.key_to_item.remap_indexes(&remap, |ix| items[ix].key());
}
}

/// Iterates over the items in the map.
Expand Down Expand Up @@ -591,7 +599,7 @@ impl<T: IdOrdItem> IdOrdMap<T> {

// Check that the indexes are all correct.

for (&ix, item) in self.items.iter() {
for (ix, item) in self.items.iter() {
let key = item.key();
let ix1 = match chaos {
ValidateChaos::Yes => {
Expand Down Expand Up @@ -1364,7 +1372,7 @@ impl<T: IdOrdItem> IdOrdMap<T> {
Q: ?Sized + Ord + Equivalent<T::Key<'a>>,
{
self.items.iter().find_map(|(index, item)| {
(k.equivalent(&item.key())).then_some(*index)
(k.equivalent(&item.key())).then_some(index)
})
}

Expand Down
Loading
Loading