1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::setref::multiple::RefMulti;
use crate::DashSet;
use core::hash::{BuildHasher, Hash};
use rayon::iter::plumbing::UnindexedConsumer;
use rayon::iter::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator};
use std::collections::hash_map::RandomState;

impl<K, S> ParallelExtend<K> for DashSet<K, S>
where
    K: Send + Sync + Eq + Hash,
    S: Send + Sync + Clone + BuildHasher,
{
    fn par_extend<I>(&mut self, par_iter: I)
    where
        I: IntoParallelIterator<Item = K>,
    {
        (&*self).par_extend(par_iter);
    }
}

// Since we don't actually need mutability, we can implement this on a
// reference, similar to `io::Write for &File`.
impl<K, S> ParallelExtend<K> for &'_ DashSet<K, S>
where
    K: Send + Sync + Eq + Hash,
    S: Send + Sync + Clone + BuildHasher,
{
    fn par_extend<I>(&mut self, par_iter: I)
    where
        I: IntoParallelIterator<Item = K>,
    {
        let &mut set = self;
        par_iter.into_par_iter().for_each(move |key| {
            set.insert(key);
        });
    }
}

impl<K, S> FromParallelIterator<K> for DashSet<K, S>
where
    K: Send + Sync + Eq + Hash,
    S: Send + Sync + Clone + Default + BuildHasher,
{
    fn from_par_iter<I>(par_iter: I) -> Self
    where
        I: IntoParallelIterator<Item = K>,
    {
        let set = Self::default();
        (&set).par_extend(par_iter);
        set
    }
}

impl<K, S> IntoParallelIterator for DashSet<K, S>
where
    K: Send + Eq + Hash,
    S: Send + Clone + BuildHasher,
{
    type Iter = OwningIter<K, S>;
    type Item = K;

    fn into_par_iter(self) -> Self::Iter {
        OwningIter {
            inner: self.inner.into_par_iter(),
        }
    }
}

pub struct OwningIter<K, S = RandomState> {
    inner: super::map::OwningIter<K, (), S>,
}

impl<K, S> ParallelIterator for OwningIter<K, S>
where
    K: Send + Eq + Hash,
    S: Send + Clone + BuildHasher,
{
    type Item = K;

    fn drive_unindexed<C>(self, consumer: C) -> C::Result
    where
        C: UnindexedConsumer<Self::Item>,
    {
        self.inner.map(|(k, _)| k).drive_unindexed(consumer)
    }
}

// This impl also enables `IntoParallelRefIterator::par_iter`
impl<'a, K, S> IntoParallelIterator for &'a DashSet<K, S>
where
    K: Send + Sync + Eq + Hash,
    S: Send + Sync + Clone + BuildHasher,
{
    type Iter = Iter<'a, K, S>;
    type Item = RefMulti<'a, K, S>;

    fn into_par_iter(self) -> Self::Iter {
        Iter {
            inner: (&self.inner).into_par_iter(),
        }
    }
}

pub struct Iter<'a, K, S = RandomState> {
    inner: super::map::Iter<'a, K, (), S>,
}

impl<'a, K, S> ParallelIterator for Iter<'a, K, S>
where
    K: Send + Sync + Eq + Hash,
    S: Send + Sync + Clone + BuildHasher,
{
    type Item = RefMulti<'a, K, S>;

    fn drive_unindexed<C>(self, consumer: C) -> C::Result
    where
        C: UnindexedConsumer<Self::Item>,
    {
        self.inner.map(RefMulti::new).drive_unindexed(consumer)
    }
}