Skip to content

Commit

Permalink
Merge pull request #296 from cuviper/relax
Browse files Browse the repository at this point in the history
Relax some over-constrained impl bounds
  • Loading branch information
cuviper authored Jan 16, 2024
2 parents 5c7e734 + 908084e commit f7cf609
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 62 deletions.
35 changes: 19 additions & 16 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,7 @@ impl<K, V, S> IndexMap<K, V, S> {
hash_builder: self.hash_builder.clone(),
}
}
}

impl<K, V, S> IndexMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
/// Reserve capacity for `additional` more key-value pairs.
///
/// Computes in **O(n)** time.
Expand Down Expand Up @@ -374,13 +368,13 @@ where
pub fn shrink_to(&mut self, min_capacity: usize) {
self.core.shrink_to(min_capacity);
}
}

fn hash<Q: ?Sized + Hash>(&self, key: &Q) -> HashValue {
let mut h = self.hash_builder.build_hasher();
key.hash(&mut h);
HashValue(h.finish() as usize)
}

impl<K, V, S> IndexMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
/// Insert a key-value pair in the map.
///
/// If an equivalent key already exists in the map: the key remains and
Expand Down Expand Up @@ -424,6 +418,17 @@ where
let hash = self.hash(&key);
self.core.entry(hash, key)
}
}

impl<K, V, S> IndexMap<K, V, S>
where
S: BuildHasher,
{
fn hash<Q: ?Sized + Hash>(&self, key: &Q) -> HashValue {
let mut h = self.hash_builder.build_hasher();
key.hash(&mut h);
HashValue(h.finish() as usize)
}

/// Return `true` if an equivalent to `key` exists in the map.
///
Expand Down Expand Up @@ -663,7 +668,9 @@ where
let hash = self.hash(key);
self.core.shift_remove_full(hash, key)
}
}

impl<K, V, S> IndexMap<K, V, S> {
/// Remove the last key-value pair
///
/// This preserves the order of the remaining elements.
Expand Down Expand Up @@ -861,9 +868,7 @@ where
pub fn reverse(&mut self) {
self.core.reverse()
}
}

impl<K, V, S> IndexMap<K, V, S> {
/// Returns a slice of all the key-value pairs in the map.
///
/// Computes in **O(1)** time.
Expand Down Expand Up @@ -1037,7 +1042,6 @@ impl<K, V, S> IndexMap<K, V, S> {
impl<K, V, Q: ?Sized, S> Index<&Q> for IndexMap<K, V, S>
where
Q: Hash + Equivalent<K>,
K: Hash + Eq,
S: BuildHasher,
{
type Output = V;
Expand Down Expand Up @@ -1082,7 +1086,6 @@ where
impl<K, V, Q: ?Sized, S> IndexMut<&Q> for IndexMap<K, V, S>
where
Q: Hash + Equivalent<K>,
K: Hash + Eq,
S: BuildHasher,
{
/// Returns a mutable reference to the value corresponding to the supplied `key`.
Expand Down
3 changes: 1 addition & 2 deletions src/map/serde_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ where
/// ```
pub fn serialize<K, V, S, T>(map: &IndexMap<K, V, S>, serializer: T) -> Result<T::Ok, T::Error>
where
K: Serialize + Hash + Eq,
K: Serialize,
V: Serialize,
S: BuildHasher,
T: Serializer,
{
serializer.collect_seq(map)
Expand Down
1 change: 0 additions & 1 deletion src/mutable_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ pub trait MutableKeys: private::Sealed {
/// See [`MutableKeys`] for more information.
impl<K, V, S> MutableKeys for IndexMap<K, V, S>
where
K: Eq + Hash,
S: BuildHasher,
{
type Key = K;
Expand Down
3 changes: 1 addition & 2 deletions src/rayon/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,8 @@ where

impl<K, V, S> IndexMap<K, V, S>
where
K: Hash + Eq + Send,
K: Send,
V: Send,
S: BuildHasher,
{
/// Sort the map’s key-value pairs in parallel, by the default ordering of the keys.
pub fn par_sort_keys(&mut self)
Expand Down
3 changes: 1 addition & 2 deletions src/rayon/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ where
/// The following methods **require crate feature `"rayon"`**.
impl<T, S> IndexSet<T, S>
where
T: Hash + Eq + Send,
S: BuildHasher + Send,
T: Send,
{
/// Sort the set’s values in parallel by their default ordering.
pub fn par_sort(&mut self)
Expand Down
6 changes: 2 additions & 4 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ use crate::IndexMap;

impl<K, V, S> Serialize for IndexMap<K, V, S>
where
K: Serialize + Hash + Eq,
K: Serialize,
V: Serialize,
S: BuildHasher,
{
fn serialize<T>(&self, serializer: T) -> Result<T::Ok, T::Error>
where
Expand Down Expand Up @@ -87,8 +86,7 @@ use crate::IndexSet;

impl<T, S> Serialize for IndexSet<T, S>
where
T: Serialize + Hash + Eq,
S: BuildHasher,
T: Serialize,
{
fn serialize<Se>(&self, serializer: Se) -> Result<Se::Ok, Se::Error>
where
Expand Down
75 changes: 40 additions & 35 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,7 @@ impl<T, S> IndexSet<T, S> {
map: self.map.split_off(at),
}
}
}

impl<T, S> IndexSet<T, S>
where
T: Hash + Eq,
S: BuildHasher,
{
/// Reserve capacity for `additional` more values.
///
/// Computes in **O(n)** time.
Expand Down Expand Up @@ -324,7 +318,13 @@ where
pub fn shrink_to(&mut self, min_capacity: usize) {
self.map.shrink_to(min_capacity);
}
}

impl<T, S> IndexSet<T, S>
where
T: Hash + Eq,
S: BuildHasher,
{
/// Insert the value into the set.
///
/// If an equivalent item already exists in the set, it returns
Expand All @@ -351,6 +351,33 @@ where
(index, existing.is_none())
}

/// Adds a value to the set, replacing the existing value, if any, that is
/// equal to the given one, without altering its insertion order. Returns
/// the replaced value.
///
/// Computes in **O(1)** time (average).
pub fn replace(&mut self, value: T) -> Option<T> {
self.replace_full(value).1
}

/// Adds a value to the set, replacing the existing value, if any, that is
/// equal to the given one, without altering its insertion order. Returns
/// the index of the item and its replaced value.
///
/// Computes in **O(1)** time (average).
pub fn replace_full(&mut self, value: T) -> (usize, Option<T>) {
use super::map::Entry::*;

match self.map.entry(value) {
Vacant(e) => {
let index = e.index();
e.insert(());
(index, None)
}
Occupied(e) => (e.index(), Some(e.replace_key())),
}
}

/// Return an iterator over the values that are in `self` but not `other`.
///
/// Values are produced in the same order that they appear in `self`.
Expand Down Expand Up @@ -396,7 +423,12 @@ where
{
Union::new(self, other)
}
}

impl<T, S> IndexSet<T, S>
where
S: BuildHasher,
{
/// Return `true` if an equivalent to `value` exists in the set.
///
/// Computes in **O(1)** time (average).
Expand Down Expand Up @@ -436,33 +468,6 @@ where
self.map.get_index_of(value)
}

/// Adds a value to the set, replacing the existing value, if any, that is
/// equal to the given one, without altering its insertion order. Returns
/// the replaced value.
///
/// Computes in **O(1)** time (average).
pub fn replace(&mut self, value: T) -> Option<T> {
self.replace_full(value).1
}

/// Adds a value to the set, replacing the existing value, if any, that is
/// equal to the given one, without altering its insertion order. Returns
/// the index of the item and its replaced value.
///
/// Computes in **O(1)** time (average).
pub fn replace_full(&mut self, value: T) -> (usize, Option<T>) {
use super::map::Entry::*;

match self.map.entry(value) {
Vacant(e) => {
let index = e.index();
e.insert(());
(index, None)
}
Occupied(e) => (e.index(), Some(e.replace_key())),
}
}

/// Remove the value from the set, and return `true` if it was present.
///
/// **NOTE:** This is equivalent to [`.swap_remove(value)`][Self::swap_remove], replacing this
Expand Down Expand Up @@ -587,7 +592,9 @@ where
{
self.map.shift_remove_full(value).map(|(i, x, ())| (i, x))
}
}

impl<T, S> IndexSet<T, S> {
/// Remove the last value
///
/// This preserves the order of the remaining elements.
Expand Down Expand Up @@ -756,9 +763,7 @@ where
pub fn reverse(&mut self) {
self.map.reverse()
}
}

impl<T, S> IndexSet<T, S> {
/// Returns a slice of all the values in the set.
///
/// Computes in **O(1)** time.
Expand Down

0 comments on commit f7cf609

Please sign in to comment.