Browse Source

Don't return a mutable interval from IntervalSet.intersection()

Instead, always take the subset, which creates a new interval.
Also adds a small optimization by moving the 'if orig' check outside the
loop.
tags/nilmdb-1.4.2
Jim Paris 11 years ago
parent
commit
cda2ac3e77
1 changed files with 12 additions and 17 deletions
  1. +12
    -17
      nilmdb/server/interval.pyx

+ 12
- 17
nilmdb/server/interval.pyx View File

@@ -286,23 +286,18 @@ cdef class IntervalSet:
(potentially) subsetted to make the one that is being
returned.
"""
if not isinstance(interval, Interval):
raise TypeError("bad type")
for n in self.tree.intersect(interval.start, interval.end):
i = n.obj
if i:
if i.start >= interval.start and i.end <= interval.end:
if orig:
yield (i, i)
else:
yield i
else:
subset = i.subset(max(i.start, interval.start),
min(i.end, interval.end))
if orig:
yield (subset, i)
else:
yield subset
if orig:
for n in self.tree.intersect(interval.start, interval.end):
i = n.obj
subset = i.subset(max(i.start, interval.start),
min(i.end, interval.end))
yield (subset, i)
else:
for n in self.tree.intersect(interval.start, interval.end):
i = n.obj
subset = i.subset(max(i.start, interval.start),
min(i.end, interval.end))
yield subset

cpdef intersects(self, Interval other):
"""Return True if this IntervalSet intersects another interval"""


Loading…
Cancel
Save