pub struct MergingIter<Key: ?Sized, Cmp, Iter> { /* private fields */ }alloc only.Expand description
A MergingIter takes several SeekableLendingIterators as input, and iterates over the
sorted union of their entries.
The given iterators may have overlap in their keys, and can be provided in any order.
Conceptually, each SeekableLendingIterator is a circular iterator over the entries of some
sorted collection; this also holds of MergingIter. The collection corresponding to a
MergingIter is the sorted union (without de-duplication) of its given iterators’
collections. However, note that the presence of duplicate keys across different iterators can
cause unexpected behavior in certain well-defined scenarios (see below). Ideally, the
iterators that are merged should not have duplicate keys.
§Note on backwards iteration
Some SeekableLendingIterator implementations have better performance for
forwards iteration than backwards iteration. MergingIter itself otherwise has roughly equal
performance in either direction, but has overhead for switching the direction of iteration
(see below for more information). Moreover, switching direction does not play well with
duplicate keys. Therefore, MergingIter::prev, MergingIter::seek_before, and
MergingIter::seek_to_last (the three methods that use backwards iteration) should be
avoided if possible.
§Warning for duplicate keys
If a key is present in multiple iterators, then repeatedly calling next or repeatedly
calling prev will yield all items with that key. That is, as expected, iterating through the
entire collection associated with a MergingIter is possible, and can be done in either the
forwards or backwards direction.
However, switching between next and prev will return at least one but not necessarily all of
the items with the key returned by MergingIter::current at the time of the switch in
direction.
To be precise, the items with duplicate keys may be skipped whenever the MergingIter changes
which “direction” (forwards or backwards) that it is iterating in. When switching direction,
some of the items whose keys compare equal to MergingIter::current may be skipped over.
The following methods need to switch direction if necessary, and iterate in a certain direction:
- Forwards:
- Backwards:
The following methods are not impacted by the direction, but set the direction:
- Set direction to forwards:
- Set direction to backwards:
The following methods do not impact and are not impacted by the direction:
§Time Complexity
MergingIter::new takes O(n) time and O(1) space, where n is iterators.len().
Switching direction, seeking, or resetting takes O(n) time. MergingIter::valid and
MergingIter::current are O(1). Currently, MergingIter::next and MergingIter::prev
take O(n) time even if they do not switch direction; soon, they will be O(log n) unless
switching direction, in exchange for MergingIter::new taking O(n) space.
Implementations§
Source§impl<Key, Cmp, Iter> MergingIter<Key, Cmp, Iter>
impl<Key, Cmp, Iter> MergingIter<Key, Cmp, Iter>
Sourcepub fn new(iterators: Vec<Iter>, cmp: Cmp) -> Self
pub fn new(iterators: Vec<Iter>, cmp: Cmp) -> Self
Create a new MergingIter. See the type-level documentation for details on behavior.
§Comparator requirements
The Comparators used by each of the provided iterators must all behave identically
to each other and to the provided cmp value. In particular, this requirement is met
if the Cmp generic is a ZST, or if all the comparators were cloned from some common
source.
§Panics
Panics if the length of iterators is usize::MAX. Any other number of iterators
can, theoretically, be merged.
Trait Implementations§
Source§impl<Key, Cmp, Iter> CursorLendingIterator for MergingIter<Key, Cmp, Iter>
impl<Key, Cmp, Iter> CursorLendingIterator for MergingIter<Key, Cmp, Iter>
Source§fn prev(&mut self) -> Option<LentItem<'_, Self>>
fn prev(&mut self) -> Option<LentItem<'_, Self>>
Move the iterator one position back, and return the entry at that position.
Returns None if the iterator was at the first entry.
The inner Iter iterators may have worse performance for backwards iteration than forwards
iteration, so prefer to not use prev. Additionally, MergingIter has overhead
for switching between backwards and forwards iteration; check the type-level documentation
if you wish to use prev.
Source§fn valid(&self) -> bool
fn valid(&self) -> bool
Source§fn next(&mut self) -> Option<LentItem<'_, Self>>
fn next(&mut self) -> Option<LentItem<'_, Self>>
None if the iterator was at the last entry.Source§fn current(&self) -> Option<LentItem<'_, Self>>
fn current(&self) -> Option<LentItem<'_, Self>>
Source§fn into_lender(self) -> LenderAdapter<Self>where
Self: Sized,
fn into_lender(self) -> LenderAdapter<Self>where
Self: Sized,
lender only.Source§fn into_lending_iterator(self) -> LendingIteratorAdapter<Self>where
Self: Sized,
fn into_lending_iterator(self) -> LendingIteratorAdapter<Self>where
Self: Sized,
lending-iterator only.Source§impl<Key, Cmp, Iter> ItemToKey<Key> for MergingIter<Key, Cmp, Iter>
impl<Key, Cmp, Iter> ItemToKey<Key> for MergingIter<Key, Cmp, Iter>
Source§fn item_to_key(item: LentItem<'_, Self>) -> &Key
fn item_to_key(item: LentItem<'_, Self>) -> &Key
Key reference, intended for use with
a SeekableLendingIterator. Read moreSource§impl<'lend, Key, Cmp, Iter> LendItem<'lend> for MergingIter<Key, Cmp, Iter>
impl<'lend, Key, Cmp, Iter> LendItem<'lend> for MergingIter<Key, Cmp, Iter>
Source§impl<Key, Cmp, Iter> Seekable<Key, Cmp> for MergingIter<Key, Cmp, Iter>
impl<Key, Cmp, Iter> Seekable<Key, Cmp> for MergingIter<Key, Cmp, Iter>
Source§fn seek_before(&mut self, strict_upper_bound: &Key)
fn seek_before(&mut self, strict_upper_bound: &Key)
Move the iterator to the greatest key which is strictly less than the provided
strict_upper_bound.
If there is no such key, the iterator becomes !valid(), and is conceptually
one position before the first entry and one position after the last entry (if there are
any entries in the collection).
The inner Iter iterators may have worse performance for seek_before than seek.
Additionally, MergingIter has overhead for switching between backwards and forwards
iteration; check the type-level documentation if you wish to use seek_before.
Source§fn seek_to_last(&mut self)
fn seek_to_last(&mut self)
Move the iterator to the greatest key in the collection.
If the collection is empty, the iterator is !valid().
MergingIter has overhead for switching between backwards and forwards
iteration; check the type-level documentation if you wish to use seek_before.
Source§fn reset(&mut self)
fn reset(&mut self)
Source§fn seek(&mut self, min_bound: &Key)
fn seek(&mut self, min_bound: &Key)
min_bound. Read moreSource§fn seek_to_first(&mut self)
fn seek_to_first(&mut self)
Auto Trait Implementations§
impl<Key, Cmp, Iter> Freeze for MergingIter<Key, Cmp, Iter>
impl<Key, Cmp, Iter> RefUnwindSafe for MergingIter<Key, Cmp, Iter>
impl<Key, Cmp, Iter> Send for MergingIter<Key, Cmp, Iter>
impl<Key, Cmp, Iter> Sync for MergingIter<Key, Cmp, Iter>
impl<Key, Cmp, Iter> Unpin for MergingIter<Key, Cmp, Iter>
impl<Key, Cmp, Iter> UnwindSafe for MergingIter<Key, Cmp, Iter>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FragileContainer<T> for Twhere
T: ?Sized,
impl<T> FragileContainer<T> for Twhere
T: ?Sized,
Source§fn get_ref(&self) -> <T as FragileTryContainer<T>>::Ref<'_>
fn get_ref(&self) -> <T as FragileTryContainer<T>>::Ref<'_>
Infallibly get immutable access to the T.
Source§impl<T> FragileMutContainer<T> for Twhere
T: ?Sized,
impl<T> FragileMutContainer<T> for Twhere
T: ?Sized,
Source§fn get_mut(&mut self) -> <T as FragileTryMutContainer<T>>::RefMut<'_>
fn get_mut(&mut self) -> <T as FragileTryMutContainer<T>>::RefMut<'_>
Infallibly get mutable access to the T.
Source§impl<T> FragileTryContainer<T> for Twhere
T: ?Sized,
impl<T> FragileTryContainer<T> for Twhere
T: ?Sized,
Source§fn into_inner(self) -> Option<T>
fn into_inner(self) -> Option<T>
Infallibly get the T.
Source§fn try_get_ref(
&self,
) -> Result<<T as FragileTryContainer<T>>::Ref<'_>, <T as FragileTryContainer<T>>::RefError>
fn try_get_ref( &self, ) -> Result<<T as FragileTryContainer<T>>::Ref<'_>, <T as FragileTryContainer<T>>::RefError>
Infallibly get immutable access to the T.
Source§type RefError = Infallible
type RefError = Infallible
try_get_ref. This type should implement
std::error::Error. Read moreSource§fn new_container(t: T) -> T
fn new_container(t: T) -> T
T.Source§impl<T> FragileTryMutContainer<T> for Twhere
T: ?Sized,
impl<T> FragileTryMutContainer<T> for Twhere
T: ?Sized,
Source§fn try_get_mut(
&mut self,
) -> Result<<T as FragileTryMutContainer<T>>::RefMut<'_>, <T as FragileTryMutContainer<T>>::RefMutError>
fn try_get_mut( &mut self, ) -> Result<<T as FragileTryMutContainer<T>>::RefMut<'_>, <T as FragileTryMutContainer<T>>::RefMutError>
Infallibly get mutable access to the T.
Source§type RefMut<'a> = &'a mut T
where
T: 'a
type RefMut<'a> = &'a mut T where T: 'a
Source§type RefMutError = Infallible
type RefMutError = Infallible
try_get_mut. This type should implement
std::error::Error. Read more