Commit 3ebb53cc authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent ff58a7c0
...@@ -79,6 +79,7 @@ from __future__ import print_function, absolute_import ...@@ -79,6 +79,7 @@ from __future__ import print_function, absolute_import
from BTrees import check as bcheck from BTrees import check as bcheck
from golang import panic from golang import panic
from golang.gcompat import qq
import itertools import itertools
import re import re
inf = float('inf') inf = float('inf')
...@@ -178,7 +179,7 @@ def StructureOf(node): ...@@ -178,7 +179,7 @@ def StructureOf(node):
# TopoEncode returns topology encoding for internal structure of the tree. # TopoEncode returns topology encoding for internal structure of the tree.
# #
# See top-level docstring for what topology is. # See top-level docstring for description of topology encoding.
def TopoEncode(tree): def TopoEncode(tree):
assert isinstance(tree, Tree) assert isinstance(tree, Tree)
topo = '' topo = ''
...@@ -214,9 +215,54 @@ def TopoEncode(tree): ...@@ -214,9 +215,54 @@ def TopoEncode(tree):
# TopoDecode decodes topology-encoded text into Tree structure. # TopoDecode decodes topology-encoded text into Tree structure.
# #
# XXX see -> ? # See top-level docstring for description of topology encoding.
class TopoDecodeError(Exception):
pass
def TopoDecode(text): def TopoDecode(text):
return Tree([], Bucket()) # XXX stub levelv = text.split('/') # T3/T-T/B1-T5/B-B7,8,9 -> T3 T-T B1-T5 B-B7,8,9
# build nodes from bottop-up
currentv = [] # of nodes on current level (that we are building)
bottomq = [] # of nodes below current level that we are building
# shrinks as fifo as nodes added to currentv link to bottom
while len(levelv) > 0:
level = levelv.pop() # e.g. B1-T5
tnodev = level.split('-') # e.g. B1 T5
bottomq = currentv
currentv = []
for tnode in tnodev:
if tnode[:1] == 'T':
typ = Tree
elif tnode[:1] == 'B':
typ = Bucket
else:
raise TopoDecodeError("incorrect node %s: unknown prefix" % qq(tnode))
tkeyv = tnode[1:].split(',') # e.g. 7 8 9
keyv = [int(_) for _ in tkeyv]
if typ is Bucket:
node = Bucket(*keyv)
else:
# Tree
nchild = len(keyv) + 1
if len(bottomq) < nchild:
raise TopoDecodeError(
"node %s at level %d: next level does not have enough children to link to" %
(qq(tnode), len(levelv)+1))
children = bottomq[:nchild]
bottomq = bottomq[nchild:]
node = Tree(keyv, *children)
currentv.append(node)
if len(bottomq) != 0:
raise TopoDecodeError("level %d does not link to all nodes in the next level" %
len(levelv+1))
if len(currentv) != 0:
raise TopoDecodeError("first level has %d entries; must be 1" % len(currentv))
root = currentv[0]
return root
# Restructure reorganizes BTree instance (not Tree) according to new structure. # Restructure reorganizes BTree instance (not Tree) according to new structure.
......
...@@ -63,7 +63,7 @@ def test_topoEncoding(): ...@@ -63,7 +63,7 @@ def test_topoEncoding():
def X(tree): def X(tree):
topo = xbtree.TopoEncode(tree) topo = xbtree.TopoEncode(tree)
t2 = xbtree.TopoDecode(topo) t2 = xbtree.TopoDecode(topo)
#assert t2 == tree # XXX reenable assert t2 == tree
return topo return topo
assert X(T([], B())) == 'T/B' assert X(T([], B())) == 'T/B'
......
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