Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 6 additions & 25 deletions portion/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,31 +292,12 @@ def _how(x, y, i):
intersection = dom1 & dom2
d1, d2 = self[intersection], other[intersection]

i_iter, o_iter = iter(d1.items()), iter(d2.items())
i_current, o_current = next(i_iter, None), next(o_iter, None)

while i_current is not None and o_current is not None:
i1, v1 = i_current
i2, v2 = o_current

if i1.overlaps(i2):
i = i1 & i2
v = _how(v1, v2, i)
new_items.append((i, v))

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 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)

Expand Down
61 changes: 61 additions & 0 deletions tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -323,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
Expand Down
Loading