pub struct MergedIter<const STABLE_TIE_BREAKING: bool, S, Cmp> { /* private fields */ }
Expand description
An iterator that pulls the smallest item from multiple iterators.
MergedIter
is created by [Merged
]. It merges items from several base iterators
based on a comparison function.
§Examples
use iter_merge::Merged;
let iter1 = vec![1, 3, 5];
let iter2 = vec![2, 4, 6];
let mut merged = Merged::new([iter1, iter2]).build();
let result = merged.into_vec();
assert_eq!(result, vec![1, 2, 3, 4, 5, 6]);
Implementations§
Source§impl<const STABLE_TIE_BREAKING: bool, S, Cmp, Item, Iter> MergedIter<STABLE_TIE_BREAKING, S, Cmp>
impl<const STABLE_TIE_BREAKING: bool, S, Cmp, Item, Iter> MergedIter<STABLE_TIE_BREAKING, S, Cmp>
Sourcepub fn add_iter(&mut self, iter: impl IntoIterator<IntoIter = Iter>)
pub fn add_iter(&mut self, iter: impl IntoIterator<IntoIter = Iter>)
Adds a single iterator to the merge.
This method allows you to dynamically add iterators to an existing MergedIter
.
The new iterator will be integrated into the merge according to the comparison
function, maintaining the sorted order of the output.
Empty iterators are ignored and do not affect the merge state.
§Arguments
iter
- An iterator (or anything that implementsIntoIterator
) to add to the merge
§Examples
use iter_merge::Merged;
let mut merged = Merged::new([vec![2, 5, 8]]).build();
assert_eq!(merged.next(), Some(2));
// Add another iterator dynamically
merged.add_iter(vec![1, 4, 7]);
assert_eq!(merged.next(), Some(1));
merged.add_iter(vec![3, 6, 9]);
let result = merged.into_vec();
assert_eq!(result, vec![3, 4, 5, 6, 7, 8, 9]);
Sourcepub fn add_iters(
&mut self,
iters: impl IntoIterator<Item = impl IntoIterator<IntoIter = Iter>>,
)
pub fn add_iters( &mut self, iters: impl IntoIterator<Item = impl IntoIterator<IntoIter = Iter>>, )
Adds multiple iterators to the merge at once.
This method efficiently adds multiple iterators to an existing MergedIter
in a single
operation. It’s more efficient than calling add_iter
multiple times because it can
optimize memory allocation and minimize index recalculations.
Empty iterators in the collection are automatically filtered out and ignored.
§Arguments
iters
- A collection of iterators to add to the merge. Each element should implementIntoIterator
with the same item type as the existing merge.
§Performance
This method attempts to reserve storage space based on the size hint of the input collection, which can improve performance by reducing memory reallocations.
§Examples
use iter_merge::Merged;
let mut merged = Merged::new([vec![1, 6, 11]]).build();
// Add multiple iterators at once
merged.add_iters([
vec![2, 7, 12],
vec![3, 8, 13],
vec![4, 9, 14],
vec![5, 10, 15],
]);
let result = merged.into_vec();
assert_eq!(
result,
vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
);
Sourcepub fn replace_cmp<F>(self, cmp: F) -> MergedIter<STABLE_TIE_BREAKING, S, F> ⓘ
pub fn replace_cmp<F>(self, cmp: F) -> MergedIter<STABLE_TIE_BREAKING, S, F> ⓘ
Replaces the comparison function and returns a new MergedIter
.
This method consumes the current iterator and creates a new one with a different comparison function. The new iterator will use the provided comparison function to determine the ordering of items from the merged iterators.
§Arguments
cmp
- A function that compares two items and returns anOrdering
§Examples
use std::cmp::Ordering;
use iter_merge::Merged;
let mut merged = Merged::new([vec![1, 4], vec![2, 5], vec![3, 6]]).build();
assert_eq!(merged.next(), Some(1));
assert_eq!(merged.next(), Some(2));
assert_eq!(merged.next(), Some(3));
// Reverse the ordering
let mut merged = merged.replace_cmp(|a, b| b.cmp(a));
assert_eq!(merged.next(), Some(6));
assert_eq!(merged.next(), Some(5));
assert_eq!(merged.next(), Some(4));
Sourcepub fn into_vec(&mut self) -> Vec<Item>
pub fn into_vec(&mut self) -> Vec<Item>
Consumes the iterator and produces a Vec
of its items.
This method is an optimization over collect::<Vec<_>>()
. It can be more
efficient, specifically when only one iterator remains, as it can extend the
result vector with the remaining items of that iterator directly.
§Examples
use iter_merge::Merged;
let iter1 = vec![1, 3, 5];
let iter2 = vec![2, 4, 6];
let mut merged = Merged::new([iter1, iter2]).build();
let result = merged.into_vec();
assert_eq!(result, vec![1, 2, 3, 4, 5, 6]);
Sourcepub fn peek<'a>(&'a mut self) -> Option<&'a Item>where
Iter: 'a,
pub fn peek<'a>(&'a mut self) -> Option<&'a Item>where
Iter: 'a,
Returns a reference to the next item that will be returned by next()
without
consuming it.
This method behaves identically to Peekable::peek
from the standard library:
it returns a reference to the next item, or None
if the iterator is empty.
§Examples
use iter_merge::Merged;
let iter1 = vec![1, 3, 5];
let iter2 = vec![2, 4, 6];
let mut merged = Merged::new([iter1, iter2]).build();
assert_eq!(merged.peek(), Some(&1));
assert_eq!(merged.next(), Some(1));
assert_eq!(merged.peek(), Some(&2));
Sourcepub fn next_if(&mut self, func: impl FnOnce(&Item) -> bool) -> Option<Item>
pub fn next_if(&mut self, func: impl FnOnce(&Item) -> bool) -> Option<Item>
Returns the next item of the iterator if it satisfies a predicate.
This method behaves identically to Peekable::next_if
from the standard library:
it returns the next item if it satisfies the predicate, otherwise returns None
.
§Examples
use iter_merge::Merged;
let iter1 = vec![1, 1, 2, 3];
let iter2 = vec![1, 4];
let mut merged = Merged::new([iter1, iter2]).build();
// Consume all 1s
while let Some(item) = merged.next_if(|&x| x == 1) {
assert_eq!(item, 1);
}
assert_eq!(merged.next(), Some(2));
Sourcepub fn next_if_eq<T>(&mut self, expected: &T) -> Option<Item>
pub fn next_if_eq<T>(&mut self, expected: &T) -> Option<Item>
Returns the next item of the iterator if it is equal to a given value.
This method behaves identically to Peekable::next_if_eq
from the standard library:
it returns the next item if it is equal to the given value, otherwise returns None
.
This is a convenience method that is equivalent to next_if(|item| item == expected)
.
§Examples
use iter_merge::Merged;
let iter1 = vec![1, 1, 2, 3];
let iter2 = vec![1, 4];
let mut merged = Merged::new([iter1, iter2]).build();
// Consume all 1s
while let Some(item) = merged.next_if_eq(&1) {
assert_eq!(item, 1);
}
assert_eq!(merged.next(), Some(2));
Sourcepub fn break_up(self) -> S
pub fn break_up(self) -> S
Consumes the MergedIter
iterator and returns the remaining iterators.
This method returns the internal storage, which contains the remaining “peeked”
items and their associated iterators. Each element in the returned storage is
a tuple of the form
(Item, Iter)
, where:
Item
is the “peeked” value from that component iterator,Iter
is the remaining iterator for that component.
The order of the elements in the storage may be different from the order of original
iterators if [Merged::arbitrary_tie_breaking
] was used.
If an input iterator was fully consumed, it will be missing from the returned storage.
§Examples
use iter_merge::Merged;
let iter1 = vec![1, 3];
let iter2 = vec![2, 4];
let mut merged = Merged::new([iter1, iter2]).build();
assert_eq!(merged.next(), Some(1));
let mut storage = merged.break_up();
assert_eq!(storage[0].0, 3);
assert_eq!(storage[0].1.next(), None);
assert_eq!(storage[1].0, 2);
assert_eq!(storage[1].1.next(), Some(4));
assert_eq!(storage[1].1.next(), None);
Trait Implementations§
Source§impl<const STABLE_TIE_BREAKING: bool, S, Cmp, Item, Iter> Clone for MergedIter<STABLE_TIE_BREAKING, S, Cmp>
impl<const STABLE_TIE_BREAKING: bool, S, Cmp, Item, Iter> Clone for MergedIter<STABLE_TIE_BREAKING, S, Cmp>
Source§impl<const STABLE_TIE_BREAKING: bool, S: Debug, Cmp: Debug> Debug for MergedIter<STABLE_TIE_BREAKING, S, Cmp>
impl<const STABLE_TIE_BREAKING: bool, S: Debug, Cmp: Debug> Debug for MergedIter<STABLE_TIE_BREAKING, S, Cmp>
Source§impl<const STABLE_TIE_BREAKING: bool, Item, Iter, S, Cmp> Iterator for MergedIter<STABLE_TIE_BREAKING, S, Cmp>
impl<const STABLE_TIE_BREAKING: bool, Item, Iter, S, Cmp> Iterator for MergedIter<STABLE_TIE_BREAKING, S, Cmp>
Source§fn next(&mut self) -> Option<Self::Item>
fn next(&mut self) -> Option<Self::Item>
Source§fn size_hint(&self) -> (usize, Option<usize>)
fn size_hint(&self) -> (usize, Option<usize>)
Source§fn next_chunk<const N: usize>(
&mut self,
) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>where
Self: Sized,
fn next_chunk<const N: usize>(
&mut self,
) -> Result<[Self::Item; N], IntoIter<Self::Item, N>>where
Self: Sized,
iter_next_chunk
)N
values. Read more1.0.0 · Source§fn count(self) -> usizewhere
Self: Sized,
fn count(self) -> usizewhere
Self: Sized,
1.0.0 · Source§fn last(self) -> Option<Self::Item>where
Self: Sized,
fn last(self) -> Option<Self::Item>where
Self: Sized,
Source§fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
fn advance_by(&mut self, n: usize) -> Result<(), NonZero<usize>>
iter_advance_by
)n
elements. Read more1.0.0 · Source§fn nth(&mut self, n: usize) -> Option<Self::Item>
fn nth(&mut self, n: usize) -> Option<Self::Item>
n
th element of the iterator. Read more1.28.0 · Source§fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
fn step_by(self, step: usize) -> StepBy<Self>where
Self: Sized,
1.0.0 · Source§fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter>
fn chain<U>(self, other: U) -> Chain<Self, <U as IntoIterator>::IntoIter>
1.0.0 · Source§fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter>where
Self: Sized,
U: IntoIterator,
fn zip<U>(self, other: U) -> Zip<Self, <U as IntoIterator>::IntoIter>where
Self: Sized,
U: IntoIterator,
Source§fn intersperse(self, separator: Self::Item) -> Intersperse<Self>
fn intersperse(self, separator: Self::Item) -> Intersperse<Self>
iter_intersperse
)separator
between adjacent
items of the original iterator. Read moreSource§fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G>
fn intersperse_with<G>(self, separator: G) -> IntersperseWith<Self, G>
iter_intersperse
)separator
between adjacent items of the original iterator. Read more1.0.0 · Source§fn map<B, F>(self, f: F) -> Map<Self, F>
fn map<B, F>(self, f: F) -> Map<Self, F>
1.0.0 · Source§fn filter<P>(self, predicate: P) -> Filter<Self, P>
fn filter<P>(self, predicate: P) -> Filter<Self, P>
1.0.0 · Source§fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
fn filter_map<B, F>(self, f: F) -> FilterMap<Self, F>
1.0.0 · Source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
1.0.0 · Source§fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
fn skip_while<P>(self, predicate: P) -> SkipWhile<Self, P>
1.0.0 · Source§fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
fn take_while<P>(self, predicate: P) -> TakeWhile<Self, P>
1.57.0 · Source§fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P>
fn map_while<B, P>(self, predicate: P) -> MapWhile<Self, P>
1.0.0 · Source§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n
elements. Read more1.0.0 · Source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n
elements, or fewer
if the underlying iterator ends sooner. Read more1.0.0 · Source§fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>
1.29.0 · Source§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Source§fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N>
fn map_windows<F, R, const N: usize>(self, f: F) -> MapWindows<Self, F, N>
iter_map_windows
)f
for each contiguous window of size N
over
self
and returns an iterator over the outputs of f
. Like slice::windows()
,
the windows during mapping overlap as well. Read more1.0.0 · Source§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Iterator
. Read moreSource§fn try_collect<B>(
&mut self,
) -> <<Self::Item as Try>::Residual as Residual<B>>::TryType
fn try_collect<B>( &mut self, ) -> <<Self::Item as Try>::Residual as Residual<B>>::TryType
iterator_try_collect
)Source§fn collect_into<E>(self, collection: &mut E) -> &mut E
fn collect_into<E>(self, collection: &mut E) -> &mut E
iter_collect_into
)1.0.0 · Source§fn partition<B, F>(self, f: F) -> (B, B)
fn partition<B, F>(self, f: F) -> (B, B)
Source§fn is_partitioned<P>(self, predicate: P) -> bool
fn is_partitioned<P>(self, predicate: P) -> bool
iter_is_partitioned
)true
precede all those that return false
. Read more1.27.0 · Source§fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R
1.27.0 · Source§fn try_for_each<F, R>(&mut self, f: F) -> R
fn try_for_each<F, R>(&mut self, f: F) -> R
1.0.0 · Source§fn fold<B, F>(self, init: B, f: F) -> B
fn fold<B, F>(self, init: B, f: F) -> B
1.51.0 · Source§fn reduce<F>(self, f: F) -> Option<Self::Item>
fn reduce<F>(self, f: F) -> Option<Self::Item>
Source§fn try_reduce<R>(
&mut self,
f: impl FnMut(Self::Item, Self::Item) -> R,
) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType
fn try_reduce<R>( &mut self, f: impl FnMut(Self::Item, Self::Item) -> R, ) -> <<R as Try>::Residual as Residual<Option<<R as Try>::Output>>>::TryType
iterator_try_reduce
)1.0.0 · Source§fn all<F>(&mut self, f: F) -> bool
fn all<F>(&mut self, f: F) -> bool
1.0.0 · Source§fn any<F>(&mut self, f: F) -> bool
fn any<F>(&mut self, f: F) -> bool
1.0.0 · Source§fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
fn find<P>(&mut self, predicate: P) -> Option<Self::Item>
1.30.0 · Source§fn find_map<B, F>(&mut self, f: F) -> Option<B>
fn find_map<B, F>(&mut self, f: F) -> Option<B>
Source§fn try_find<R>(
&mut self,
f: impl FnMut(&Self::Item) -> R,
) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType
fn try_find<R>( &mut self, f: impl FnMut(&Self::Item) -> R, ) -> <<R as Try>::Residual as Residual<Option<Self::Item>>>::TryType
try_find
)1.0.0 · Source§fn position<P>(&mut self, predicate: P) -> Option<usize>
fn position<P>(&mut self, predicate: P) -> Option<usize>
1.0.0 · Source§fn max(self) -> Option<Self::Item>
fn max(self) -> Option<Self::Item>
1.0.0 · Source§fn min(self) -> Option<Self::Item>
fn min(self) -> Option<Self::Item>
1.6.0 · Source§fn max_by_key<B, F>(self, f: F) -> Option<Self::Item>
fn max_by_key<B, F>(self, f: F) -> Option<Self::Item>
1.15.0 · Source§fn max_by<F>(self, compare: F) -> Option<Self::Item>
fn max_by<F>(self, compare: F) -> Option<Self::Item>
1.6.0 · Source§fn min_by_key<B, F>(self, f: F) -> Option<Self::Item>
fn min_by_key<B, F>(self, f: F) -> Option<Self::Item>
1.15.0 · Source§fn min_by<F>(self, compare: F) -> Option<Self::Item>
fn min_by<F>(self, compare: F) -> Option<Self::Item>
1.0.0 · Source§fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB)
fn unzip<A, B, FromA, FromB>(self) -> (FromA, FromB)
1.36.0 · Source§fn copied<'a, T>(self) -> Copied<Self>
fn copied<'a, T>(self) -> Copied<Self>
Source§fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N>where
Self: Sized,
fn array_chunks<const N: usize>(self) -> ArrayChunks<Self, N>where
Self: Sized,
iter_array_chunks
)N
elements of the iterator at a time. Read more1.11.0 · Source§fn product<P>(self) -> P
fn product<P>(self) -> P
Source§fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering
fn cmp_by<I, F>(self, other: I, cmp: F) -> Ordering
iter_order_by
)Iterator
with those
of another with respect to the specified comparison function. Read more1.5.0 · Source§fn partial_cmp<I>(self, other: I) -> Option<Ordering>
fn partial_cmp<I>(self, other: I) -> Option<Ordering>
PartialOrd
elements of
this Iterator
with those of another. The comparison works like short-circuit
evaluation, returning a result without comparing the remaining elements.
As soon as an order can be determined, the evaluation stops and a result is returned. Read moreSource§fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering>where
Self: Sized,
I: IntoIterator,
F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering>,
fn partial_cmp_by<I, F>(self, other: I, partial_cmp: F) -> Option<Ordering>where
Self: Sized,
I: IntoIterator,
F: FnMut(Self::Item, <I as IntoIterator>::Item) -> Option<Ordering>,
iter_order_by
)Iterator
with those
of another with respect to the specified comparison function. Read moreSource§fn eq_by<I, F>(self, other: I, eq: F) -> bool
fn eq_by<I, F>(self, other: I, eq: F) -> bool
iter_order_by
)1.5.0 · Source§fn lt<I>(self, other: I) -> bool
fn lt<I>(self, other: I) -> bool
Iterator
are lexicographically
less than those of another. Read more1.5.0 · Source§fn le<I>(self, other: I) -> bool
fn le<I>(self, other: I) -> bool
Iterator
are lexicographically
less or equal to those of another. Read more1.5.0 · Source§fn gt<I>(self, other: I) -> bool
fn gt<I>(self, other: I) -> bool
Iterator
are lexicographically
greater than those of another. Read more1.5.0 · Source§fn ge<I>(self, other: I) -> bool
fn ge<I>(self, other: I) -> bool
Iterator
are lexicographically
greater than or equal to those of another. Read more