Skip to content

Commit

Permalink
Merge pull request #141 from cuviper/relax
Browse files Browse the repository at this point in the history
Relax some bounds to match std
  • Loading branch information
cuviper authored Jul 24, 2020
2 parents 4f10104 + f4cacc1 commit e3e86a2
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 155 deletions.
152 changes: 65 additions & 87 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,8 @@ impl<K, V, S> Entries for IndexMap<K, V, S> {

impl<K, V, S> fmt::Debug for IndexMap<K, V, S>
where
K: fmt::Debug + Hash + Eq,
K: fmt::Debug,
V: fmt::Debug,
S: BuildHasher,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if cfg!(not(feature = "test_debug")) {
Expand Down Expand Up @@ -165,10 +164,7 @@ impl<K, V, S> IndexMap<K, V, S> {
///
/// Computes in **O(n)** time.
#[inline]
pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self
where
S: BuildHasher,
{
pub fn with_capacity_and_hasher(n: usize, hash_builder: S) -> Self {
if n == 0 {
IndexMap {
core: IndexMapCore::new(),
Expand All @@ -182,6 +178,21 @@ impl<K, V, S> IndexMap<K, V, S> {
}
}

/// Create a new map with `hash_builder`
pub fn with_hasher(hash_builder: S) -> Self {
Self::with_capacity_and_hasher(0, hash_builder)
}

/// Computes in **O(1)** time.
pub fn capacity(&self) -> usize {
self.core.capacity()
}

/// Return a reference to the map's `BuildHasher`.
pub fn hasher(&self) -> &S {
&self.hash_builder
}

/// Return the number of key-value pairs in the map.
///
/// Computes in **O(1)** time.
Expand All @@ -198,40 +209,63 @@ impl<K, V, S> IndexMap<K, V, S> {
self.len() == 0
}

/// Create a new map with `hash_builder`
pub fn with_hasher(hash_builder: S) -> Self
where
S: BuildHasher,
{
Self::with_capacity_and_hasher(0, hash_builder)
/// Return an iterator over the key-value pairs of the map, in their order
pub fn iter(&self) -> Iter<'_, K, V> {
Iter {
iter: self.as_entries().iter(),
}
}

/// Return a reference to the map's `BuildHasher`.
pub fn hasher(&self) -> &S
where
S: BuildHasher,
{
&self.hash_builder
/// Return an iterator over the key-value pairs of the map, in their order
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
IterMut {
iter: self.as_entries_mut().iter_mut(),
}
}

/// Computes in **O(1)** time.
pub fn capacity(&self) -> usize {
self.core.capacity()
/// Return an iterator over the keys of the map, in their order
pub fn keys(&self) -> Keys<'_, K, V> {
Keys {
iter: self.as_entries().iter(),
}
}

/// Return an iterator over the values of the map, in their order
pub fn values(&self) -> Values<'_, K, V> {
Values {
iter: self.as_entries().iter(),
}
}

/// Return an iterator over mutable references to the the values of the map,
/// in their order
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
ValuesMut {
iter: self.as_entries_mut().iter_mut(),
}
}
}

impl<K, V, S> IndexMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
/// Remove all key-value pairs in the map, while preserving its capacity.
///
/// Computes in **O(n)** time.
pub fn clear(&mut self) {
self.core.clear();
}

/// Clears the `IndexMap`, returning all key-value pairs as a drain iterator.
/// Keeps the allocated memory for reuse.
pub fn drain(&mut self, range: RangeFull) -> Drain<'_, K, V> {
Drain {
iter: self.core.drain(range),
}
}
}

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 @@ -296,42 +330,6 @@ where
self.core.entry(hash, key)
}

/// Return an iterator over the key-value pairs of the map, in their order
pub fn iter(&self) -> Iter<'_, K, V> {
Iter {
iter: self.as_entries().iter(),
}
}

/// Return an iterator over the key-value pairs of the map, in their order
pub fn iter_mut(&mut self) -> IterMut<'_, K, V> {
IterMut {
iter: self.as_entries_mut().iter_mut(),
}
}

/// Return an iterator over the keys of the map, in their order
pub fn keys(&self) -> Keys<'_, K, V> {
Keys {
iter: self.as_entries().iter(),
}
}

/// Return an iterator over the values of the map, in their order
pub fn values(&self) -> Values<'_, K, V> {
Values {
iter: self.as_entries().iter(),
}
}

/// Return an iterator over mutable references to the the values of the map,
/// in their order
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> {
ValuesMut {
iter: self.as_entries_mut().iter_mut(),
}
}

/// Return `true` if an equivalent to `key` exists in the map.
///
/// Computes in **O(1)** time (average).
Expand Down Expand Up @@ -660,14 +658,6 @@ where
pub fn reverse(&mut self) {
self.core.reverse()
}

/// Clears the `IndexMap`, returning all key-value pairs as a drain iterator.
/// Keeps the allocated memory for reuse.
pub fn drain(&mut self, range: RangeFull) -> Drain<'_, K, V> {
Drain {
iter: self.core.drain(range),
}
}
}

impl<K, V, S> IndexMap<K, V, S> {
Expand Down Expand Up @@ -963,35 +953,23 @@ impl<K, V> DoubleEndedIterator for Drain<'_, K, V> {
double_ended_iterator_methods!(Bucket::key_value);
}

impl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
impl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}

impl<'a, K, V, S> IntoIterator for &'a mut IndexMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
impl<'a, K, V, S> IntoIterator for &'a mut IndexMap<K, V, S> {
type Item = (&'a K, &'a mut V);
type IntoIter = IterMut<'a, K, V>;
fn into_iter(self) -> Self::IntoIter {
self.iter_mut()
}
}

impl<K, V, S> IntoIterator for IndexMap<K, V, S>
where
K: Hash + Eq,
S: BuildHasher,
{
impl<K, V, S> IntoIterator for IndexMap<K, V, S> {
type Item = (K, V);
type IntoIter = IntoIter<K, V>;
fn into_iter(self) -> Self::IntoIter {
Expand Down Expand Up @@ -1099,7 +1077,7 @@ where

impl<K, V, S> Default for IndexMap<K, V, S>
where
S: BuildHasher + Default,
S: Default,
{
/// Return an empty `IndexMap`
fn default() -> Self {
Expand Down
28 changes: 19 additions & 9 deletions src/rayon/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ use crate::IndexMap;
/// Requires crate feature `"rayon"`.
impl<K, V, S> IntoParallelIterator for IndexMap<K, V, S>
where
K: Hash + Eq + Send,
K: Send,
V: Send,
S: BuildHasher,
{
type Item = (K, V);
type Iter = IntoParIter<K, V>;
Expand Down Expand Up @@ -66,9 +65,8 @@ impl<K: Send, V: Send> IndexedParallelIterator for IntoParIter<K, V> {
/// Requires crate feature `"rayon"`.
impl<'a, K, V, S> IntoParallelIterator for &'a IndexMap<K, V, S>
where
K: Hash + Eq + Sync,
K: Sync,
V: Sync,
S: BuildHasher,
{
type Item = (&'a K, &'a V);
type Iter = ParIter<'a, K, V>;
Expand Down Expand Up @@ -117,7 +115,7 @@ impl<K: Sync, V: Sync> IndexedParallelIterator for ParIter<'_, K, V> {
/// Requires crate feature `"rayon"`.
impl<'a, K, V, S> IntoParallelIterator for &'a mut IndexMap<K, V, S>
where
K: Hash + Eq + Sync + Send,
K: Sync + Send,
V: Send,
S: BuildHasher,
{
Expand Down Expand Up @@ -159,9 +157,8 @@ impl<K: Sync + Send, V: Send> IndexedParallelIterator for ParIterMut<'_, K, V> {
/// See also the `IntoParallelIterator` implementations.
impl<K, V, S> IndexMap<K, V, S>
where
K: Hash + Eq + Sync,
K: Sync,
V: Sync,
S: BuildHasher,
{
/// Return a parallel iterator over the keys of the map.
///
Expand All @@ -182,7 +179,14 @@ where
entries: self.as_entries(),
}
}
}

impl<K, V, S> IndexMap<K, V, S>
where
K: Hash + Eq + Sync,
V: Sync,
S: BuildHasher,
{
/// Returns `true` if `self` contains all of the same key-value pairs as `other`,
/// regardless of each map's indexed order, determined in parallel.
pub fn par_eq<V2, S2>(&self, other: &IndexMap<K, V2, S2>) -> bool
Expand Down Expand Up @@ -269,9 +273,8 @@ impl<K: Sync, V: Sync> IndexedParallelIterator for ParValues<'_, K, V> {
/// Requires crate feature `"rayon"`.
impl<K, V, S> IndexMap<K, V, S>
where
K: Hash + Eq + Send,
K: Send,
V: Send,
S: BuildHasher,
{
/// Return a parallel iterator over mutable references to the the values of the map
///
Expand All @@ -282,7 +285,14 @@ where
entries: self.as_entries_mut(),
}
}
}

impl<K, V, S> IndexMap<K, V, S>
where
K: Hash + Eq + 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)
where
Expand Down
6 changes: 2 additions & 4 deletions src/rayon/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ type Bucket<T> = crate::Bucket<T, ()>;
/// Requires crate feature `"rayon"`.
impl<T, S> IntoParallelIterator for IndexSet<T, S>
where
T: Hash + Eq + Send,
S: BuildHasher,
T: Send,
{
type Item = T;
type Iter = IntoParIter<T>;
Expand Down Expand Up @@ -66,8 +65,7 @@ impl<T: Send> IndexedParallelIterator for IntoParIter<T> {
/// Requires crate feature `"rayon"`.
impl<'a, T, S> IntoParallelIterator for &'a IndexSet<T, S>
where
T: Hash + Eq + Sync,
S: BuildHasher,
T: Sync,
{
type Item = &'a T;
type Iter = ParIter<'a, T>;
Expand Down
Loading

0 comments on commit e3e86a2

Please sign in to comment.