| 1 |
use crate::prelude::*; |
| 2 |
|
| 3 |
pub trait DuplicateInsertsLastWinsSet<T> { |
| 4 |
fn new(size_hint: Option<usize>) -> Self; |
| 5 |
|
| 6 |
/// Insert or replace the existing value |
| 7 |
fn replace(&mut self, value: T); |
| 8 |
} |
| 9 |
|
| 10 |
#[cfg(feature = "std")] |
| 11 |
impl<T, S> DuplicateInsertsLastWinsSet<T> for HashSet<T, S> |
| 12 |
where |
| 13 |
T: Eq + Hash, |
| 14 |
S: BuildHasher + Default, |
| 15 |
{ |
| 16 |
#[inline] |
| 17 |
fn new(size_hint: Option<usize>) -> Self { |
| 18 |
match size_hint { |
| 19 |
Some(size) => Self::with_capacity_and_hasher(size, S::default()), |
| 20 |
None => Self::with_hasher(S::default()), |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
#[inline] |
| 25 |
fn replace(&mut self, value: T) { |
| 26 |
// Hashset already fulfils the contract |
| 27 |
self.replace(value); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
#[cfg(feature = "hashbrown_0_14")] |
| 32 |
impl<T, S> DuplicateInsertsLastWinsSet<T> for hashbrown_0_14::HashSet<T, S> |
| 33 |
where |
| 34 |
T: Eq + Hash, |
| 35 |
S: BuildHasher + Default, |
| 36 |
{ |
| 37 |
#[inline] |
| 38 |
fn new(size_hint: Option<usize>) -> Self { |
| 39 |
match size_hint { |
| 40 |
Some(size) => Self::with_capacity_and_hasher(size, S::default()), |
| 41 |
None => Self::with_hasher(S::default()), |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
#[inline] |
| 46 |
fn replace(&mut self, value: T) { |
| 47 |
// Hashset already fulfils the contract |
| 48 |
self.replace(value); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
#[cfg(feature = "hashbrown_0_15")] |
| 53 |
impl<T, S> DuplicateInsertsLastWinsSet<T> for hashbrown_0_15::HashSet<T, S> |
| 54 |
where |
| 55 |
T: Eq + Hash, |
| 56 |
S: BuildHasher + Default, |
| 57 |
{ |
| 58 |
#[inline] |
| 59 |
fn new(size_hint: Option<usize>) -> Self { |
| 60 |
match size_hint { |
| 61 |
Some(size) => Self::with_capacity_and_hasher(size, S::default()), |
| 62 |
None => Self::with_hasher(S::default()), |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
#[inline] |
| 67 |
fn replace(&mut self, value: T) { |
| 68 |
// Hashset already fulfils the contract |
| 69 |
self.replace(value); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
#[cfg(feature = "indexmap_1")] |
| 74 |
impl<T, S> DuplicateInsertsLastWinsSet<T> for indexmap_1::IndexSet<T, S> |
| 75 |
where |
| 76 |
T: Eq + Hash, |
| 77 |
S: BuildHasher + Default, |
| 78 |
{ |
| 79 |
#[inline] |
| 80 |
fn new(size_hint: Option<usize>) -> Self { |
| 81 |
match size_hint { |
| 82 |
Some(size) => Self::with_capacity_and_hasher(size, S::default()), |
| 83 |
None => Self::with_hasher(S::default()), |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
#[inline] |
| 88 |
fn replace(&mut self, value: T) { |
| 89 |
// Hashset already fulfils the contract |
| 90 |
self.replace(value); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
#[cfg(feature = "indexmap_2")] |
| 95 |
impl<T, S> DuplicateInsertsLastWinsSet<T> for indexmap_2::IndexSet<T, S> |
| 96 |
where |
| 97 |
T: Eq + Hash, |
| 98 |
S: BuildHasher + Default, |
| 99 |
{ |
| 100 |
#[inline] |
| 101 |
fn new(size_hint: Option<usize>) -> Self { |
| 102 |
match size_hint { |
| 103 |
Some(size) => Self::with_capacity_and_hasher(size, S::default()), |
| 104 |
None => Self::with_hasher(S::default()), |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
#[inline] |
| 109 |
fn replace(&mut self, value: T) { |
| 110 |
// Hashset already fulfils the contract |
| 111 |
self.replace(value); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
impl<T> DuplicateInsertsLastWinsSet<T> for BTreeSet<T> |
| 116 |
where |
| 117 |
T: Ord, |
| 118 |
{ |
| 119 |
#[inline] |
| 120 |
fn new(_size_hint: Option<usize>) -> Self { |
| 121 |
Self::new() |
| 122 |
} |
| 123 |
|
| 124 |
#[inline] |
| 125 |
fn replace(&mut self, value: T) { |
| 126 |
// BTreeSet already fulfils the contract |
| 127 |
self.replace(value); |
| 128 |
} |
| 129 |
} |