Commit 84f943c5 authored by Tim Peters's avatar Tim Peters

Repaired more of the weightedIntersection and weightedUnion docs. I'm

not sure it's telling the full truth yet.
parent f6513a88
...@@ -286,7 +286,7 @@ class IIMerge(IMerge): ...@@ -286,7 +286,7 @@ class IIMerge(IMerge):
""" """
def weightedUnion(c1, c2, weight1=1, weight2=1): def weightedUnion(c1, c2, weight1=1, weight2=1):
"""Compute the weighted Union of c1 and c2. """Compute the weighted union of c1 and c2.
If c1 and c2 are None, the output is (0, None). If c1 and c2 are None, the output is (0, None).
...@@ -294,18 +294,28 @@ class IIMerge(IMerge): ...@@ -294,18 +294,28 @@ class IIMerge(IMerge):
If c1 is not None and c2 is None, the output is (weight1, c1). If c1 is not None and c2 is None, the output is (weight1, c1).
If c1 and c2 are not None and not both sets, the output is 1 Else, and hereafter, c1 is not None and c2 is not None.
and a Bucket such that the output values are::
If c1 and c2 are both sets, the output is the sum of the weights
and the (unweighted) union of the sets.
Else the output is 1 and a Bucket whose keys are the union of c1 and
c2's keys, and whose values are::
v1*weight1 + v2*weight2 v1*weight1 + v2*weight2
where: where:
v1 is 0 if the key was not in c1. Otherwise, v1 is 1, if v1 is 0 if the key is not in c1
c1 is a set, or the value from c1. 1 if the key is in c1 and c1 is a set
c1[key] if the key is in c1 and c1 is a mapping
v2 is 0 if the key was not in c2. Otherwise, v2 is 2, if v2 is 0 if the key is not in c2
c2 is a set, or the value from c2. 1 if the key is in c2 and c2 is a set
c2[key] if the key is in c2 and c2 is a mapping
XXX All of the above is wrong if either weight is negative. I think
XXX that's a bug in the implementation and will fix it.
Note that c1 and c2 must be collections. Note that c1 and c2 must be collections.
...@@ -320,21 +330,26 @@ class IIMerge(IMerge): ...@@ -320,21 +330,26 @@ class IIMerge(IMerge):
If c1 is not None and c2 is None, the output is (weight1, c1). If c1 is not None and c2 is None, the output is (weight1, c1).
Else, and hereafter, c1 is not None and c2 is not None.
If c1 and c2 are both sets, the output is the sum of the weights If c1 and c2 are both sets, the output is the sum of the weights
and the (unweighted) intersection of the sets. and the (unweighted) intersection of the sets.
If c1 and c2 are not None and not both sets, the output is 1 Else the output is 1 and a Bucket whose keys are the intersection of
and a Bucket such that the output values are:: c1 and c2's keys, and whose values are::
v1*weight1 + v2*weight2 v1*weight1 + v2*weight2
where: where:
v1 is 0 if the key was not in c1. Otherwise, v1 is 1, if v1 is 1 if c1 is a set
c1 is a set, or the value from c1. c1[key] if c1 is a mapping
v2 is 1 if c2 is a set
c1[key] if c2 is a mapping
v2 is 0 if the key was not in c2. Otherwise, v2 is 2, if XXX All of the above is wrong if either weight is negative. I think
c2 is a set, or the value from c2. XXX that's a bug in the implementation and will fix it.
Note that c1 and c2 must be collections. Note that c1 and c2 must be collections.
""" """
......
...@@ -19,10 +19,6 @@ from BTrees.IOBTree import IOBTree, IOBucket, IOSet, IOTreeSet ...@@ -19,10 +19,6 @@ from BTrees.IOBTree import IOBTree, IOBucket, IOSet, IOTreeSet
from BTrees.IIBTree import IIBTree, IIBucket, IISet, IITreeSet from BTrees.IIBTree import IIBTree, IIBucket, IISet, IITreeSet
from BTrees.OIBTree import OIBTree, OIBucket, OISet, OITreeSet from BTrees.OIBTree import OIBTree, OIBucket, OISet, OITreeSet
# XXX TODO Needs more tests.
# This file was created when multiunion was added. The other set operations
# don't appear to be tested anywhere yet.
# Subclasses have to set up: # Subclasses have to set up:
# builders - functions to build inputs, taking an optional keys arg # builders - functions to build inputs, taking an optional keys arg
# intersection, union, difference - set to the type-correct versions # intersection, union, difference - set to the type-correct versions
...@@ -388,6 +384,12 @@ class TestWeightedOI(Weighted): ...@@ -388,6 +384,12 @@ class TestWeightedOI(Weighted):
builders = OIBucket, OIBTree, itemsToSet(OISet), itemsToSet(OITreeSet) builders = OIBucket, OIBTree, itemsToSet(OISet), itemsToSet(OITreeSet)
# 'thing' is a bucket, btree, set or treeset. Return true iff it's one of the
# latter two.
def isaset(thing):
return not hasattr(thing, 'values')
def test_suite(): def test_suite():
s = TestSuite() s = TestSuite()
for klass in (TestIIMultiUnion, TestIOMultiUnion, for klass in (TestIIMultiUnion, TestIOMultiUnion,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment