Skip to content

Commit

Permalink
Fix for bug #3, intersection of multiple range sets not working corre…
Browse files Browse the repository at this point in the history
…ctly
  • Loading branch information
runfalk committed Jul 13, 2016
1 parent 3998ef7 commit b42e26f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
7 changes: 7 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Version are structured like the following: ``<major>.<minor>.<bugfix>``. The
first `0.1` release does not properly adhere to this. Unless explicitly stated,
changes are made by `Andreas Runfalk <https://github.com/runfalk>`_.

Version 0.2.2
-------------
Unreleased

- Fixed `bug #3 <https://github.com/runfalk/spans/issues/3>`_, intersection of
multiple range sets not working correctly.

Version 0.2.1
-------------
Released on 27th June, 2016
Expand Down
28 changes: 24 additions & 4 deletions spans/settypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,34 @@ def intersection(self, *others):
`other`.
"""

# TODO: Optimize this
# Initialize output with a reference to this rangeset. When
# intersecting against multiple rangesets at once this will be replaced
# after each iteration.
output = self

intersection = self.__class__([])
for other in others:
for a in self:
# Intermediate rangeset containing intersection for this current
# iteration.
intersection = self.__class__([])

# Intersect every range within the current output with every range
# within the currently processed other rangeset. All intersecting
# parts are added to the intermediate intersection set.
for a in output:
for b in other:
intersection.add(a.intersection(b))
return intersection

# If the intermediate intersection rangeset is still empty, there
# where no intersections with at least one of the arguments and
# we can quit early, since any intersection with the empty set will
# always be empty.
if not intersection:
return intersection

# Update output with intersection for the current iteration.
output = intersection

return output

__contains__ = contains

Expand Down
17 changes: 17 additions & 0 deletions spans/tests/test_settypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,20 @@ def test_greater_than(self):
intrangeset([range_b]) > intrangeset([range_a, range_b]))
self.assertTrue(
intrangeset([range_a, range_b]) >= intrangeset([range_a]))

def test_bug3_intersection(self):
"""
`Bug #3 <https://github.com/runfalk/spans/issues/3>`_
"""

range_a = intrange(1, 5)
range_b = intrange(5, 10)
range_c = intrange(10, 15)

rangeset_a = intrangeset([range_a, range_c])
rangeset_b = intrangeset([range_b])
rangeset_c = intrangeset([range_c])
rangeset_empty = intrangeset([])

self.assertEqual(
rangeset_a.intersection(rangeset_b, rangeset_c), rangeset_empty)

0 comments on commit b42e26f

Please sign in to comment.