range_map/
lib.rs

1// Copyright 2015 Joe Neeman.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9extern crate num_traits;
10
11#[cfg(test)]
12extern crate num_iter;
13#[cfg(test)]
14extern crate quickcheck;
15
16use num_traits::PrimInt;
17use std::cmp::{max, min, Ordering};
18use std::fmt::{Debug, Formatter};
19use std::iter::FromIterator;
20use std::{mem, usize};
21
22const DISPLAY_LIMIT: usize = 10;
23
24/// A range of elements, including the endpoints.
25#[derive(Copy, Clone, Hash, PartialEq, PartialOrd, Eq, Ord)]
26pub struct Range<T> {
27    pub start: T,
28    pub end: T,
29}
30
31impl<T: Debug> Debug for Range<T> {
32    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
33        try!(self.start.fmt(f));
34        try!(f.write_str(" -- "));
35        try!(self.end.fmt(f));
36        Ok(())
37    }
38}
39
40impl<T: PrimInt> Range<T> {
41    /// Creates a new range with the given start and endpoints (inclusive).
42    ///
43    /// # Panics
44    ///  - if `start` is strictly larger than `end`
45    pub fn new(start: T, end: T) -> Range<T> {
46        if start > end {
47            panic!("Ranges must be ordered");
48        }
49        Range {
50            start: start,
51            end: end,
52        }
53    }
54
55    /// Creates a new range containing everything.
56    pub fn full() -> Range<T> {
57        Range {
58            start: T::min_value(),
59            end: T::max_value(),
60        }
61    }
62
63    /// Creates a new range containing a single thing.
64    pub fn single(x: T) -> Range<T> {
65        Range::new(x, x)
66    }
67
68    /// Tests whether a given element belongs to this range.
69    pub fn contains(&self, x: T) -> bool {
70        self.start <= x && x <= self.end
71    }
72
73    /// Checks whether the intersections overlap.
74    pub fn intersects(&self, other: &Self) -> bool {
75        self.start <= other.end && self.end >= other.start
76    }
77
78    /// Computes the intersection between two ranges. Returns none if the intersection is empty.
79    pub fn intersection(&self, other: &Self) -> Option<Self> {
80        if self.intersects(other) {
81            Some(Range::new(
82                max(self.start, other.start),
83                min(self.end, other.end),
84            ))
85        } else {
86            None
87        }
88    }
89
90    /// Returns the smallest range that covers `self` and `other`.
91    pub fn cover(&self, other: &Self) -> Self {
92        Range::new(min(self.start, other.start), max(self.end, other.end))
93    }
94}
95
96impl<T: PrimInt> PartialEq<T> for Range<T> {
97    fn eq(&self, x: &T) -> bool {
98        self.contains(*x)
99    }
100}
101
102impl<T: PrimInt> PartialOrd<T> for Range<T> {
103    fn partial_cmp(&self, x: &T) -> Option<Ordering> {
104        if self.end < *x {
105            Some(Ordering::Less)
106        } else if self.start > *x {
107            Some(Ordering::Greater)
108        } else {
109            Some(Ordering::Equal)
110        }
111    }
112}
113
114/// When creating a [`RangeMap`] from a list of ranges and values, there's a possiblity that two
115/// ranges will overlap. In this case, it's a problem if they want to be associated to different
116/// values (because we don't know which value should be assigned to the intersection of the
117/// ranges). An `OverlapError` is the result of such a situation. It contains two members. The
118/// first is a [`RangeMap`] obtained by simply ignoring all the ranges that would cause a bad
119/// overlap. The second is the collection of ranges that were ignored.
120// TODO: an example
121#[derive(Clone, Debug, Eq, Hash, PartialEq)]
122pub struct OverlapError<T, V> {
123    pub non_overlapping: RangeMap<T, V>,
124    pub discarded: Vec<(Range<T>, V)>,
125}
126
127/// A set of characters. Optionally, each character in the set may be associated with some data.
128#[derive(Clone, Eq, Hash, PartialEq)]
129pub struct RangeMap<T, V> {
130    elts: Vec<(Range<T>, V)>,
131}
132
133impl<T: Debug, V: Debug> Debug for RangeMap<T, V> {
134    // When alternate formatting is specified, only prints out the first bunch of mappings.
135    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
136        try!(f.write_fmt(format_args!("RangeMap (")));
137
138        if f.alternate() {
139            try!(f
140                .debug_map()
141                .entries(self.elts.iter().map(|x| (&x.0, &x.1)).take(DISPLAY_LIMIT))
142                .finish());
143            if self.elts.len() > DISPLAY_LIMIT {
144                try!(f.write_str("..."));
145            }
146        } else {
147            try!(f
148                .debug_map()
149                .entries(self.elts.iter().map(|x| (&x.0, &x.1)))
150                .finish());
151        }
152        try!(f.write_str(")"));
153        Ok(())
154    }
155}
156
157impl<T: Debug + PrimInt, V: Clone + Debug + Eq> FromIterator<(Range<T>, V)> for RangeMap<T, V> {
158    /// Builds a `RangeMap` from an iterator over pairs. If any ranges overlap, they must map to
159    /// the same value.
160    ///
161    /// # Panics
162    /// Panics if there are ranges that overlap and do not map to the same value. If you are not
163    /// sure whether this could happen, use [`RangeMap::try_from_iter`] instead.
164    fn from_iter<I: IntoIterator<Item = (Range<T>, V)>>(iter: I) -> Self {
165        RangeMap::try_from_iter(iter).ok().unwrap()
166    }
167}
168
169impl<T: Debug + PrimInt, V: Clone + Debug + Eq> RangeMap<T, V> {
170    /// Creates a new empty `RangeMap`.
171    pub fn new() -> RangeMap<T, V> {
172        RangeMap { elts: Vec::new() }
173    }
174
175    /// Builds a `RangeMap` from an iterator over pairs. If any ranges overlap, they should map to
176    /// the same value. If not, returns an [`OverlapError`].
177    pub fn try_from_iter<I: IntoIterator<Item = (Range<T>, V)>>(
178        iter: I,
179    ) -> Result<RangeMap<T, V>, OverlapError<T, V>> {
180        let mut vec: Vec<_> = iter.into_iter().collect();
181        vec.sort_by(|x, y| x.0.cmp(&y.0));
182        let mut ret = RangeMap { elts: vec };
183        let discarded = ret.normalize();
184
185        if discarded.is_empty() {
186            Ok(ret)
187        } else {
188            Err(OverlapError {
189                non_overlapping: ret,
190                discarded: discarded,
191            })
192        }
193    }
194
195    // Creates a `RangeMap` from a `Vec`, which must contain ranges in ascending order. If any
196    // ranges overlap, they must map to the same value.
197    //
198    // Panics if the ranges are not sorted, or if they overlap without mapping to the same value.
199    fn from_sorted_vec(vec: Vec<(Range<T>, V)>) -> RangeMap<T, V> {
200        let mut ret = RangeMap { elts: vec };
201        ret.normalize();
202        ret
203    }
204
205    // Creates a RangeMap from a Vec, which must be sorted and normalized.
206    //
207    // Panics unless `vec` is sorted and normalized.
208    fn from_norm_vec(vec: Vec<(Range<T>, V)>) -> RangeMap<T, V> {
209        for i in 1..vec.len() {
210            if vec[i].0.start <= vec[i - 1].0.end {
211                panic!(
212                    "vector {:?} has overlapping ranges {:?} and {:?}",
213                    vec,
214                    vec[i - 1],
215                    vec[i]
216                );
217            }
218            // If vec[i-1].0.end is T::max_value() then we've already panicked, so the unwrap is
219            // safe.
220            if vec[i].0.start == vec[i - 1].0.end.checked_add(&T::one()).unwrap()
221                && vec[i].1 == vec[i - 1].1
222            {
223                panic!(
224                    "vector {:?} has adjacent ranges with same value {:?} and {:?}",
225                    vec,
226                    vec[i - 1],
227                    vec[i]
228                );
229            }
230        }
231
232        RangeMap { elts: vec }
233    }
234
235    /// Returns the number of mapped ranges.
236    ///
237    /// Note that this is not usually the same as the number of mapped values.
238    pub fn num_ranges(&self) -> usize {
239        self.elts.len()
240    }
241
242    /// Tests whether this map is empty.
243    pub fn is_empty(&self) -> bool {
244        self.elts.is_empty()
245    }
246
247    /// Tests whether this `CharMap` maps every value.
248    pub fn is_full(&self) -> bool {
249        let mut last_end = T::min_value();
250        for &(range, _) in &self.elts {
251            if range.start > last_end {
252                return false;
253            }
254            last_end = range.end;
255        }
256        last_end == T::max_value()
257    }
258
259    /// Iterates over all the mapped ranges and values.
260    pub fn ranges_values<'a>(&'a self) -> std::slice::Iter<'a, (Range<T>, V)> {
261        self.elts.iter()
262    }
263
264    /// Iterates over all mappings.
265    pub fn keys_values<'a>(&'a self) -> PairIter<'a, T, V> {
266        PairIter {
267            map: self,
268            next_range_idx: if self.is_empty() { None } else { Some(0) },
269            next_key: if self.is_empty() {
270                T::min_value()
271            } else {
272                self.elts[0].0.start
273            },
274        }
275    }
276
277    /// Finds the value that `x` maps to, if it exists.
278    ///
279    /// Runs in `O(log n)` time, where `n` is the number of mapped ranges.
280    pub fn get(&self, x: T) -> Option<&V> {
281        self.elts
282            // The unwrap is ok because Range<T>::partial_cmp(&T) never returns None.
283            .binary_search_by(|r| r.0.partial_cmp(&x).unwrap())
284            .ok()
285            .map(|idx| &self.elts[idx].1)
286    }
287
288    // Minimizes the number of ranges in this map.
289    //
290    // If there are any overlapping ranges that map to the same data, merges them. Assumes that the
291    // ranges are sorted according to their start.
292    //
293    // If there are overlapping ranges that map to different values, we delete them. The return
294    // value is the collection of all ranges that were deleted.
295    //
296    // TODO: because the output is always smaller than the input, this could be done in-place.
297    fn normalize(&mut self) -> Vec<(Range<T>, V)> {
298        let mut vec = Vec::with_capacity(self.elts.len());
299        let mut discarded = Vec::new();
300        mem::swap(&mut vec, &mut self.elts);
301
302        for (range, val) in vec.into_iter() {
303            if let Some(&mut (ref mut last_range, ref last_val)) = self.elts.last_mut() {
304                if range.start <= last_range.end && &val != last_val {
305                    discarded.push((range, val));
306                    continue;
307                }
308
309                if range.start <= last_range.end.saturating_add(T::one()) && &val == last_val {
310                    last_range.end = max(range.end, last_range.end);
311                    continue;
312                }
313            }
314
315            self.elts.push((range, val));
316        }
317
318        discarded
319    }
320
321    /// Returns those mappings whose keys belong to the given set.
322    pub fn intersection(&self, other: &RangeSet<T>) -> RangeMap<T, V> {
323        let mut ret = Vec::new();
324        let mut other_iter = other.map.elts.iter().peekable();
325
326        for &(ref r, ref data) in &self.elts {
327            while let Some(&&(ref s, _)) = other_iter.peek() {
328                if let Some(int) = s.intersection(r) {
329                    ret.push((int, data.clone()));
330                }
331
332                if s.end >= r.end {
333                    break;
334                } else {
335                    other_iter.next();
336                }
337            }
338        }
339
340        RangeMap::from_sorted_vec(ret)
341    }
342
343    /// Counts the number of mapped keys.
344    ///
345    /// This saturates at `usize::MAX`.
346    pub fn num_keys(&self) -> usize {
347        self.ranges_values().fold(0, |acc, range| {
348            acc.saturating_add(
349                (range.0.end - range.0.start)
350                    .to_usize()
351                    .unwrap_or(usize::MAX),
352            )
353            .saturating_add(1)
354        })
355    }
356
357    /// Returns the set of mapped chars, forgetting what they are mapped to.
358    pub fn to_range_set(&self) -> RangeSet<T> {
359        RangeSet::from_sorted_vec(self.elts.iter().map(|x| (x.0, ())).collect())
360    }
361
362    /// Modifies the values in place.
363    pub fn map_values<F>(&mut self, mut f: F)
364    where
365        F: FnMut(&V) -> V,
366    {
367        for &mut (_, ref mut data) in &mut self.elts {
368            *data = f(data);
369        }
370
371        // We need to re-normalize, because we might have mapped two adjacent ranges to the same
372        // value.
373        self.normalize();
374    }
375
376    /// Modifies this map to contain only those mappings with values `v` satisfying `f(v)`.
377    pub fn retain_values<F>(&mut self, mut f: F)
378    where
379        F: FnMut(&V) -> bool,
380    {
381        self.elts.retain(|x| f(&x.1));
382    }
383
384    /// Returns a mutable view into this map.
385    ///
386    /// The ranges should not be modified, since that might violate our invariants.
387    ///
388    /// This method will eventually be removed, probably once anonymous return values allow is to
389    /// write a values_mut() iterator more easily.
390    pub fn as_mut_slice(&mut self) -> &mut [(Range<T>, V)] {
391        &mut self.elts
392    }
393}
394
395#[derive(Copy, Clone, Debug)]
396pub struct PairIter<'a, T: 'a, V: 'a> {
397    map: &'a RangeMap<T, V>,
398    next_range_idx: Option<usize>,
399    next_key: T,
400}
401
402impl<'a, T: PrimInt, V> Iterator for PairIter<'a, T, V> {
403    type Item = (T, &'a V);
404    fn next(&mut self) -> Option<Self::Item> {
405        if let Some(idx) = self.next_range_idx {
406            let ret = (self.next_key, &self.map.elts[idx].1);
407
408            if self.next_key < self.map.elts[idx].0.end {
409                self.next_key = self.next_key + T::one();
410            } else if idx < self.map.elts.len() - 1 {
411                self.next_range_idx = Some(idx + 1);
412                self.next_key = self.map.elts[idx + 1].0.start;
413            } else {
414                self.next_range_idx = None;
415            }
416
417            Some(ret)
418        } else {
419            None
420        }
421    }
422}
423
424/// A set of integers, implemented as a sorted list of (inclusive) ranges.
425#[derive(Clone, Eq, Hash, PartialEq)]
426pub struct RangeSet<T> {
427    map: RangeMap<T, ()>,
428}
429
430#[derive(Clone, Debug, Eq, Hash, PartialEq)]
431pub struct RangeIter<'a, T: PrimInt + 'a> {
432    set: &'a RangeSet<T>,
433    next_idx: usize,
434}
435
436impl<'a, T: Debug + PrimInt> Iterator for RangeIter<'a, T> {
437    type Item = Range<T>;
438    fn next(&mut self) -> Option<Range<T>> {
439        if self.next_idx < self.set.num_ranges() {
440            let ret = Some(self.set.map.elts[self.next_idx].0);
441            self.next_idx += 1;
442            ret
443        } else {
444            None
445        }
446    }
447}
448
449#[derive(Copy, Clone, Debug)]
450pub struct EltIter<'a, T: 'a + PrimInt> {
451    set: &'a RangeSet<T>,
452    next_range_idx: Option<usize>,
453    next_elt: T,
454}
455
456impl<'a, T: Debug + PrimInt> Iterator for EltIter<'a, T> {
457    type Item = T;
458    fn next(&mut self) -> Option<T> {
459        if let Some(idx) = self.next_range_idx {
460            let ret = Some(self.next_elt);
461            if self.next_elt >= self.set.map.elts[idx].0.end {
462                if idx + 1 < self.set.num_ranges() {
463                    self.next_range_idx = Some(idx + 1);
464                    self.next_elt = self.set.map.elts[idx + 1].0.start;
465                } else {
466                    self.next_range_idx = None;
467                }
468            } else {
469                self.next_elt = self.next_elt + T::one();
470            }
471            ret
472        } else {
473            None
474        }
475    }
476}
477
478impl<T: Debug + PrimInt> Debug for RangeSet<T> {
479    // When alternate formatting is specified, only prints out the first buch of mappings.
480    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
481        try!(f.write_fmt(format_args!("RangeSet (")));
482
483        if f.alternate() {
484            try!(f
485                .debug_set()
486                .entries(self.ranges().take(DISPLAY_LIMIT))
487                .finish());
488            if self.num_ranges() > DISPLAY_LIMIT {
489                try!(f.write_str("..."));
490            }
491        } else {
492            try!(f.debug_set().entries(self.ranges()).finish());
493        }
494        try!(f.write_str(")"));
495        Ok(())
496    }
497}
498
499impl<T: Debug + PrimInt> FromIterator<Range<T>> for RangeSet<T> {
500    /// Builds a `RangeSet` from an iterator over `Range`s.
501    fn from_iter<I: IntoIterator<Item = Range<T>>>(iter: I) -> Self {
502        RangeSet {
503            // The unwrap here is ok because RangeMap::try_from_iter only fails when two
504            // overlapping ranges map to different values. Since every range here maps to the same
505            // value, (i.e.  ()), this will never happen.
506            map: RangeMap::try_from_iter(iter.into_iter().map(|x| (x, ())))
507                .ok()
508                .unwrap(),
509        }
510    }
511}
512
513impl<T: Debug + PrimInt> RangeSet<T> {
514    /// Creates a new empty `RangeSet`.
515    pub fn new() -> RangeSet<T> {
516        RangeSet {
517            map: RangeMap::new(),
518        }
519    }
520
521    /// Tests if this set is empty.
522    pub fn is_empty(&self) -> bool {
523        self.map.is_empty()
524    }
525
526    /// Tests whether this set contains every valid value of `T`.
527    pub fn is_full(&self) -> bool {
528        // We are assuming normalization here.
529        self.num_ranges() == 1 && self.map.elts[0].0 == Range::full()
530    }
531
532    /// Returns the number of ranges used to represent this set.
533    pub fn num_ranges(&self) -> usize {
534        self.map.num_ranges()
535    }
536
537    /// Returns the number of elements in the set.
538    ///
539    /// This saturates at `usize::MAX`.
540    pub fn num_elements(&self) -> usize {
541        self.map.num_keys()
542    }
543
544    /// Returns an iterator over all ranges in this set.
545    pub fn ranges<'a>(&'a self) -> RangeIter<'a, T> {
546        RangeIter {
547            set: self,
548            next_idx: 0,
549        }
550    }
551
552    /// Returns an iterator over all elements in this set.
553    pub fn elements<'a>(&'a self) -> EltIter<'a, T> {
554        if self.map.elts.is_empty() {
555            EltIter {
556                set: self,
557                next_range_idx: None,
558                next_elt: T::min_value(),
559            }
560        } else {
561            EltIter {
562                set: self,
563                next_range_idx: Some(0),
564                next_elt: self.map.elts[0].0.start,
565            }
566        }
567    }
568
569    /// Checks if this set contains a value.
570    pub fn contains(&self, val: T) -> bool {
571        self.map.get(val).is_some()
572    }
573
574    // Creates a RangeSet from a vector. The vector must be sorted, but it does not need to be
575    // normalized.
576    fn from_sorted_vec(vec: Vec<(Range<T>, ())>) -> RangeSet<T> {
577        RangeSet {
578            map: RangeMap::from_sorted_vec(vec),
579        }
580    }
581
582    // Creates a RangeSet from a vector. The vector must be normalized, in the sense that it should
583    // contain no adjacent ranges.
584    fn from_norm_vec(vec: Vec<(Range<T>, ())>) -> RangeSet<T> {
585        RangeSet {
586            map: RangeMap::from_norm_vec(vec),
587        }
588    }
589
590    /// Returns the union between `self` and `other`.
591    pub fn union(&self, other: &RangeSet<T>) -> RangeSet<T> {
592        if self.is_empty() {
593            return other.clone();
594        } else if other.is_empty() {
595            return self.clone();
596        }
597
598        let mut ret = Vec::with_capacity(self.map.elts.len() + other.map.elts.len());
599        let mut it1 = self.map.elts.iter();
600        let mut it2 = other.map.elts.iter();
601        let mut r1 = it1.next();
602        let mut r2 = it2.next();
603        let mut cur_range: Option<Range<T>> = None;
604
605        while r1.is_some() || r2.is_some() {
606            let r1_start = if let Some(&(r, _)) = r1 {
607                r.start
608            } else {
609                T::max_value()
610            };
611            let r2_start = if let Some(&(r, _)) = r2 {
612                r.start
613            } else {
614                T::max_value()
615            };
616            if let Some(cur) = cur_range {
617                if min(r1_start, r2_start) > cur.end.saturating_add(T::one()) {
618                    ret.push((cur_range.unwrap(), ()));
619                    cur_range = None;
620                }
621            }
622
623            let cover = |cur: &mut Option<Range<T>>, next: &Range<T>| {
624                if let &mut Some(ref mut r) = cur {
625                    *r = r.cover(next);
626                } else {
627                    *cur = Some(*next);
628                }
629            };
630
631            if r1_start < r2_start || r2.is_none() {
632                cover(&mut cur_range, &r1.unwrap().0);
633                r1 = it1.next();
634            } else {
635                cover(&mut cur_range, &r2.unwrap().0);
636                r2 = it2.next();
637            }
638        }
639
640        if cur_range.is_some() {
641            ret.push((cur_range.unwrap(), ()));
642        }
643
644        RangeSet::from_norm_vec(ret)
645    }
646
647    /// Creates a set that contains every value of `T`.
648    pub fn full() -> RangeSet<T> {
649        RangeSet::from_norm_vec(vec![(Range::full(), ())])
650    }
651
652    /// Creates a set containing a single element.
653    pub fn single(x: T) -> RangeSet<T> {
654        RangeSet::from_norm_vec(vec![(Range::single(x), ())])
655    }
656
657    /// Creates a set containing all elements except the given ones. The input iterator must be
658    /// sorted. If it is not, this will return `None`.
659    pub fn except<I: Iterator<Item = T>>(it: I) -> Option<RangeSet<T>> {
660        let mut ret = Vec::new();
661        let mut next_allowed = T::min_value();
662        let mut last_forbidden = T::max_value();
663
664        for i in it {
665            if i > next_allowed {
666                ret.push((Range::new(next_allowed, i - T::one()), ()));
667            } else if i < next_allowed.saturating_sub(T::one()) {
668                return None;
669            }
670
671            last_forbidden = i;
672            next_allowed = i.saturating_add(T::one());
673        }
674
675        if last_forbidden < T::max_value() {
676            ret.push((Range::new(last_forbidden + T::one(), T::max_value()), ()));
677        }
678        Some(RangeSet::from_norm_vec(ret))
679    }
680
681    /// Finds the intersection between this set and `other`.
682    pub fn intersection(&self, other: &RangeSet<T>) -> RangeSet<T> {
683        RangeSet {
684            map: self.map.intersection(other),
685        }
686    }
687
688    /// Returns the set of all characters that are not in this set.
689    pub fn negated(&self) -> RangeSet<T> {
690        let mut ret = Vec::with_capacity(self.num_ranges() + 1);
691        let mut last_end = T::min_value();
692
693        for range in self.ranges() {
694            if range.start > last_end {
695                ret.push((Range::new(last_end, range.start - T::one()), ()));
696            }
697            last_end = range.end.saturating_add(T::one());
698        }
699        if last_end < T::max_value() {
700            ret.push((Range::new(last_end, T::max_value()), ()));
701        }
702
703        RangeSet::from_norm_vec(ret)
704    }
705}
706
707/// A multi-valued mapping from primitive integers to other data.
708#[derive(Clone, Eq, Hash, PartialEq)]
709pub struct RangeMultiMap<T, V> {
710    elts: Vec<(Range<T>, V)>,
711}
712
713impl<T: Debug + PrimInt, V: Clone + Debug + PartialEq> FromIterator<(Range<T>, V)>
714    for RangeMultiMap<T, V>
715{
716    /// Builds a `RangeMultiMap` from an iterator over `Range` and values..
717    fn from_iter<I: IntoIterator<Item = (Range<T>, V)>>(iter: I) -> Self {
718        RangeMultiMap::from_vec(iter.into_iter().collect())
719    }
720}
721
722impl<T: Debug + PrimInt, V: Clone + Debug + PartialEq> Debug for RangeMultiMap<T, V> {
723    fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
724        try!(f.write_fmt(format_args!("RangeMultiMap (")));
725
726        if f.alternate() {
727            try!(f
728                .debug_map()
729                .entries(
730                    self.ranges_values()
731                        .map(|x| (&x.0, &x.1))
732                        .take(DISPLAY_LIMIT)
733                )
734                .finish());
735            if self.num_ranges() > DISPLAY_LIMIT {
736                try!(f.write_str("..."));
737            }
738        } else {
739            try!(f
740                .debug_set()
741                .entries(self.ranges_values().map(|x| (&x.0, &x.1)))
742                .finish());
743        }
744        try!(f.write_str(")"));
745        Ok(())
746    }
747}
748
749impl<T: Debug + PrimInt, V: Clone + Debug + PartialEq> RangeMultiMap<T, V> {
750    /// Creates a new empty map.
751    pub fn new() -> RangeMultiMap<T, V> {
752        RangeMultiMap { elts: Vec::new() }
753    }
754
755    /// Returns the number of mapped ranges.
756    pub fn num_ranges(&self) -> usize {
757        self.elts.len()
758    }
759
760    /// Checks if the map is empty.
761    pub fn is_empty(&self) -> bool {
762        self.elts.is_empty()
763    }
764
765    /// Adds a new mapping from a range of characters to `value`.
766    pub fn insert(&mut self, range: Range<T>, value: V) {
767        self.elts.push((range, value));
768    }
769
770    /// Creates a map from a vector of pairs.
771    pub fn from_vec(vec: Vec<(Range<T>, V)>) -> RangeMultiMap<T, V> {
772        RangeMultiMap { elts: vec }
773    }
774
775    /// Returns a new `RangeMultiMap` containing only the mappings for keys that belong to the
776    /// given set.
777    pub fn intersection(&self, other: &RangeSet<T>) -> RangeMultiMap<T, V> {
778        let mut ret = Vec::new();
779        for &(ref my_range, ref data) in &self.elts {
780            let start_idx = other
781                .map
782                .elts
783                .binary_search_by(|r| r.0.end.cmp(&my_range.start))
784                .unwrap_or_else(|x| x);
785            for &(ref other_range, _) in &other.map.elts[start_idx..] {
786                if my_range.start > other_range.end {
787                    break;
788                } else if let Some(r) = my_range.intersection(other_range) {
789                    ret.push((r, data.clone()));
790                }
791            }
792        }
793        RangeMultiMap::from_vec(ret)
794    }
795
796    pub fn map_values<F>(&mut self, mut f: F)
797    where
798        F: FnMut(&V) -> V,
799    {
800        for i in 0..self.elts.len() {
801            self.elts[i].1 = f(&self.elts[i].1);
802        }
803    }
804
805    /// Modifies this map in place to only contain mappings whose values `v` satisfy `f(v)`.
806    pub fn retain_values<F>(&mut self, mut f: F)
807    where
808        F: FnMut(&V) -> bool,
809    {
810        self.elts.retain(|x| f(&x.1));
811    }
812
813    /// Returns the underlying `Vec`.
814    pub fn into_vec(self) -> Vec<(Range<T>, V)> {
815        self.elts
816    }
817
818    /// Iterates over all the mapped ranges and values.
819    pub fn ranges_values<'a>(&'a self) -> std::slice::Iter<'a, (Range<T>, V)> {
820        self.elts.iter()
821    }
822}
823
824impl<T: Debug + PrimInt, V: Clone + Debug + Ord> RangeMultiMap<T, V> {
825    /// Makes the ranges sorted and non-overlapping. The data associated with each range will
826    /// be a `Vec<T>` instead of a single `T`.
827    pub fn group(&self) -> RangeMap<T, Vec<V>> {
828        if self.elts.is_empty() {
829            return RangeMap::new();
830        }
831
832        let mut start_chars = Vec::with_capacity(self.elts.len() * 2);
833
834        for &(ref range, _) in self.elts.iter() {
835            start_chars.push(range.start);
836            if range.end < T::max_value() {
837                start_chars.push(range.end + T::one());
838            }
839        }
840        start_chars.sort();
841        start_chars.dedup();
842
843        let mut ret: Vec<(Range<T>, Vec<V>)> = Vec::with_capacity(start_chars.len());
844        for pair in start_chars.windows(2) {
845            ret.push((Range::new(pair[0], pair[1] - T::one()), Vec::new()));
846        }
847        ret.push((
848            Range::new(*start_chars.last().unwrap(), T::max_value()),
849            Vec::new(),
850        ));
851        for &(range, ref val) in self.elts.iter() {
852            // The unwrap is OK because start_chars contains range.start for every range in elts.
853            let mut idx = start_chars.binary_search(&range.start).unwrap();
854            while idx < start_chars.len() && start_chars[idx] <= range.end {
855                ret[idx].1.push(val.clone());
856                idx += 1;
857            }
858        }
859        ret.retain(|x| !x.1.is_empty());
860        RangeMap::from_sorted_vec(ret)
861    }
862}
863
864#[cfg(test)]
865mod tests {
866    use super::*;
867    use num_iter::range_inclusive;
868    use num_traits::PrimInt;
869    use quickcheck::{quickcheck, Arbitrary, Gen, TestResult};
870    use std::cmp::{max, min};
871    use std::fmt::Debug;
872    use std::i32;
873    use std::ops::Add;
874
875    impl<T: Arbitrary + Debug + PrimInt> Arbitrary for Range<T> {
876        fn arbitrary<G: Gen>(g: &mut G) -> Self {
877            let a = T::arbitrary(g);
878            let b = T::arbitrary(g);
879            Range::new(min(a, b), max(a, b))
880        }
881    }
882
883    impl<T> Arbitrary for RangeMultiMap<T, i32>
884    where
885        T: Arbitrary + Debug + PrimInt,
886    {
887        fn arbitrary<G: Gen>(g: &mut G) -> Self {
888            RangeMultiMap::from_vec(Vec::arbitrary(g))
889        }
890
891        fn shrink(&self) -> Box<Iterator<Item = Self>> {
892            Box::new(self.elts.shrink().map(|v| RangeMultiMap::from_vec(v)))
893        }
894    }
895
896    impl<T> Arbitrary for RangeMap<T, i32>
897    where
898        T: Arbitrary + Debug + PrimInt,
899    {
900        fn arbitrary<G: Gen>(g: &mut G) -> Self {
901            let map: RangeMap<T, Vec<_>> = RangeMultiMap::arbitrary(g).group();
902            // TODO: replace fold with sum once it's stable
903            map.ranges_values()
904                .map(|x| (x.0, x.1.iter().fold(0, Add::add)))
905                .collect()
906        }
907
908        fn shrink(&self) -> Box<Iterator<Item = Self>> {
909            Box::new(self.elts.shrink().map(|v| RangeMap::from_norm_vec(v)))
910        }
911    }
912
913    impl<T: Arbitrary + Debug + PrimInt> Arbitrary for RangeSet<T> {
914        fn arbitrary<G: Gen>(g: &mut G) -> Self {
915            RangeMap::arbitrary(g).to_range_set()
916        }
917
918        fn shrink(&self) -> Box<Iterator<Item = Self>> {
919            Box::new(self.map.elts.shrink().map(|v| RangeSet::from_norm_vec(v)))
920        }
921    }
922
923    #[test]
924    fn range_intersects_intersection() {
925        fn prop(r1: Range<i32>, r2: Range<i32>) -> bool {
926            r1.intersection(&r2).is_some() == r1.intersects(&r2)
927        }
928        quickcheck(prop as fn(_, _) -> _);
929    }
930
931    #[test]
932    fn range_intersection_contains() {
933        fn prop(r1: Range<i32>, r2: Range<i32>, x: i32) -> TestResult {
934            if let Some(r) = r1.intersection(&r2) {
935                TestResult::from_bool(r.contains(x) == (r1.contains(x) && r2.contains(x)))
936            } else {
937                TestResult::discard()
938            }
939        }
940        quickcheck(prop as fn(_, _, _) -> _);
941    }
942
943    #[test]
944    #[should_panic]
945    fn range_backwards() {
946        map(vec![(5, 1, 1), (6, 10, 2)]);
947    }
948
949    #[test]
950    fn range_intersection_cover() {
951        fn prop(r1: Range<i32>, r2: Range<i32>) -> bool {
952            r1 == r1.cover(&r2).intersection(&r1).unwrap()
953        }
954        quickcheck(prop as fn(_, _) -> _);
955    }
956
957    fn map(vec: Vec<(i32, i32, i32)>) -> RangeMap<i32, i32> {
958        vec.into_iter()
959            .map(|(a, b, c)| (Range::new(a, b), c))
960            .collect()
961    }
962
963    #[test]
964    fn rangemap_overlapping() {
965        assert_eq!(map(vec![(1, 5, 1), (2, 10, 1)]), map(vec![(1, 10, 1)]));
966        assert_eq!(
967            map(vec![(1, 5, 1), (2, 10, 1), (9, 11, 1)]),
968            map(vec![(1, 11, 1)])
969        );
970        map(vec![(1, 5, 1), (6, 10, 2)]);
971    }
972
973    #[test]
974    #[should_panic]
975    fn rangemap_overlapping_nonequal() {
976        map(vec![(1, 5, 1), (5, 10, 2)]);
977    }
978
979    #[test]
980    fn rangemap_intersection() {
981        fn prop(map: RangeMap<i32, i32>, set: RangeSet<i32>) -> bool {
982            let int = map.intersection(&set);
983            set.elements().all(|x| map.get(x) == int.get(x))
984                && int.keys_values().all(|x| set.contains(x.0))
985        }
986        quickcheck(prop as fn(_, _) -> _);
987    }
988
989    #[test]
990    fn rangemap_num_ranges() {
991        fn prop(map: RangeMap<i32, i32>) -> bool {
992            map.num_ranges() == map.ranges_values().count()
993        }
994        quickcheck(prop as fn(_) -> _);
995    }
996
997    #[test]
998    fn rangemap_num_keys() {
999        fn prop(map: RangeMap<i32, i32>) -> bool {
1000            map.num_keys() == map.keys_values().count()
1001        }
1002        quickcheck(prop as fn(_) -> _);
1003    }
1004
1005    #[test]
1006    fn rangemap_map_values() {
1007        fn prop(map: RangeMap<i32, i32>, x: i32) -> bool {
1008            let f = |y: &i32| (x + *y) % 10;
1009            let new_map = {
1010                let mut new_map = map.clone();
1011                new_map.map_values(&f);
1012                new_map
1013            };
1014            let new_map_norm = {
1015                let mut new_map_norm = new_map.clone();
1016                new_map_norm.normalize();
1017                new_map_norm
1018            };
1019
1020            new_map
1021                .keys_values()
1022                .all(|(k, v)| f(map.get(k).unwrap()) == *v)
1023                && map
1024                    .keys_values()
1025                    .all(|(k, v)| *new_map.get(k).unwrap() == f(v))
1026                && new_map == new_map_norm
1027        }
1028        quickcheck(prop as fn(_, _) -> _);
1029    }
1030
1031    #[test]
1032    fn rangemap_retain_values() {
1033        fn prop(map: RangeMap<i32, i32>, r: Range<i32>) -> bool {
1034            let mut new_map = map.clone();
1035            new_map.retain_values(|v| r.contains(*v));
1036            new_map.keys_values().all(|(_, v)| r.contains(*v))
1037                && map
1038                    .keys_values()
1039                    .all(|(k, v)| !r.contains(*v) || new_map.get(k).unwrap() == v)
1040        }
1041        quickcheck(prop as fn(_, _) -> _);
1042    }
1043
1044    #[test]
1045    fn rangeset_contains() {
1046        fn prop(set: RangeSet<i32>) -> bool {
1047            set.elements().all(|e| set.contains(e))
1048        }
1049        quickcheck(prop as fn(_) -> _);
1050    }
1051
1052    #[test]
1053    fn rangeset_num_ranges() {
1054        fn prop(set: RangeSet<i32>) -> bool {
1055            set.num_ranges() == set.ranges().count()
1056        }
1057        quickcheck(prop as fn(_) -> _);
1058    }
1059
1060    #[test]
1061    fn rangeset_num_elements() {
1062        fn prop(set: RangeSet<i32>) -> bool {
1063            set.num_elements() == set.elements().count()
1064        }
1065        quickcheck(prop as fn(_) -> _);
1066    }
1067
1068    #[test]
1069    fn rangeset_union() {
1070        fn prop(s1: RangeSet<i32>, s2: RangeSet<i32>) -> bool {
1071            let un = s1.union(&s2);
1072            un.elements().all(|e| s1.contains(e) || s2.contains(e))
1073                && s1.elements().all(|e| un.contains(e))
1074                && s2.elements().all(|e| un.contains(e))
1075        }
1076        quickcheck(prop as fn(_, _) -> _);
1077    }
1078
1079    #[test]
1080    fn rangeset_intersection() {
1081        fn prop(s1: RangeSet<i32>, s2: RangeSet<i32>) -> bool {
1082            let int = s1.intersection(&s2);
1083            int.elements().all(|e| s1.contains(e) && s2.contains(e))
1084                && s1.elements().all(|e| int.contains(e) == s2.contains(e))
1085        }
1086        quickcheck(prop as fn(_, _) -> _);
1087    }
1088
1089    #[test]
1090    fn rangeset_negate() {
1091        fn prop(set: RangeSet<i8>) -> bool {
1092            let neg = set.negated();
1093            neg.elements().all(|e| !set.contains(e))
1094                && set.elements().all(|e| !neg.contains(e))
1095                && neg.negated() == set
1096        }
1097        quickcheck(prop as fn(_) -> _);
1098    }
1099
1100    #[test]
1101    fn rangeset_except() {
1102        fn prop(mut except: Vec<i8>) -> bool {
1103            except.sort();
1104            let set = RangeSet::except(except.iter().cloned()).unwrap();
1105            set.elements().all(|e| except.binary_search(&e).is_err())
1106                && except.iter().all(|&e| !set.contains(e))
1107        }
1108        quickcheck(prop as fn(_) -> _);
1109    }
1110
1111    #[test]
1112    fn rangeset_except_unsorted() {
1113        assert_eq!(None, RangeSet::except([1i32, 3, 2].iter().cloned()));
1114    }
1115
1116    // Check that things don't panic when we have MIN and MAX in the ranges (quickcheck doesn't
1117    // check this properly).
1118    #[test]
1119    fn rangemultimap_boundaries() {
1120        assert_eq!(
1121            RangeMultiMap::from_vec(vec![
1122                (Range::new(i32::MIN, 200), 5),
1123                (Range::new(100, i32::MAX), 10),
1124            ])
1125            .group(),
1126            RangeMap::from_sorted_vec(vec![
1127                (Range::new(i32::MIN, 99), vec![5]),
1128                (Range::new(100, 200), vec![5, 10]),
1129                (Range::new(201, i32::MAX), vec![10]),
1130            ])
1131        );
1132    }
1133
1134    #[test]
1135    fn rangemultimap_group() {
1136        fn prop(mm_vec: Vec<(Range<i32>, i32)>) -> bool {
1137            let mm = RangeMultiMap::from_vec(mm_vec.clone());
1138            let grouped = mm.group();
1139            mm_vec.into_iter().all(|(range, val)| {
1140                range_inclusive(range.start, range.end)
1141                    .all(|i| grouped.get(i).unwrap().contains(&val))
1142            })
1143        }
1144        quickcheck(prop as fn(_) -> _);
1145    }
1146}