Intersection with BTrees

Tim Peters tim at zope.com
Wed May 28 11:05:03 EDT 2003


[Thomas Güttler, using ZODB's BTrees]
> I to the multiintersection like this:
>
>         intersect=None
>         for value in values:
>             value=value.upper()
>             result=self.docs.get(value.upper())
>             if not result:
>                 return OOBTree()
>             if not intersect:
>                 intersect=result
>             else:
>                 intersect=intersection(intersect, result)
>             print "intersection: %s %s %s" % (value, intersect,
>                                                len(intersect))
>         return intersect
>
> Unfortunatley there is no multiintersection, but only a
> multiunion.

That's because multiunion can be done vastly more efficiently in C than in
Python, and multiintersection can't.  Note that multiunion is only supplied
for types with integer keys.

> Are there better ways to do this?

The only trick is to sort the things you want to intersect by increasing
order of size first.  Then your "intersect" accumulator will never get
larger than the smallest input.  len(Bucket) is cheap, but len(BTree) is
expensive, so the exact types of the inputs feed into the best way to do
this too.

Note that you'll probably get better responses to BTree questions on a ZODB
mailing list.






More information about the Python-list mailing list