segmap 0.1.0

Map and set data structures whose keys are stored as ranges. Contiguous and overlapping ranges that map to the same value are coalesced into a single range. Originated as a fork of Jeff Parsons' "rangemap"
Documentation
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use core::{
    cmp::Ordering,
    fmt::Debug,
    ops::Bound::{self, *},
};

mod bounds;

pub(crate) use bounds::{End, Start};

/// Monotonically increasing segment, for use as a concrete range type in
/// [`SegmentMap`].
#[derive(Clone, Copy, Hash, Eq)]
pub struct Segment<T> {
    pub(crate) start: Start<T>,
    pub(crate) end: End<T>,
}

impl<T> core::ops::RangeBounds<T> for Segment<T> {
    fn start_bound(&self) -> Bound<&T> {
        self.start.as_bound_inner_ref()
    }
    fn end_bound(&self) -> Bound<&T> {
        self.end.as_bound_inner_ref()
    }
}

impl<T> core::ops::RangeBounds<T> for &Segment<T> {
    fn start_bound(&self) -> Bound<&T> {
        self.start.as_bound_inner_ref()
    }
    fn end_bound(&self) -> Bound<&T> {
        self.end.as_bound_inner_ref()
    }
}

impl<T: Debug> Debug for Segment<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match &self.start.0 {
            Unbounded => write!(f, "(-∞, ")?,
            Excluded(t) => write!(f, "({:?}, ", t)?,
            Included(t) => write!(f, "[{:?}, ", t)?,
        }
        match &self.end.0 {
            Unbounded => write!(f, "∞)"),
            Excluded(t) => write!(f, "{:?})", t),
            Included(t) => write!(f, "{:?}]", t),
        }
    }
}

impl<T> Segment<T> {
    /// Construct a new segment from range bounds
    ///
    /// If the range given is backwards (decreasing), it will be reversed
    ///
    /// # Weird Ranges
    ///
    /// Oddly constructed ranges will be coerced to "sane" structure:
    /// - Point ranges (where both start and end are bounded to the same value
    /// and at least one is included) will be coerced to (Included, Included)
    ///
    /// # Panics
    ///
    /// Only one range is too strange to be included here. If you pass a point
    /// range with both the start and end excluded, this will panic, as the
    /// range is impossible to evaluate
    ///
    /// ```
    /// # use segmap::*;
    /// # use core::ops::RangeBounds;
    ///
    /// let r = Segment::new(Bound::Included(0), Bound::Excluded(5));
    /// assert_eq!(r.start_bound(), Bound::Included(&0));
    /// assert_eq!(r.end_bound(), Bound::Excluded(&5));
    /// ```
    ///
    /// # See Also
    ///
    /// `Segment` also implements `From` for all of the `core::ops` range types,
    /// so you may find it more convenient to construct a range like
    /// `Segment::from(a..b)`
    ///
    pub fn new(start: Bound<T>, end: Bound<T>) -> Self
    where
        T: Ord,
    {
        match (&start, &end) {
            (Included(s), Included(e))
            | (Included(s), Excluded(e))
            | (Excluded(s), Included(e))
            | (Excluded(s), Excluded(e)) => match s.cmp(e) {
                // If equal, coerce to included
                Ordering::Equal => Self {
                    start: Start(Included(bound_value(start).unwrap())),
                    end: End(Included(bound_value(end).unwrap())),
                },
                Ordering::Less => Self {
                    start: Start(start),
                    end: End(end),
                },
                // If backwards, flip bounds
                Ordering::Greater => Self {
                    start: Start(end),
                    end: End(start),
                },
            },

            // Otherwise, only one side has a value
            _ => Self {
                start: Start(start),
                end: End(end),
            },
        }
    }

    /// Construct a new [`Range`] that spans all possible values
    ///
    /// This is the same as `Range::from(..)`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let r = Segment::<u32>::full();
    /// assert_eq!(r.start_bound(), Bound::Unbounded);
    /// assert_eq!(r.end_bound(), Bound::Unbounded);
    /// ```
    pub fn full() -> Self {
        Self {
            start: Start(Unbounded),
            end: End(Unbounded),
        }
    }

    /// Constructs a new point segement at the given value
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let r = Segment::point(5);
    /// assert_eq!(r.start_bound(), Bound::Included(&5));
    /// assert_eq!(r.end_bound(), Bound::Included(&5));
    /// ```
    pub fn point(value: T) -> Self
    where
        T: Clone,
    {
        Self {
            start: Start(Included(value.clone())),
            end: End(Included(value)),
        }
    }

    /// Get the start value of the range (if it is bounded)
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let bounded = Segment::from(5..10);
    /// assert_eq!(bounded.start_value(), Some(&5));
    ///
    /// let unbounded = Segment::from(..10);
    /// assert_eq!(unbounded.start_value(), None);
    /// ```
    pub fn start_value(&self) -> Option<&T> {
        self.start.value()
    }

    /// Get the end value of the range (if it is bounded)
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// let bounded = Segment::from(5..10);
    /// assert_eq!(bounded.end_value(), Some(&10));
    ///
    /// let unbounded = Segment::from(5..);
    /// assert_eq!(unbounded.end_value(), None);
    /// ```
    pub fn end_value(&self) -> Option<&T> {
        self.end.value()
    }

    /// Converts from `Segment<T>` to `Segment<&T>`.
    ///
    /// Many iterators from this crate return a `Segment<&T>` instead of a
    /// `&Segment<T>` since bounds need to be constructed or adjusted. This allows
    /// us to avoid `clone`ing `T`.
    ///
    #[inline]
    pub fn as_ref(&self) -> Segment<&T> {
        Segment {
            start: self.start.as_ref(),
            end: self.end.as_ref(),
        }
    }

    /// Check whether the range overlaps with another
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// // Overlapping Segments
    /// let a = Segment::from(0..10);
    /// let b = Segment::from(5..15);
    /// assert!(a.overlaps(&b));
    ///
    /// // Touching Segments
    /// let a = Segment::from(0..10);
    /// let b = Segment::from(10..20);
    /// assert!(!a.overlaps(&b));
    ///
    /// // Separate Segments
    /// let a = Segment::from(0..10);
    /// let b = Segment::from(15..20);
    /// assert!(!a.overlaps(&b));
    ///
    /// ```
    ///
    /// # See Also
    ///
    /// - [`Segment::touches`] for overlapping OR adjacent ranges
    ///
    pub fn overlaps(&self, other: &Self) -> bool
    where
        T: Ord,
    {
        match (
            core::cmp::max(self.start.as_ref(), other.start.as_ref()).0,
            core::cmp::min(self.end.as_ref(), other.end.as_ref()).0,
        ) {
            // Both starts and/or both ends are unbounded, must overlap
            (Unbounded, _) | (_, Unbounded) => true,

            // If both are included, check less than or equal (overlap on same value)
            (Included(a), Included(b)) => a <= b,

            // Otherwise, no overlap on equal
            (Included(a) | Excluded(a), Included(b) | Excluded(b)) => a < b,
        }
    }

    /// Check whether the range touches another, either overlapping it or
    /// directly adjacent to it
    ///
    /// # Examples
    ///
    /// ```
    /// # use segmap::*;
    /// // Overlapping Segments
    /// let a = Segment::from(0..10);
    /// let b = Segment::from(5..15);
    /// assert!(a.touches(&b));
    ///
    /// // Touching Segments
    /// let a = Segment::from(0..10);
    /// let b = Segment::from(10..20);
    /// assert!(a.touches(&b));
    ///
    /// // Separate Segments
    /// let a = Segment::from(0..10);
    /// let b = Segment::from(15..20);
    /// assert!(!a.touches(&b));
    ///
    /// ```
    ///
    /// # See Also
    ///
    /// - [`Segment::overlaps`] for only overlapping ranges
    ///
    pub fn touches(&self, other: &Self) -> bool
    where
        T: Ord,
    {
        match (
            core::cmp::max(self.start.as_ref(), other.start.as_ref()).0,
            core::cmp::min(self.end.as_ref(), other.end.as_ref()).0,
        ) {
            // Both starts and/or both ends are unbounded, must overlap
            (Unbounded, _) | (_, Unbounded) => true,

            // If both are excluded, ranges don't touch on equal
            // (there is a point gap when a==b)
            (Excluded(a), Excluded(b)) => a < b,

            // If either are included, check less than or equal
            // (overlap on both included, touching when only one is)
            (Included(a) | Excluded(a), Included(b) | Excluded(b)) => a <= b,
        }
    }

    // TODO: Range convenience methods

    // /// Shift the entire range by some value
    // pub fn shift(&mut self, by: T)
    // where
    //     T: Clone + core::ops::AddAssign,
    // {
    //     if let Some(value) = self.start.value_mut() {
    //         *value += by.clone();
    //     }
    //     if let Some(value) = self.end.value_mut() {
    //         *value += by;
    //     }
    // }

    // /// Shift the entire range to the left by some value (useful if using an
    // /// unsigned type, where [`shift`] isn't useful)
    // pub fn shift_left(&mut self, by: T)
    // where
    //     T: Clone + core::ops::SubAssign,
    // {
    //     if let Some(value) = self.start.value_mut() {
    //         *value -= by.clone();
    //     }
    //     if let Some(value) = self.end.value_mut() {
    //         *value -= by;
    //     }
    // }

    // pub fn shift_right(&mut self, by: T)
    // where
    //     T: Clone + core::ops::AddAssign,
    // {
    //     if let Some(value) = self.start.value_mut() {
    //         *value += by.clone();
    //     }
    //     if let Some(value) = self.end.value_mut() {
    //         *value += by;
    //     }
    // }

    // /// Adjust the start of a range to a new lower bound.
    // pub fn adjust_left(&mut self, _new_start: Bound<T>) -> Option<Self> {
    //     // panic if empty range, or just none?
    // }

    // /// Adjust the end of a range to a new upper bound.
    // pub fn adjust_right(&mut self, _new_end: Bound<T>) -> Option<Self> {
    // }
}

impl<T: Clone> Segment<&T> {
    pub(crate) fn cloned(&self) -> Segment<T> {
        Segment {
            start: self.start.cloned(),
            end: self.end.cloned(),
        }
    }
}

/// Crate Methods
impl<T> Segment<T> {
    /// Obtain the adjacent end bound before the start of this range (if it
    /// exists)
    pub(crate) fn bound_before(&self) -> Option<End<&T>> {
        self.start.before()
    }
    /// Obtain the adjacent start bound after the end of this range (if it
    /// exists)
    pub(crate) fn bound_after(&self) -> Option<Start<&T>> {
        self.end.after()
    }
}

impl<'a, T> Segment<&'a T> {
    pub(crate) fn borrow_bound_before(&self) -> Option<End<&'a T>> {
        self.start.borrow_before()
    }
    pub(crate) fn borrow_bound_after(&self) -> Option<Start<&'a T>> {
        self.end.borrow_after()
    }
}

// Utility, since it's messy to match everwhere
fn bound_cloned<T: Clone>(b: Bound<&T>) -> Bound<T> {
    match b {
        Unbounded => Unbounded,
        Included(x) => Included(x.clone()),
        Excluded(x) => Excluded(x.clone()),
    }
}
fn bound_value<T>(b: Bound<T>) -> Option<T> {
    match b {
        Unbounded => None,
        Included(t) => Some(t),
        Excluded(t) => Some(t),
    }
}

impl<T: Ord> From<core::ops::Range<T>> for Segment<T> {
    fn from(r: core::ops::Range<T>) -> Self {
        Segment::new(Included(r.start), Excluded(r.end))
    }
}

impl<T: Ord> From<core::ops::RangeFrom<T>> for Segment<T> {
    fn from(r: core::ops::RangeFrom<T>) -> Self {
        Segment::new(Included(r.start), Unbounded)
    }
}

impl<T: Ord> From<core::ops::RangeTo<T>> for Segment<T> {
    fn from(r: core::ops::RangeTo<T>) -> Self {
        Segment::new(Unbounded, Excluded(r.end))
    }
}

impl<T: Ord> From<core::ops::RangeFull> for Segment<T> {
    fn from(_: core::ops::RangeFull) -> Self {
        Segment::new(Unbounded, Unbounded)
    }
}

impl<T: Ord> From<core::ops::RangeInclusive<T>> for Segment<T> {
    fn from(r: core::ops::RangeInclusive<T>) -> Self {
        let (start, end) = r.into_inner();
        Segment::new(Included(start), Included(end))
    }
}

impl<T: Ord> From<core::ops::RangeToInclusive<T>> for Segment<T> {
    fn from(r: core::ops::RangeToInclusive<T>) -> Self {
        Segment::new(Unbounded, Included(r.end))
    }
}

// TODO: no clone, return a Range<&T>

impl<T: Ord + Clone, R: core::ops::RangeBounds<T>> From<&R> for Segment<T> {
    fn from(r: &R) -> Self {
        Segment::new(bound_cloned(r.start_bound()), bound_cloned(r.end_bound()))
    }
}

impl<T: PartialEq, R: core::ops::RangeBounds<T>> PartialEq<R> for Segment<T> {
    fn eq(&self, other: &R) -> bool {
        (match (&self.start.0, other.start_bound()) {
            (Unbounded, Unbounded) => true,
            (Included(t), Included(u)) | (Excluded(t), Excluded(u)) => t.eq(u),
            _ => false,
        }) && (match (&self.end.0, other.end_bound()) {
            (Unbounded, Unbounded) => true,
            (Included(t), Included(u)) | (Excluded(t), Excluded(u)) => t.eq(u),
            _ => false,
        })
    }
}