]>
Commit | Line | Data |
---|---|---|
e74abb32 | 1 | use super::noop::NoopConsumer; |
532ac7d7 | 2 | use super::{FromParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator}; |
2c00a5a8 XL |
3 | |
4 | use std::borrow::Cow; | |
2c00a5a8 | 5 | use std::collections::LinkedList; |
532ac7d7 | 6 | use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; |
2c00a5a8 | 7 | use std::collections::{BinaryHeap, VecDeque}; |
532ac7d7 | 8 | use std::hash::{BuildHasher, Hash}; |
2c00a5a8 | 9 | |
923072b8 | 10 | /// Creates an empty default collection and extends it. |
2c00a5a8 | 11 | fn collect_extended<C, I>(par_iter: I) -> C |
532ac7d7 XL |
12 | where |
13 | I: IntoParallelIterator, | |
14 | C: ParallelExtend<I::Item> + Default, | |
2c00a5a8 XL |
15 | { |
16 | let mut collection = C::default(); | |
17 | collection.par_extend(par_iter); | |
18 | collection | |
19 | } | |
20 | ||
923072b8 | 21 | /// Collects items from a parallel iterator into a vector. |
2c00a5a8 | 22 | impl<T> FromParallelIterator<T> for Vec<T> |
532ac7d7 XL |
23 | where |
24 | T: Send, | |
2c00a5a8 XL |
25 | { |
26 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
27 | where |
28 | I: IntoParallelIterator<Item = T>, | |
2c00a5a8 XL |
29 | { |
30 | collect_extended(par_iter) | |
31 | } | |
32 | } | |
33 | ||
923072b8 | 34 | /// Collects items from a parallel iterator into a vecdeque. |
2c00a5a8 | 35 | impl<T> FromParallelIterator<T> for VecDeque<T> |
532ac7d7 XL |
36 | where |
37 | T: Send, | |
2c00a5a8 XL |
38 | { |
39 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
40 | where |
41 | I: IntoParallelIterator<Item = T>, | |
2c00a5a8 XL |
42 | { |
43 | Vec::from_par_iter(par_iter).into() | |
44 | } | |
45 | } | |
46 | ||
923072b8 | 47 | /// Collects items from a parallel iterator into a binaryheap. |
2c00a5a8 XL |
48 | /// The heap-ordering is calculated serially after all items are collected. |
49 | impl<T> FromParallelIterator<T> for BinaryHeap<T> | |
532ac7d7 XL |
50 | where |
51 | T: Ord + Send, | |
2c00a5a8 XL |
52 | { |
53 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
54 | where |
55 | I: IntoParallelIterator<Item = T>, | |
2c00a5a8 XL |
56 | { |
57 | Vec::from_par_iter(par_iter).into() | |
58 | } | |
59 | } | |
60 | ||
923072b8 | 61 | /// Collects items from a parallel iterator into a freshly allocated |
2c00a5a8 XL |
62 | /// linked list. |
63 | impl<T> FromParallelIterator<T> for LinkedList<T> | |
532ac7d7 XL |
64 | where |
65 | T: Send, | |
2c00a5a8 XL |
66 | { |
67 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
68 | where |
69 | I: IntoParallelIterator<Item = T>, | |
2c00a5a8 XL |
70 | { |
71 | collect_extended(par_iter) | |
72 | } | |
73 | } | |
74 | ||
923072b8 | 75 | /// Collects (key, value) pairs from a parallel iterator into a |
2c00a5a8 XL |
76 | /// hashmap. If multiple pairs correspond to the same key, then the |
77 | /// ones produced earlier in the parallel iterator will be | |
78 | /// overwritten, just as with a sequential iterator. | |
79 | impl<K, V, S> FromParallelIterator<(K, V)> for HashMap<K, V, S> | |
532ac7d7 XL |
80 | where |
81 | K: Eq + Hash + Send, | |
82 | V: Send, | |
83 | S: BuildHasher + Default + Send, | |
2c00a5a8 XL |
84 | { |
85 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
86 | where |
87 | I: IntoParallelIterator<Item = (K, V)>, | |
2c00a5a8 XL |
88 | { |
89 | collect_extended(par_iter) | |
90 | } | |
91 | } | |
92 | ||
923072b8 | 93 | /// Collects (key, value) pairs from a parallel iterator into a |
2c00a5a8 XL |
94 | /// btreemap. If multiple pairs correspond to the same key, then the |
95 | /// ones produced earlier in the parallel iterator will be | |
96 | /// overwritten, just as with a sequential iterator. | |
97 | impl<K, V> FromParallelIterator<(K, V)> for BTreeMap<K, V> | |
532ac7d7 XL |
98 | where |
99 | K: Ord + Send, | |
100 | V: Send, | |
2c00a5a8 XL |
101 | { |
102 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
103 | where |
104 | I: IntoParallelIterator<Item = (K, V)>, | |
2c00a5a8 XL |
105 | { |
106 | collect_extended(par_iter) | |
107 | } | |
108 | } | |
109 | ||
923072b8 | 110 | /// Collects values from a parallel iterator into a hashset. |
2c00a5a8 | 111 | impl<V, S> FromParallelIterator<V> for HashSet<V, S> |
532ac7d7 XL |
112 | where |
113 | V: Eq + Hash + Send, | |
114 | S: BuildHasher + Default + Send, | |
2c00a5a8 XL |
115 | { |
116 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
117 | where |
118 | I: IntoParallelIterator<Item = V>, | |
2c00a5a8 XL |
119 | { |
120 | collect_extended(par_iter) | |
121 | } | |
122 | } | |
123 | ||
923072b8 | 124 | /// Collects values from a parallel iterator into a btreeset. |
2c00a5a8 | 125 | impl<V> FromParallelIterator<V> for BTreeSet<V> |
532ac7d7 XL |
126 | where |
127 | V: Send + Ord, | |
2c00a5a8 XL |
128 | { |
129 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
130 | where |
131 | I: IntoParallelIterator<Item = V>, | |
2c00a5a8 XL |
132 | { |
133 | collect_extended(par_iter) | |
134 | } | |
135 | } | |
136 | ||
923072b8 | 137 | /// Collects characters from a parallel iterator into a string. |
2c00a5a8 XL |
138 | impl FromParallelIterator<char> for String { |
139 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
140 | where |
141 | I: IntoParallelIterator<Item = char>, | |
2c00a5a8 XL |
142 | { |
143 | collect_extended(par_iter) | |
144 | } | |
145 | } | |
146 | ||
923072b8 | 147 | /// Collects characters from a parallel iterator into a string. |
2c00a5a8 XL |
148 | impl<'a> FromParallelIterator<&'a char> for String { |
149 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
150 | where |
151 | I: IntoParallelIterator<Item = &'a char>, | |
2c00a5a8 XL |
152 | { |
153 | collect_extended(par_iter) | |
154 | } | |
155 | } | |
156 | ||
923072b8 | 157 | /// Collects string slices from a parallel iterator into a string. |
2c00a5a8 XL |
158 | impl<'a> FromParallelIterator<&'a str> for String { |
159 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
160 | where |
161 | I: IntoParallelIterator<Item = &'a str>, | |
2c00a5a8 XL |
162 | { |
163 | collect_extended(par_iter) | |
164 | } | |
165 | } | |
166 | ||
923072b8 | 167 | /// Collects strings from a parallel iterator into one large string. |
2c00a5a8 XL |
168 | impl FromParallelIterator<String> for String { |
169 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
170 | where |
171 | I: IntoParallelIterator<Item = String>, | |
2c00a5a8 XL |
172 | { |
173 | collect_extended(par_iter) | |
174 | } | |
175 | } | |
176 | ||
923072b8 | 177 | /// Collects string slices from a parallel iterator into a string. |
2c00a5a8 XL |
178 | impl<'a> FromParallelIterator<Cow<'a, str>> for String { |
179 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
180 | where |
181 | I: IntoParallelIterator<Item = Cow<'a, str>>, | |
2c00a5a8 XL |
182 | { |
183 | collect_extended(par_iter) | |
184 | } | |
185 | } | |
186 | ||
923072b8 | 187 | /// Collects an arbitrary `Cow` collection. |
2c00a5a8 XL |
188 | /// |
189 | /// Note, the standard library only has `FromIterator` for `Cow<'a, str>` and | |
190 | /// `Cow<'a, [T]>`, because no one thought to add a blanket implementation | |
191 | /// before it was stabilized. | |
192 | impl<'a, C: ?Sized, T> FromParallelIterator<T> for Cow<'a, C> | |
532ac7d7 XL |
193 | where |
194 | C: ToOwned, | |
195 | C::Owned: FromParallelIterator<T>, | |
196 | T: Send, | |
2c00a5a8 XL |
197 | { |
198 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
199 | where |
200 | I: IntoParallelIterator<Item = T>, | |
2c00a5a8 XL |
201 | { |
202 | Cow::Owned(C::Owned::from_par_iter(par_iter)) | |
203 | } | |
204 | } | |
205 | ||
206 | /// Collapses all unit items from a parallel iterator into one. | |
207 | /// | |
208 | /// This is more useful when combined with higher-level abstractions, like | |
209 | /// collecting to a `Result<(), E>` where you only care about errors: | |
210 | /// | |
211 | /// ``` | |
212 | /// use std::io::*; | |
213 | /// use rayon::prelude::*; | |
214 | /// | |
215 | /// let data = vec![1, 2, 3, 4, 5]; | |
216 | /// let res: Result<()> = data.par_iter() | |
217 | /// .map(|x| writeln!(stdout(), "{}", x)) | |
218 | /// .collect(); | |
219 | /// assert!(res.is_ok()); | |
220 | /// ``` | |
221 | impl FromParallelIterator<()> for () { | |
222 | fn from_par_iter<I>(par_iter: I) -> Self | |
532ac7d7 XL |
223 | where |
224 | I: IntoParallelIterator<Item = ()>, | |
2c00a5a8 | 225 | { |
e74abb32 | 226 | par_iter.into_par_iter().drive_unindexed(NoopConsumer) |
2c00a5a8 XL |
227 | } |
228 | } |