closed_interval_set/
union.rs

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
use crate::Backing;
use crate::ClosedRange;
use crate::Endpoint;
use crate::RangeCase;
use crate::RangeVec;

/// Computes the normalized union of `acc` and `src`.
///
/// There's no particularly smart way to do this (except maybe with a
/// sorted merge, but hopefully the default [`slice::sort`] handles
/// sorted runs): concatenate everything and normalize in place.
///
/// This operation reuses the storage from `acc` and takes
/// \\(\mathcal{O}(n \log n)\\) time, where \\(n\\) is the total
/// number of ranges in the two inputs.
#[inline]
pub fn union_vec<T: Endpoint>(
    acc: impl Into<RangeCase<T>>,
    src: impl IntoIterator<Item: ClosedRange<EndT = T>>,
) -> RangeVec<T> {
    fn doit<T: Endpoint>(
        mut acc: Backing<T>,
        src: impl Iterator<Item: ClosedRange<EndT = T>>,
    ) -> RangeVec<T> {
        acc.extend(src.map(|range| range.get()));
        crate::normalize_vec(acc)
    }

    doit(acc.into().into_inner(), src.into_iter())
}

impl<T: Endpoint> RangeVec<T> {
    /// Constructs the union of this [`RangeVec`] and any of a
    /// [`RangeVec`], a [`SmallVec`], or a [`Vec`].
    ///
    /// This operation reuses the storage from `self` and takes
    /// \\(\mathcal{O}(n \log n)\\) time, where \\(n\\) is the total
    /// size of the two inputs. This is worse than the linear-time
    /// [`RangeVec::union`], but the constant factors are pretty good.
    ///
    /// See [`union_vec`] for more general types.
    ///
    /// [`SmallVec`]: https://docs.rs/smallvec/latest/smallvec/struct.SmallVec.html
    /// [`Vec`]: https://doc.rust-lang.org/std/vec/struct.Vec.html
    #[inline(always)]
    pub fn into_union(self, other: impl Into<RangeCase<T>>) -> Self {
        #[cfg(feature = "internal_checks")]
        use crate::NormalizedRangeIter;

        fn doit<T: Endpoint>(mut x: Backing<T>, mut y: Backing<T>) -> RangeVec<T> {
            if y.len() > x.len() {
                core::mem::swap(&mut x, &mut y);
            }

            // More efficient when the first argument is longer
            debug_assert!(x.len() >= y.len());
            union_vec(x, y)
        }

        let other = other.into().into_inner();

        #[cfg(feature = "internal_checks")]
        let expected = (
            self.iter()
                .union(RangeVec::from_smallvec(other.clone()))
                .collect_range_vec(),
            RangeVec::from_smallvec(other.clone())
                .iter()
                .union(self.iter())
                .collect_range_vec(),
        );

        let ret = doit(self.into_inner(), other);

        #[cfg(feature = "internal_checks")]
        {
            assert!(expected.0.eqv(&ret));
            assert!(expected.1.eqv(&ret));
        }

        ret
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod test {
    use super::*;
    use alloc::vec;
    use alloc::vec::Vec;

    #[test]
    fn test_union_smoke() {
        use crate::NormalizedRangeIter;

        let acc = vec![(1u8, 4u8), (5u8, 1u8), (5u8, 10u8)];
        let src = [(0u8, 2u8), (11u8, 15u8)];

        assert_eq!(
            crate::normalize_vec(acc.clone())
                .into_union(src.to_vec())
                .into_vec(),
            vec![(0u8, 15u8)]
        );

        assert_eq!(
            crate::normalize_vec(src.to_vec())
                .union(crate::normalize_vec(vec![]))
                .into_vec(),
            src.to_vec()
        );

        let result = union_vec(acc, src);
        assert_eq!(&*result, &vec![(0u8, 15u8)]);
        assert_eq!(result.inner(), &vec![(0u8, 15u8)]);
        assert_eq!(result.iter().collect::<Vec<_>>(), vec![(0u8, 15u8)]);
        assert_eq!(
            result.iter().collect_range_vec().into_vec(),
            vec![(0u8, 15u8)]
        );
        assert_eq!(result.into_vec(), vec![(0u8, 15u8)]);
    }

    proptest::proptest! {
        #[test]
        fn test_union(xs: Vec<(u8, u8)>, ys: Vec<(u8, u8)>)
        {
            use crate::ranges_to_bits;

            let bits = {
                let mut all_ranges = xs.clone();
                all_ranges.extend(&ys);
                ranges_to_bits(&all_ranges)
            };

            // union_vec
            {
                let union = union_vec(xs.clone(), &ys);
                assert_eq!(bits, ranges_to_bits(&union));
            }

            {
                let union = union_vec(RangeVec::from_vec(xs.clone()), &ys);
                assert_eq!(bits, ranges_to_bits(&union));
            }

            {
                let union = union_vec(xs.clone(), RangeVec::from_vec(ys.clone()).iter());
                assert_eq!(bits, ranges_to_bits(&union));
            }

            {
                let union = union_vec(RangeVec::from_vec(xs.clone()), RangeVec::from_vec(ys.clone()).iter());
                assert_eq!(bits, ranges_to_bits(&union));
            }

            // union
            {
                let union = RangeVec::from_vec(xs.clone()).into_union(ys.clone());
                assert_eq!(bits, ranges_to_bits(&union));
            }

            {
                let union = RangeVec::from_vec(ys.clone()).union(RangeVec::from_vec(xs.clone()));
                assert_eq!(bits, ranges_to_bits(&union));
            }
        }
    }
}