Commit 7cdbd680 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 71354250
......@@ -607,33 +607,7 @@ def _iterSplitKeyvByN(lo, hi, keyv, nsplit): # -> i[] of [lo, s1, s2, ..., sn, h
yield [lo] + tail
# ---- topology encoding ----
# TopoEncode returns topology encoding for internal structure of the tree.
#
# See top-level docstring for description of topology encoding.
def TopoEncode(tree):
assert isinstance(tree, Tree)
topo = ''
# breadth-first traversal of the tree with '/' injected in between layers
for nodev in _walkBFS(tree):
if len(topo) != 0:
topo += '/'
tnodev = []
for node in nodev:
assert isinstance(node, (Tree, Bucket))
tnode = ('T' if isinstance(node, Tree) else 'B') + \
(','.join(['%d' % _ for _ in node.keyv]))
tnodev.append(tnode)
topo += '-'.join(tnodev)
if 1: # make sure that every topology we emit, can be loaded back
t2 = TopoDecode(topo)
if t2 != tree:
panic("BUG: TopoEncode: D(E(·)) != identity\n· = %s\n D(E(·) = %s" % (tree, t2))
return topo
# ---- treewalk ----
# _Range represents a range under which a node is placed in its tree.
class _Range:
......@@ -752,6 +726,35 @@ def __zwalkBFS(ztree): # i[] of [](of _NodeInRange on each level)
nextq.append(_NodeInRange(_Range(*rv[i]), children[i]))
# ---- topology encoding ----
# TopoEncode returns topology encoding for internal structure of the tree.
#
# See top-level docstring for description of topology encoding.
def TopoEncode(tree):
assert isinstance(tree, Tree)
topo = ''
# breadth-first traversal of the tree with '/' injected in between layers
for nodev in _walkBFS(tree):
if len(topo) != 0:
topo += '/'
tnodev = []
for node in nodev:
assert isinstance(node, (Tree, Bucket))
tnode = ('T' if isinstance(node, Tree) else 'B') + \
(','.join(['%d' % _ for _ in node.keyv]))
tnodev.append(tnode)
topo += '-'.join(tnodev)
if 1: # make sure that every topology we emit, can be loaded back
t2 = TopoDecode(topo)
if t2 != tree:
panic("BUG: TopoEncode: D(E(·)) != identity\n· = %s\n D(E(·) = %s" % (tree, t2))
return topo
# TopoDecode decodes topology-encoded text into Tree structure.
#
# See top-level docstring for description of topology encoding.
......
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