| 1 | use craterust-analyzer cargo itertools 0.14.0 crate/::size_hintrust-analyzer cargo itertools 0.14.0 size_hint/; |
| 2 | |
| 3 | use allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/; |
| 5 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator#; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::replacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/replace().; |
| 7 | |
| 8 | /// Head element and Tail iterator pair |
| 9 | /// |
| 10 | /// `PartialEq`, `Eq`, `PartialOrd` and `Ord` are implemented by comparing sequences based on |
| 11 | /// first items (which are guaranteed to exist). |
| 12 | /// |
| 13 | /// The meanings of `PartialOrd` and `Ord` are reversed so as to turn the heap used in |
| 14 | /// `KMerge` into a min-heap. |
| 15 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 16 | struct HeadTailrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#<I> |
| 17 | where |
| 18 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 19 | { |
| 20 | headrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#head.: I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#, |
| 21 | tailrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#tail.: I, |
| 22 | } |
| 23 | |
| 24 | impl<I> HeadTailrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#<I> |
| 25 | where |
| 26 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 27 | { |
| 28 | /// Constructs a `HeadTail` from an `Iterator`. Returns `None` if the `Iterator` is empty. |
| 29 | fn newrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`HeadTail<I>`]new().(mut it: I) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`HeadTail<I>`]> { |
| 30 | let head = it.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().(); |
| 31 | head.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|h| Selfrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`HeadTail<I>`] { headrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#head.: h, tailrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#tail.: it }) |
| 32 | } |
| 33 | |
| 34 | /// Get the next element and update `head`, returning the old head in `Some`. |
| 35 | /// |
| 36 | /// Returns `None` when the tail is exhausted (only `head` then remains). |
| 37 | fn nextrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`HeadTail<I>`]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 38 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next) = self.tailrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#tail..nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() { |
| 39 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(replacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/replace().(&mut self.headrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#head., next)) |
| 40 | } else { |
| 41 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// Hints at the size of the sequence, same as the `Iterator` method. |
| 46 | fn size_hintrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`HeadTail<I>`]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 47 | size_hintrust-analyzer cargo itertools 0.14.0 size_hint/::add_scalarrust-analyzer cargo itertools 0.14.0 size_hint/add_scalar().(self.tailrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#tail..size_hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#size_hint().(), 1) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl<I> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for HeadTailrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#<I> |
| 52 | where |
| 53 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 54 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 55 | { |
| 56 | clone_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/clone_fields!!(headrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#head., tailrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#tail.); |
| 57 | } |
| 58 | |
| 59 | /// Make `data` a heap (min-heap w.r.t the sorting). |
| 60 | fn heapifyrust-analyzer cargo itertools 0.14.0 kmerge_impl/heapify().<T, S>(data: &mut [T], mut less_than: S) |
| 61 | where |
| 62 | S: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T, &T) -> bool, |
| 63 | { |
| 64 | for i in (0..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/Range#data.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().2).revrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#rev().() { |
| 65 | sift_downrust-analyzer cargo itertools 0.14.0 kmerge_impl/sift_down().(data, i, &mut less_than); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// Sift down element at `index` (`heap` is a min-heap wrt the ordering) |
| 70 | fn sift_downrust-analyzer cargo itertools 0.14.0 kmerge_impl/sift_down().<T, S>(heap: &mut [T], index: usize, mut less_than: S) |
| 71 | where |
| 72 | S: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T, &T) -> bool, |
| 73 | { |
| 74 | debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(index <= heap.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 75 | let mut pos = index; |
| 76 | let mut child = 2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 77 | // Require the right child to be present |
| 78 | // This allows to find the index of the smallest child without a branch |
| 79 | // that wouldn't be predicted if present |
| 80 | while child rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().heap.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 81 | // pick the smaller of the two children |
| 82 | // use arithmetic to avoid an unpredictable branch |
| 83 | child rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().less_than(&heap[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().child rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &heap[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().child]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) as usize; |
| 84 | |
| 85 | // sift down is done if we are already in order |
| 86 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().less_than(&heap[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().child]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &heap[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().pos]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) { |
| 87 | return; |
| 88 | } |
| 89 | heap.swaprust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]swap().(pos, child); |
| 90 | pos = child; |
| 91 | child = 2 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().pos rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1; |
| 92 | } |
| 93 | // Check if the last (left) child was an only child |
| 94 | // if it is then it has to be compared with the parent |
| 95 | if child rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().heap.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() && less_than(&heap[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().child]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &heap[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().pos]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) { |
| 96 | heap.swaprust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]swap().(pos, child); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /// An iterator adaptor that merges an abitrary number of base iterators in ascending order. |
| 101 | /// If all base iterators are sorted (ascending), the result is sorted. |
| 102 | /// |
| 103 | /// Iterator element type is `I::Item`. |
| 104 | /// |
| 105 | /// See [`.kmerge()`](crate::Itertools::kmerge) for more information. |
| 106 | pub type KMergerust-analyzer cargo itertools 0.14.0 kmerge_impl/KMerge#<I> = KMergeByrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#<I, KMergeByLtrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeByLt#>; |
| 107 | |
| 108 | pub trait KMergePredicaterust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergePredicate#<T> { |
| 109 | fn kmerge_predrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergePredicate#kmerge_pred().(&mut self, a: &T, b: &T) -> bool; |
| 110 | } |
| 111 | |
| 112 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 113 | pub struct KMergeByLtrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeByLt#; |
| 114 | |
| 115 | impl<T: PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#> KMergePredicaterust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergePredicate#<T> for KMergeByLtrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeByLt# { |
| 116 | fn kmerge_predrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[KMergeByLt][`KMergePredicate<T>`]kmerge_pred().(&mut self, a: &T, b: &T) -> bool { |
| 117 | a rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialOrd<&B>`]lt().b |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | impl<T, F: FnMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnMut#(&T, &T) -> bool> KMergePredicaterust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergePredicate#<T> for F { |
| 122 | fn kmerge_predrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[F][`KMergePredicate<T>`]kmerge_pred().(&mut self, a: &T, b: &T) -> bool { |
| 123 | self(a, b) |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// Create an iterator that merges elements of the contained iterators using |
| 128 | /// the ordering function. |
| 129 | /// |
| 130 | /// [`IntoIterator`] enabled version of [`Itertools::kmerge`](crate::Itertools::kmerge). |
| 131 | /// |
| 132 | /// ``` |
| 133 | /// use itertools::kmerge; |
| 134 | /// |
| 135 | /// for elt in kmerge(vec![vec![0, 2, 4], vec![1, 3, 5], vec![6, 7]]) { |
| 136 | /// /* loop body */ |
| 137 | /// # let _ = elt; |
| 138 | /// } |
| 139 | /// ``` |
| 140 | pub fn kmergerust-analyzer cargo itertools 0.14.0 kmerge_impl/kmerge().<I>(iterable: I) -> KMergerust-analyzer cargo itertools 0.14.0 kmerge_impl/KMerge#<<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# as IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#>::IntoIterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#IntoIter#> |
| 141 | where |
| 142 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#, |
| 143 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item#: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#, |
| 144 | <<I as IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#>::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# as IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#>::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item#: PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#, |
| 145 | { |
| 146 | kmerge_byrust-analyzer cargo itertools 0.14.0 kmerge_impl/kmerge_by().(iterable, KMergeByLtrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeByLt#) |
| 147 | } |
| 148 | |
| 149 | /// An iterator adaptor that merges an abitrary number of base iterators |
| 150 | /// according to an ordering function. |
| 151 | /// |
| 152 | /// Iterator element type is `I::Item`. |
| 153 | /// |
| 154 | /// See [`.kmerge_by()`](crate::Itertools::kmerge_by) for more |
| 155 | /// information. |
| 156 | #[must_use = "this iterator adaptor is not lazy but does nearly nothing unless consumed"] |
| 157 | pub struct KMergeByrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#<I, F> |
| 158 | where |
| 159 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 160 | { |
| 161 | heaprust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#heap.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<HeadTailrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#<I>>, |
| 162 | less_thanrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#less_than.: F, |
| 163 | } |
| 164 | |
| 165 | impl<I, F> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for KMergeByrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#<I, F> |
| 166 | where |
| 167 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# + fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, |
| 168 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#, |
| 169 | { |
| 170 | debug_fmt_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/debug_fmt_fields!!(KMergeBy, heaprust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#heap.); |
| 171 | } |
| 172 | |
| 173 | /// Create an iterator that merges elements of the contained iterators. |
| 174 | /// |
| 175 | /// [`IntoIterator`] enabled version of [`Itertools::kmerge_by`](crate::Itertools::kmerge_by). |
| 176 | pub fn kmerge_byrust-analyzer cargo itertools 0.14.0 kmerge_impl/kmerge_by().<I, F>( |
| 177 | iterable: I, |
| 178 | mut less_than: F, |
| 179 | ) -> KMergeByrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#<<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# as IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#>::IntoIterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#IntoIter#, F> |
| 180 | where |
| 181 | I: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#, |
| 182 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item#: IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#, |
| 183 | F: KMergePredicaterust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergePredicate#<<<I as IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#>::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# as IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#>::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item#>, |
| 184 | { |
| 185 | let iter = iterable.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().(); |
| 186 | let (lower, _) = iter.size_hintrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#size_hint().(); |
| 187 | let mut heap: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(lower); |
| 188 | heap.extendrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Extend<T>`]extend().(iter.filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|it| HeadTailrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#::newrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`HeadTail<I>`]new().(it.into_iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#into_iter().()))); |
| 189 | heapifyrust-analyzer cargo itertools 0.14.0 kmerge_impl/heapify().(&mut heap, |a, b| less_than.kmerge_predrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergePredicate#kmerge_pred().(&a.headrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#head., &b.headrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#head.)); |
| 190 | KMergeByrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy# { heaprust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#heap., less_thanrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#less_than. } |
| 191 | } |
| 192 | |
| 193 | impl<I, F> Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for KMergeByrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#<I, F> |
| 194 | where |
| 195 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 196 | I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 197 | F: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 198 | { |
| 199 | clone_fieldsrust-analyzer cargo itertools 0.14.0 impl_macros/clone_fields!!(heaprust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#heap., less_thanrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#less_than.); |
| 200 | } |
| 201 | |
| 202 | impl<I, F> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for KMergeByrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#<I, F> |
| 203 | where |
| 204 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 205 | F: KMergePredicaterust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergePredicate#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#>, |
| 206 | { |
| 207 | type Itemrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`KMergeBy<I, F>`][Iterator]Item# = I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#; |
| 208 | |
| 209 | fn nextrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`KMergeBy<I, F>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`KMergeBy<I, F>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 210 | if self.heaprust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#heap..is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 211 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 212 | } |
| 213 | let result = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next) = self.heaprust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#heap.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut()..nextrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`HeadTail<I>`]next().() { |
| 214 | next |
| 215 | } else { |
| 216 | self.heaprust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#heap..swap_removerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]swap_remove().(0).headrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#head. |
| 217 | }; |
| 218 | let less_than = &mut self.less_thanrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#less_than.; |
| 219 | sift_downrust-analyzer cargo itertools 0.14.0 kmerge_impl/sift_down().(&mut self.heaprust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#heap., 0, |a, b| { |
| 220 | less_than.kmerge_predrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergePredicate#kmerge_pred().(&a.headrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#head., &b.headrust-analyzer cargo itertools 0.14.0 kmerge_impl/HeadTail#head.) |
| 221 | }); |
| 222 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(result) |
| 223 | } |
| 224 | |
| 225 | fn size_hintrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`KMergeBy<I, F>`][Iterator]size_hint().(&self) -> (usize, Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize>) { |
| 226 | self.heaprust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#heap. |
| 227 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 228 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|i| i.size_hintrust-analyzer cargo itertools 0.14.0 kmerge_impl/impl#[`HeadTail<I>`]size_hint().()) |
| 229 | .reducerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#reduce().(size_hintrust-analyzer cargo itertools 0.14.0 size_hint/::addrust-analyzer cargo itertools 0.14.0 size_hint/add().) |
| 230 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().((0, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0))) |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | impl<I, F> FusedIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/marker/FusedIterator# for KMergeByrust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergeBy#<I, F> |
| 235 | where |
| 236 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 237 | F: KMergePredicaterust-analyzer cargo itertools 0.14.0 kmerge_impl/KMergePredicate#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#>, |
| 238 | { |
| 239 | } |