Skip to content

Commit 32f9cce

Browse files
authored
Merge pull request #198 from schuster/rename_split_off
Rename split_off to split_off_left (as discussed in PR #189)
2 parents dc60e60 + dc0f576 commit 32f9cce

17 files changed

+49
-48
lines changed

src/par_iter/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ modes (which is why there are two):
4141
accepts an index where the split should be performed. All
4242
iterators can work in this mode. The resulting halves thus have an
4343
idea about how much data they expect to consume.
44-
- in the `UnindexedConsumer` trait, splitting is done with `split`.
45-
There is no index: the resulting halves must be prepared to
46-
process any amount of data, and they don't know where that data
47-
falls in the overall stream.
44+
- in the `UnindexedConsumer` trait, splitting is done with
45+
`split_off_left`. There is no index: the resulting halves must be
46+
prepared to process any amount of data, and they don't know where that
47+
data falls in the overall stream.
4848
- Not all consumers can operate in this mode. It works for
4949
`for_each` and `reduce`, for example, but it does not work for
5050
`collect_into`, since in that case the position of each item is

src/par_iter/chain.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ impl<A, B> ParallelIterator for ChainIter<A, B>
2929
fn drive_unindexed<C>(self, consumer: C) -> C::Result
3030
where C: UnindexedConsumer<Self::Item>
3131
{
32-
let a = self.a.drive_unindexed(consumer.split_off());
33-
let b = self.b.drive_unindexed(consumer.split_off());
34-
consumer.to_reducer().reduce(a, b)
32+
let reducer = consumer.to_reducer();
33+
let a = self.a.drive_unindexed(consumer.split_off_left());
34+
let b = self.b.drive_unindexed(consumer);
35+
reducer.reduce(a, b)
3536
}
3637

3738
fn opt_len(&mut self) -> Option<usize> {

src/par_iter/collect/consumer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'c, ITEM: Send + 'c> Folder<ITEM> for CollectFolder<'c, ITEM> {
9090
/// Pretend to be unindexed for `special_collect_into`,
9191
/// but we should never actually get used that way...
9292
impl<'c, ITEM: Send + 'c> UnindexedConsumer<ITEM> for CollectConsumer<'c, ITEM> {
93-
fn split_off(&self) -> Self {
93+
fn split_off_left(&self) -> Self {
9494
unreachable!("CollectConsumer must be indexed!")
9595
}
9696
fn to_reducer(&self) -> Self::Reducer {

src/par_iter/filter.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ impl<'f, ITEM, C, FILTER_OP: 'f> UnindexedConsumer<ITEM> for FilterConsumer<'f,
104104
where C: UnindexedConsumer<ITEM>,
105105
FILTER_OP: Fn(&ITEM) -> bool + Sync
106106
{
107-
fn split_off(&self) -> Self {
108-
FilterConsumer::new(self.base.split_off(), &self.filter_op)
107+
fn split_off_left(&self) -> Self {
108+
FilterConsumer::new(self.base.split_off_left(), &self.filter_op)
109109
}
110110

111111
fn to_reducer(&self) -> Self::Reducer {

src/par_iter/filter_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ impl<'f, ITEM, MAPPED_ITEM, C, FILTER_OP> UnindexedConsumer<ITEM>
107107
where C: UnindexedConsumer<MAPPED_ITEM>,
108108
FILTER_OP: Fn(ITEM) -> Option<MAPPED_ITEM> + Sync + 'f
109109
{
110-
fn split_off(&self) -> Self {
111-
FilterMapConsumer::new(self.base.split_off(), &self.filter_op)
110+
fn split_off_left(&self) -> Self {
111+
FilterMapConsumer::new(self.base.split_off_left(), &self.filter_op)
112112
}
113113

114114
fn to_reducer(&self) -> Self::Reducer {

src/par_iter/find.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'f, ITEM, FIND_OP: 'f> Consumer<ITEM> for FindConsumer<'f, FIND_OP>
4040
}
4141

4242
fn split_at(self, _index: usize) -> (Self, Self, Self::Reducer) {
43-
(self.split_off(), self, FindReducer)
43+
(self.split_off_left(), self, FindReducer)
4444
}
4545

4646
fn into_folder(self) -> Self::Folder {
@@ -61,7 +61,7 @@ impl<'f, ITEM, FIND_OP: 'f> UnindexedConsumer<ITEM> for FindConsumer<'f, FIND_OP
6161
where ITEM: Send,
6262
FIND_OP: Fn(&ITEM) -> bool + Sync
6363
{
64-
fn split_off(&self) -> Self {
64+
fn split_off_left(&self) -> Self {
6565
FindConsumer::new(self.find_op, self.found)
6666
}
6767

src/par_iter/find_first_last/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'f, ITEM, FIND_OP> Consumer<ITEM> for FindConsumer<'f, FIND_OP>
9999

100100
fn split_at(self, _index: usize) -> (Self, Self, Self::Reducer) {
101101
let dir = self.match_position;
102-
(self.split_off(),
102+
(self.split_off_left(),
103103
self,
104104
FindReducer { match_position: dir })
105105
}
@@ -127,7 +127,7 @@ impl<'f, ITEM, FIND_OP> UnindexedConsumer<ITEM> for FindConsumer<'f, FIND_OP>
127127
where ITEM: Send,
128128
FIND_OP: Fn(&ITEM) -> bool + Sync
129129
{
130-
fn split_off(&self) -> Self {
130+
fn split_off_left(&self) -> Self {
131131
// Upper bound for one consumer will be lower bound for the other. This
132132
// overlap is okay, because only one of the bounds will be used for
133133
// comparing against best_found; the other is kept only to be able to
@@ -139,10 +139,6 @@ impl<'f, ITEM, FIND_OP> UnindexedConsumer<ITEM> for FindConsumer<'f, FIND_OP>
139139
// consumer to stop working when the other finds a better match, but the
140140
// reducer ensures that the best answer is still returned (see the test
141141
// above).
142-
//
143-
// This code assumes that the caller of split_off will use the result as
144-
// the *left* side of this iterator, and the remainder of self as the
145-
// *right* side.
146142
let old_lower_bound = self.lower_bound.get();
147143
let median = old_lower_bound + ((self.upper_bound - old_lower_bound) / 2);
148144
self.lower_bound.set(median);

src/par_iter/find_first_last/test.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ fn same_range_first_consumers_return_correct_answer() {
99

1010
// We save a consumer that will be far to the right of the main consumer (and therefore not
1111
// sharing an index range with that consumer) for fullness testing
12-
let consumer = far_right_consumer.split_off();
12+
let consumer = far_right_consumer.split_off_left();
1313

1414
// split until we have an indivisible range
1515
let bits_in_usize = usize::min_value().count_zeros();
16+
1617
for _ in 0..bits_in_usize {
17-
consumer.split_off();
18+
consumer.split_off_left();
1819
}
1920

2021
let reducer = consumer.to_reducer();
2122
// the left and right folders should now have the same range, having
2223
// exhausted the resolution of usize
23-
let left_folder = consumer.split_off().into_folder();
24+
let left_folder = consumer.split_off_left().into_folder();
2425
let right_folder = consumer.into_folder();
2526

2627
let left_folder = left_folder.consume(0).consume(1);
@@ -42,20 +43,20 @@ fn same_range_last_consumers_return_correct_answer() {
4243

4344
// We save a consumer that will be far to the left of the main consumer (and therefore not
4445
// sharing an index range with that consumer) for fullness testing
45-
let far_left_consumer = consumer.split_off();
46+
let far_left_consumer = consumer.split_off_left();
4647

4748
// split until we have an indivisible range
4849
let bits_in_usize = usize::min_value().count_zeros();
4950
for _ in 0..bits_in_usize {
50-
consumer.split_off();
51+
consumer.split_off_left();
5152
}
5253

5354
let reducer = consumer.to_reducer();
54-
// due to the exact calculation in split_off, the very last consumer has a
55+
// due to the exact calculation in split_off_left, the very last consumer has a
5556
// range of width 2, so we use the second-to-last consumer instead to get
5657
// the same boundary on both folders
57-
let consumer = consumer.split_off();
58-
let left_folder = consumer.split_off().into_folder();
58+
let consumer = consumer.split_off_left();
59+
let left_folder = consumer.split_off_left().into_folder();
5960
let right_folder = consumer.into_folder();
6061
let right_folder = right_folder.consume(2).consume(3);
6162
assert_eq!(left_folder.boundary, right_folder.boundary);

src/par_iter/flat_map.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ impl<'m, ITEM, MAPPED_ITEM, C, MAP_OP> Consumer<ITEM> for FlatMapConsumer<'m, C,
7373
}
7474

7575
fn split_at(self, _index: usize) -> (Self, Self, C::Reducer) {
76-
(FlatMapConsumer::new(self.base.split_off(), self.map_op),
77-
FlatMapConsumer::new(self.base.split_off(), self.map_op),
76+
(FlatMapConsumer::new(self.base.split_off_left(), self.map_op),
77+
FlatMapConsumer::new(self.base.split_off_left(), self.map_op),
7878
self.base.to_reducer())
7979
}
8080

@@ -96,8 +96,8 @@ impl<'m, ITEM, MAPPED_ITEM, C, MAP_OP> UnindexedConsumer<ITEM> for FlatMapConsum
9696
MAP_OP: Fn(ITEM) -> MAPPED_ITEM + Sync,
9797
MAPPED_ITEM: IntoParallelIterator
9898
{
99-
fn split_off(&self) -> Self {
100-
FlatMapConsumer::new(self.base.split_off(), self.map_op)
99+
fn split_off_left(&self) -> Self {
100+
FlatMapConsumer::new(self.base.split_off_left(), self.map_op)
101101
}
102102

103103
fn to_reducer(&self) -> Self::Reducer {
@@ -122,7 +122,7 @@ impl<'m, ITEM, MAPPED_ITEM, C, MAP_OP> Folder<ITEM> for FlatMapFolder<'m, C, MAP
122122
fn consume(self, item: ITEM) -> Self {
123123
let map_op = self.map_op;
124124
let par_iter = map_op(item).into_par_iter();
125-
let result = par_iter.drive_unindexed(self.base.split_off());
125+
let result = par_iter.drive_unindexed(self.base.split_off_left());
126126

127127
// We expect that `previous` is `None`, because we drive
128128
// the cost up so high, but just in case.

src/par_iter/fold.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ impl<'r, U, ITEM, C, IDENTITY, FOLD_OP> UnindexedConsumer<ITEM>
106106
IDENTITY: Fn() -> U + Sync,
107107
U: Send
108108
{
109-
fn split_off(&self) -> Self {
110-
FoldConsumer { base: self.base.split_off(), ..*self }
109+
fn split_off_left(&self) -> Self {
110+
FoldConsumer { base: self.base.split_off_left(), ..*self }
111111
}
112112

113113
fn to_reducer(&self) -> Self::Reducer {

src/par_iter/for_each.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl<'f, OP, ITEM> Consumer<ITEM> for ForEachConsumer<'f, OP>
2929
}
3030

3131
fn split_at(self, _index: usize) -> (Self, Self, NoopReducer) {
32-
(self.split_off(), self.split_off(), NoopReducer)
32+
(self.split_off_left(), self.split_off_left(), NoopReducer)
3333
}
3434

3535
fn into_folder(self) -> Self {
@@ -53,7 +53,7 @@ impl<'f, OP, ITEM> Folder<ITEM> for ForEachConsumer<'f, OP>
5353
impl<'f, OP, ITEM> UnindexedConsumer<ITEM> for ForEachConsumer<'f, OP>
5454
where OP: Fn(ITEM) + Sync
5555
{
56-
fn split_off(&self) -> Self {
56+
fn split_off_left(&self) -> Self {
5757
ForEachConsumer { op: self.op }
5858
}
5959

src/par_iter/internal.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ pub trait Reducer<Result> {
102102

103103
/// A stateless consumer can be freely copied.
104104
pub trait UnindexedConsumer<ITEM>: Consumer<ITEM> {
105-
fn split_off(&self) -> Self;
105+
// The result of split_off_left should be used for the left side of the
106+
// data it consumes, and the remaining consumer for the right side
107+
// (this matters for methods like find_first).
108+
fn split_off_left(&self) -> Self;
106109
fn to_reducer(&self) -> Self::Reducer;
107110
}
108111

@@ -270,7 +273,7 @@ fn bridge_unindexed_producer_consumer<P, C>(mut splitter: Splitter,
270273
consumer.into_folder().complete()
271274
} else if let Some(right_producer) = splitter.try_unindexed(&mut producer) {
272275
let (reducer, left_consumer, right_consumer) =
273-
(consumer.to_reducer(), consumer.split_off(), consumer);
276+
(consumer.to_reducer(), consumer.split_off_left(), consumer);
274277
let (left_result, right_result) =
275278
join(|| bridge_unindexed_producer_consumer(splitter, producer, left_consumer),
276279
|| bridge_unindexed_producer_consumer(splitter, right_producer, right_consumer));

src/par_iter/map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ impl<'m, ITEM, C, MAP_OP> UnindexedConsumer<ITEM> for MapConsumer<'m, C, MAP_OP>
256256
where C: UnindexedConsumer<MAP_OP::Output>,
257257
MAP_OP: MapOp<ITEM>
258258
{
259-
fn split_off(&self) -> Self {
260-
MapConsumer::new(self.base.split_off(), &self.map_op)
259+
fn split_off_left(&self) -> Self {
260+
MapConsumer::new(self.base.split_off_left(), &self.map_op)
261261
}
262262

263263
fn to_reducer(&self) -> Self::Reducer {

src/par_iter/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -629,9 +629,9 @@ pub trait ParallelIterator: Sized {
629629
/// statically. This can be used by consumers to trigger special fast
630630
/// paths. Therefore, if `Some(_)` is returned, this iterator must only
631631
/// use the (indexed) `Consumer` methods when driving a consumer, such
632-
/// as `split_at()`. Calling `UnindexedConsumer::split_off()` or other
633-
/// `UnindexedConsumer` methods -- or returning an inaccurate value --
634-
/// may result in panics.
632+
/// as `split_at()`. Calling `UnindexedConsumer::split_off_left()` or
633+
/// other `UnindexedConsumer` methods -- or returning an inaccurate
634+
/// value -- may result in panics.
635635
///
636636
/// This is hidden & considered internal for now, until we decide
637637
/// whether it makes sense for a public API. Right now it is only used

src/par_iter/noop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl<ITEM> Folder<ITEM> for NoopConsumer {
3737
}
3838

3939
impl<ITEM> UnindexedConsumer<ITEM> for NoopConsumer {
40-
fn split_off(&self) -> Self {
40+
fn split_off_left(&self) -> Self {
4141
NoopConsumer
4242
}
4343

src/par_iter/reduce.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'r, REDUCE_OP, ITEM> UnindexedConsumer<ITEM> for ReduceConsumer<'r, REDUCE_
8181
where REDUCE_OP: ReduceOp<ITEM>,
8282
ITEM: Send
8383
{
84-
fn split_off(&self) -> Self {
84+
fn split_off_left(&self) -> Self {
8585
ReduceConsumer { reduce_op: self.reduce_op }
8686
}
8787

src/par_iter/weight.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ impl<C, ITEM> Consumer<ITEM> for WeightConsumer<C>
169169
impl<C, ITEM> UnindexedConsumer<ITEM> for WeightConsumer<C>
170170
where C: UnindexedConsumer<ITEM>
171171
{
172-
fn split_off(&self) -> Self {
173-
WeightConsumer::new(self.base.split_off(), self.weight)
172+
fn split_off_left(&self) -> Self {
173+
WeightConsumer::new(self.base.split_off_left(), self.weight)
174174
}
175175

176176
fn to_reducer(&self) -> Self::Reducer {

0 commit comments

Comments
 (0)