Commit 283fde0f authored by Tres Seaver's avatar Tres Seaver

Fix AttributeErrors for MERGE / MERGE_WEIGHT in set operations.

parent 4dfe87a0
...@@ -483,6 +483,19 @@ class Bucket(_MappingBase, _BucketBase): ...@@ -483,6 +483,19 @@ class Bucket(_MappingBase, _BucketBase):
keys.append(state[i]) keys.append(state[i])
values.append(state[i+1]) values.append(state[i+1])
def MERGE_WEIGHT(self, value, weight):
return 0 # fload / int value trees override to return value * weight
class NumericValueBucket(Bucket):
# Base for ?FTree, ?ITree, ?LTree classes.
def MERGE(self, value1, weight1, value2, weight2):
return (value1 * weight1) + (value2 * weight1)
def MERGE_WEIGHT(self, value, weight):
return value * weight
class Set(_SetBase, _BucketBase): class Set(_SetBase, _BucketBase):
def __getstate__(self): def __getstate__(self):
...@@ -989,6 +1002,18 @@ class Tree(_Tree): ...@@ -989,6 +1002,18 @@ class Tree(_Tree):
def insert(self, key, value): def insert(self, key, value):
return bool(self._set(key, value, True)[0]) return bool(self._set(key, value, True)[0])
def MERGE_WEIGHT(self, value, weight):
return 0 # fload / int value trees override to return value * weight
class NumericValueTree(Tree):
# Base for ?FTree, ?ITree, ?LTree classes.
def MERGE(self, value1, weight1, value2, weight2):
return (value1 * weight1) + (value2 * weight1)
def MERGE_WEIGHT(self, value, weight):
return value * weight
class TreeSet(_SetBase, _Tree): class TreeSet(_SetBase, _Tree):
pass pass
...@@ -1001,8 +1026,8 @@ def _set_operation(s1, s2, ...@@ -1001,8 +1026,8 @@ def _set_operation(s1, s2,
i2 = _SetIteration(s2, usevalues2) i2 = _SetIteration(s2, usevalues2)
merge = i1.useValues or i2.useValues merge = i1.useValues or i2.useValues
MERGE = getattr(s1, 'MERGE', None) MERGE = getattr(s1, 'MERGE', None)
MERGE_WEIGHT = getattr(s1, 'MERGE_WEIGHT', None)
if merge: if merge:
MERGE_WEIGHT = getattr(s1, 'MERGE_WEIGHT')
if MERGE is None and c12 and i1.useValues and i2.useValues: if MERGE is None and c12 and i1.useValues and i2.useValues:
raise TypeError("invalid set operation") raise TypeError("invalid set operation")
...@@ -1026,7 +1051,7 @@ def _set_operation(s1, s2, ...@@ -1026,7 +1051,7 @@ def _set_operation(s1, s2,
def copy(i, w): def copy(i, w):
r._keys.append(i.key) r._keys.append(i.key)
r._values.append(MERGE_WEIGHT(i, w)) r._values.append(MERGE_WEIGHT(i.value, w))
else: else:
r = s1._set_type() r = s1._set_type()
def copy(i, w): def copy(i, w):
...@@ -1172,6 +1197,9 @@ def to_str(l): ...@@ -1172,6 +1197,9 @@ def to_str(l):
return to return to
tos = dict(I=to_int, L=to_long, F=to_float, O=to_ob) tos = dict(I=to_int, L=to_long, F=to_float, O=to_ob)
treetypes = dict(I=NumericValueTree, L=NumericValueTree, F=NumericValueTree)
buckettypes = dict(I=NumericValueBucket, L=NumericValueBucket,
F=NumericValueBucket)
def _import(globals, prefix, bucket_size, tree_size, def _import(globals, prefix, bucket_size, tree_size,
to_key=None, to_value=None): to_key=None, to_value=None):
...@@ -1179,11 +1207,13 @@ def _import(globals, prefix, bucket_size, tree_size, ...@@ -1179,11 +1207,13 @@ def _import(globals, prefix, bucket_size, tree_size,
to_key = tos[prefix[0]] to_key = tos[prefix[0]]
if to_value is None: if to_value is None:
to_value = tos[prefix[1]] to_value = tos[prefix[1]]
mc = Bucket.__class__ buckettype = buckettypes.get(prefix[1], Bucket)
bucket = mc(prefix+'Bucket', (Bucket, ), dict(MAX_SIZE=bucket_size, mc = buckettype.__class__
bucket = mc(prefix+'Bucket', (buckettype, ), dict(MAX_SIZE=bucket_size,
_to_value=to_value)) _to_value=to_value))
set = mc(prefix+'Set', (Set, ), dict(MAX_SIZE=bucket_size)) set = mc(prefix+'Set', (Set, ), dict(MAX_SIZE=bucket_size))
tree = mc(prefix+'BTree', (Tree, ), dict(MAX_SIZE=tree_size, treetype = treetypes.get(prefix[1], Tree)
tree = mc(prefix+'BTree', (treetype, ), dict(MAX_SIZE=tree_size,
_to_value=to_value)) _to_value=to_value))
treeset = mc(prefix+'TreeSet', (TreeSet, ), dict(MAX_SIZE=tree_size)) treeset = mc(prefix+'TreeSet', (TreeSet, ), dict(MAX_SIZE=tree_size))
for c in bucket, set, tree, treeset: for c in bucket, set, tree, treeset:
......
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