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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
use core::{
cmp::{max, min},
iter::FusedIterator,
marker::PhantomData,
ops::RangeInclusive,
};
use crate::{map::CloneBorrow, SortedDisjoint, SortedDisjointMap};
use crate::{map::ValueOwned, Integer};
/// Turns one [`SortedDisjoint`] iterator and one [`SortedDisjointMap`] iterator into
/// the [`SortedDisjointMap`] iterator of their intersection,
///
/// cmk
///
/// [`SortedDisjointMap`]: crate::SortedDisjointMap
/// [`Merge`]: crate::Merge
/// [`KMerge`]: crate::KMerge
///
/// # Examples
///
/// ```
/// use itertools::Itertools;
/// use range_set_blaze::{IntersectionIterMap, Merge, SortedDisjointMap, CheckSortedDisjoint};
///
/// let a = CheckSortedDisjoint::new([1..=2, 5..=100].into_iter());
/// let b = CheckSortedDisjoint::from([2..=6]);
/// let intersection = IntersectionIterMap::new(Merge::new(a, b));
/// assert_eq!(intersection.into_string(), "1..=100");
///
/// // Or, equivalently:
/// let a = CheckSortedDisjoint::new([1..=2, 5..=100].into_iter());
/// let b = CheckSortedDisjoint::from([2..=6]);
/// let intersection = a | b;
/// assert_eq!(intersection.into_string(), "1..=100")
/// ```
// cmk #[derive(Clone, Debug)]
#[allow(dead_code)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct IntersectionIterMap<T, V, VR, IM, IS>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
IM: SortedDisjointMap<T, V, VR>,
IS: SortedDisjoint<T>,
{
iter_left: IM,
iter_right: IS,
right: Option<RangeInclusive<T>>,
left: Option<(RangeInclusive<T>, VR)>,
phantom: PhantomData<V>,
}
impl<T, V, VR, IM, IS> IntersectionIterMap<T, V, VR, IM, IS>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
IM: SortedDisjointMap<T, V, VR>,
IS: SortedDisjoint<T>,
{
// cmk fix the comment on the set size. It should say inputs are SortedStarts not SortedDisjoint.
/// Creates a new [`IntersectionIterMap`] from zero or more [`SortedStartsMap`] iterators. See [`IntersectionIterMap`] for more details and examples.
#[allow(dead_code)]
pub fn new(iter_map: IM, iter_set: IS) -> Self {
Self {
iter_left: iter_map,
iter_right: iter_set,
right: None,
left: None,
phantom: PhantomData,
}
}
}
// impl<T: Integer, V: PartialEqClone, const N: usize> From<[T; N]>
// for IntersectionIterMap<T, V, SortedRangeInclusiveVec<T, V>>
// {
// fn from(arr: [T; N]) -> Self {
// arr.as_slice().into()
// }
// }
// impl<T: Integer, V: PartialEqClone> From<&[T]> for IntersectionIterMap<T, V, SortedRangeInclusiveVec<T, V>> {
// fn from(slice: &[T]) -> Self {
// slice.iter().cloned().collect()
// }
// }
// impl<T: Integer, V: PartialEqClone, const N: usize> From<[RangeValue<T, V>; N]>
// for IntersectionIterMap<T, V, SortedRangeInclusiveVec<T, V>>
// {
// fn from(arr: [RangeValue<T, V>; N]) -> Self {
// arr.as_slice().into()
// }
// }
impl<T, V, VR, IM, IS> FusedIterator for IntersectionIterMap<T, V, VR, IM, IS>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
IM: SortedDisjointMap<T, V, VR>,
IS: SortedDisjoint<T>,
{
}
impl<T, V, VR, IM, IS> Iterator for IntersectionIterMap<T, V, VR, IM, IS>
where
T: Integer,
V: ValueOwned,
VR: CloneBorrow<V>,
IM: SortedDisjointMap<T, V, VR>,
IS: SortedDisjoint<T>,
{
type Item = (RangeInclusive<T>, VR);
fn next(&mut self) -> Option<(RangeInclusive<T>, VR)> {
// println!("cmk begin next");
loop {
// Be sure both currents are loaded.
self.left = self.left.take().or_else(|| self.iter_left.next());
self.right = self.right.take().or_else(|| self.iter_right.next());
// If either is still none, we are done.
let (Some(left), Some(right)) = (self.left.take(), self.right.take()) else {
return None;
};
let (left_start, left_end) = left.0.clone().into_inner();
let (right_start, right_end) = right.into_inner();
// println!("cmk {:?} {:?}", current_range, current_range_value.range);
// if current_range ends before current_range_value, clear it and loop for a new value.
if right_end < left_start {
// println!("cmk getting new range");
self.right = None;
self.left = Some(left);
continue;
}
// if current_range_value ends before current_range, clear it and loop for a new value.
if left_end < right_start {
// println!("cmk getting new range value");
self.right = Some(RangeInclusive::new(right_start, right_end));
self.left = None;
continue;
}
// Thus, they overlap
let start = max(right_start, left_start);
let end = min(right_end, left_end);
// remove any ranges that match "end" and set them None
let value = match (right_end == end, left_end == end) {
(true, true) => {
self.right = None;
self.left = None;
left.1
}
(true, false) => {
self.right = None;
let value = left.1.clone_borrow();
self.left = Some(left);
value
}
(false, true) => {
self.right = Some(RangeInclusive::new(right_start, right_end));
self.left = None;
left.1
}
(false, false) => {
panic!("impossible case")
}
};
let range_value = (start..=end, value);
return Some(range_value);
}
}
// // There could be a few as 1 (or 0 if the iter is empty) or as many as the iter.
// // Plus, possibly one more if we have a range is in progress.
// fn size_hint(&self) -> (usize, Option<usize>) {
// let (low, high) = self.iter.size_hint();
// let low = low.min(1);
// if self.option_range_value.is_some() {
// (low, high.map(|x| x + 1))
// } else {
// (low, high)
// }
// }
}
// cmk
// impl<T: Integer, V: PartialEqClone, I> ops::Not for IntersectionIterMap<T, V, I>
// where
// I: SortedStartsMap<T, V>,
// {
// type Output = NotIterMap<T, V, Self>;
// fn not(self) -> Self::Output {
// self.complement()
// }
// }
// impl<'a, T: Integer, V: ValueOwned + 'a, R, L> ops::BitOr<R> for IntersectionIterMap<'a, T, V, L>
// where
// L: SortedStartsMap<'a, T, V>,
// R: SortedDisjointMap<'a, T, V>,
// {
// type Output = BitOrMergeMap<'a, T, V, Self, R>;
// fn bitor(self, rhs: R) -> Self::Output {
// // It might be fine to optimize to self.iter, but that would require
// // also considering field 'range'
// SortedDisjointMap::intersection(self, rhs)
// }
// }
// impl<T: Integer, V: PartialEqClone, R, L> ops::Sub<R> for IntersectionIterMap<T, V, L>
// where
// L: SortedStartsMap<T, V>,
// R: SortedDisjointMap<T, V>,
// {
// type Output = BitSubMergeMap<T, V, Self, R>;
// fn sub(self, rhs: R) -> Self::Output {
// SortedDisjointMap::difference(self, rhs)
// }
// }
// impl<T: Integer, V: PartialEqClone, R, L> ops::BitXor<R> for IntersectionIterMap<T, V, L>
// where
// L: SortedStartsMap<T, V>,
// R: SortedDisjointMap<T, V>,
// {
// type Output = BitXOrTeeMap<T, V, Self, R>;
// #[allow(clippy::suspicious_arithmetic_impl)]
// fn bitxor(self, rhs: R) -> Self::Output {
// SortedDisjointMap::symmetric_difference(self, rhs)
// }
// }
// impl<T: Integer, V: PartialEqClone, R, L> ops::BitAnd<R> for IntersectionIterMap<T, V, L>
// where
// L: SortedStartsMap<T, V>,
// R: SortedDisjointMap<T, V>,
// {
// type Output = BitAndMergeMap<T, V, Self, R>;
// fn bitand(self, other: R) -> Self::Output {
// SortedDisjointMap::intersection(self, other)
// }
// }
// impl<'a, T, V, VR, IM, IS> SortedStartsMap<'a, T, V, VR>
// for IntersectionIterMap<'a, T, V, VR, IM, IS>
// where
// T: Integer,
// V: ValueOwned,
// VR: CloneBorrow<V> + 'a,
// IM: SortedDisjointMap<'a, T, V, VR> + 'a,
// IS: SortedDisjoint<T>,
// {
// }
// impl<'a, T, V, VR, IM, IS> SortedDisjointMap<'a, T, V, VR>
// for IntersectionIterMap<'a, T, V, VR, IM, IS>
// where
// T: Integer,
// V: ValueOwned,
// VR: CloneBorrow<V> + 'a,
// IM: SortedDisjointMap<'a, T, V, VR> + 'a,
// IS: SortedDisjoint<T>,
// {
// }