Skip to main content

version_ranges/
lib.rs

1// SPDX-License-Identifier: MPL-2.0
2
3//! This crate contains a performance-optimized type for generic version ranges and operations on
4//! them.
5//!
6//! [`Ranges`] can represent version selectors such as `(>=1, <2) OR (==3) OR (>4)`. Internally,
7//! it is an ordered list of contiguous intervals (segments) with inclusive, exclusive or open-ended
8//! ends, similar to a `Vec<(Bound<T>, Bound<T>)>`.
9//!
10//! You can construct a basic range from one of the following build blocks. All other ranges are
11//! concatenation, union, and complement of these basic ranges.
12//!  - [empty()](Ranges::empty): No version
13//!  - [full()](Ranges::full): All versions
14//!  - [singleton(v)](Ranges::singleton): Only the version v exactly
15//!  - [higher_than(v)](Ranges::higher_than): All versions `v <= versions`
16//!  - [strictly_higher_than(v)](Ranges::strictly_higher_than): All versions `v < versions`
17//!  - [lower_than(v)](Ranges::lower_than): All versions `versions <= v`
18//!  - [strictly_lower_than(v)](Ranges::strictly_lower_than): All versions `versions < v`
19//!  - [between(v1, v2)](Ranges::between): All versions `v1 <= versions < v2`
20//!
21//! [`Ranges`] is generic over any type that implements [`Ord`] + [`Clone`] and can represent all
22//! kinds of slices with ordered coordinates, not just version ranges. While built as a
23//! performance-critical piece of [pubgrub](https://github.com/pubgrub-rs/pubgrub), it can be
24//! adopted for other domains, too.
25//!
26//! Note that there are limitations to the equality implementation: Given a `Ranges<u32>`,
27//! the segments `(Unbounded, Included(42u32))` and `(Included(0), Included(42u32))` as well as
28//! `(Included(1), Included(5))` and  `(Included(1), Included(3)) + (Included(4), Included(5))`
29//! are reported as unequal, even though the match the same versions: We can't tell that there isn't
30//! a version between `0` and `-inf` or `3` and `4` respectively.
31//!
32//! ## Optional features
33//!
34//! * `serde`: serialization and deserialization for the version range, given that the version type
35//!   also supports it.
36//! * `proptest`: Exports are proptest strategy for [`Ranges<u32>`].
37
38#[cfg(feature = "semver")]
39pub mod semver;
40
41use std::borrow::Borrow;
42use std::cmp::Ordering;
43use std::fmt::{Debug, Display, Formatter};
44use std::ops::Bound::{self, Excluded, Included, Unbounded};
45use std::ops::RangeBounds;
46
47#[cfg(any(feature = "proptest", test))]
48use proptest::prelude::*;
49use smallvec::{smallvec, SmallVec};
50
51/// Ranges represents multiple intervals of a continuous range of monotone increasing values.
52///
53/// Internally, [`Ranges`] are an ordered list of segments, where segment is a bounds pair.
54///
55/// Invariants:
56/// 1. The segments are sorted, from lowest to highest (through `Ord`).
57/// 2. Each segment contains at least one version (start < end).
58/// 3. There is at least one version between two segments.
59///
60/// These ensure that equivalent instances have an identical representation, which is important
61/// for `Eq` and `Hash`. Note that this representation cannot strictly guaranty equality of
62/// [`Ranges`] with equality of its representation without also knowing the nature of the underlying
63/// versions. In particular, if the version space is discrete, different representations, using
64/// different types of bounds (exclusive/inclusive) may correspond to the same set of existing
65/// versions. It is a tradeoff we acknowledge, but which makes representations of continuous version
66/// sets more accessible, to better handle features like pre-releases and other types of version
67/// modifiers. For example, `[(Included(3u32), Excluded(7u32))]` and
68/// `[(Included(3u32), Included(6u32))]` refer to the same version set, since there is no version
69/// between 6 and 7, which this crate doesn't know about.
70
71#[derive(Debug, Clone, Eq, PartialEq, Hash)]
72#[cfg_attr(feature = "serde", derive(serde::Serialize))]
73#[cfg_attr(feature = "serde", serde(transparent))]
74pub struct Ranges<V> {
75    /// Profiling in <https://github.com/pubgrub-rs/pubgrub/pull/262#discussion_r1804276278> showed
76    /// that a single stack entry is the most efficient. This is most likely due to `Interval<V>`
77    /// being large.
78    segments: SmallVec<[Interval<V>; 1]>,
79}
80
81/// Describes how one set relates to another.
82///
83/// [`SetRelation::Subset`] takes precedence when the left-hand set is empty and therefore also
84/// disjoint.
85#[derive(Clone, Copy, Debug, Eq, PartialEq)]
86pub enum SetRelation {
87    /// Every value in the left-hand set is also in the right-hand set.
88    Subset,
89    /// The two sets have no values in common.
90    Disjoint,
91    /// The sets overlap, but the left-hand set is not a subset of the right-hand set.
92    Overlapping,
93}
94
95// TODO: Replace the tuple type with a custom enum inlining the bounds to reduce the type's size.
96type Interval<V> = (Bound<V>, Bound<V>);
97
98impl<V> Ranges<V> {
99    /// Empty set of versions.
100    pub fn empty() -> Self {
101        Self {
102            segments: SmallVec::new(),
103        }
104    }
105
106    /// Set of all possible versions
107    pub fn full() -> Self {
108        Self {
109            segments: smallvec![(Unbounded, Unbounded)],
110        }
111    }
112
113    /// Set of all versions higher or equal to some version
114    pub fn higher_than(v: impl Into<V>) -> Self {
115        Self {
116            segments: smallvec![(Included(v.into()), Unbounded)],
117        }
118    }
119
120    /// Set of all versions higher to some version
121    pub fn strictly_higher_than(v: impl Into<V>) -> Self {
122        Self {
123            segments: smallvec![(Excluded(v.into()), Unbounded)],
124        }
125    }
126
127    /// Set of all versions lower to some version
128    pub fn strictly_lower_than(v: impl Into<V>) -> Self {
129        Self {
130            segments: smallvec![(Unbounded, Excluded(v.into()))],
131        }
132    }
133
134    /// Set of all versions lower or equal to some version
135    pub fn lower_than(v: impl Into<V>) -> Self {
136        Self {
137            segments: smallvec![(Unbounded, Included(v.into()))],
138        }
139    }
140
141    /// Set of versions greater or equal to `v1` but less than `v2`.
142    pub fn between(v1: impl Into<V>, v2: impl Into<V>) -> Self {
143        Self {
144            segments: smallvec![(Included(v1.into()), Excluded(v2.into()))],
145        }
146    }
147
148    /// Whether the set is empty, i.e. it has not ranges
149    pub fn is_empty(&self) -> bool {
150        self.segments.is_empty()
151    }
152}
153
154impl<V: Clone> Ranges<V> {
155    /// Set containing exactly one version
156    pub fn singleton(v: impl Into<V>) -> Self {
157        let v = v.into();
158        Self {
159            segments: smallvec![(Included(v.clone()), Included(v))],
160        }
161    }
162
163    /// Returns the complement, which contains everything not included in `self`.
164    pub fn complement(&self) -> Self {
165        match self.segments.first() {
166            // Complement of ∅ is ∞
167            None => Self::full(),
168
169            // Complement of ∞ is ∅
170            Some((Unbounded, Unbounded)) => Self::empty(),
171
172            // First high bound is +∞
173            Some((Included(v), Unbounded)) => Self::strictly_lower_than(v.clone()),
174            Some((Excluded(v), Unbounded)) => Self::lower_than(v.clone()),
175
176            Some((Unbounded, Included(v))) => {
177                Self::negate_segments(Excluded(v.clone()), &self.segments[1..])
178            }
179            Some((Unbounded, Excluded(v))) => {
180                Self::negate_segments(Included(v.clone()), &self.segments[1..])
181            }
182            Some((Included(_), Included(_)))
183            | Some((Included(_), Excluded(_)))
184            | Some((Excluded(_), Included(_)))
185            | Some((Excluded(_), Excluded(_))) => Self::negate_segments(Unbounded, &self.segments),
186        }
187    }
188
189    /// Helper function performing the negation of intervals in segments.
190    fn negate_segments(start: Bound<V>, segments: &[Interval<V>]) -> Self {
191        let mut complement_segments = SmallVec::new();
192        let mut start = start;
193        for (v1, v2) in segments {
194            complement_segments.push((
195                start,
196                match v1 {
197                    Included(v) => Excluded(v.clone()),
198                    Excluded(v) => Included(v.clone()),
199                    Unbounded => unreachable!(),
200                },
201            ));
202            start = match v2 {
203                Included(v) => Excluded(v.clone()),
204                Excluded(v) => Included(v.clone()),
205                Unbounded => Unbounded,
206            }
207        }
208        if !matches!(start, Unbounded) {
209            complement_segments.push((start, Unbounded));
210        }
211
212        Self {
213            segments: complement_segments,
214        }
215    }
216}
217
218impl<V: Ord> Ranges<V> {
219    /// If self contains exactly a single version, return it, otherwise, return [None].
220    pub fn as_singleton(&self) -> Option<&V> {
221        match self.segments.as_slice() {
222            [(Included(v1), Included(v2))] => {
223                if v1 == v2 {
224                    Some(v1)
225                } else {
226                    None
227                }
228            }
229            _ => None,
230        }
231    }
232
233    /// Convert to something that can be used with
234    /// [BTreeMap::range](std::collections::BTreeMap::range).
235    /// All versions contained in self, will be in the output,
236    /// but there may be versions in the output that are not contained in self.
237    /// Returns None if the range is empty.
238    pub fn bounding_range(&self) -> Option<(Bound<&V>, Bound<&V>)> {
239        self.segments.first().map(|(start, _)| {
240            let end = self
241                .segments
242                .last()
243                .expect("if there is a first element, there must be a last element");
244            (start.as_ref(), end.1.as_ref())
245        })
246    }
247
248    /// Returns true if self contains the specified value.
249    pub fn contains<Q>(&self, version: &Q) -> bool
250    where
251        V: Borrow<Q>,
252        Q: ?Sized + PartialOrd,
253    {
254        self.segments
255            .binary_search_by(|segment| {
256                // We have to reverse because we need the segment wrt to the version, while
257                // within bounds tells us the version wrt to the segment.
258                within_bounds(version, segment).reverse()
259            })
260            // An equal interval is one that contains the version
261            .is_ok()
262    }
263
264    /// Returns true if self contains the specified values.
265    ///
266    /// The `versions` iterator must be sorted.
267    /// Functionally equivalent to `versions.map(|v| self.contains(v))`.
268    /// Except it runs in `O(size_of_range + len_of_versions)` not `O(size_of_range * len_of_versions)`
269    pub fn contains_many<'s, I, BV>(&'s self, versions: I) -> impl Iterator<Item = bool> + 's
270    where
271        I: Iterator<Item = BV> + 's,
272        BV: Borrow<V> + 's,
273    {
274        #[cfg(debug_assertions)]
275        let mut last: Option<BV> = None;
276        versions.scan(0, move |i, v| {
277            #[cfg(debug_assertions)]
278            {
279                if let Some(l) = last.as_ref() {
280                    assert!(
281                        l.borrow() <= v.borrow(),
282                        "`contains_many` `versions` argument incorrectly sorted"
283                    );
284                }
285            }
286            while let Some(segment) = self.segments.get(*i) {
287                match within_bounds(v.borrow(), segment) {
288                    Ordering::Less => return Some(false),
289                    Ordering::Equal => return Some(true),
290                    Ordering::Greater => *i += 1,
291                }
292            }
293            #[cfg(debug_assertions)]
294            {
295                last = Some(v);
296            }
297            Some(false)
298        })
299    }
300
301    /// Construct a simple range from anything that impls [RangeBounds] like `v1..v2`.
302    pub fn from_range_bounds<R, IV>(bounds: R) -> Self
303    where
304        R: RangeBounds<IV>,
305        IV: Clone + Into<V>,
306    {
307        let start = match bounds.start_bound() {
308            Included(v) => Included(v.clone().into()),
309            Excluded(v) => Excluded(v.clone().into()),
310            Unbounded => Unbounded,
311        };
312        let end = match bounds.end_bound() {
313            Included(v) => Included(v.clone().into()),
314            Excluded(v) => Excluded(v.clone().into()),
315            Unbounded => Unbounded,
316        };
317        if valid_segment(&start, &end) {
318            Self {
319                segments: smallvec![(start, end)],
320            }
321        } else {
322            Self::empty()
323        }
324    }
325
326    /// See [`Ranges`] for the invariants checked.
327    fn check_invariants(self) -> Self {
328        if cfg!(debug_assertions) {
329            for p in self.segments.as_slice().windows(2) {
330                assert!(end_before_start_with_gap(&p[0].1, &p[1].0));
331            }
332            for (s, e) in self.segments.iter() {
333                assert!(valid_segment(s, e));
334            }
335        }
336        self
337    }
338}
339
340/// Implementing `PartialOrd` for start `Bound` of an interval.
341///
342/// Legend: `∞` is unbounded, `[1,2]` is `>=1,<=2`, `]1,2[` is `>1,<2`.
343///
344/// ```text
345/// left:   ∞-------]
346/// right:    [-----]
347/// left is smaller, since it starts earlier.
348///
349/// left:   [-----]
350/// right:  ]-----]
351/// left is smaller, since it starts earlier.
352/// ```
353fn cmp_bounds_start<V: PartialOrd>(left: Bound<&V>, right: Bound<&V>) -> Option<Ordering> {
354    Some(match (left, right) {
355        // left:   ∞-----
356        // right:  ∞-----
357        (Unbounded, Unbounded) => Ordering::Equal,
358        // left:     [---
359        // right:  ∞-----
360        (Included(_left), Unbounded) => Ordering::Greater,
361        // left:     ]---
362        // right:  ∞-----
363        (Excluded(_left), Unbounded) => Ordering::Greater,
364        // left:   ∞-----
365        // right:    [---
366        (Unbounded, Included(_right)) => Ordering::Less,
367        // left:   [----- OR [----- OR   [-----
368        // right:    [--- OR [----- OR [---
369        (Included(left), Included(right)) => left.partial_cmp(right)?,
370        (Excluded(left), Included(right)) => match left.partial_cmp(right)? {
371            // left:   ]-----
372            // right:    [---
373            Ordering::Less => Ordering::Less,
374            // left:   ]-----
375            // right:  [---
376            Ordering::Equal => Ordering::Greater,
377            // left:     ]---
378            // right:  [-----
379            Ordering::Greater => Ordering::Greater,
380        },
381        // left:   ∞-----
382        // right:    ]---
383        (Unbounded, Excluded(_right)) => Ordering::Less,
384        (Included(left), Excluded(right)) => match left.partial_cmp(right)? {
385            // left:   [-----
386            // right:    ]---
387            Ordering::Less => Ordering::Less,
388            // left:   [-----
389            // right:  ]---
390            Ordering::Equal => Ordering::Less,
391            // left:     [---
392            // right:  ]-----
393            Ordering::Greater => Ordering::Greater,
394        },
395        // left:   ]----- OR ]----- OR   ]---
396        // right:    ]--- OR ]----- OR ]-----
397        (Excluded(left), Excluded(right)) => left.partial_cmp(right)?,
398    })
399}
400
401/// Implementing `PartialOrd` for end `Bound` of an interval.
402///
403/// We flip the unbounded ranges from `-∞` to `∞`, while `V`-valued bounds checks remain the same.
404///
405/// Legend: `∞` is unbounded, `[1,2]` is `>=1,<=2`, `]1,2[` is `>1,<2`.
406///
407/// ```text
408/// left:   [--------∞
409/// right:  [-----]
410/// left is greater, since it starts earlier.
411///
412/// left:   [-----[
413/// right:  [-----]
414/// left is smaller, since it ends earlier.
415/// ```
416fn cmp_bounds_end<V: PartialOrd>(left: Bound<&V>, right: Bound<&V>) -> Option<Ordering> {
417    Some(match (left, right) {
418        // left:   -----∞
419        // right:  -----∞
420        (Unbounded, Unbounded) => Ordering::Equal,
421        // left:   ---]
422        // right:  -----∞
423        (Included(_left), Unbounded) => Ordering::Less,
424        // left:   ---[
425        // right:  -----∞
426        (Excluded(_left), Unbounded) => Ordering::Less,
427        // left:  -----∞
428        // right: ---]
429        (Unbounded, Included(_right)) => Ordering::Greater,
430        // left:   -----] OR -----] OR ---]
431        // right:    ---] OR -----] OR -----]
432        (Included(left), Included(right)) => left.partial_cmp(right)?,
433        (Excluded(left), Included(right)) => match left.partial_cmp(right)? {
434            // left:   ---[
435            // right:  -----]
436            Ordering::Less => Ordering::Less,
437            // left:   -----[
438            // right:  -----]
439            Ordering::Equal => Ordering::Less,
440            // left:   -----[
441            // right:  ---]
442            Ordering::Greater => Ordering::Greater,
443        },
444        (Unbounded, Excluded(_right)) => Ordering::Greater,
445        (Included(left), Excluded(right)) => match left.partial_cmp(right)? {
446            // left:   ---]
447            // right:  -----[
448            Ordering::Less => Ordering::Less,
449            // left:   -----]
450            // right:  -----[
451            Ordering::Equal => Ordering::Greater,
452            // left:   -----]
453            // right:  ---[
454            Ordering::Greater => Ordering::Greater,
455        },
456        // left:   -----[ OR -----[ OR ---[
457        // right:  ---[   OR -----[ OR -----[
458        (Excluded(left), Excluded(right)) => left.partial_cmp(right)?,
459    })
460}
461
462impl<V: PartialOrd> PartialOrd for Ranges<V> {
463    /// A simple ordering scheme where we zip the segments and compare all bounds in order. If all
464    /// bounds are equal, the longer range is considered greater. (And if all zipped bounds are
465    /// equal and we have the same number of segments, the ranges are equal).
466    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
467        for (left, right) in self.segments.iter().zip(other.segments.iter()) {
468            let start_cmp = cmp_bounds_start(left.start_bound(), right.start_bound())?;
469            if start_cmp != Ordering::Equal {
470                return Some(start_cmp);
471            }
472            let end_cmp = cmp_bounds_end(left.end_bound(), right.end_bound())?;
473            if end_cmp != Ordering::Equal {
474                return Some(end_cmp);
475            }
476        }
477        Some(self.segments.len().cmp(&other.segments.len()))
478    }
479}
480
481impl<V: Ord> Ord for Ranges<V> {
482    fn cmp(&self, other: &Self) -> Ordering {
483        self.partial_cmp(other)
484            .expect("PartialOrd must be `Some(Ordering)` for types that implement `Ord`")
485    }
486}
487
488/// The ordering of the version wrt to the interval.
489/// ```text
490///      |-------|
491///   ^      ^      ^
492///   less   equal  greater
493/// ```
494fn within_bounds<Q, V>(version: &Q, segment: &Interval<V>) -> Ordering
495where
496    V: Borrow<Q>,
497    Q: ?Sized + PartialOrd,
498{
499    let below_lower_bound = match segment {
500        (Excluded(start), _) => version <= start.borrow(),
501        (Included(start), _) => version < start.borrow(),
502        (Unbounded, _) => false,
503    };
504    if below_lower_bound {
505        return Ordering::Less;
506    }
507    let below_upper_bound = match segment {
508        (_, Unbounded) => true,
509        (_, Included(end)) => version <= end.borrow(),
510        (_, Excluded(end)) => version < end.borrow(),
511    };
512    if below_upper_bound {
513        return Ordering::Equal;
514    }
515    Ordering::Greater
516}
517
518/// A valid segment is one where at least one version fits between start and end
519fn valid_segment<T: PartialOrd>(start: &Bound<T>, end: &Bound<T>) -> bool {
520    match (start, end) {
521        // Singleton interval are allowed
522        (Included(s), Included(e)) => s <= e,
523        (Included(s), Excluded(e)) => s < e,
524        (Excluded(s), Included(e)) => s < e,
525        (Excluded(s), Excluded(e)) => s < e,
526        (Unbounded, _) | (_, Unbounded) => true,
527    }
528}
529
530/// The end of one interval is before the start of the next one, so they can't be concatenated
531/// into a single interval. The `union` method calling with both intervals and then the intervals
532/// switched. If either is true, the intervals are separate in the union and if both are false, they
533/// are merged.
534/// ```text
535/// True for these two:
536///  |----|
537///                |-----|
538///       ^ end    ^ start
539/// False for these two:
540///  |----|
541///     |-----|
542/// Here it depends: If they both exclude the position they share, there is a version in between
543/// them that blocks concatenation
544///  |----|
545///       |-----|
546/// ```
547fn end_before_start_with_gap<V: PartialOrd>(end: &Bound<V>, start: &Bound<V>) -> bool {
548    match (end, start) {
549        (_, Unbounded) => false,
550        (Unbounded, _) => false,
551        (Included(left), Included(right)) => left < right,
552        (Included(left), Excluded(right)) => left < right,
553        (Excluded(left), Included(right)) => left < right,
554        (Excluded(left), Excluded(right)) => left <= right,
555    }
556}
557
558fn left_start_is_smaller<V: PartialOrd>(left: Bound<V>, right: Bound<V>) -> bool {
559    match (left, right) {
560        (Unbounded, _) => true,
561        (_, Unbounded) => false,
562        (Included(l), Included(r)) => l <= r,
563        (Excluded(l), Excluded(r)) => l <= r,
564        (Included(l), Excluded(r)) => l <= r,
565        (Excluded(l), Included(r)) => l < r,
566    }
567}
568
569fn left_end_is_smaller<V: PartialOrd>(left: Bound<V>, right: Bound<V>) -> bool {
570    match (left, right) {
571        (_, Unbounded) => true,
572        (Unbounded, _) => false,
573        (Included(l), Included(r)) => l <= r,
574        (Excluded(l), Excluded(r)) => l <= r,
575        (Excluded(l), Included(r)) => l <= r,
576        (Included(l), Excluded(r)) => l < r,
577    }
578}
579
580/// Group adjacent versions locations.
581///
582/// ```text
583/// [None, 3, 6, 7, None] -> [(3, 7)]
584/// [3, 6, 7, None] -> [(None, 7)]
585/// [3, 6, 7] -> [(None, None)]
586/// [None, 1, 4, 7, None, None, None, 8, None, 9] -> [(1, 7), (8, 8), (9, None)]
587/// ```
588fn group_adjacent_locations(
589    mut locations: impl Iterator<Item = Option<usize>>,
590) -> impl Iterator<Item = (Option<usize>, Option<usize>)> {
591    // If the first version matched, then the lower bound of that segment is not needed
592    let mut seg = locations.next().flatten().map(|ver| (None, Some(ver)));
593    std::iter::from_fn(move || {
594        for ver in locations.by_ref() {
595            if let Some(ver) = ver {
596                // As long as were still matching versions, we keep merging into the currently matching segment
597                seg = Some((seg.map_or(Some(ver), |(s, _)| s), Some(ver)));
598            } else {
599                // If we have found a version that doesn't match, then right the merge segment and prepare for a new one.
600                if seg.is_some() {
601                    return seg.take();
602                }
603            }
604        }
605        // If the last version matched, then write out the merged segment but the upper bound is not needed.
606        seg.take().map(|(s, _)| (s, None))
607    })
608}
609
610impl<V: Ord + Clone> Ranges<V> {
611    /// Computes the union of this `Ranges` and another.
612    pub fn union(&self, other: &Self) -> Self {
613        let mut output = SmallVec::new();
614        let mut accumulator: Option<(&Bound<_>, &Bound<_>)> = None;
615        let mut left_iter = self.segments.iter().peekable();
616        let mut right_iter = other.segments.iter().peekable();
617        loop {
618            let smaller_interval = match (left_iter.peek(), right_iter.peek()) {
619                (Some((left_start, left_end)), Some((right_start, right_end))) => {
620                    if left_start_is_smaller(left_start.as_ref(), right_start.as_ref()) {
621                        left_iter.next();
622                        (left_start, left_end)
623                    } else {
624                        right_iter.next();
625                        (right_start, right_end)
626                    }
627                }
628                (Some((left_start, left_end)), None) => {
629                    left_iter.next();
630                    (left_start, left_end)
631                }
632                (None, Some((right_start, right_end))) => {
633                    right_iter.next();
634                    (right_start, right_end)
635                }
636                (None, None) => break,
637            };
638
639            if let Some(accumulator_) = accumulator {
640                if end_before_start_with_gap(accumulator_.1, smaller_interval.0) {
641                    output.push((accumulator_.0.clone(), accumulator_.1.clone()));
642                    accumulator = Some(smaller_interval);
643                } else {
644                    let accumulator_end = match (accumulator_.1, smaller_interval.1) {
645                        (_, Unbounded) | (Unbounded, _) => &Unbounded,
646                        (Included(l), Excluded(r) | Included(r)) if l == r => accumulator_.1,
647                        (Included(l) | Excluded(l), Included(r) | Excluded(r)) => {
648                            if l > r {
649                                accumulator_.1
650                            } else {
651                                smaller_interval.1
652                            }
653                        }
654                    };
655                    accumulator = Some((accumulator_.0, accumulator_end));
656                }
657            } else {
658                accumulator = Some(smaller_interval)
659            }
660        }
661
662        if let Some(accumulator) = accumulator {
663            output.push((accumulator.0.clone(), accumulator.1.clone()));
664        }
665
666        Self { segments: output }.check_invariants()
667    }
668
669    /// Computes the intersection of two sets of versions.
670    pub fn intersection(&self, other: &Self) -> Self {
671        let mut output = SmallVec::new();
672        let mut left_iter = self.segments.iter().peekable();
673        let mut right_iter = other.segments.iter().peekable();
674        // By the definition of intersection any point that is matched by the output
675        // must have a segment in each of the inputs that it matches.
676        // Therefore, every segment in the output must be the intersection of a segment from each of the inputs.
677        // It would be correct to do the "O(n^2)" thing, by computing the intersection of every segment from one input
678        // with every segment of the other input, and sorting the result.
679        // We can avoid the sorting by generating our candidate segments with an increasing `end` value.
680        while let Some(((left_start, left_end), (right_start, right_end))) =
681            left_iter.peek().zip(right_iter.peek())
682        {
683            // The next smallest `end` value is going to come from one of the inputs.
684            let left_end_is_smaller = left_end_is_smaller(left_end.as_ref(), right_end.as_ref());
685            // Now that we are processing `end` we will never have to process any segment smaller than that.
686            // We can ensure that the input that `end` came from is larger than `end` by advancing it one step.
687            // `end` is the smaller available input, so we know the other input is already larger than `end`.
688            // Note: We can call `other_iter.next_if( == end)`, but the ends lining up is rare enough that
689            // it does not end up being faster in practice.
690            let (other_start, end) = if left_end_is_smaller {
691                left_iter.next();
692                (right_start, left_end)
693            } else {
694                right_iter.next();
695                (left_start, right_end)
696            };
697            // `start` will either come from the input `end` came from or the other input, whichever one is larger.
698            // The intersection is invalid if `start` > `end`.
699            // But, we already know that the segments in our input are valid.
700            // So we do not need to check if the `start` from the input `end` came from is smaller than `end`.
701            // If the `other_start` is larger than end, then the intersection will be invalid.
702            if !valid_segment(other_start, end) {
703                // Note: We can call `this_iter.next_if(!valid_segment(other_start, this_end))` in a loop.
704                // But the checks make it slower for the benchmarked inputs.
705                continue;
706            }
707            let start = match (left_start, right_start) {
708                (Included(l), Included(r)) => Included(std::cmp::max(l, r)),
709                (Excluded(l), Excluded(r)) => Excluded(std::cmp::max(l, r)),
710
711                (Included(i), Excluded(e)) | (Excluded(e), Included(i)) => {
712                    if i <= e {
713                        Excluded(e)
714                    } else {
715                        Included(i)
716                    }
717                }
718                (s, Unbounded) | (Unbounded, s) => s.as_ref(),
719            };
720            // Now we clone and push a new segment.
721            // By dealing with references until now we ensure that NO cloning happens when we reject the segment.
722            output.push((start.cloned(), end.clone()))
723        }
724
725        Self { segments: output }.check_invariants()
726    }
727
728    /// Return true if there can be no `V` so that `V` is contained in both `self` and `other`.
729    ///
730    /// Note that we don't know that set of all existing `V`s here, so we only check if the segments
731    /// are disjoint, not if no version is contained in both.
732    pub fn is_disjoint(&self, other: &Self) -> bool {
733        // The operation is symmetric
734        let mut left_iter = self.segments.iter().peekable();
735        let mut right_iter = other.segments.iter().peekable();
736
737        while let Some((left, right)) = left_iter.peek().zip(right_iter.peek()) {
738            if !valid_segment(&right.start_bound(), &left.end_bound()) {
739                left_iter.next();
740            } else if !valid_segment(&left.start_bound(), &right.end_bound()) {
741                right_iter.next();
742            } else {
743                return false;
744            }
745        }
746
747        // The remaining element(s) can't intersect anymore
748        true
749    }
750
751    /// Classifies `self` as a subset of, disjoint from, or partially overlapping with `other`.
752    ///
753    /// This combines [`Self::subset_of`] and [`Self::is_disjoint`] into a single traversal.
754    /// An empty `self` is classified as [`SetRelation::Subset`].
755    pub fn relation(&self, other: &Self) -> SetRelation {
756        // Equality is common for long accumulated ranges during PubGrub conflict resolution.
757        if self.segments.len() > 1
758            && self.segments.len() == other.segments.len()
759            && self.segments == other.segments
760        {
761            return SetRelation::Subset;
762        }
763
764        let mut other_iter = other.segments.iter().peekable();
765        let mut is_subset = true;
766        let mut overlaps = false;
767
768        for subset_elem in &self.segments {
769            while other_iter.peek().is_some_and(|containing_elem| {
770                !valid_segment(&subset_elem.start_bound(), &containing_elem.end_bound())
771            }) {
772                other_iter.next();
773            }
774
775            let Some(containing_elem) = other_iter.peek() else {
776                is_subset = false;
777                break;
778            };
779
780            if !valid_segment(&containing_elem.start_bound(), &subset_elem.end_bound()) {
781                is_subset = false;
782                continue;
783            }
784
785            overlaps = true;
786            if !left_start_is_smaller(containing_elem.start_bound(), subset_elem.start_bound())
787                || !left_end_is_smaller(subset_elem.end_bound(), containing_elem.end_bound())
788            {
789                is_subset = false;
790            }
791        }
792
793        if is_subset {
794            SetRelation::Subset
795        } else if overlaps {
796            SetRelation::Overlapping
797        } else {
798            SetRelation::Disjoint
799        }
800    }
801
802    /// Return true if any `V` that is contained in `self` is also contained in `other`.
803    ///
804    /// Note that we don't know that set of all existing `V`s here, so we only check if all
805    /// segments `self` are contained in a segment of `other`.
806    pub fn subset_of(&self, other: &Self) -> bool {
807        // Equality is common for long accumulated ranges during PubGrub conflict resolution.
808        if self.segments.len() > 1
809            && self.segments.len() == other.segments.len()
810            && self.segments == other.segments
811        {
812            return true;
813        }
814
815        let mut containing_iter = other.segments.iter();
816        let mut subset_iter = self.segments.iter();
817        let Some(mut containing_elem) = containing_iter.next() else {
818            // As long as we have subset elements, we need containing elements
819            return subset_iter.next().is_none();
820        };
821
822        for subset_elem in subset_iter {
823            // Check if the current containing element ends before the subset element.
824            // There needs to be another containing element for our subset element in this case.
825            while !valid_segment(&subset_elem.start_bound(), &containing_elem.end_bound()) {
826                if let Some(containing_elem_) = containing_iter.next() {
827                    containing_elem = containing_elem_;
828                } else {
829                    return false;
830                };
831            }
832
833            let start_contained =
834                left_start_is_smaller(containing_elem.start_bound(), subset_elem.start_bound());
835
836            if !start_contained {
837                // The start element is not contained
838                return false;
839            }
840
841            let end_contained =
842                left_end_is_smaller(subset_elem.end_bound(), containing_elem.end_bound());
843
844            if !end_contained {
845                // The end element is not contained
846                return false;
847            }
848        }
849
850        true
851    }
852
853    /// Returns a simpler representation that contains the same versions.
854    ///
855    /// For every one of the Versions provided in versions the existing range and the simplified range will agree on whether it is contained.
856    /// The simplified version may include or exclude versions that are not in versions as the implementation wishes.
857    ///
858    /// If none of the versions are contained in the original than the range will be returned unmodified.
859    /// If the range includes a single version, it will be returned unmodified.
860    /// If all the versions are contained in the original than the range will be simplified to `full`.
861    ///
862    /// If the given versions are not sorted the correctness of this function is not guaranteed.
863    pub fn simplify<'s, I, BV>(&self, versions: I) -> Self
864    where
865        I: Iterator<Item = BV> + 's,
866        BV: Borrow<V> + 's,
867    {
868        // Do not simplify singletons
869        if self.as_singleton().is_some() {
870            return self.clone();
871        }
872
873        #[cfg(debug_assertions)]
874        let mut last: Option<BV> = None;
875        // Return the segment index in the range for each version in the range, None otherwise
876        let version_locations = versions.scan(0, move |i, v| {
877            #[cfg(debug_assertions)]
878            {
879                if let Some(l) = last.as_ref() {
880                    assert!(
881                        l.borrow() <= v.borrow(),
882                        "`simplify` `versions` argument incorrectly sorted"
883                    );
884                }
885            }
886            while let Some(segment) = self.segments.get(*i) {
887                match within_bounds(v.borrow(), segment) {
888                    Ordering::Less => return Some(None),
889                    Ordering::Equal => return Some(Some(*i)),
890                    Ordering::Greater => *i += 1,
891                }
892            }
893            #[cfg(debug_assertions)]
894            {
895                last = Some(v);
896            }
897            Some(None)
898        });
899        let mut kept_segments = group_adjacent_locations(version_locations).peekable();
900
901        // Do not return null sets
902        if kept_segments.peek().is_none() {
903            return self.clone();
904        }
905
906        self.keep_segments(kept_segments)
907    }
908
909    /// Create a new range with a subset of segments at given location bounds.
910    ///
911    /// Each new segment is constructed from a pair of segments, taking the
912    /// start of the first and the end of the second.
913    fn keep_segments(
914        &self,
915        kept_segments: impl Iterator<Item = (Option<usize>, Option<usize>)>,
916    ) -> Ranges<V> {
917        let mut segments = SmallVec::new();
918        for (s, e) in kept_segments {
919            segments.push((
920                s.map_or(Unbounded, |s| self.segments[s].0.clone()),
921                e.map_or(Unbounded, |e| self.segments[e].1.clone()),
922            ));
923        }
924        Self { segments }.check_invariants()
925    }
926
927    /// Iterate over the parts of the range.
928    pub fn iter(&self) -> impl DoubleEndedIterator<Item = (Bound<&V>, Bound<&V>)> {
929        self.segments
930            .iter()
931            .map(|(start, end)| (start.as_ref(), end.as_ref()))
932    }
933}
934
935// Newtype to avoid leaking our internal representation.
936pub struct RangesIter<V>(smallvec::IntoIter<[Interval<V>; 1]>);
937
938impl<V> Iterator for RangesIter<V> {
939    type Item = Interval<V>;
940
941    fn next(&mut self) -> Option<Self::Item> {
942        self.0.next()
943    }
944
945    fn size_hint(&self) -> (usize, Option<usize>) {
946        (self.0.len(), Some(self.0.len()))
947    }
948}
949
950impl<V> ExactSizeIterator for RangesIter<V> {}
951
952impl<V> DoubleEndedIterator for RangesIter<V> {
953    fn next_back(&mut self) -> Option<Self::Item> {
954        self.0.next_back()
955    }
956}
957
958impl<V> IntoIterator for Ranges<V> {
959    type Item = (Bound<V>, Bound<V>);
960    // Newtype to avoid leaking our internal representation.
961    type IntoIter = RangesIter<V>;
962
963    fn into_iter(self) -> Self::IntoIter {
964        RangesIter(self.segments.into_iter())
965    }
966}
967
968impl<V: Ord> FromIterator<(Bound<V>, Bound<V>)> for Ranges<V> {
969    /// Constructor from arbitrary, unsorted and potentially overlapping ranges.
970    ///
971    /// This is equivalent, but faster, to computing the [`Ranges::union`] of the
972    /// [`Ranges::from_range_bounds`] of each segment.
973    fn from_iter<T: IntoIterator<Item = (Bound<V>, Bound<V>)>>(iter: T) -> Self {
974        // We have three constraints we need to fulfil:
975        // 1. The segments are sorted, from lowest to highest (through `Ord`): By sorting.
976        // 2. Each segment contains at least one version (start < end): By skipping invalid
977        //    segments.
978        // 3. There is at least one version between two segments: By merging overlapping elements.
979        //
980        // Technically, the implementation has a O(n²) worst case complexity since we're inserting
981        // and removing. This has two motivations: One is that we don't have any performance
982        // critical usages of this method as of this writing, so we have no real world benchmark.
983        // The other is that we get the elements from an iterator, so to avoid moving elements
984        // around we would first need to build a different, sorted collection with extra
985        // allocation(s), before we could build our real segments. --Konsti
986
987        // For this implementation, we choose to only build a single smallvec and insert or remove
988        // in it, instead of e.g. collecting the segments into a sorted datastructure first and then
989        // construction the second smallvec without shifting.
990        let mut segments: SmallVec<[Interval<V>; 1]> = SmallVec::new();
991
992        for segment in iter {
993            if !valid_segment(&segment.start_bound(), &segment.end_bound()) {
994                continue;
995            }
996            // Find where to insert the new segment
997            let insertion_point = segments.partition_point(|elem: &Interval<V>| {
998                cmp_bounds_start(elem.start_bound(), segment.start_bound())
999                    .unwrap()
1000                    .is_lt()
1001            });
1002            // Is it overlapping with the previous segment?
1003            let previous_overlapping = insertion_point > 0
1004                && !end_before_start_with_gap(
1005                    &segments[insertion_point - 1].end_bound(),
1006                    &segment.start_bound(),
1007                );
1008
1009            // Is it overlapping with the following segment? We'll check if there's more than one
1010            // overlap later.
1011            let next_overlapping = insertion_point < segments.len()
1012                && !end_before_start_with_gap(
1013                    &segment.end_bound(),
1014                    &segments[insertion_point].start_bound(),
1015                );
1016
1017            match (previous_overlapping, next_overlapping) {
1018                (true, true) => {
1019                    // previous:  |------|
1020                    // segment:       |------|
1021                    // following:          |------|
1022                    // final:     |---------------|
1023                    //
1024                    // OR
1025                    //
1026                    // previous:  |------|
1027                    // segment:       |-----------|
1028                    // following:          |----|
1029                    // final:     |---------------|
1030                    //
1031                    // OR
1032                    //
1033                    // previous:  |------|
1034                    // segment:       |----------------|
1035                    // following:          |----|   |------|
1036                    // final:     |------------------------|
1037                    // We merge all three segments into one, which is effectively removing one of
1038                    // two previously inserted and changing the bounds on the other.
1039
1040                    // Remove all elements covered by the final element
1041                    let mut following = segments.remove(insertion_point);
1042                    while insertion_point < segments.len()
1043                        && !end_before_start_with_gap(
1044                            &segment.end_bound(),
1045                            &segments[insertion_point].start_bound(),
1046                        )
1047                    {
1048                        following = segments.remove(insertion_point);
1049                    }
1050
1051                    // Set end to max(segment.end, <last overlapping segment>.end)
1052                    if cmp_bounds_end(segment.end_bound(), following.end_bound())
1053                        .unwrap()
1054                        .is_lt()
1055                    {
1056                        segments[insertion_point - 1].1 = following.1;
1057                    } else {
1058                        segments[insertion_point - 1].1 = segment.1;
1059                    }
1060                }
1061                (true, false) => {
1062                    // previous:  |------|
1063                    // segment:       |------|
1064                    // following:                |------|
1065                    //
1066                    // OR
1067                    //
1068                    // previous:  |----------|
1069                    // segment:       |---|
1070                    // following:                |------|
1071                    //
1072                    // final:     |----------|   |------|
1073                    // We can reuse the existing element by extending it.
1074
1075                    // Set end to max(segment.end, <previous>.end)
1076                    if cmp_bounds_end(
1077                        segments[insertion_point - 1].end_bound(),
1078                        segment.end_bound(),
1079                    )
1080                    .unwrap()
1081                    .is_lt()
1082                    {
1083                        segments[insertion_point - 1].1 = segment.1;
1084                    }
1085                }
1086                (false, true) => {
1087                    // previous:  |------|
1088                    // segment:             |------|
1089                    // following:               |------|
1090                    // final:    |------|   |----------|
1091                    //
1092                    // OR
1093                    //
1094                    // previous:  |------|
1095                    // segment:             |----------|
1096                    // following:               |---|
1097                    // final:    |------|   |----------|
1098                    //
1099                    // OR
1100                    //
1101                    // previous:  |------|
1102                    // segment:             |------------|
1103                    // following:               |---|  |------|
1104                    //
1105                    // final:    |------|   |-----------------|
1106                    // We can reuse the existing element by extending it.
1107
1108                    // Remove all fully covered segments so the next element is the last one that
1109                    // overlaps.
1110                    while insertion_point + 1 < segments.len()
1111                        && !end_before_start_with_gap(
1112                            &segment.end_bound(),
1113                            &segments[insertion_point + 1].start_bound(),
1114                        )
1115                    {
1116                        // We know that the one after also overlaps, so we can drop the current
1117                        // following.
1118                        segments.remove(insertion_point);
1119                    }
1120
1121                    // Set end to max(segment.end, <last overlapping segment>.end)
1122                    if cmp_bounds_end(segments[insertion_point].end_bound(), segment.end_bound())
1123                        .unwrap()
1124                        .is_lt()
1125                    {
1126                        segments[insertion_point].1 = segment.1;
1127                    }
1128                    segments[insertion_point].0 = segment.0;
1129                }
1130                (false, false) => {
1131                    // previous:  |------|
1132                    // segment:             |------|
1133                    // following:                      |------|
1134                    //
1135                    // final:    |------|   |------|   |------|
1136
1137                    // This line is O(n), which makes the algorithm O(n²), but it should be good
1138                    // enough for now.
1139                    segments.insert(insertion_point, segment);
1140                }
1141            }
1142        }
1143
1144        Self { segments }.check_invariants()
1145    }
1146}
1147
1148// REPORT ######################################################################
1149
1150impl<V: Display + Eq> Display for Ranges<V> {
1151    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1152        if self.segments.is_empty() {
1153            write!(f, "∅")?;
1154        } else {
1155            for (idx, segment) in self.segments.iter().enumerate() {
1156                if idx > 0 {
1157                    write!(f, " | ")?;
1158                }
1159                match segment {
1160                    (Unbounded, Unbounded) => write!(f, "*")?,
1161                    (Unbounded, Included(v)) => write!(f, "<={v}")?,
1162                    (Unbounded, Excluded(v)) => write!(f, "<{v}")?,
1163                    (Included(v), Unbounded) => write!(f, ">={v}")?,
1164                    (Included(v), Included(b)) => {
1165                        if v == b {
1166                            write!(f, "=={v}")?
1167                        } else {
1168                            write!(f, ">={v}, <={b}")?
1169                        }
1170                    }
1171                    (Included(v), Excluded(b)) => write!(f, ">={v}, <{b}")?,
1172                    (Excluded(v), Unbounded) => write!(f, ">{v}")?,
1173                    (Excluded(v), Included(b)) => write!(f, ">{v}, <={b}")?,
1174                    (Excluded(v), Excluded(b)) => write!(f, ">{v}, <{b}")?,
1175                };
1176            }
1177        }
1178        Ok(())
1179    }
1180}
1181
1182// SERIALIZATION ###############################################################
1183
1184#[cfg(feature = "serde")]
1185impl<'de, V: serde::Deserialize<'de>> serde::Deserialize<'de> for Ranges<V> {
1186    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
1187        // This enables conversion from the "old" discrete implementation of `Ranges` to the new
1188        // bounded one.
1189        //
1190        // Serialization is always performed in the new format.
1191        #[derive(serde::Deserialize)]
1192        #[serde(untagged)]
1193        enum EitherInterval<V> {
1194            B(Bound<V>, Bound<V>),
1195            D(V, Option<V>),
1196        }
1197
1198        let bounds: SmallVec<[EitherInterval<V>; 2]> =
1199            serde::Deserialize::deserialize(deserializer)?;
1200
1201        let mut segments = SmallVec::new();
1202        for i in bounds {
1203            match i {
1204                EitherInterval::B(l, r) => segments.push((l, r)),
1205                EitherInterval::D(l, Some(r)) => segments.push((Included(l), Excluded(r))),
1206                EitherInterval::D(l, None) => segments.push((Included(l), Unbounded)),
1207            }
1208        }
1209
1210        Ok(Ranges { segments })
1211    }
1212}
1213
1214/// Generate version sets from a random vector of deltas between randomly inclusive or exclusive
1215/// bounds.
1216#[cfg(any(feature = "proptest", test))]
1217pub fn proptest_strategy() -> impl Strategy<Value = Ranges<u32>> {
1218    (
1219        any::<bool>(),
1220        prop::collection::vec(any::<(u32, bool)>(), 0..10),
1221    )
1222        .prop_map(|(start_unbounded, deltas)| {
1223            let mut start = if start_unbounded {
1224                Some(Unbounded)
1225            } else {
1226                None
1227            };
1228            let mut largest: u32 = 0;
1229            let mut last_bound_was_inclusive = false;
1230            let mut segments = SmallVec::new();
1231            for (delta, inclusive) in deltas {
1232                // Add the offset to the current bound
1233                largest = match largest.checked_add(delta) {
1234                    Some(s) => s,
1235                    None => {
1236                        // Skip this offset, if it would result in a too large bound.
1237                        continue;
1238                    }
1239                };
1240
1241                let current_bound = if inclusive {
1242                    Included(largest)
1243                } else {
1244                    Excluded(largest)
1245                };
1246
1247                // If we already have a start bound, the next offset defines the complete range.
1248                // If we don't have a start bound, we have to generate one.
1249                if let Some(start_bound) = start.take() {
1250                    // If the delta from the start bound is 0, the only authorized configuration is
1251                    // Included(x), Included(x)
1252                    if delta == 0 && !(matches!(start_bound, Included(_)) && inclusive) {
1253                        start = Some(start_bound);
1254                        continue;
1255                    }
1256                    last_bound_was_inclusive = inclusive;
1257                    segments.push((start_bound, current_bound));
1258                } else {
1259                    // If the delta from the end bound of the last range is 0 and
1260                    // any of the last ending or current starting bound is inclusive,
1261                    // we skip the delta because they basically overlap.
1262                    if delta == 0 && (last_bound_was_inclusive || inclusive) {
1263                        continue;
1264                    }
1265                    start = Some(current_bound);
1266                }
1267            }
1268
1269            // If we still have a start bound, but didn't have enough deltas to complete another
1270            // segment, we add an unbounded upperbound.
1271            if let Some(start_bound) = start {
1272                segments.push((start_bound, Unbounded));
1273            }
1274
1275            Ranges { segments }.check_invariants()
1276        })
1277}
1278
1279#[cfg(test)]
1280pub mod tests {
1281    use proptest::prelude::*;
1282
1283    use super::*;
1284
1285    fn version_strat() -> impl Strategy<Value = u32> {
1286        any::<u32>()
1287    }
1288
1289    proptest! {
1290
1291        // Testing serde ----------------------------------
1292
1293        #[cfg(feature = "serde")]
1294        #[test]
1295        fn serde_round_trip(range in proptest_strategy()) {
1296            let s = ron::ser::to_string(&range).unwrap();
1297            let r = ron::de::from_str(&s).unwrap();
1298            assert_eq!(range, r);
1299        }
1300
1301        // Testing negate ----------------------------------
1302
1303        #[test]
1304        fn negate_is_different(range in proptest_strategy()) {
1305            assert_ne!(range.complement(), range);
1306        }
1307
1308        #[test]
1309        fn double_negate_is_identity(range in proptest_strategy()) {
1310            assert_eq!(range.complement().complement(), range);
1311        }
1312
1313        #[test]
1314        fn negate_contains_opposite(range in proptest_strategy(), version in version_strat()) {
1315            assert_ne!(range.contains(&version), range.complement().contains(&version));
1316        }
1317
1318        // Testing intersection ----------------------------
1319
1320        #[test]
1321        fn intersection_is_symmetric(r1 in proptest_strategy(), r2 in proptest_strategy()) {
1322            assert_eq!(r1.intersection(&r2), r2.intersection(&r1));
1323        }
1324
1325        #[test]
1326        fn intersection_with_any_is_identity(range in proptest_strategy()) {
1327            assert_eq!(Ranges::full().intersection(&range), range);
1328        }
1329
1330        #[test]
1331        fn intersection_with_none_is_none(range in proptest_strategy()) {
1332            assert_eq!(Ranges::empty().intersection(&range), Ranges::empty());
1333        }
1334
1335        #[test]
1336        fn intersection_is_idempotent(r1 in proptest_strategy(), r2 in proptest_strategy()) {
1337            assert_eq!(r1.intersection(&r2).intersection(&r2), r1.intersection(&r2));
1338        }
1339
1340        #[test]
1341        fn intersection_is_associative(r1 in proptest_strategy(), r2 in proptest_strategy(), r3 in proptest_strategy()) {
1342            assert_eq!(r1.intersection(&r2).intersection(&r3), r1.intersection(&r2.intersection(&r3)));
1343        }
1344
1345        #[test]
1346        fn intesection_of_complements_is_none(range in proptest_strategy()) {
1347            assert_eq!(range.complement().intersection(&range), Ranges::empty());
1348        }
1349
1350        #[test]
1351        fn intesection_contains_both(r1 in proptest_strategy(), r2 in proptest_strategy(), version in version_strat()) {
1352            assert_eq!(r1.intersection(&r2).contains(&version), r1.contains(&version) && r2.contains(&version));
1353        }
1354
1355        // Testing union -----------------------------------
1356
1357        #[test]
1358        fn union_of_complements_is_any(range in proptest_strategy()) {
1359            assert_eq!(range.complement().union(&range), Ranges::full());
1360        }
1361
1362        #[test]
1363        fn union_contains_either(r1 in proptest_strategy(), r2 in proptest_strategy(), version in version_strat()) {
1364            assert_eq!(r1.union(&r2).contains(&version), r1.contains(&version) || r2.contains(&version));
1365        }
1366
1367        #[test]
1368        fn is_disjoint_through_intersection(r1 in proptest_strategy(), r2 in proptest_strategy()) {
1369            let disjoint_def = r1.intersection(&r2) == Ranges::empty();
1370            assert_eq!(r1.is_disjoint(&r2), disjoint_def);
1371        }
1372
1373        #[test]
1374        fn subset_of_through_intersection(r1 in proptest_strategy(), r2 in proptest_strategy()) {
1375            let disjoint_def = r1.intersection(&r2) == r1;
1376            assert_eq!(r1.subset_of(&r2), disjoint_def);
1377        }
1378
1379        #[test]
1380        fn relation_through_subset_and_disjoint(r1 in proptest_strategy(), r2 in proptest_strategy()) {
1381            let relation_def = if r1.subset_of(&r2) {
1382                SetRelation::Subset
1383            } else if r1.is_disjoint(&r2) {
1384                SetRelation::Disjoint
1385            } else {
1386                SetRelation::Overlapping
1387            };
1388            assert_eq!(r1.relation(&r2), relation_def);
1389        }
1390
1391        #[test]
1392        fn union_through_intersection(r1 in proptest_strategy(), r2 in proptest_strategy()) {
1393            let union_def = r1
1394                .complement()
1395                .intersection(&r2.complement())
1396                .complement()
1397                .check_invariants();
1398            assert_eq!(r1.union(&r2), union_def);
1399        }
1400
1401        // Testing contains --------------------------------
1402
1403        #[test]
1404        fn always_contains_exact(version in version_strat()) {
1405            assert!(Ranges::<u32>::singleton(version).contains(&version));
1406        }
1407
1408        #[test]
1409        fn contains_negation(range in proptest_strategy(), version in version_strat()) {
1410            assert_ne!(range.contains(&version), range.complement().contains(&version));
1411        }
1412
1413        #[test]
1414        fn contains_intersection(range in proptest_strategy(), version in version_strat()) {
1415            assert_eq!(range.contains(&version), range.intersection(&Ranges::singleton(version)) != Ranges::empty());
1416        }
1417
1418        #[test]
1419        fn contains_bounding_range(range in proptest_strategy(), version in version_strat()) {
1420            if range.contains(&version) {
1421                assert!(range.bounding_range().map(|b| b.contains(&version)).unwrap_or(false));
1422            }
1423        }
1424
1425        #[test]
1426        fn from_range_bounds(range in any::<(Bound<u32>, Bound<u32>)>(), version in version_strat()) {
1427            let rv: Ranges<_> = Ranges::<u32>::from_range_bounds(range);
1428            assert_eq!(range.contains(&version), rv.contains(&version));
1429        }
1430
1431        #[test]
1432        fn from_range_bounds_round_trip(range in any::<(Bound<u32>, Bound<u32>)>()) {
1433            let rv: Ranges<u32> = Ranges::from_range_bounds(range);
1434            let rv2: Ranges<u32> = rv.bounding_range().map(Ranges::from_range_bounds::<_, u32>).unwrap_or_else(Ranges::empty);
1435            assert_eq!(rv, rv2);
1436        }
1437
1438        #[test]
1439        fn contains(range in proptest_strategy(), versions in proptest::collection::vec(version_strat(), ..30)) {
1440            for v in versions {
1441                assert_eq!(range.contains(&v), range.segments.iter().any(|s| RangeBounds::contains(s, &v)));
1442            }
1443        }
1444
1445        #[test]
1446        fn contains_many(range in proptest_strategy(), mut versions in proptest::collection::vec(version_strat(), ..30)) {
1447            versions.sort();
1448            assert_eq!(versions.len(), range.contains_many(versions.iter()).count());
1449            for (a, b) in versions.iter().zip(range.contains_many(versions.iter())) {
1450                assert_eq!(range.contains(a), b);
1451            }
1452        }
1453
1454        #[test]
1455        fn simplify(range in proptest_strategy(), mut versions in proptest::collection::vec(version_strat(), ..30)) {
1456            versions.sort();
1457            let simp = range.simplify(versions.iter());
1458
1459            for v in versions {
1460                assert_eq!(range.contains(&v), simp.contains(&v));
1461            }
1462            assert!(simp.segments.len() <= range.segments.len())
1463        }
1464
1465        #[test]
1466        fn from_iter_valid(segments in proptest::collection::vec(any::<(Bound<u32>, Bound<u32>)>(), ..30)) {
1467            let mut expected = Ranges::empty();
1468            for segment in &segments {
1469                expected = expected.union(&Ranges::from_range_bounds(*segment));
1470            }
1471            let actual =  Ranges::from_iter(segments.clone());
1472            assert_eq!(expected, actual, "{segments:?}");
1473        }
1474    }
1475
1476    #[test]
1477    fn contains_many_can_take_owned() {
1478        let range: Ranges<u8> = Ranges::singleton(1);
1479        let versions = vec![1, 2, 3];
1480        // Check that iter can be a Cow
1481        assert_eq!(
1482            range.contains_many(versions.iter()).count(),
1483            range
1484                .contains_many(versions.iter().map(std::borrow::Cow::Borrowed))
1485                .count()
1486        );
1487        // Check that iter can be a V
1488        assert_eq!(
1489            range.contains_many(versions.iter()).count(),
1490            range.contains_many(versions.into_iter()).count()
1491        );
1492    }
1493
1494    #[test]
1495    fn contains_can_take_owned() {
1496        let range: Ranges<Box<u8>> = Ranges::singleton(1);
1497        let version = 1;
1498
1499        assert_eq!(range.contains(&Box::new(version)), range.contains(&version));
1500        let range: Ranges<String> = Ranges::singleton(1.to_string());
1501        let version = 1.to_string();
1502        assert_eq!(range.contains(&version), range.contains("1"));
1503    }
1504
1505    #[test]
1506    fn simplify_can_take_owned() {
1507        let range: Ranges<u8> = Ranges::singleton(1);
1508        let versions = vec![1, 2, 3];
1509        // Check that iter can be a Cow
1510        assert_eq!(
1511            range.simplify(versions.iter()),
1512            range.simplify(versions.iter().map(std::borrow::Cow::Borrowed))
1513        );
1514        // Check that iter can be a V
1515        assert_eq!(
1516            range.simplify(versions.iter()),
1517            range.simplify(versions.into_iter())
1518        );
1519    }
1520
1521    #[test]
1522    fn version_ord() {
1523        let versions: &[Ranges<u32>] = &[
1524            Ranges::strictly_lower_than(1u32),
1525            Ranges::lower_than(1u32),
1526            Ranges::singleton(1u32),
1527            Ranges::between(1u32, 3u32),
1528            Ranges::higher_than(1u32),
1529            Ranges::strictly_higher_than(1u32),
1530            Ranges::singleton(2u32),
1531            Ranges::singleton(2u32).union(&Ranges::singleton(3u32)),
1532            Ranges::singleton(2u32)
1533                .union(&Ranges::singleton(3u32))
1534                .union(&Ranges::singleton(4u32)),
1535            Ranges::singleton(2u32).union(&Ranges::singleton(4u32)),
1536            Ranges::singleton(3u32),
1537        ];
1538
1539        let mut versions_sorted = versions.to_vec();
1540        versions_sorted.sort();
1541        assert_eq!(versions_sorted, versions);
1542
1543        // Check that the sorting isn't just stable because we're returning equal.
1544        let mut version_reverse_sorted = versions.to_vec();
1545        version_reverse_sorted.reverse();
1546        version_reverse_sorted.sort();
1547        assert_eq!(version_reverse_sorted, versions);
1548    }
1549}