Compute the union of this range with the `other`. This raises a ``ValueError`` exception if the two ranges are "disjunct", that is neither adjacent nor overlapping.
(self, other: Range[_T])
| 497 | ) |
| 498 | |
| 499 | def union(self, other: Range[_T]) -> Range[_T]: |
| 500 | """Compute the union of this range with the `other`. |
| 501 | |
| 502 | This raises a ``ValueError`` exception if the two ranges are |
| 503 | "disjunct", that is neither adjacent nor overlapping. |
| 504 | """ |
| 505 | |
| 506 | # Empty ranges are "additive identities" |
| 507 | if self.empty: |
| 508 | return other |
| 509 | if other.empty: |
| 510 | return self |
| 511 | |
| 512 | if not self.overlaps(other) and not self.adjacent_to(other): |
| 513 | raise ValueError( |
| 514 | "Adding non-overlapping and non-adjacent" |
| 515 | " ranges is not implemented" |
| 516 | ) |
| 517 | |
| 518 | slower = self.lower |
| 519 | slower_b = self.bounds[0] |
| 520 | supper = self.upper |
| 521 | supper_b = self.bounds[1] |
| 522 | olower = other.lower |
| 523 | olower_b = other.bounds[0] |
| 524 | oupper = other.upper |
| 525 | oupper_b = other.bounds[1] |
| 526 | |
| 527 | if self._compare_edges(slower, slower_b, olower, olower_b) < 0: |
| 528 | rlower = slower |
| 529 | rlower_b = slower_b |
| 530 | else: |
| 531 | rlower = olower |
| 532 | rlower_b = olower_b |
| 533 | |
| 534 | if self._compare_edges(supper, supper_b, oupper, oupper_b) > 0: |
| 535 | rupper = supper |
| 536 | rupper_b = supper_b |
| 537 | else: |
| 538 | rupper = oupper |
| 539 | rupper_b = oupper_b |
| 540 | |
| 541 | return Range( |
| 542 | rlower, rupper, bounds=cast(_BoundsType, rlower_b + rupper_b) |
| 543 | ) |
| 544 | |
| 545 | def __add__(self, other: Range[_T]) -> Range[_T]: |
| 546 | return self.union(other) |
no test coverage detected