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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// Copyright (C) Microsoft Corporation.
// Licensed under the MIT License.

//! Implement a range map data structure.

#![warn(missing_docs)]
#![forbid(unsafe_code)]

use core::cmp::Ordering;
use std::ops::RangeInclusive;

/// A range map that supports lookups for a value V, based on a key type K.
/// Ranges are defined as a [`RangeInclusive`]. The map does not allow
/// overlapping ranges.
///
/// This implementation is done by using a sorted vec and using binary search
/// for insertion and removal.
#[derive(Debug, Clone)]
pub struct RangeMap<K, V> {
    // This vec _must_ be in sorted order.
    data: Vec<(K, K, V)>,
}

/// An entry returned by [`RangeMap::entry`].
#[derive(Debug)]
pub enum Entry<'a, K, V> {
    /// An entry already exists that overlaps.
    Overlapping(OverlappingEntry<'a, K, V>),
    /// No entry exists.
    Vacant(VacantEntry<'a, K, V>),
}

/// An object representing a an existing entry that overlaps the range passed to
/// [`RangeMap::entry`].
#[derive(Debug)]
pub struct OverlappingEntry<'a, K, V>(&'a Vec<(K, K, V)>, usize);

impl<K, V> OverlappingEntry<'_, K, V> {
    /// Gets the entry that's already in the map.
    pub fn get(&self) -> &(K, K, V) {
        &self.0[self.1]
    }
}

/// An object representing a range of the map with no entries.
#[derive(Debug)]
pub struct VacantEntry<'a, K, V> {
    data: &'a mut Vec<(K, K, V)>,
    insert_index: usize,
    start: K,
    end: K,
}

impl<K, V> VacantEntry<'_, K, V> {
    /// Inserts a value into the map.
    pub fn insert(self, value: V) {
        self.data
            .insert(self.insert_index, (self.start, self.end, value));
    }
}

impl<K, V> RangeMap<K, V>
where
    K: PartialOrd + Clone,
{
    /// Check if a given range contains the following value.
    fn range_contains(range: &(K, K, V), value: &K) -> bool {
        *value >= range.0 && *value <= range.1
    }

    /// Check if two ranges overlap in any way.
    fn ranges_overlaps(a_start: &K, a_end: &K, b_start: &K, b_end: &K) -> bool {
        *a_start <= *b_end && *b_start <= *a_end
    }

    /// Do an ordered comparison for a given value against an range.
    fn range_compare(range: &(K, K, V), value: &K) -> Ordering {
        if *value < range.0 {
            Ordering::Less
        } else if *value > range.1 {
            Ordering::Greater
        } else {
            debug_assert!(RangeMap::<K, V>::range_contains(range, value));
            Ordering::Equal
        }
    }

    /// Do an ordered comparison against an range and another.
    fn range_compare_range(range: &(K, K, V), start: &K, end: &K) -> Ordering {
        debug_assert!(end >= start);
        if *end < range.0 {
            debug_assert!(!RangeMap::<K, V>::ranges_overlaps(
                &range.0, &range.1, start, end
            ));
            Ordering::Less
        } else if *start > range.1 {
            debug_assert!(!RangeMap::<K, V>::ranges_overlaps(
                &range.0, &range.1, start, end
            ));
            Ordering::Greater
        } else {
            debug_assert!(RangeMap::<K, V>::ranges_overlaps(
                &range.0, &range.1, start, end
            ));
            Ordering::Equal
        }
    }

    /// Binary search the data member for a range containing the given value,
    /// or a valid insert location.
    fn binary_search_find(&self, value: &K) -> Result<usize, usize> {
        self.data
            .binary_search_by(|element| RangeMap::<K, V>::range_compare(element, value))
    }

    /// Binary search the data member for a range overlapping the given range,
    /// or a valid insert location.
    ///
    /// The range must be a non-empty range, or else this function will panic.
    fn binary_search_find_range(&self, range: &RangeInclusive<K>) -> Result<usize, usize> {
        assert!(!range.is_empty());
        self.data.binary_search_by(|element| {
            RangeMap::<K, V>::range_compare_range(element, range.start(), range.end())
        })
    }

    /// Create a new empty [`RangeMap`].
    pub fn new() -> Self {
        RangeMap { data: Vec::new() }
    }

    /// Returns an entry for the given range. If the range overlaps an existing
    /// region, returns an [`Entry::Overlapping`]; otherwise, returns
    /// [`Entry::Vacant`].
    ///
    /// Note that there could be multiple ranges in the map that overlap the
    /// given `range` but only one overlap will be returned by this function.
    ///
    /// This function panics if `range.is_empty()` is true.
    pub fn entry(&mut self, range: RangeInclusive<K>) -> Entry<'_, K, V> {
        assert!(!range.is_empty());

        match self.binary_search_find_range(&range) {
            Ok(index) => Entry::Overlapping(OverlappingEntry(&self.data, index)),
            Err(insert_index) => Entry::Vacant(VacantEntry {
                data: &mut self.data,
                insert_index,
                start: range.start().clone(),
                end: range.end().clone(),
            }),
        }
    }

    /// Insert a new range into the map.
    ///
    /// Returns true if the map did not contain an overlapping range. Returns
    /// false if the map contained an overlapping range.
    ///
    /// This function panics if `range.is_empty()` is true.
    ///
    /// Note that two entries with adjacent ranges that contain the same value
    /// are not merged. Adjacent entries can be merged using
    /// [`RangeMap::merge_adjacent`].
    ///
    /// # Examples
    ///
    /// ```
    /// use range_map_vec::RangeMap;
    ///
    /// let mut map: RangeMap<u64, u64> = RangeMap::new();
    /// assert_eq!(map.insert(0..=5, 0), true);
    /// assert_eq!(map.insert(1..=20, 1), false);
    /// assert_eq!(map.get_entry(&3).unwrap(), &(0, 5, 0));
    /// ```
    pub fn insert(&mut self, range: RangeInclusive<K>, value: V) -> bool {
        assert!(!range.is_empty());

        match self.entry(range) {
            Entry::Overlapping(_) => false,
            Entry::Vacant(entry) => {
                entry.insert(value);
                true
            }
        }
    }

    /// Remove a given range from the map given a value covered by the range.
    /// Returns the value removed from the map, (start, end, value), if any.
    pub fn remove(&mut self, value: &K) -> Option<(K, K, V)> {
        if let Ok(pos) = self.binary_search_find(value) {
            Some(self.data.remove(pos))
        } else {
            None
        }
    }

    /// Removes any entries that overlaps the specified range. Returns a sorted
    /// vector representing ranges removed.
    pub fn remove_range(&mut self, range: RangeInclusive<K>) -> Vec<(K, K, V)> {
        let mut removed = vec![];

        while let Ok(index) = self.binary_search_find_range(&range) {
            removed.push(self.data.remove(index));
        }

        removed.sort_by(|a, b| Self::range_compare_range(b, &a.0, &a.1));
        removed
    }

    /// Remove all ranges in the map and return them as a Vec, with `(start,
    /// end, value)`.
    pub fn into_vec(self) -> Vec<(K, K, V)> {
        self.data
    }

    /// Returns true if the map contains an range that covers the value.
    pub fn contains(&self, value: &K) -> bool {
        self.binary_search_find(value).is_ok()
    }

    /// Returns a reference to the value covered by a range in the map, if any.
    ///
    /// ```
    /// use range_map_vec::RangeMap;
    ///
    /// let mut map: RangeMap<u64, u64> = RangeMap::new();
    /// assert_eq!(map.insert(0..=3, 0), true);
    /// assert_eq!(map.insert(5..=10, 1), true);
    /// assert_eq!(map.get(&3).unwrap(), &0);
    /// assert!(map.get(&4).is_none());
    /// ```
    pub fn get(&self, value: &K) -> Option<&V> {
        match self.binary_search_find(value) {
            Ok(index) => Some(&self.data[index].2),
            Err(_) => None,
        }
    }

    /// Returns a reference to the value that overlaps the given range, if any.
    ///
    /// Note that there could be multiple ranges in the map that overlap the
    /// given `range` but only one overlap will be returned by this function.
    ///
    /// This function panics if `range.is_empty()` is true.
    pub fn get_range(&self, range: RangeInclusive<K>) -> Option<&V> {
        assert!(!range.is_empty());
        match self.binary_search_find_range(&range) {
            Ok(index) => Some(&self.data[index].2),
            Err(_) => None,
        }
    }

    /// Returns a reference to the entry that covers `value`, if any.
    pub fn get_entry(&self, value: &K) -> Option<&(K, K, V)> {
        match self.binary_search_find(value) {
            Ok(index) => Some(&self.data[index]),
            Err(_) => None,
        }
    }

    /// Returns a reference to the entry overlapping the specified `range`, if
    /// any.
    ///
    /// Note that there could be multiple ranges in the map that overlap the
    /// given `range` but only one overlap will be returned by this function.
    pub fn get_range_entry(&self, range: RangeInclusive<K>) -> Option<&(K, K, V)> {
        assert!(!range.is_empty());
        match self.binary_search_find_range(&range) {
            Ok(index) => Some(&self.data[index]),
            Err(_) => None,
        }
    }

    /// Provides an iterator to iterate through the whole map.
    pub fn iter(&self) -> impl Clone + DoubleEndedIterator<Item = (RangeInclusive<K>, &V)> {
        self.data
            .iter()
            .map(|(start, end, v)| (start.clone()..=end.clone(), v))
    }

    /// Merge adjacent ranges that hold the same value using the provided
    /// closure to determine if a range is adjacent to another.
    ///
    /// The closure accepts two arguments, with the first argument being smaller
    /// than the second.
    ///
    /// # Examples
    ///
    /// ```
    /// use range_map_vec::RangeMap;
    ///
    /// let mut map: RangeMap<u64, u64> = RangeMap::new();
    /// assert_eq!(map.insert(0..=2, 0), true);
    /// assert_eq!(map.insert(3..=5, 0), true);
    /// assert_eq!(map.insert(7..=10, 0), true);
    ///
    /// map.merge_adjacent(|smaller, larger| {
    ///     let next = *smaller.end() + 1;
    ///     next == *larger.start()
    /// });
    ///
    /// assert_eq!(map.get_entry(&3).unwrap(), &(0, 5, 0));
    /// assert_eq!(map.get_entry(&8).unwrap(), &(7, 10, 0));
    /// ```
    pub fn merge_adjacent<F>(&mut self, is_adjacent: F)
    where
        F: Fn(RangeInclusive<K>, RangeInclusive<K>) -> bool,
        V: Eq,
    {
        loop {
            let mut new_range = None;
            for (left, right) in self.data.iter().zip(self.data.iter().skip(1)) {
                let left_range = left.0.clone()..=left.1.clone();
                let right_range = right.0.clone()..=right.1.clone();

                // Range map is sorted in descending order, so swap left and
                // right so the smaller range is passed to the closure first.
                if is_adjacent(right_range, left_range) && left.2 == right.2 {
                    new_range = Some(right.0.clone()..=left.1.clone());
                    break;
                }
            }

            match new_range {
                Some(new_range) => {
                    let value = self
                        .remove_range(new_range.clone())
                        .pop()
                        .expect("should have removed ranges")
                        .2;
                    assert!(self.insert(new_range, value));
                }
                None => return,
            }
        }
    }
}

impl<K, V> Default for RangeMap<K, V>
where
    K: PartialOrd + Clone,
{
    fn default() -> Self {
        Self::new()
    }
}

/// A default implementation for a u64 key type for
/// [`RangeMap::merge_adjacent`].
pub fn u64_is_adjacent(smaller: RangeInclusive<u64>, larger: RangeInclusive<u64>) -> bool {
    let next = *smaller.end() + 1;
    next == *larger.start()
}

#[cfg(test)]
mod tests {
    use super::Entry;
    use super::RangeMap;

    #[test]
    fn basic_functionality() {
        let mut tree: RangeMap<u64, u64> = RangeMap::new();

        assert_eq!(tree.insert(0..=5, 0), true);
        assert_eq!(tree.insert(10..=20, 1), true);

        assert_eq!(tree.contains(&0), true);
        assert_eq!(tree.contains(&3), true);
        assert_eq!(tree.contains(&5), true);
        assert_eq!(tree.contains(&8), false);
        assert_eq!(tree.contains(&15), true);

        assert_eq!(tree.get(&0), Some(&0));
        assert_eq!(tree.get(&3), Some(&0));
        assert_eq!(tree.get(&5), Some(&0));
        assert_eq!(tree.get(&8), None);
        assert_eq!(tree.get(&15), Some(&1));

        assert_eq!(tree.remove(&2), Some((0, 5, 0)));
        assert_eq!(tree.remove(&18), Some((10, 20, 1)));
        assert_eq!(tree.remove(&0), None);
    }

    #[test]
    #[should_panic]
    #[allow(clippy::reversed_empty_ranges)]
    fn test_insert_invalid_range() {
        let mut tree: RangeMap<u64, u64> = RangeMap::new();
        tree.insert(20..=10, 0);
    }

    #[test]
    #[should_panic]
    #[allow(clippy::reversed_empty_ranges)]
    fn test_entry_invalid_range() {
        let mut tree: RangeMap<u64, u64> = RangeMap::new();
        tree.entry(20..=10);
    }

    #[test]
    #[should_panic]
    #[allow(clippy::reversed_empty_ranges)]
    fn test_get_range_invalid_range() {
        let tree: RangeMap<u64, u64> = RangeMap::new();
        tree.get_range(20..=10);
    }

    #[test]
    fn test_add_multiple_overlap() {
        let mut tree: RangeMap<u64, u64> = RangeMap::new();

        assert_eq!(tree.insert(1..=5, 0), true);
        assert_eq!(tree.insert(1..=5, 0), false);
        assert_eq!(tree.insert(2..=3, 0), false);
        assert_eq!(tree.insert(0..=1, 0), false);
        assert_eq!(tree.insert(5..=10, 0), false);
        assert_eq!(tree.insert(2..=10, 0), false);
        assert_eq!(tree.insert(0..=10, 0), false);
    }

    #[test]
    fn test_get() {
        let mut tree: RangeMap<u64, u64> = RangeMap::new();

        assert_eq!(tree.insert(1..=5, 0), true);
        assert_eq!(tree.insert(6..=10, 1), true);
        assert_eq!(tree.insert(12..=13, 2), true);
        assert_eq!(tree.insert(20..=30, 3), true);

        assert_eq!(tree.get(&0), None);

        for x in 1..=5 {
            assert_eq!(tree.get(&x), Some(&0));
        }

        for x in 6..=10 {
            assert_eq!(tree.get(&x), Some(&1));
        }

        assert_eq!(tree.get(&11), None);

        for x in 12..=13 {
            assert_eq!(tree.get(&x), Some(&2));
        }

        for x in 14..=19 {
            assert_eq!(tree.get(&x), None);
        }

        for x in 20..=30 {
            assert_eq!(tree.get(&x), Some(&3));
        }

        for x in 31..40 {
            assert_eq!(tree.get(&x), None);
        }
    }

    #[test]
    fn test_remove() {
        let mut tree: RangeMap<u64, u64> = RangeMap::new();

        assert_eq!(tree.insert(10..=20, 1), true);

        assert_eq!(tree.insert(1..=5, 0), true);
        assert_eq!(tree.remove(&1), Some((1, 5, 0)));
        assert_eq!(tree.remove(&1), None);
        assert_eq!(tree.remove(&3), None);
        assert_eq!(tree.remove(&5), None);

        assert_eq!(tree.insert(1..=5, 0), true);
        assert_eq!(tree.remove(&5), Some((1, 5, 0)));
        assert_eq!(tree.remove(&1), None);
        assert_eq!(tree.remove(&3), None);
        assert_eq!(tree.remove(&5), None);

        assert_eq!(tree.insert(1..=5, 0), true);
        assert_eq!(tree.remove(&3), Some((1, 5, 0)));
        assert_eq!(tree.remove(&1), None);
        assert_eq!(tree.remove(&3), None);
        assert_eq!(tree.remove(&5), None);
    }

    #[test]
    fn test_contains() {
        let mut tree: RangeMap<u64, u64> = RangeMap::new();

        assert_eq!(tree.insert(1..=5, 0), true);
        assert_eq!(tree.insert(6..=10, 1), true);
        assert_eq!(tree.insert(12..=13, 2), true);
        assert_eq!(tree.insert(20..=30, 3), true);

        assert_eq!(tree.contains(&0), false);

        for x in 1..=5 {
            assert_eq!(tree.contains(&x), true);
        }

        for x in 6..=10 {
            assert_eq!(tree.contains(&x), true);
        }

        assert_eq!(tree.contains(&11), false);

        for x in 12..=13 {
            assert_eq!(tree.contains(&x), true);
        }

        for x in 14..=19 {
            assert_eq!(tree.contains(&x), false);
        }

        for x in 20..=30 {
            assert_eq!(tree.contains(&x), true);
        }

        for x in 31..40 {
            assert_eq!(tree.contains(&x), false);
        }
    }

    #[test]
    fn test_start_end_equal() {
        let mut tree: RangeMap<u64, u64> = RangeMap::new();

        assert_eq!(tree.insert(0..=0, 0), true);
        assert_eq!(tree.insert(1..=1, 1), true);
        assert_eq!(tree.insert(2..=2, 2), true);
        assert_eq!(tree.insert(3..=3, 3), true);
        assert_eq!(tree.insert(0..=3, 4), false);
    }

    #[test]
    fn test_entry() {
        let mut tree: RangeMap<u64, u64> = RangeMap::new();

        match tree.entry(1..=2) {
            Entry::Overlapping(_) => panic!(),
            Entry::Vacant(e) => e.insert(0x1000),
        }

        match tree.entry(2..=4) {
            Entry::Overlapping(e) => {
                assert_eq!(e.get(), &(1, 2, 0x1000));
            }
            Entry::Vacant(_) => panic!(),
        }
    }

    #[test]
    fn test_remove_range() {
        let mut map: RangeMap<u64, u64> = RangeMap::new();

        assert_eq!(map.insert(1..=5, 0), true);
        assert_eq!(map.insert(6..=10, 1), true);
        assert_eq!(map.insert(12..=13, 2), true);
        assert_eq!(map.insert(20..=30, 3), true);

        let removed = map.remove_range(2..=19);
        assert_eq!(removed, vec![(1, 5, 0), (6, 10, 1), (12, 13, 2)]);
        let removed = map.remove_range(1..=100);
        assert_eq!(removed, vec![(20, 30, 3)]);
        let removed = map.remove_range(1..=100);
        assert_eq!(removed, vec![]);
    }

    #[test]
    fn test_merge_adjacent() {
        let mut map: RangeMap<u64, u64> = RangeMap::new();

        assert_eq!(map.insert(1..=5, 0), true);
        assert_eq!(map.insert(6..=7, 0), true);
        assert_eq!(map.insert(8..=10, 0), true);
        assert_eq!(map.insert(11..=13, 1), true);
        assert_eq!(map.insert(15..=30, 1), true);
        assert_eq!(map.insert(31..=32, 1), true);

        map.merge_adjacent(super::u64_is_adjacent);

        let expected = vec![(1, 10, 0), (11, 13, 1), (15, 32, 1)];
        let mut actual = map.into_vec();
        actual.sort();

        assert_eq!(expected, actual);
    }
}