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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use super::{IndexedParallelIterator, IntoParallelIterator, ParallelExtend, ParallelIterator};
use std::collections::LinkedList;
use std::slice;
use std::sync::atomic::{AtomicUsize, Ordering};
mod consumer;
use self::consumer::CollectConsumer;
use super::unzip::unzip_indexed;
mod test;
pub(super) fn collect_into_vec<I, T>(pi: I, v: &mut Vec<T>)
where
I: IndexedParallelIterator<Item = T>,
T: Send,
{
v.truncate(0);
let mut collect = Collect::new(v, pi.len());
pi.drive(collect.as_consumer());
collect.complete();
}
fn special_extend<I, T>(pi: I, len: usize, v: &mut Vec<T>)
where
I: ParallelIterator<Item = T>,
T: Send,
{
let mut collect = Collect::new(v, len);
pi.drive_unindexed(collect.as_consumer());
collect.complete();
}
pub(super) fn unzip_into_vecs<I, A, B>(pi: I, left: &mut Vec<A>, right: &mut Vec<B>)
where
I: IndexedParallelIterator<Item = (A, B)>,
A: Send,
B: Send,
{
left.truncate(0);
right.truncate(0);
let len = pi.len();
let mut left = Collect::new(left, len);
let mut right = Collect::new(right, len);
unzip_indexed(pi, left.as_consumer(), right.as_consumer());
left.complete();
right.complete();
}
struct Collect<'c, T: Send + 'c> {
writes: AtomicUsize,
vec: &'c mut Vec<T>,
len: usize,
}
impl<'c, T: Send + 'c> Collect<'c, T> {
fn new(vec: &'c mut Vec<T>, len: usize) -> Self {
Collect {
writes: AtomicUsize::new(0),
vec,
len,
}
}
fn as_consumer(&mut self) -> CollectConsumer<'_, T> {
self.vec.reserve(self.len);
let start = self.vec.len();
let mut slice = &mut self.vec[start..];
slice = unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr(), self.len) };
CollectConsumer::new(&self.writes, slice)
}
fn complete(self) {
unsafe {
let actual_writes = self.writes.load(Ordering::Relaxed);
assert!(
actual_writes == self.len,
"expected {} total writes, but got {}",
self.len,
actual_writes
);
let new_len = self.vec.len() + self.len;
self.vec.set_len(new_len);
}
}
}
impl<T> ParallelExtend<T> for Vec<T>
where
T: Send,
{
fn par_extend<I>(&mut self, par_iter: I)
where
I: IntoParallelIterator<Item = T>,
{
let par_iter = par_iter.into_par_iter();
match par_iter.opt_len() {
Some(len) => {
special_extend(par_iter, len, self);
}
None => {
let list: LinkedList<_> = par_iter
.fold(Vec::new, |mut vec, elem| {
vec.push(elem);
vec
})
.map(|vec| {
let mut list = LinkedList::new();
list.push_back(vec);
list
})
.reduce(LinkedList::new, |mut list1, mut list2| {
list1.append(&mut list2);
list1
});
self.reserve(list.iter().map(Vec::len).sum());
for mut vec in list {
self.append(&mut vec);
}
}
}
}
}