Skip to content

Commit 80cd096

Browse files
phimuemuejswrenn
authored andcommitted
Uniform field names for Interleave[Shortest]
1 parent f4afe79 commit 80cd096

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

src/adaptors/mod.rs

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use std::marker::PhantomData;
2828
#[derive(Clone, Debug)]
2929
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
3030
pub struct Interleave<I, J> {
31-
a: Fuse<I>,
32-
b: Fuse<J>,
33-
flag: bool,
31+
i: Fuse<I>,
32+
j: Fuse<J>,
33+
next_coming_from_j: bool,
3434
}
3535

3636
/// Create an iterator that interleaves elements in `i` and `j`.
@@ -45,9 +45,9 @@ where
4545
J: IntoIterator<Item = I::Item>,
4646
{
4747
Interleave {
48-
a: i.into_iter().fuse(),
49-
b: j.into_iter().fuse(),
50-
flag: false,
48+
i: i.into_iter().fuse(),
49+
j: j.into_iter().fuse(),
50+
next_coming_from_j: false,
5151
}
5252
}
5353

@@ -59,45 +59,49 @@ where
5959
type Item = I::Item;
6060
#[inline]
6161
fn next(&mut self) -> Option<Self::Item> {
62-
self.flag = !self.flag;
63-
if self.flag {
64-
match self.a.next() {
65-
None => self.b.next(),
62+
self.next_coming_from_j = !self.next_coming_from_j;
63+
if self.next_coming_from_j {
64+
match self.i.next() {
65+
None => self.j.next(),
6666
r => r,
6767
}
6868
} else {
69-
match self.b.next() {
70-
None => self.a.next(),
69+
match self.j.next() {
70+
None => self.i.next(),
7171
r => r,
7272
}
7373
}
7474
}
7575

7676
fn size_hint(&self) -> (usize, Option<usize>) {
77-
size_hint::add(self.a.size_hint(), self.b.size_hint())
77+
size_hint::add(self.i.size_hint(), self.j.size_hint())
7878
}
7979

8080
fn fold<B, F>(self, mut init: B, mut f: F) -> B
8181
where
8282
F: FnMut(B, Self::Item) -> B,
8383
{
84-
let Self { mut a, mut b, flag } = self;
85-
if flag {
86-
match b.next() {
84+
let Self {
85+
mut i,
86+
mut j,
87+
next_coming_from_j,
88+
} = self;
89+
if next_coming_from_j {
90+
match j.next() {
8791
Some(y) => init = f(init, y),
88-
None => return a.fold(init, f),
92+
None => return i.fold(init, f),
8993
}
9094
}
91-
let res = a.try_fold(init, |mut acc, x| {
95+
let res = i.try_fold(init, |mut acc, x| {
9296
acc = f(acc, x);
93-
match b.next() {
97+
match j.next() {
9498
Some(y) => Ok(f(acc, y)),
9599
None => Err(acc),
96100
}
97101
});
98102
match res {
99-
Ok(acc) => b.fold(acc, f),
100-
Err(acc) => a.fold(acc, f),
103+
Ok(acc) => j.fold(acc, f),
104+
Err(acc) => i.fold(acc, f),
101105
}
102106
}
103107
}
@@ -123,21 +127,21 @@ where
123127
I: Iterator,
124128
J: Iterator<Item = I::Item>,
125129
{
126-
it0: I,
127-
it1: J,
128-
phase: bool, // false ==> it0, true ==> it1
130+
i: I,
131+
j: J,
132+
next_coming_from_j: bool,
129133
}
130134

131135
/// Create a new `InterleaveShortest` iterator.
132-
pub fn interleave_shortest<I, J>(a: I, b: J) -> InterleaveShortest<I, J>
136+
pub fn interleave_shortest<I, J>(i: I, j: J) -> InterleaveShortest<I, J>
133137
where
134138
I: Iterator,
135139
J: Iterator<Item = I::Item>,
136140
{
137141
InterleaveShortest {
138-
it0: a,
139-
it1: b,
140-
phase: false,
142+
i,
143+
j,
144+
next_coming_from_j: false,
141145
}
142146
}
143147

@@ -150,26 +154,26 @@ where
150154

151155
#[inline]
152156
fn next(&mut self) -> Option<Self::Item> {
153-
let e = if self.phase {
154-
self.it1.next()
157+
let e = if self.next_coming_from_j {
158+
self.j.next()
155159
} else {
156-
self.it0.next()
160+
self.i.next()
157161
};
158162
if e.is_some() {
159-
self.phase = !self.phase;
163+
self.next_coming_from_j = !self.next_coming_from_j;
160164
}
161165
e
162166
}
163167

164168
#[inline]
165169
fn size_hint(&self) -> (usize, Option<usize>) {
166170
let (curr_hint, next_hint) = {
167-
let it0_hint = self.it0.size_hint();
168-
let it1_hint = self.it1.size_hint();
169-
if self.phase {
170-
(it1_hint, it0_hint)
171+
let i_hint = self.i.size_hint();
172+
let j_hint = self.j.size_hint();
173+
if self.next_coming_from_j {
174+
(j_hint, i_hint)
171175
} else {
172-
(it0_hint, it1_hint)
176+
(i_hint, j_hint)
173177
}
174178
};
175179
let (curr_lower, curr_upper) = curr_hint;
@@ -201,19 +205,19 @@ where
201205
F: FnMut(B, Self::Item) -> B,
202206
{
203207
let Self {
204-
mut it0,
205-
mut it1,
206-
phase,
208+
mut i,
209+
mut j,
210+
next_coming_from_j,
207211
} = self;
208-
if phase {
209-
match it1.next() {
212+
if next_coming_from_j {
213+
match j.next() {
210214
Some(y) => init = f(init, y),
211215
None => return init,
212216
}
213217
}
214-
let res = it0.try_fold(init, |mut acc, x| {
218+
let res = i.try_fold(init, |mut acc, x| {
215219
acc = f(acc, x);
216-
match it1.next() {
220+
match j.next() {
217221
Some(y) => Ok(f(acc, y)),
218222
None => Err(acc),
219223
}

0 commit comments

Comments
 (0)