]>
git.proxmox.com Git - rustc.git/blob - vendor/itertools-0.8.2/src/diff.rs
1 //! "Diff"ing iterators for caching elements to sequential collections without requiring the new
2 //! elements' iterator to be `Clone`.
4 //! - [**Diff**](./enum.Diff.html) (produced by the [**diff_with**](./fn.diff_with.html) function)
5 //! describes the difference between two non-`Clone` iterators `I` and `J` after breaking ASAP from
6 //! a lock-step comparison.
11 /// A type returned by the [`diff_with`](./fn.diff_with.html) function.
13 /// `Diff` represents the way in which the elements yielded by the iterator `I` differ to some
19 /// The index of the first non-matching element along with both iterator's remaining elements
20 /// starting with the first mis-match.
21 FirstMismatch(usize, PutBack
<I
>, PutBack
<J
>),
22 /// The total number of elements that were in `J` along with the remaining elements of `I`.
23 Shorter(usize, PutBack
<I
>),
24 /// The total number of elements that were in `I` along with the remaining elements of `J`.
25 Longer(usize, PutBack
<J
>),
28 /// Compares every element yielded by both `i` and `j` with the given function in lock-step and
29 /// returns a `Diff` which describes how `j` differs from `i`.
31 /// If the number of elements yielded by `j` is less than the number of elements yielded by `i`,
32 /// the number of `j` elements yielded will be returned along with `i`'s remaining elements as
35 /// If the two elements of a step differ, the index of those elements along with the remaining
36 /// elements of both `i` and `j` are returned as `Diff::FirstMismatch`.
38 /// If `i` becomes exhausted before `j` becomes exhausted, the number of elements in `i` along with
39 /// the remaining `j` elements will be returned as `Diff::Longer`.
40 pub fn diff_with
<I
, J
, F
>(i
: I
, j
: J
, is_equal
: F
)
41 -> Option
<Diff
<I
::IntoIter
, J
::IntoIter
>>
42 where I
: IntoIterator
,
44 F
: Fn(&I
::Item
, &J
::Item
) -> bool
46 let mut i
= i
.into_iter();
47 let mut j
= j
.into_iter();
49 while let Some(i_elem
) = i
.next() {
51 None
=> return Some(Diff
::Shorter(idx
, put_back(i
).with_value(i_elem
))),
52 Some(j_elem
) => if !is_equal(&i_elem
, &j_elem
) {
53 let remaining_i
= put_back(i
).with_value(i_elem
);
54 let remaining_j
= put_back(j
).with_value(j_elem
);
55 return Some(Diff
::FirstMismatch(idx
, remaining_i
, remaining_j
));
60 j
.next().map(|j_elem
| Diff
::Longer(idx
, put_back(j
).with_value(j_elem
)))