From 5687353afe412fa93f4fa1d75850ade018290439 Mon Sep 17 00:00:00 2001 From: Mayur Dhavan <118877752+mayur-dhavan@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:55:07 +0530 Subject: [PATCH 1/7] Fix IntervalDict.combine dropping intersection pieces The sweep introduced in 8db341e iterates d.items() (non-atomic keys) and can advance past a later key that fills an interior gap of an earlier non-atomic key (e.g. a singleton inside a union), silently dropping those points from the combined result. Iterate globally sorted atomic pieces on both sides instead. --- portion/dict.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/portion/dict.py b/portion/dict.py index f779104..1622e61 100644 --- a/portion/dict.py +++ b/portion/dict.py @@ -292,7 +292,20 @@ def _how(x, y, i): intersection = dom1 & dom2 d1, d2 = self[intersection], other[intersection] - i_iter, o_iter = iter(d1.items()), iter(d2.items()) + def _atomic_items(d): + # Iterating keys then their atomic pieces does not guarantee a + # globally sorted stream: later keys may fill gaps in earlier, + # non-atomic ones (e.g. a singleton inside a union). The sweep + # below requires sorted, disjoint atomic intervals. + items = [ + (atomic, value) + for interval, value in d.items() + for atomic in interval + ] + items.sort(key=lambda item: (item[0].lower, item[0].left is Bound.OPEN)) + return iter(items) + + i_iter, o_iter = _atomic_items(d1), _atomic_items(d2) i_current, o_current = next(i_iter, None), next(o_iter, None) while i_current is not None and o_current is not None: From 9bc9ab951a469bdfaeffc3874e391a164c046a54 Mon Sep 17 00:00:00 2001 From: Mayur Dhavan <118877752+mayur-dhavan@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:55:50 +0530 Subject: [PATCH 2/7] Remove trailing blank line From 03866ad6a511c9c1ca1254ee4aced88209b321ab Mon Sep 17 00:00:00 2001 From: Mayur Dhavan <118877752+mayur-dhavan@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:57:11 +0530 Subject: [PATCH 3/7] Remove trailing blank line From bc15aeb3bfb86afb8edf6f19a54cca81121bce3c Mon Sep 17 00:00:00 2001 From: Mayur Dhavan <118877752+mayur-dhavan@users.noreply.github.com> Date: Mon, 26 Oct 2026 14:58:23 +0530 Subject: [PATCH 4/7] Add regression test for combine with non-atomic key and interior piece Added a test for combining non-atomic intervals with interior pieces in the IntervalDict class. --- tests/test_dict.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/test_dict.py b/tests/test_dict.py index 12fa559..764511d 100644 --- a/tests/test_dict.py +++ b/tests/test_dict.py @@ -279,6 +279,33 @@ def test_combine_one_interval_over_many(self): ] ) + def test_combine_non_atomic_interval_with_interior_piece(self): + # A non-atomic interval key merged with a singleton that fills its + # interior gap must not lose the singleton when combined. + left = P.IntervalDict( + [ + (P.closedopen(3, 4) | P.openclosed(4, 5), "sides"), + (P.singleton(4), "middle"), + ] + ) + right = P.IntervalDict([(P.open(3, 5), "right")]) + + assert left.combine(right, lambda x, y: (x, y)) == P.IntervalDict( + [ + (P.singleton(3) | P.singleton(5), "sides"), + (P.open(3, 4) | P.open(4, 5), ("sides", "right")), + (P.singleton(4), ("middle", "right")), + ] + ) + # Mirrored: non-atomic key on the right, singleton on the left. + assert right.combine(left, lambda x, y: (x, y)) == P.IntervalDict( + [ + (P.singleton(3) | P.singleton(5), "sides"), + (P.open(3, 4) | P.open(4, 5), ("right", "sides")), + (P.singleton(4), ("right", "middle")), + ] + ) + def test_combine_missing(self): def how(x, y): return x, y From 581a5a96d3be0f2b52d10f7b56e16d5f3c27dd93 Mon Sep 17 00:00:00 2001 From: Mayur Dhavan <118877752+mayur-dhavan@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:35:32 +0530 Subject: [PATCH 5/7] Invoke how once per intersecting key pair with the union of the intersection --- portion/dict.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/portion/dict.py b/portion/dict.py index 1622e61..6543782 100644 --- a/portion/dict.py +++ b/portion/dict.py @@ -296,9 +296,11 @@ def _atomic_items(d): # Iterating keys then their atomic pieces does not guarantee a # globally sorted stream: later keys may fill gaps in earlier, # non-atomic ones (e.g. a singleton inside a union). The sweep - # below requires sorted, disjoint atomic intervals. + # below requires sorted, disjoint atomic intervals. Each atomic + # piece belongs to exactly one stored key, which is carried along + # to group intersections by original key pair. items = [ - (atomic, value) + (atomic, value, interval) for interval, value in d.items() for atomic in interval ] @@ -308,14 +310,21 @@ def _atomic_items(d): i_iter, o_iter = _atomic_items(d1), _atomic_items(d2) i_current, o_current = next(i_iter, None), next(o_iter, None) + # Intersections are accumulated per original key pair, so that `how` + # is invoked exactly once for each pair of intersecting keys, with the + # (possibly non-atomic) intersection of these keys. + pairwise = {} + while i_current is not None and o_current is not None: - i1, v1 = i_current - i2, v2 = o_current + i1, v1, k1 = i_current + i2, v2, k2 = o_current if i1.overlaps(i2): - i = i1 & i2 - v = _how(v1, v2, i) - new_items.append((i, v)) + key = (k1, k2) + if key in pairwise: + pairwise[key][0].append(i1 & i2) + else: + pairwise[key] = [[i1 & i2], v1, v2] if i1.upper < i2.upper: i_current = next(i_iter, None) @@ -331,6 +340,10 @@ def _atomic_items(d): # Equal upper bounds and openness: advance both intervals. i_current, o_current = next(i_iter, None), next(o_iter, None) + for pieces, v1, v2 in pairwise.values(): + i = pieces[0] if len(pieces) == 1 else Interval(*pieces) + new_items.append((i, _how(v1, v2, i))) + return self.__class__(new_items) def as_dict(self, atomic=False): From 0f622ab0174d97247a3d9da5279bf54ac60c6cad Mon Sep 17 00:00:00 2001 From: Mayur Dhavan <118877752+mayur-dhavan@users.noreply.github.com> Date: Mon, 26 Oct 2026 15:35:53 +0530 Subject: [PATCH 6/7] Add regression tests for pass_interval with non-atomic keys --- tests/test_dict.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/test_dict.py b/tests/test_dict.py index 764511d..8b5335e 100644 --- a/tests/test_dict.py +++ b/tests/test_dict.py @@ -350,6 +350,40 @@ def how(x, y, z): ] ) + def test_combine_pass_interval_non_atomic_keys(self): + # With non-atomic keys on both sides, `how` must be invoked once per + # pair of intersecting keys, with the (non-atomic) intersection. + calls = [] + + def how(x, y, i): + calls.append((x, y, i)) + return i + + key = P.closed(0, 1) | P.closed(2, 3) + d1 = P.IntervalDict([(key, 1)]) + d2 = P.IntervalDict([(key, 2)]) + + assert d1.combine(d2, how, pass_interval=True) == P.IntervalDict([(key, key)]) + assert calls == [(1, 2, key)] + + def test_combine_pass_interval_non_atomic_spanning_keys(self): + # A non-atomic key spanning another key's gap must still lead to a + # single `how` call per intersecting key pair, with the union. + calls = [] + + def how(x, y, i): + calls.append((x, y, i)) + return i + + d1 = P.IntervalDict([(P.closed(0, 1) | P.closed(2, 3), 1)]) + d2 = P.IntervalDict([(P.closed(0, 3), 2)]) + + union = P.closed(0, 1) | P.closed(2, 3) + assert d1.combine(d2, how, pass_interval=True) == P.IntervalDict( + [(union, union), (P.open(1, 2), 2)] + ) + assert calls == [(1, 2, union)] + def test_containment(self): d = P.IntervalDict([(P.closed(0, 3), 0)]) assert 0 in d From 6b4774319a3ed0bcb97394bdcccc332171f6451d Mon Sep 17 00:00:00 2001 From: Mayur Dhavan <118877752+mayur-dhavan@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:21:39 +0530 Subject: [PATCH 7/7] Restore the pairwise intersection loop for combine --- portion/dict.py | 57 ++++++------------------------------------------- 1 file changed, 6 insertions(+), 51 deletions(-) diff --git a/portion/dict.py b/portion/dict.py index 6543782..14b30b6 100644 --- a/portion/dict.py +++ b/portion/dict.py @@ -292,57 +292,12 @@ def _how(x, y, i): intersection = dom1 & dom2 d1, d2 = self[intersection], other[intersection] - def _atomic_items(d): - # Iterating keys then their atomic pieces does not guarantee a - # globally sorted stream: later keys may fill gaps in earlier, - # non-atomic ones (e.g. a singleton inside a union). The sweep - # below requires sorted, disjoint atomic intervals. Each atomic - # piece belongs to exactly one stored key, which is carried along - # to group intersections by original key pair. - items = [ - (atomic, value, interval) - for interval, value in d.items() - for atomic in interval - ] - items.sort(key=lambda item: (item[0].lower, item[0].left is Bound.OPEN)) - return iter(items) - - i_iter, o_iter = _atomic_items(d1), _atomic_items(d2) - i_current, o_current = next(i_iter, None), next(o_iter, None) - - # Intersections are accumulated per original key pair, so that `how` - # is invoked exactly once for each pair of intersecting keys, with the - # (possibly non-atomic) intersection of these keys. - pairwise = {} - - while i_current is not None and o_current is not None: - i1, v1, k1 = i_current - i2, v2, k2 = o_current - - if i1.overlaps(i2): - key = (k1, k2) - if key in pairwise: - pairwise[key][0].append(i1 & i2) - else: - pairwise[key] = [[i1 & i2], v1, v2] - - if i1.upper < i2.upper: - i_current = next(i_iter, None) - elif i2.upper < i1.upper: - o_current = next(o_iter, None) - else: - # Equal upper bounds include the i1 == i2 case; openness breaks ties. - if i1.right is Bound.OPEN and i2.right is Bound.CLOSED: - i_current = next(i_iter, None) - elif i2.right is Bound.OPEN and i1.right is Bound.CLOSED: - o_current = next(o_iter, None) - else: - # Equal upper bounds and openness: advance both intervals. - i_current, o_current = next(i_iter, None), next(o_iter, None) - - for pieces, v1, v2 in pairwise.values(): - i = pieces[0] if len(pieces) == 1 else Interval(*pieces) - new_items.append((i, _how(v1, v2, i))) + for i1, v1 in d1.items(): + for i2, v2 in d2.items(): + if i1.overlaps(i2): + i = i1 & i2 + v = _how(v1, v2, i) + new_items.append((i, v)) return self.__class__(new_items)