use super::{
Entry, IdHashItem, IntoIter, Iter, IterMut, OccupiedEntry, RefMut,
VacantEntry, tables::IdHashMapTables,
};
use crate::{
DefaultHashBuilder,
errors::DuplicateItem,
internal::{ValidateCompact, ValidationError},
support::{
alloc::{Allocator, Global, global_alloc},
borrow::DormantMutRef,
item_set::ItemSet,
map_hash::MapHash,
},
};
use alloc::collections::BTreeSet;
use core::{
fmt,
hash::{BuildHasher, Hash},
};
use equivalent::Equivalent;
use hashbrown::hash_table;
#[derive(Clone)]
pub struct IdHashMap<T, S = DefaultHashBuilder, A: Allocator = Global> {
pub(super) items: ItemSet<T, A>,
pub(super) tables: IdHashMapTables<S, A>,
}
impl<T: IdHashItem, S: Default, A: Allocator + Default> Default
for IdHashMap<T, S, A>
{
fn default() -> Self {
Self {
items: ItemSet::with_capacity_in(0, A::default()),
tables: IdHashMapTables::default(),
}
}
}
#[cfg(feature = "default-hasher")]
impl<T: IdHashItem> IdHashMap<T> {
#[inline]
pub fn new() -> Self {
Self { items: ItemSet::new(), tables: IdHashMapTables::default() }
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
items: ItemSet::with_capacity_in(capacity, global_alloc()),
tables: IdHashMapTables::with_capacity_and_hasher_in(
capacity,
DefaultHashBuilder::default(),
global_alloc(),
),
}
}
}
impl<T: IdHashItem, S: BuildHasher> IdHashMap<T, S> {
pub const fn with_hasher(hasher: S) -> Self {
Self {
items: ItemSet::new(),
tables: IdHashMapTables::with_hasher_in(hasher, global_alloc()),
}
}
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
Self {
items: ItemSet::with_capacity_in(capacity, global_alloc()),
tables: IdHashMapTables::with_capacity_and_hasher_in(
capacity,
hasher,
global_alloc(),
),
}
}
}
#[cfg(feature = "default-hasher")]
impl<T: IdHashItem, A: Clone + Allocator> IdHashMap<T, DefaultHashBuilder, A> {
pub fn new_in(alloc: A) -> Self {
Self {
items: ItemSet::with_capacity_in(0, alloc.clone()),
tables: IdHashMapTables::with_capacity_and_hasher_in(
0,
DefaultHashBuilder::default(),
alloc,
),
}
}
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
Self {
items: ItemSet::with_capacity_in(capacity, alloc.clone()),
tables: IdHashMapTables::with_capacity_and_hasher_in(
capacity,
DefaultHashBuilder::default(),
alloc,
),
}
}
}
impl<T: IdHashItem, S: BuildHasher, A: Clone + Allocator> IdHashMap<T, S, A> {
pub fn with_hasher_in(hasher: S, alloc: A) -> Self {
Self {
items: ItemSet::new_in(alloc.clone()),
tables: IdHashMapTables::with_hasher_in(hasher, alloc),
}
}
pub fn with_capacity_and_hasher_in(
capacity: usize,
hasher: S,
alloc: A,
) -> Self {
Self {
items: ItemSet::with_capacity_in(capacity, alloc.clone()),
tables: IdHashMapTables::with_capacity_and_hasher_in(
capacity, hasher, alloc,
),
}
}
}
impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IdHashMap<T, S, A> {
#[cfg(feature = "daft")]
pub(crate) fn hasher(&self) -> &S {
self.tables.hasher()
}
pub fn allocator(&self) -> &A {
self.items.allocator()
}
pub fn capacity(&self) -> usize {
self.items.capacity()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.items.is_empty()
}
#[inline]
pub fn len(&self) -> usize {
self.items.len()
}
pub fn clear(&mut self) {
self.items.clear();
self.tables.key_to_item.clear();
}
pub fn reserve(&mut self, additional: usize) {
self.items.reserve(additional);
let items = &self.items;
let state = &self.tables.state;
self.tables
.key_to_item
.reserve(additional, |ix| state.hash_one(items[*ix].key()));
}
pub fn try_reserve(
&mut self,
additional: usize,
) -> Result<(), crate::errors::TryReserveError> {
self.items
.try_reserve(additional)
.map_err(crate::errors::TryReserveError::from_hashbrown)?;
let items = &self.items;
let state = &self.tables.state;
self.tables
.key_to_item
.try_reserve(additional, |ix| state.hash_one(items[*ix].key()))
.map_err(crate::errors::TryReserveError::from_hashbrown)?;
Ok(())
}
pub fn shrink_to_fit(&mut self) {
self.items.shrink_to_fit();
let items = &self.items;
let state = &self.tables.state;
self.tables
.key_to_item
.shrink_to_fit(|ix| state.hash_one(items[*ix].key()));
}
pub fn shrink_to(&mut self, min_capacity: usize) {
self.items.shrink_to(min_capacity);
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()));
}
#[inline]
pub fn iter(&self) -> Iter<'_, T> {
Iter::new(&self.items)
}
#[inline]
pub fn iter_mut(&mut self) -> IterMut<'_, T, S, A> {
IterMut::new(&self.tables, &mut self.items)
}
#[doc(hidden)]
pub fn validate(
&self,
compactness: ValidateCompact,
) -> Result<(), ValidationError>
where
T: fmt::Debug,
{
self.items.validate(compactness)?;
self.tables.validate(self.len(), compactness)?;
for (&ix, item) in self.items.iter() {
let key = item.key();
let Some(ix1) = self.find_index(&key) else {
return Err(ValidationError::general(format!(
"item at index {ix} has no key1 index"
)));
};
if ix1 != ix {
return Err(ValidationError::General(format!(
"item at index {ix} has mismatched indexes: ix1: {ix1}",
)));
}
}
Ok(())
}
#[doc(alias = "insert")]
pub fn insert_overwrite(&mut self, value: T) -> Option<T> {
let duplicate = self.remove(&value.key());
if self.insert_unique(value).is_err() {
panic!("insert_unique failed after removing duplicates");
}
duplicate
}
pub fn insert_unique(
&mut self,
value: T,
) -> Result<(), DuplicateItem<T, &T>> {
let _ = self.insert_unique_impl(value)?;
Ok(())
}
pub fn contains_key<'a, Q>(&'a self, key1: &Q) -> bool
where
Q: ?Sized + Hash + Equivalent<T::Key<'a>>,
{
self.find_index(key1).is_some()
}
pub fn get<'a, Q>(&'a self, key: &Q) -> Option<&'a T>
where
Q: ?Sized + Hash + Equivalent<T::Key<'a>>,
{
self.find_index(key).map(|ix| &self.items[ix])
}
pub fn get_mut<'a, Q>(&'a mut self, key: &Q) -> Option<RefMut<'a, T, S>>
where
Q: ?Sized + Hash + Equivalent<T::Key<'a>>,
{
let (dormant_map, index) = {
let (map, dormant_map) = DormantMutRef::new(self);
let index = map.find_index(key)?;
(dormant_map, index)
};
let awakened_map = unsafe { dormant_map.awaken() };
let item = &mut awakened_map.items[index];
let state = awakened_map.tables.state.clone();
let hashes = awakened_map.tables.make_hash(item);
Some(RefMut::new(state, hashes, item))
}
pub fn remove<'a, Q>(&'a mut self, key: &Q) -> Option<T>
where
Q: ?Sized + Hash + Equivalent<T::Key<'a>>,
{
let (dormant_map, remove_index) = {
let (map, dormant_map) = DormantMutRef::new(self);
let remove_index = map.find_index(key)?;
(dormant_map, remove_index)
};
let awakened_map = unsafe { dormant_map.awaken() };
let value = awakened_map
.items
.remove(remove_index)
.expect("items missing key1 that was just retrieved");
let state = &awakened_map.tables.state;
let Ok(item1) = awakened_map.tables.key_to_item.find_entry(
state,
&value.key(),
|index| {
if index == remove_index {
value.key()
} else {
awakened_map.items[index].key()
}
},
) else {
panic!("we just looked this item up");
};
item1.remove();
Some(value)
}
pub fn entry<'a>(&'a mut self, key: T::Key<'_>) -> Entry<'a, T, S, A> {
let (map, dormant_map) = DormantMutRef::new(self);
let key = T::upcast_key(key);
{
let index: Option<usize> = map.tables.key_to_item.find_index(
&map.tables.state,
&key,
|index| map.items[index].key(),
);
if let Some(index) = index {
drop(key);
return Entry::Occupied(
unsafe { OccupiedEntry::new(dormant_map, index) },
);
}
}
let hash = map.make_key_hash(&key);
Entry::Vacant(
unsafe { VacantEntry::new(dormant_map, hash) },
)
}
pub fn retain<'a, F>(&'a mut self, mut f: F)
where
F: FnMut(RefMut<'a, T, S>) -> bool,
{
let hash_state = self.tables.state.clone();
let (_, mut dormant_items) = DormantMutRef::new(&mut self.items);
self.tables.key_to_item.retain(|index| {
let (item, dormant_items) = {
let items = unsafe { dormant_items.reborrow() };
let (items, dormant_items) = DormantMutRef::new(items);
let item: &'a mut T = items
.get_mut(index)
.expect("all indexes are present in self.items");
(item, dormant_items)
};
let (hash, dormant_item) = {
let (item, dormant_item): (&'a mut T, _) =
DormantMutRef::new(item);
let key = T::key(item);
let hash = hash_state.hash_one(key);
(MapHash::new(hash), dormant_item)
};
let retain = {
let item = unsafe { dormant_item.awaken() };
let ref_mut = RefMut::new(hash_state.clone(), hash, item);
f(ref_mut)
};
if retain {
true
} else {
let items = unsafe { dormant_items.awaken() };
items.remove(index);
false
}
});
}
fn find_index<'a, Q>(&'a self, k: &Q) -> Option<usize>
where
Q: Hash + Equivalent<T::Key<'a>> + ?Sized,
{
self.tables
.key_to_item
.find_index(&self.tables.state, k, |index| self.items[index].key())
}
fn make_hash(&self, item: &T) -> MapHash {
self.tables.make_hash(item)
}
fn make_key_hash(&self, key: &T::Key<'_>) -> MapHash {
self.tables.make_key_hash::<T>(key)
}
pub(super) fn get_by_index(&self, index: usize) -> Option<&T> {
self.items.get(index)
}
pub(super) fn get_by_index_mut(
&mut self,
index: usize,
) -> Option<RefMut<'_, T, S>> {
let state = self.tables.state.clone();
let hashes = self.make_hash(&self.items[index]);
let item = &mut self.items[index];
Some(RefMut::new(state, hashes, item))
}
pub(super) fn insert_unique_impl(
&mut self,
value: T,
) -> Result<usize, DuplicateItem<T, &T>> {
let mut duplicates = BTreeSet::new();
let key = value.key();
let state = &self.tables.state;
let entry = match self
.tables
.key_to_item
.entry(state, key, |index| self.items[index].key())
{
hash_table::Entry::Occupied(slot) => {
duplicates.insert(*slot.get());
None
}
hash_table::Entry::Vacant(slot) => Some(slot),
};
if !duplicates.is_empty() {
return Err(DuplicateItem::__internal_new(
value,
duplicates.iter().map(|ix| &self.items[*ix]).collect(),
));
}
let next_index = self.items.insert_at_next_index(value);
entry.unwrap().insert(next_index);
Ok(next_index)
}
pub(super) fn remove_by_index(&mut self, remove_index: usize) -> Option<T> {
let value = self.items.remove(remove_index)?;
let state = &self.tables.state;
let Ok(item) =
self.tables.key_to_item.find_entry(state, &value.key(), |index| {
if index == remove_index {
value.key()
} else {
self.items[index].key()
}
})
else {
panic!("we just looked this item up");
};
item.remove();
Some(value)
}
pub(super) fn replace_at_index(&mut self, index: usize, value: T) -> T {
let old_key =
self.get_by_index(index).expect("index is known to be valid").key();
if T::upcast_key(old_key) != value.key() {
panic!(
"must insert a value with \
the same key used to create the entry"
);
}
self.items.replace(index, value)
}
}
impl<'a, T, S: Clone + BuildHasher, A: Allocator> fmt::Debug
for IdHashMap<T, S, A>
where
T: IdHashItem + fmt::Debug,
T::Key<'a>: fmt::Debug,
T: 'a,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut map = f.debug_map();
for item in self.iter() {
let key = item.key();
let key: T::Key<'a> =
unsafe { core::mem::transmute::<T::Key<'_>, T::Key<'a>>(key) };
map.entry(&key, item);
}
map.finish()
}
}
impl<T: IdHashItem + PartialEq, S: Clone + BuildHasher, A: Allocator> PartialEq
for IdHashMap<T, S, A>
{
fn eq(&self, other: &Self) -> bool {
if self.items.len() != other.items.len() {
return false;
}
for item in self.items.values() {
let k1 = item.key();
let Some(other_ix) = other.find_index(&k1) else {
return false;
};
let other_item = &other.items[other_ix];
if item != other_item {
return false;
}
}
true
}
}
impl<T: IdHashItem + Eq, S: Clone + BuildHasher, A: Allocator> Eq
for IdHashMap<T, S, A>
{
}
impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> Extend<T>
for IdHashMap<T, S, A>
{
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
let iter = iter.into_iter();
let reserve = if self.is_empty() {
iter.size_hint().0
} else {
iter.size_hint().0.div_ceil(2)
};
self.reserve(reserve);
for item in iter {
self.insert_overwrite(item);
}
}
}
impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator
for &'a IdHashMap<T, S, A>
{
type Item = &'a T;
type IntoIter = Iter<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<'a, T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator
for &'a mut IdHashMap<T, S, A>
{
type Item = RefMut<'a, T, S>;
type IntoIter = IterMut<'a, T, S, A>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
impl<T: IdHashItem, S: Clone + BuildHasher, A: Allocator> IntoIterator
for IdHashMap<T, S, A>
{
type Item = T;
type IntoIter = IntoIter<T, A>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self.items)
}
}
impl<T: IdHashItem, S: Default + Clone + BuildHasher, A: Allocator + Default>
FromIterator<T> for IdHashMap<T, S, A>
{
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
let mut map = IdHashMap::default();
for item in iter {
map.insert_overwrite(item);
}
map
}
}