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