Commit 2bb7a9d6 authored by Tres Seaver's avatar Tres Seaver

Disuse '_set_operation' mega-function for 'weightedIntersection'.

parent 314021f6
......@@ -1352,8 +1352,41 @@ def weightedIntersection(set_type, o1, o2, w1=1, w2=1):
return w2, o2
if o2 is None:
return w1, o1
result = _set_operation(o1, o2, 1, 1, w1, w2, 0, 1, 0)
return (w1 + w2 if isinstance(result, (Set, TreeSet)) else 1, result)
#result = _set_operation(o1, o2, 1, 1, w1, w2, 0, 1, 0)
MERGE_DEFAULT = getattr(o1, 'MERGE_DEFAULT', None)
i1 = _SetIteration(o1, True, MERGE_DEFAULT)
i2 = _SetIteration(o2, True, MERGE_DEFAULT)
result = o1._mapping_type()
MERGE = getattr(o1, 'MERGE', None)
if MERGE is None and i1.useValues and i2.useValues:
raise TypeError("invalid set operation")
MERGE_WEIGHT = getattr(o1, 'MERGE_WEIGHT')
if (not i1.useValues) and i2.useValues:
i1, i2 = i2, i1
w1, w2 = w2, w1
if MERGE_DEFAULT is None:
if i1.useValues:
if (not i2.useValues):
raise TypeError("invalid set operation")
else:
raise TypeError("invalid set operation")
def copy(i, w):
result._keys.append(i.key)
result._values.append(MERGE_WEIGHT(i.value, w))
while i1.active and i2.active:
cmp_ = cmp(i1.key, i2.key)
if cmp_ < 0:
i1.advance()
elif cmp_ == 0:
result._keys.append(i1.key)
result._values.append(MERGE(i1.value, w1, i2.value, w2))
i1.advance()
i2.advance()
else:
i2.advance()
if isinstance(result, (Set, TreeSet)):
return w1 + w2, result
return 1, result
def multiunion(set_type, seqs):
# XXX simple/slow implementation. Goal is just to get tests to pass.
......
......@@ -2623,6 +2623,7 @@ class Test_weightedUnion(unittest.TestCase, _SetObBase):
lhs = self._makeMapping({'a': 13, 'b': 12, 'c': 11})
rhs = self._makeSet('a', 'd')
weight, result = self._callFUT(lhs.__class__, lhs, rhs)
self.assertEqual(weight, 1)
self.assertTrue(isinstance(result, _Mapping))
self.assertEqual(list(result), ['a', 'b', 'c', 'd'])
self.assertEqual(result['a'], 55)
......@@ -2634,6 +2635,7 @@ class Test_weightedUnion(unittest.TestCase, _SetObBase):
lhs = self._makeMapping({'a': 13, 'b': 12, 'c': 11})
rhs = self._makeMapping({})
weight, result = self._callFUT(lhs.__class__, lhs, rhs)
self.assertEqual(weight, 1)
self.assertEqual(list(result), ['a', 'b', 'c'])
self.assertEqual(result['a'], 13)
self.assertEqual(result['b'], 12)
......@@ -2643,6 +2645,7 @@ class Test_weightedUnion(unittest.TestCase, _SetObBase):
lhs = self._makeMapping({'a': 13, 'c': 12, 'e': 11})
rhs = self._makeMapping({'a': 10, 'b': 22, 'd': 33})
weight, result = self._callFUT(lhs.__class__, lhs, rhs)
self.assertEqual(weight, 1)
self.assertEqual(list(result), ['a', 'b', 'c', 'd', 'e'])
self.assertEqual(result['a'], 23)
self.assertEqual(result['b'], 22)
......@@ -2672,6 +2675,7 @@ class Test_weightedIntersection(unittest.TestCase, _SetObBase):
lhs = self._makeMapping({'a': 13, 'b': 12, 'c': 11})
rhs = self._makeSet('a', 'd')
weight, result = self._callFUT(lhs.__class__, lhs, rhs)
self.assertEqual(weight, 1)
self.assertTrue(isinstance(result, _Mapping))
self.assertEqual(list(result), ['a'])
self.assertEqual(result['a'], 55)
......@@ -2680,12 +2684,14 @@ class Test_weightedIntersection(unittest.TestCase, _SetObBase):
lhs = self._makeMapping({'a': 13, 'b': 12, 'c': 11})
rhs = self._makeMapping({})
weight, result = self._callFUT(lhs.__class__, lhs, rhs)
self.assertEqual(weight, 1)
self.assertEqual(list(result), [])
def test_both_mappings_rhs_non_empty(self):
lhs = self._makeMapping({'a': 13, 'c': 12, 'e': 11})
rhs = self._makeMapping({'a': 10, 'b': 22, 'd': 33})
weight, result = self._callFUT(lhs.__class__, lhs, rhs)
self.assertEqual(weight, 1)
self.assertEqual(list(result), ['a'])
self.assertEqual(result['a'], 23)
......
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