use crate::prelude::*;
#[cfg(any(
feature = "std",
feature = "hashbrown_0_14",
feature = "hashbrown_0_15",
feature = "hashbrown_0_16",
feature = "hashbrown_0_17",
feature = "indexmap_1",
feature = "indexmap_2"
))]
use crate::utils::size_hint_cautious;
pub trait DuplicateInsertsFirstWinsMap<K, V> {
fn new(size_hint: Option<usize>) -> Self;
fn insert(&mut self, key: K, value: V);
}
#[cfg(feature = "std")]
impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
}
#[inline]
fn insert(&mut self, key: K, value: V) {
use std::collections::hash_map::Entry;
match self.entry(key) {
Entry::Occupied(_) => {}
Entry::Vacant(vacant) => {
vacant.insert(value);
}
}
}
}
#[cfg(feature = "hashbrown_0_14")]
impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for hashbrown_0_14::HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
}
#[inline]
fn insert(&mut self, key: K, value: V) {
use hashbrown_0_14::hash_map::Entry;
match self.entry(key) {
Entry::Occupied(_) => {}
Entry::Vacant(vacant) => {
vacant.insert(value);
}
}
}
}
#[cfg(feature = "hashbrown_0_15")]
impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for hashbrown_0_15::HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
}
#[inline]
fn insert(&mut self, key: K, value: V) {
use hashbrown_0_15::hash_map::Entry;
match self.entry(key) {
Entry::Occupied(_) => {}
Entry::Vacant(vacant) => {
vacant.insert(value);
}
}
}
}
#[cfg(feature = "hashbrown_0_16")]
impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for hashbrown_0_16::HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
}
#[inline]
fn insert(&mut self, key: K, value: V) {
use hashbrown_0_16::hash_map::Entry;
match self.entry(key) {
Entry::Occupied(_) => {}
Entry::Vacant(vacant) => {
vacant.insert(value);
}
}
}
}
#[cfg(feature = "hashbrown_0_17")]
impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for hashbrown_0_17::HashMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
}
#[inline]
fn insert(&mut self, key: K, value: V) {
use hashbrown_0_17::hash_map::Entry;
match self.entry(key) {
Entry::Occupied(_) => {}
Entry::Vacant(vacant) => {
vacant.insert(value);
}
}
}
}
#[cfg(feature = "indexmap_1")]
impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for indexmap_1::IndexMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
}
#[inline]
fn insert(&mut self, key: K, value: V) {
use indexmap_1::map::Entry;
match self.entry(key) {
Entry::Occupied(_) => {}
Entry::Vacant(vacant) => {
vacant.insert(value);
}
}
}
}
#[cfg(feature = "indexmap_2")]
impl<K, V, S> DuplicateInsertsFirstWinsMap<K, V> for indexmap_2::IndexMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher + Default,
{
#[inline]
fn new(size_hint: Option<usize>) -> Self {
Self::with_capacity_and_hasher(size_hint_cautious::<(K, V)>(size_hint), S::default())
}
#[inline]
fn insert(&mut self, key: K, value: V) {
use indexmap_2::map::Entry;
match self.entry(key) {
Entry::Occupied(_) => {}
Entry::Vacant(vacant) => {
vacant.insert(value);
}
}
}
}
impl<K, V> DuplicateInsertsFirstWinsMap<K, V> for BTreeMap<K, V>
where
K: Ord,
{
#[inline]
fn new(_size_hint: Option<usize>) -> Self {
Self::new()
}
#[inline]
fn insert(&mut self, key: K, value: V) {
use alloc::collections::btree_map::Entry;
match self.entry(key) {
Entry::Occupied(_) => {}
Entry::Vacant(vacant) => {
vacant.insert(value);
}
}
}
}