Converts this set into a canonical ordering.
(&mut self)
| 271 | |
| 272 | /// Converts this set into a canonical ordering. |
| 273 | fn canonicalize(&mut self) { |
| 274 | if self.is_canonical() { |
| 275 | return; |
| 276 | } |
| 277 | self.ranges.sort(); |
| 278 | assert!(!self.ranges.is_empty()); |
| 279 | |
| 280 | // Is there a way to do this in-place with constant memory? I couldn't |
| 281 | // figure out a way to do it. So just append the canonicalization to |
| 282 | // the end of this range, and then drain it before we're done. |
| 283 | let drain_end = self.ranges.len(); |
| 284 | for oldi in 0..drain_end { |
| 285 | // If we've added at least one new range, then check if we can |
| 286 | // merge this range in the previously added range. |
| 287 | if self.ranges.len() > drain_end { |
| 288 | let (last, rest) = self.ranges.split_last_mut().unwrap(); |
| 289 | if let Some(union) = last.union(&rest[oldi]) { |
| 290 | *last = union; |
| 291 | continue; |
| 292 | } |
| 293 | } |
| 294 | let range = self.ranges[oldi]; |
| 295 | self.ranges.push(range); |
| 296 | } |
| 297 | self.ranges.drain(..drain_end); |
| 298 | } |
| 299 | |
| 300 | /// Returns true if and only if this class is in a canonical ordering. |
| 301 | fn is_canonical(&self) -> bool { |
no test coverage detected