1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
use core::iter::FusedIterator;
use itertools::{Itertools, KMergeBy, MergeBy};
use crate::map::{CloneBorrow, ValueOwned};
use crate::range_values::SetPriorityMap;
use crate::Integer;
use crate::sorted_disjoint_map::{Priority, PrioritySortedStartsMap, SortedDisjointMap};
/// Works with [`UnionIter`] to turn any number of [`SortedDisjointMap`] iterators into a [`SortedDisjointMap`] iterator of their union,
/// i.e., all the integers in any input iterator, as sorted & disjoint ranges.
///
/// Also see [`KMergeMap`].
///
/// [`SortedDisjointMap`]: crate::SortedDisjointMap
/// [`UnionIter`]: crate::UnionIter
///
/// # Examples
///
/// ```
/// use itertools::Itertools;
/// use range_set_blaze::{UnionIter, MergeMap, SortedDisjointMap, CheckSortedDisjoint};
///
/// let a = CheckSortedDisjoint::new([1..=2, 5..=100].into_iter());
/// let b = CheckSortedDisjoint::from([2..=6]);
/// let union = UnionIter::new2(a, b);
/// assert_eq!(union.into_string(), "1..=100");
///
/// // Or, equivalently:
/// let a = CheckSortedDisjoint::new([1..=2, 5..=100].into_iter());
/// let b = CheckSortedDisjoint::from([2..=6]);
/// let c = a | b;
/// assert_eq!(c.into_string(), "1..=100")
/// ```
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct MergeMap<T, V, VR, L, R>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
L: SortedDisjointMap<T, V, VR>,
R: SortedDisjointMap<T, V, VR>,
{
#[allow(clippy::type_complexity)]
iter: MergeBy<
SetPriorityMap<T, V, VR, L>,
SetPriorityMap<T, V, VR, R>,
fn(&Priority<T, V, VR>, &Priority<T, V, VR>) -> bool,
>,
}
impl<T, V, VR, L, R> MergeMap<T, V, VR, L, R>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
L: SortedDisjointMap<T, V, VR>,
R: SortedDisjointMap<T, V, VR>,
{
/// Creates a new [`MergeMap`] iterator from two [`SortedDisjointMap`] iterators. See [`MergeMap`] for more details and examples.
pub fn new(left: L, right: R) -> Self {
let left = SetPriorityMap::new(left, 0);
let right = SetPriorityMap::new(right, 1);
Self {
// We sort only by start -- priority is not used until later.
iter: left.merge_by(right, |a, b| a.start() < b.start()),
}
}
}
impl<T, V, VR, L, R> FusedIterator for MergeMap<T, V, VR, L, R>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
L: SortedDisjointMap<T, V, VR>,
R: SortedDisjointMap<T, V, VR>,
{
}
impl<T, V, VR, L, R> Iterator for MergeMap<T, V, VR, L, R>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
L: SortedDisjointMap<T, V, VR>,
R: SortedDisjointMap<T, V, VR>,
{
type Item = Priority<T, V, VR>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<T, V, VR, L, R> PrioritySortedStartsMap<T, V, VR> for MergeMap<T, V, VR, L, R>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
L: SortedDisjointMap<T, V, VR>,
R: SortedDisjointMap<T, V, VR>,
{
}
/// Works with [`UnionIter`] to turn two [`SortedDisjointMap`] iterators into a [`SortedDisjointMap`] iterator of their union,
/// i.e., all the integers in any input iterator, as sorted & disjoint ranges.
///
/// Also see [`MergeMap`].
///
/// [`SortedDisjointMap`]: crate::SortedDisjointMap
/// [`UnionIter`]: crate::UnionIter
///
/// # Examples
///
/// ```
/// use itertools::Itertools;
/// use range_set_blaze::{UnionIter, KMergeMap, MultiwaySortedDisjoint, SortedDisjointMap, CheckSortedDisjoint};
///
/// let a = CheckSortedDisjoint::new([1..=2, 5..=100].into_iter());
/// let b = CheckSortedDisjoint::new([2..=6].into_iter());
/// let c = CheckSortedDisjoint::new([-1..=-1].into_iter());
/// let union = UnionIter::new_k([a, b, c]);
/// assert_eq!(union.into_string(), "-1..=-1, 1..=100");
///
/// // Or, equivalently:
/// let a = CheckSortedDisjoint::new([1..=2, 5..=100].into_iter());
/// let b = CheckSortedDisjoint::new([2..=6].into_iter());
/// let c = CheckSortedDisjoint::new([-1..=-1].into_iter());
/// let union = [a, b, c].union();
/// assert_eq!(union.into_string(), "-1..=-1, 1..=100");
/// ```
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct KMergeMap<T, V, VR, I>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
I: SortedDisjointMap<T, V, VR>,
{
#[allow(clippy::type_complexity)]
iter:
KMergeBy<SetPriorityMap<T, V, VR, I>, fn(&Priority<T, V, VR>, &Priority<T, V, VR>) -> bool>,
}
impl<T, V, VR, I> KMergeMap<T, V, VR, I>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
I: SortedDisjointMap<T, V, VR>,
{
/// Creates a new [`KMergeMap`] iterator from zero or more [`SortedDisjointMap`] iterators. See [`KMergeMap`] for more details and examples.
pub fn new<K>(iter: K) -> Self
where
K: IntoIterator<Item = I>,
{
// Prioritize from left to right
let iter = iter.into_iter().enumerate().map(|(i, x)| {
let priority_number = i;
SetPriorityMap::new(x, priority_number)
});
// Merge RangeValues by start with ties broken by priority
let iter: KMergeBy<
SetPriorityMap<T, V, VR, I>,
fn(&Priority<T, V, VR>, &Priority<T, V, VR>) -> bool,
> = iter.kmerge_by(|a, b| {
// We sort only by start -- priority is not used until later.
a.start() < b.start()
});
Self { iter }
}
}
impl<T, V, VR, I> FusedIterator for KMergeMap<T, V, VR, I>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
I: SortedDisjointMap<T, V, VR>,
{
}
impl<T, V, VR, I> Iterator for KMergeMap<T, V, VR, I>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
I: SortedDisjointMap<T, V, VR>,
{
type Item = Priority<T, V, VR>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<T, V, VR, I> PrioritySortedStartsMap<T, V, VR> for KMergeMap<T, V, VR, I>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
I: SortedDisjointMap<T, V, VR>,
{
}