[Checkins] SVN: BTrees/branches/pure_python/ Disuse '_set_operation' mega-function for 'weightedIntersection'.

Tres Seaver cvs-admin at zope.org
Tue Dec 4 20:17:56 UTC 2012


Log message for revision 128511:
  Disuse '_set_operation' mega-function for 'weightedIntersection'.

Changed:
  _U  BTrees/branches/pure_python/
  U   BTrees/branches/pure_python/BTrees/_base.py
  U   BTrees/branches/pure_python/BTrees/tests/test__base.py

-=-
Modified: BTrees/branches/pure_python/BTrees/_base.py
===================================================================
--- BTrees/branches/pure_python/BTrees/_base.py	2012-12-04 20:17:54 UTC (rev 128510)
+++ BTrees/branches/pure_python/BTrees/_base.py	2012-12-04 20:17:55 UTC (rev 128511)
@@ -1352,8 +1352,41 @@
         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.

Modified: BTrees/branches/pure_python/BTrees/tests/test__base.py
===================================================================
--- BTrees/branches/pure_python/BTrees/tests/test__base.py	2012-12-04 20:17:54 UTC (rev 128510)
+++ BTrees/branches/pure_python/BTrees/tests/test__base.py	2012-12-04 20:17:55 UTC (rev 128511)
@@ -2623,6 +2623,7 @@
         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 @@
         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 @@
         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 @@
         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 @@
         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)
 



More information about the checkins mailing list