Commit cdab1972 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 8c550297
......@@ -240,6 +240,8 @@ def Restructure(ztree, newStructure):
# Bj ∈ RNv is mapped into Ai ∈ RZv such that that sum_j D(A_i, Bj) is minimal.
# it is used to associate nodes in tnew to ztree nodes.
def assign(RZv, RNv):
print()
print('assign %s <- %s' % (RZv, RNv))
for rzn in RZv:
assert isinstance(rzn, _NodeInRange)
assert isinstance(rzn.node, (ztreeType, zbucketType))
......@@ -268,8 +270,10 @@ def Restructure(ztree, newStructure):
# assignments more efficiently taking that Av and Bv are key↑ and the
# property of D function so that if B2 > B1 (all keys in B2 > all keys
# in B1) and A < B1.hi, then D(B2, A) > D(B1, A).
print()
print(C)
jv, iv = scipy.optimize.linear_sum_assignment(C)
print(jv, iv)
for (j,i) in zip(jv, iv):
RNv[j].node.Z = RZv[i].node
......@@ -278,6 +282,7 @@ def Restructure(ztree, newStructure):
# to newly created Z nodes.
for j2 in range(len(RNv)):
if j2 not in jv:
print('\n\n\nXXX create new @%d\n\n\n' % j2)
n = RNv[j2].node
assert n.Z is None
assert isinstance(n, (Tree,Bucket))
......@@ -349,8 +354,10 @@ def Restructure(ztree, newStructure):
assert len(rbucketv) > 0
for i in range(len(rbucketv)-1):
assert rbucketv[i].range.khi <= rbucketv[i+1].klo
rbucketv[i].node.next_bucket = rbucketv[i+1].node
rbucketv[i] .node.next_bucket = rbucketv[i+1].node
rbucketv[i+1].node.prev_bucket = rbucketv[i] .node
rbucketv[-1].node.next_bucket = None
rbucketv[0] .node.prev_bucket = None
# associate every bucket to zbucket
assign(zrbucketv, rbucketv)
......@@ -361,27 +368,33 @@ def Restructure(ztree, newStructure):
for rnodev in rlevelv:
for rn in rnodev:
node = rn.node
assert isinstance(node, Tree) # buckets were filtered out to rbucketv
assert isinstance(node, Tree), node # buckets were filtered out to rbucketv
znode = node.Z
# https://github.com/zopefoundation/BTrees/blob/4.5.0-1-gc8bf24e/BTrees/BTreeTemplate.c#L1087
zstate = unset = object()
if len(node.keyv) == 0:
child = node.children[0]
if isinstance(child, Bucket):
if len(child.keyv) == 0:
panic("TODO: empty tree")
if child.Z._p_oid is None:
panic("TODO: tree with bucket without oid")
zstate = ()
assert len(node.children) == len(node.keyv) + 1
zstate += node.children[0]
for (child, k) in zip(node.children[1:], node.keyv):
zstate += (k, child) # (child0, k0, child1, k1, ..., childN, kN, childN+1)
zstate = (zstate,)
# firstbucket
zstate += (None,) # XXX
# empty bucket noone links to
if len(child.keyv) == 0 and child.prev_bucket is None:
zstate = None # -> empty tree
# tree with single bucket noone links to
elif child.Z._p_oid is None and child.prev_bucket is None:
zstate = ((child.Z.__getstate__(),),) # tree with bucket without oid
if zstate is unset:
# normal tree node
zstate = ()
assert len(node.children) == len(node.keyv) + 1
zstate += node.children[0]
for (child, k) in zip(node.children[1:], node.keyv):
zstate += (k, child) # (child0, k0, child1, k1, ..., childN, kN, childN+1)
zstate = (zstate,)
# firstbucket
zstate += (None,) # XXX
znode.__setstate__(zstate)
......
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