MergingIter

Struct MergingIter 

Source
pub struct MergingIter<Key: ?Sized, Cmp, Iter> { /* private fields */ }
Available on crate feature 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:

The following methods are not impacted by the direction, but set the direction:

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>
where Key: ?Sized, Cmp: Comparator<Key>, Iter: SeekableLendingIterator<Key, Cmp> + ItemToKey<Key>,

Source

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>
where Key: ?Sized, Cmp: Comparator<Key>, Iter: SeekableLendingIterator<Key, Cmp> + ItemToKey<Key>,

Source§

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

Determine whether the iterator is currently at any value in the collection. If the iterator is invalid, then it is conceptually one position before the first entry and one position after the last entry. (Or, there may be no entries.) Read more
Source§

fn next(&mut self) -> Option<LentItem<'_, Self>>

Move the iterator one position forwards, and return the entry at that position. Returns None if the iterator was at the last entry.
Source§

fn current(&self) -> Option<LentItem<'_, Self>>

Get the current value the iterator is at, if the iterator is valid.
Source§

fn into_lender(self) -> LenderAdapter<Self>
where Self: Sized,

Available on crate feature lender only.
Convert the CursorLendingIterator into a lender::Lender lending iterator. Read more
Source§

fn into_lending_iterator(self) -> LendingIteratorAdapter<Self>
where Self: Sized,

Available on crate feature lending-iterator only.
Convert the CursorLendingIterator into a lending_iterator::LendingIterator. Read more
Source§

impl<Key: Debug + ?Sized, Cmp: Debug, Iter: Debug> Debug for MergingIter<Key, Cmp, Iter>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Key, Cmp, Iter> ItemToKey<Key> for MergingIter<Key, Cmp, Iter>
where Key: ?Sized, Iter: ItemToKey<Key>,

Source§

fn item_to_key(item: LentItem<'_, Self>) -> &Key

Convert one of the items of an iterator into a Key reference, intended for use with a SeekableLendingIterator. Read more
Source§

impl<'lend, Key, Cmp, Iter> LendItem<'lend> for MergingIter<Key, Cmp, Iter>
where Key: ?Sized, Iter: LendItem<'lend>,

Source§

type Item = <Iter as LendItem<'lend>>::Item

The item of a lending iterator, with a particular lifetime. Read more
Source§

impl<Key, Cmp, Iter> Seekable<Key, Cmp> for MergingIter<Key, Cmp, Iter>
where Key: ?Sized, Cmp: Comparator<Key>, Iter: SeekableLendingIterator<Key, Cmp> + ItemToKey<Key>,

Source§

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)

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)

Reset the iterator to its initial position, before the first entry and after the last entry (if there are any entries in the collection). Read more
Source§

fn seek(&mut self, min_bound: &Key)

Move the iterator to the smallest key which is greater or equal than the provided min_bound. Read more
Source§

fn seek_to_first(&mut self)

Move the iterator to the smallest key in the collection. Read more

Auto Trait Implementations§

§

impl<Key, Cmp, Iter> Freeze for MergingIter<Key, Cmp, Iter>
where Cmp: Freeze, Key: ?Sized,

§

impl<Key, Cmp, Iter> RefUnwindSafe for MergingIter<Key, Cmp, Iter>
where Cmp: RefUnwindSafe, Iter: RefUnwindSafe, Key: ?Sized,

§

impl<Key, Cmp, Iter> Send for MergingIter<Key, Cmp, Iter>
where Cmp: Send, Iter: Send, Key: ?Sized,

§

impl<Key, Cmp, Iter> Sync for MergingIter<Key, Cmp, Iter>
where Cmp: Sync, Iter: Sync, Key: ?Sized,

§

impl<Key, Cmp, Iter> Unpin for MergingIter<Key, Cmp, Iter>
where Cmp: Unpin, Iter: Unpin, Key: ?Sized,

§

impl<Key, Cmp, Iter> UnwindSafe for MergingIter<Key, Cmp, Iter>
where Cmp: UnwindSafe, Iter: UnwindSafe, Key: ?Sized,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> FragileContainer<T> for T
where T: ?Sized,

Source§

fn get_ref(&self) -> <T as FragileTryContainer<T>>::Ref<'_>

Infallibly get immutable access to the T.

Source§

impl<T> FragileMutContainer<T> for T
where T: ?Sized,

Source§

fn get_mut(&mut self) -> <T as FragileTryMutContainer<T>>::RefMut<'_>

Infallibly get mutable access to the T.

Source§

impl<T> FragileTryContainer<T> for T
where T: ?Sized,

Source§

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>

Infallibly get immutable access to the T.

Source§

type Ref<'a> = &'a T where T: 'a

An immutably borrowed value from the container. Read more
Source§

type RefError = Infallible

An error that might be returned by try_get_ref. This type should implement std::error::Error. Read more
Source§

fn new_container(t: T) -> T

Create a new container that owns the provided T.
Source§

impl<T> FragileTryMutContainer<T> for T
where T: ?Sized,

Source§

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

A mutably borrowed value from the container. Read more
Source§

type RefMutError = Infallible

An error that might be returned by try_get_mut. This type should implement std::error::Error. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, C> BaseContainer<T> for C
where C: FragileTryContainer<T> + ?Sized, T: ?Sized,

Source§

impl<T, C> BaseMutContainer<T> for C
where C: FragileTryMutContainer<T> + ?Sized, T: ?Sized,

Source§

impl<T> Container<T> for T
where T: ?Sized,

Source§

impl<T> MutContainer<T> for T
where T: ?Sized,

Source§

impl<Key, Cmp, I> SeekableLendingIterator<Key, Cmp> for I
where Cmp: Comparator<Key> + ?Sized, I: CursorLendingIterator + Seekable<Key, Cmp>, Key: ?Sized,

Source§

impl<T> TryContainer<T> for T
where T: ?Sized,

Source§

impl<T> TryMutContainer<T> for T
where T: ?Sized,