Intersect this set with the given set, in place.
(&mut self, other: &IntervalSet<I>)
| 90 | |
| 91 | /// Intersect this set with the given set, in place. |
| 92 | pub fn intersect(&mut self, other: &IntervalSet<I>) { |
| 93 | if self.ranges.is_empty() { |
| 94 | return; |
| 95 | } |
| 96 | if other.ranges.is_empty() { |
| 97 | self.ranges.clear(); |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | // There should be a way to do this in-place with constant memory, |
| 102 | // but I couldn't figure out a simple way to do it. So just append |
| 103 | // the intersection to the end of this range, and then drain it before |
| 104 | // we're done. |
| 105 | let drain_end = self.ranges.len(); |
| 106 | |
| 107 | let mut ita = (0..drain_end).into_iter(); |
| 108 | let mut itb = (0..other.ranges.len()).into_iter(); |
| 109 | let mut a = ita.next().unwrap(); |
| 110 | let mut b = itb.next().unwrap(); |
| 111 | loop { |
| 112 | if let Some(ab) = self.ranges[a].intersect(&other.ranges[b]) { |
| 113 | self.ranges.push(ab); |
| 114 | } |
| 115 | let (it, aorb) = |
| 116 | if self.ranges[a].upper() < other.ranges[b].upper() { |
| 117 | (&mut ita, &mut a) |
| 118 | } else { |
| 119 | (&mut itb, &mut b) |
| 120 | }; |
| 121 | match it.next() { |
| 122 | Some(v) => *aorb = v, |
| 123 | None => break, |
| 124 | } |
| 125 | } |
| 126 | self.ranges.drain(..drain_end); |
| 127 | } |
| 128 | |
| 129 | /// Subtract the given set from this set, in place. |
| 130 | pub fn difference(&mut self, other: &IntervalSet<I>) { |