use core::fmt::Debug;
use core::{borrow::Borrow, hash::Hash, iter::FusedIterator, ops::Index};
#[cfg(not(feature = "no-hash-maps"))]
mod detail {
use crate::collections::hash;
use hashbrown::hash_map;
pub type MapImpl<K, V> = hash_map::HashMap<K, V, hash::RandomState>;
pub type EntryImpl<'a, K, V> = hash_map::Entry<'a, K, V, hash::RandomState>;
pub type OccupiedEntryImpl<'a, K, V> = hash_map::OccupiedEntry<'a, K, V, hash::RandomState>;
pub type VacantEntryImpl<'a, K, V> = hash_map::VacantEntry<'a, K, V, hash::RandomState>;
pub type IterImpl<'a, K, V> = hash_map::Iter<'a, K, V>;
pub type IterMutImpl<'a, K, V> = hash_map::IterMut<'a, K, V>;
pub type IntoIterImpl<K, V> = hash_map::IntoIter<K, V>;
pub type KeysImpl<'a, K, V> = hash_map::Keys<'a, K, V>;
pub type ValuesImpl<'a, K, V> = hash_map::Values<'a, K, V>;
pub type ValuesMutImpl<'a, K, V> = hash_map::ValuesMut<'a, K, V>;
pub type IntoKeysImpl<K, V> = hash_map::IntoKeys<K, V>;
pub type IntoValuesImpl<K, V> = hash_map::IntoValues<K, V>;
}
#[cfg(feature = "no-hash-maps")]
mod detail {
use alloc::collections::btree_map;
pub type MapImpl<K, V> = btree_map::BTreeMap<K, V>;
pub type EntryImpl<'a, K, V> = btree_map::Entry<'a, K, V>;
pub type OccupiedEntryImpl<'a, K, V> = btree_map::OccupiedEntry<'a, K, V>;
pub type VacantEntryImpl<'a, K, V> = btree_map::VacantEntry<'a, K, V>;
pub type IterImpl<'a, K, V> = btree_map::Iter<'a, K, V>;
pub type IterMutImpl<'a, K, V> = btree_map::IterMut<'a, K, V>;
pub type IntoIterImpl<K, V> = btree_map::IntoIter<K, V>;
pub type KeysImpl<'a, K, V> = btree_map::Keys<'a, K, V>;
pub type ValuesImpl<'a, K, V> = btree_map::Values<'a, K, V>;
pub type ValuesMutImpl<'a, K, V> = btree_map::ValuesMut<'a, K, V>;
pub type IntoKeysImpl<K, V> = btree_map::IntoKeys<K, V>;
pub type IntoValuesImpl<K, V> = btree_map::IntoValues<K, V>;
}
#[derive(Debug, Clone)]
pub struct Map<K, V> {
inner: detail::MapImpl<K, V>,
}
impl<K, V> Default for Map<K, V> {
#[inline]
fn default() -> Self {
Self {
inner: detail::MapImpl::default(),
}
}
}
impl<K, V> Map<K, V> {
#[inline]
pub fn new() -> Self {
Self::default()
}
#[inline]
pub fn clear(&mut self) {
self.inner.clear()
}
#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
#[inline]
pub fn iter(&self) -> Iter<'_, K, V> {
Iter {
inner: self.inner.iter(),
}
}
#[inline]
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
IterMut {
inner: self.inner.iter_mut(),
}
}
#[inline]
pub fn keys(&self) -> Keys<'_, K, V> {
Keys {
inner: self.inner.keys(),
}
}
#[inline]
pub fn into_keys(self) -> IntoKeys<K, V> {
IntoKeys {
inner: self.inner.into_keys(),
}
}
#[inline]
pub fn values(&self) -> Values<'_, K, V> {
Values {
inner: self.inner.values(),
}
}
#[inline]
pub fn into_values(self) -> IntoValues<K, V> {
IntoValues {
inner: self.inner.into_values(),
}
}
#[inline]
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
ValuesMut {
inner: self.inner.values_mut(),
}
}
}
impl<K, V> Map<K, V>
where
K: Hash + Eq + Ord,
{
#[inline]
pub fn reserve(&mut self, additional: usize) {
#[cfg(not(feature = "no-hash-maps"))]
self.inner.reserve(additional);
#[cfg(feature = "no-hash-maps")]
let _ = additional;
}
#[inline]
pub fn contains_key<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{
self.inner.contains_key(key)
}
#[inline]
pub fn get<Q>(&self, key: &Q) -> Option<&V>
where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{
self.inner.get(key)
}
#[inline]
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{
self.inner.get_key_value(key)
}
#[inline]
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{
self.inner.get_mut(key)
}
#[inline]
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
self.inner.insert(key, value)
}
#[inline]
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq + Ord,
{
self.inner.remove(key)
}
#[inline]
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: ?Sized + Hash + Ord,
{
self.inner.remove_entry(key)
}
#[inline]
pub fn entry(&mut self, key: K) -> Entry<'_, K, V> {
match self.inner.entry(key) {
detail::EntryImpl::Occupied(entry) => Entry::Occupied(OccupiedEntry { inner: entry }),
detail::EntryImpl::Vacant(entry) => Entry::Vacant(VacantEntry { inner: entry }),
}
}
#[inline]
pub fn retain<F>(&mut self, f: F)
where
F: FnMut(&K, &mut V) -> bool,
{
self.inner.retain(f)
}
}
impl<K, V> PartialEq for Map<K, V>
where
K: Eq + Hash,
V: Eq,
{
#[inline]
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl<K, V> Eq for Map<K, V>
where
K: Eq + Hash,
V: Eq,
{
}
impl<K, Q, V> Index<&Q> for Map<K, V>
where
K: Borrow<Q> + Hash + Eq + Ord,
Q: ?Sized + Hash + Eq + Ord,
{
type Output = V;
#[inline]
fn index(&self, key: &Q) -> &V {
&self.inner[key]
}
}
impl<'a, K, V> Extend<(&'a K, &'a V)> for Map<K, V>
where
K: Eq + Hash + Ord + Copy,
V: Copy,
{
#[inline]
fn extend<Iter: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: Iter) {
self.inner.extend(iter)
}
}
impl<K, V> Extend<(K, V)> for Map<K, V>
where
K: Eq + Hash + Ord,
{
#[inline]
fn extend<Iter: IntoIterator<Item = (K, V)>>(&mut self, iter: Iter) {
self.inner.extend(iter)
}
}
#[derive(Debug)]
pub enum Entry<'a, K: Ord, V> {
Occupied(OccupiedEntry<'a, K, V>),
Vacant(VacantEntry<'a, K, V>),
}
impl<'a, K, V> Entry<'a, K, V>
where
K: Hash + Ord,
{
#[inline]
pub fn or_insert(self, default: V) -> &'a mut V {
match self {
Self::Occupied(entry) => entry.into_mut(),
Self::Vacant(entry) => entry.insert(default),
}
}
#[inline]
pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
match self {
Self::Occupied(entry) => entry.into_mut(),
Self::Vacant(entry) => entry.insert(default()),
}
}
#[inline]
pub fn or_insert_with_key<F: FnOnce(&K) -> V>(self, default: F) -> &'a mut V {
match self {
Self::Occupied(entry) => entry.into_mut(),
Self::Vacant(entry) => {
let value = default(entry.key());
entry.insert(value)
}
}
}
#[inline]
pub fn key(&self) -> &K {
match *self {
Self::Occupied(ref entry) => entry.key(),
Self::Vacant(ref entry) => entry.key(),
}
}
#[inline]
pub fn and_modify<F>(self, f: F) -> Self
where
F: FnOnce(&mut V),
{
match self {
Self::Occupied(mut entry) => {
f(entry.get_mut());
Self::Occupied(entry)
}
Self::Vacant(entry) => Self::Vacant(entry),
}
}
}
impl<'a, K, V> Entry<'a, K, V>
where
K: Hash + Ord,
V: Default,
{
#[inline]
pub fn or_default(self) -> &'a mut V {
match self {
Self::Occupied(entry) => entry.into_mut(),
Self::Vacant(entry) => entry.insert(Default::default()),
}
}
}
pub struct OccupiedEntry<'a, K, V> {
inner: detail::OccupiedEntryImpl<'a, K, V>,
}
impl<'a, K, V> Debug for OccupiedEntry<'a, K, V>
where
K: Debug + Ord + 'a,
V: Debug + 'a,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.inner.fmt(f)
}
}
impl<'a, K, V> OccupiedEntry<'a, K, V>
where
K: Ord + 'a,
V: 'a,
{
#[inline]
pub fn key(&self) -> &K {
self.inner.key()
}
#[inline]
pub fn get(&self) -> &V {
self.inner.get()
}
#[inline]
pub fn get_mut(&mut self) -> &mut V {
self.inner.get_mut()
}
#[inline]
pub fn insert(&mut self, value: V) -> V {
self.inner.insert(value)
}
#[inline]
pub fn into_mut(self) -> &'a mut V {
self.inner.into_mut()
}
#[inline]
pub fn remove_entry(self) -> (K, V) {
self.inner.remove_entry()
}
#[inline]
pub fn remove(self) -> V {
self.inner.remove()
}
}
pub struct VacantEntry<'a, K, V> {
inner: detail::VacantEntryImpl<'a, K, V>,
}
impl<'a, K, V> Debug for VacantEntry<'a, K, V>
where
K: Debug + Ord + 'a,
V: Debug + 'a,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.inner.fmt(f)
}
}
impl<'a, K, V> VacantEntry<'a, K, V>
where
K: Ord + 'a,
V: 'a,
{
#[inline]
pub fn key(&self) -> &K {
self.inner.key()
}
#[inline]
pub fn into_key(self) -> K {
self.inner.into_key()
}
#[inline]
pub fn insert(self, value: V) -> &'a mut V
where
K: Hash,
{
self.inner.insert(value)
}
}
impl<K, V> FromIterator<(K, V)> for Map<K, V>
where
K: Hash + Eq + Ord,
{
#[inline]
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = (K, V)>,
{
Self {
inner: <detail::MapImpl<K, V>>::from_iter(iter),
}
}
}
impl<'a, K, V> IntoIterator for &'a Map<K, V> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
#[derive(Debug, Clone)]
pub struct Iter<'a, K, V> {
inner: detail::IterImpl<'a, K, V>,
}
impl<'a, K: 'a, V: 'a> Iterator for Iter<'a, K, V> {
type Item = (&'a K, &'a V);
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<'a, K: 'a, V: 'a> ExactSizeIterator for Iter<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<'a, K: 'a, V: 'a> FusedIterator for Iter<'a, K, V> where
detail::IterImpl<'a, K, V>: FusedIterator
{
}
impl<'a, K: 'a, V: 'a> IntoIterator for &'a mut Map<K, V> {
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}
#[derive(Debug)]
pub struct IterMut<'a, K, V> {
inner: detail::IterMutImpl<'a, K, V>,
}
impl<'a, K: 'a, V: 'a> Iterator for IterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<'a, K: 'a, V: 'a> ExactSizeIterator for IterMut<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<'a, K: 'a, V: 'a> FusedIterator for IterMut<'a, K, V> where
detail::IterMutImpl<'a, K, V>: FusedIterator
{
}
impl<K, V> IntoIterator for Map<K, V> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
IntoIter {
inner: self.inner.into_iter(),
}
}
}
#[derive(Debug)]
pub struct IntoIter<K, V> {
inner: detail::IntoIterImpl<K, V>,
}
impl<K, V> Iterator for IntoIter<K, V> {
type Item = (K, V);
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<K, V> ExactSizeIterator for IntoIter<K, V> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<K, V> FusedIterator for IntoIter<K, V> where detail::IntoIterImpl<K, V>: FusedIterator {}
#[derive(Debug, Clone)]
pub struct Keys<'a, K, V> {
inner: detail::KeysImpl<'a, K, V>,
}
impl<'a, K: 'a, V> Iterator for Keys<'a, K, V> {
type Item = &'a K;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<'a, K: 'a, V> ExactSizeIterator for Keys<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<'a, K: 'a, V> FusedIterator for Keys<'a, K, V> where detail::KeysImpl<'a, K, V>: FusedIterator {}
#[derive(Debug, Clone)]
pub struct Values<'a, K, V> {
inner: detail::ValuesImpl<'a, K, V>,
}
impl<'a, K, V: 'a> Iterator for Values<'a, K, V> {
type Item = &'a V;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<'a, K, V: 'a> ExactSizeIterator for Values<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<'a, K, V: 'a> FusedIterator for Values<'a, K, V> where
detail::ValuesImpl<'a, K, V>: FusedIterator
{
}
#[derive(Debug)]
pub struct ValuesMut<'a, K, V> {
inner: detail::ValuesMutImpl<'a, K, V>,
}
impl<'a, K, V: 'a> Iterator for ValuesMut<'a, K, V> {
type Item = &'a mut V;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<'a, K, V: 'a> ExactSizeIterator for ValuesMut<'a, K, V> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<'a, K, V: 'a> FusedIterator for ValuesMut<'a, K, V> where
detail::ValuesMutImpl<'a, K, V>: FusedIterator
{
}
#[derive(Debug)]
pub struct IntoKeys<K, V> {
inner: detail::IntoKeysImpl<K, V>,
}
impl<K, V> Iterator for IntoKeys<K, V> {
type Item = K;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<K, V> FusedIterator for IntoKeys<K, V> where detail::IntoKeysImpl<K, V>: FusedIterator {}
#[derive(Debug)]
pub struct IntoValues<K, V> {
inner: detail::IntoValuesImpl<K, V>,
}
impl<K, V> Iterator for IntoValues<K, V> {
type Item = V;
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
}
impl<K, V> ExactSizeIterator for IntoValues<K, V> {
#[inline]
fn len(&self) -> usize {
self.inner.len()
}
}
impl<K, V> FusedIterator for IntoValues<K, V> where detail::IntoValuesImpl<K, V>: FusedIterator {}
#[cfg(feature = "serde")]
impl<K, V> serde::Serialize for Map<K, V>
where
K: serde::Serialize + Eq + Hash + Ord,
V: serde::Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::ser::Serializer,
{
serde::Serialize::serialize(&self.inner, serializer)
}
}
#[cfg(feature = "serde")]
impl<'a, K, V> serde::Deserialize<'a> for Map<K, V>
where
K: serde::Deserialize<'a> + Eq + Hash + Ord,
V: serde::Deserialize<'a>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::de::Deserializer<'a>,
{
Ok(Map {
inner: serde::Deserialize::deserialize(deserializer)?,
})
}
}