Commit b7de2f10 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent a959e43a
...@@ -52,11 +52,20 @@ class Tree: ...@@ -52,11 +52,20 @@ class Tree:
def __hash__(t): def __hash__(t):
return hash(t.keyv) ^ hash(t.children) # XXX children ^^^ return hash(t.keyv) ^ hash(t.children) # XXX children ^^^
def __str__(t):
s = "T([" + ",".join(['%s' % _ for _ in t.keyv]) + "]"
for ch in t.children:
s += ",\n"
s += _indent(' '*4, str(ch))
s += ")"
return s
__repr__ = __str__
# Bucked represents a bucket node. # Bucked represents a bucket node.
class Bucket: class Bucket:
# .keyv () of keys # .keyv () of keys
def __init__(b, keyv): def __init__(b, *keyv): # XXX *keyv -> keyv ?
# XXX assert keyv ↑ # XXX assert keyv ↑
b.keyv = tuple(keyv) b.keyv = tuple(keyv)
...@@ -67,6 +76,11 @@ class Bucket: ...@@ -67,6 +76,11 @@ class Bucket:
def __hash__(b): def __hash__(b):
return hash(b.keyv) return hash(b.keyv)
def __str__(b):
return "B(" + ','.join(['%s' % _ for _ in b.keyv]) + ")"
__repr__ = __str__
# StructureOf returns internal structure of a tree. # StructureOf returns internal structure of a tree.
def StructureOf(node): def StructureOf(node):
...@@ -80,7 +94,7 @@ def StructureOf(node): ...@@ -80,7 +94,7 @@ def StructureOf(node):
if is_bucket: if is_bucket:
keys, _ = bcheck.crack_bucket(node, is_map) keys, _ = bcheck.crack_bucket(node, is_map)
return Bucket(keys) return Bucket(*keys)
if is_tree: if is_tree:
kind, keys, children = bcheck.crack_btree(node, is_map) kind, keys, children = bcheck.crack_btree(node, is_map)
...@@ -108,3 +122,14 @@ def TypologyOf(tree): ...@@ -108,3 +122,14 @@ def TypologyOf(tree):
1/0 1/0
treeStruct = StructureOf(tree) treeStruct = StructureOf(tree)
# XXX convert struct -> topology # XXX convert struct -> topology
# ---- misc ----
# _indent returns text with each line of it indented with prefix.
def _indent(prefix, text): # -> text
textv = text.split('\n')
textv = [prefix+_ for _ in textv]
text = '\n'.join(textv)
return text
...@@ -23,6 +23,7 @@ from BTrees.tests import testBTrees ...@@ -23,6 +23,7 @@ from BTrees.tests import testBTrees
def test_topologyOf(): def test_topologyOf():
T = xbtree.Tree T = xbtree.Tree
B = xbtree.Bucket
# XXX empty # XXX empty
...@@ -36,12 +37,12 @@ def test_topologyOf(): ...@@ -36,12 +37,12 @@ def test_topologyOf():
assert xbtree.StructureOf(t) == T([4], assert xbtree.StructureOf(t) == T([4],
T([2], T([2],
T([]), T([])), T([], B(1)), T([], B(3))),
T([], T([],
T([6, 10], T([6, 10],
T([], T([])), T([], T([], B(5))),
T([]), T([], B(7)),
T([])) )) T([], B(11))) ))
""" """
assert xbtree.TopologyOf(t) == ( (4,), assert xbtree.TopologyOf(t) == ( (4,),
......
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