Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
trac 13987 some details, no more xrange
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric Chapoton committed Nov 25, 2016
1 parent 4cf00a2 commit 847d49a
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions src/sage/combinat/mary_tree.py
Expand Up @@ -126,17 +126,17 @@ def __init__(self, parent, children=None, check=True):
if children is None:
children = []
elif children == [] or isinstance(children, (Integer, int)):
children = [None for i in xrange(m)]
children = [None] * m
if (children.__class__ is self.__class__ and
children.parent() == parent):
children.parent() == parent):
children = list(children)
else:
children = [self.__class__(parent, x) for x in children]
ClonableArray.__init__(self, parent, children, check=check)

def arity(self):
r"""
Returns the arity of the tree
Return the arity of the tree.
EXAMPLES::
Expand Down Expand Up @@ -185,7 +185,7 @@ def _repr_(self):

def is_empty(self):
"""
Returns whether ``self`` is empty.
Return whether ``self`` is empty.
EXAMPLES::
Expand All @@ -200,7 +200,7 @@ def is_empty(self):

def canonical_labelling(self, shift=0):
"""
Returns a labelled version of ``self``.
Return a labelled version of ``self``.
The actual canonical labelling is currently unspecified. However, it
is guaranteed to have labels in `1...n` where `n` is the number of
Expand Down Expand Up @@ -228,7 +228,7 @@ def canonical_labelling(self, shift=0):
shift += sz0 + 1
label = shift
otherTrees = []
for i in xrange(len(self) - 1, 0, -1):
for i in range(len(self) - 1, 0, -1):
child = self[i]
otherTrees.append(child.canonical_labelling(shift))
shift += child.node_number()
Expand Down Expand Up @@ -270,17 +270,17 @@ def unique_growth(self):
# we transform the tree into a word
word = self.prefix_word()
# we create the new node we want to add
node = [0 for i in xrange(self.arity() + 1)]
node = [0 for i in range(self.arity() + 1)]
node[0] = 1
# we find the last node of the tree
for ind in xrange(len(word) - 1, -1, -1):
for ind in range(len(word) - 1, -1, -1):
if(word[ind] == 1):
break
else:
ind = -1
# for each leaf following the last node, we create a tree
# where the leaf has been replaced by the new node
for i in xrange(ind + 1, len(word)):
for i in range(ind + 1, len(word)):
new_word = word[:i]
new_word.extend(node)
new_word.extend(word[i + 1:])
Expand All @@ -289,7 +289,7 @@ def unique_growth(self):
def prefix_word(self):
r"""
Prefix read of the tree.
We read the tree recursively in this order: ``self``,
``self[0]``, ``self[1]``, ..., ``self[m-1]`` and we write 0
for a leaf and 1 for a node.
Expand Down Expand Up @@ -401,7 +401,7 @@ def leaf(self):

def arity(self):
r"""
Returns the arity of the trees of the set.
Return the arity of the trees of the set.
EXAMPLES::
Expand Down Expand Up @@ -452,7 +452,7 @@ def __init__(self, m):

def _get_m_ary_trees_size(self, n):
r"""
Returns the set of m-ary trees of size ``n``
Return the set of m-ary trees of size ``n``
EXAMPLES::
Expand Down Expand Up @@ -501,7 +501,7 @@ def __call__(self, x=None, *args, **keywords):

def unlabelled_trees(self):
"""
Returns the set of unlabelled trees associated to ``self``
Return the set of unlabelled trees associated to ``self``
EXAMPLES::
Expand All @@ -512,7 +512,7 @@ def unlabelled_trees(self):

def labelled_trees(self):
"""
Returns the set of labelled trees associated to ``self``
Return the set of labelled trees associated to ``self``
EXAMPLES::
Expand Down Expand Up @@ -568,7 +568,7 @@ def from_prefix_word(self, word, ind=0):
return self()
ind += 1
trees = []
for i in xrange(self._m):
for i in range(self._m):
tree = self.from_prefix_word(word, ind)
trees.append(tree)
ind += tree.node_number() * self._m + 1
Expand All @@ -587,7 +587,7 @@ class MAryTrees_size(MAryTrees):
TESTS::
sage: from sage.combinat.mary_tree import MAryTrees_size
sage: for i in xrange(6): TestSuite(MAryTrees_size(3, i)).run()
sage: for i in range(6): TestSuite(MAryTrees_size(3, i)).run()
"""
def __init__(self, arity, size):
"""
Expand All @@ -598,7 +598,6 @@ def __init__(self, arity, size):
True
sage: MA3_2 is MAryTrees(3, 2)
True
"""
super(MAryTrees_size, self).__init__(category=FiniteEnumeratedSets())
self._size = size
Expand Down Expand Up @@ -626,7 +625,7 @@ def __call__(self, x=None, *args, **keywords):

def size(self):
r"""
Returns the size of the elements of the set
Return the size of the elements of the set.
EXAMPLES::
Expand Down Expand Up @@ -666,8 +665,9 @@ def __contains__(self, x):
sage: MAryTrees(4)([[], None, None, None]) in MA3_2
False
"""
return (isinstance(x, self.element_class)
and x.node_number() == self._size and x.arity() == self._m)
return (isinstance(x, self.element_class) and
x.node_number() == self._size and
x.arity() == self._m)

def _an_element_(self):
"""
Expand Down Expand Up @@ -714,11 +714,13 @@ def __iter__(self):
sage: MAryTrees(3, 2).list()
[[[., ., .], ., .], [., [., ., .], .], [., ., [., ., .]]]
"""
if(self._size == 0):
if not self._size:
yield self()
else:
roots = MAryTrees(self._m, 0)
children = lambda x: x.unique_growth()

def children(x):
return x.unique_growth()
from sage.combinat.backtrack import SearchForest
SF = SearchForest(roots, children, algorithm='breath')
it = SF.elements_of_depth_iterator(self._size)
Expand Down Expand Up @@ -862,7 +864,7 @@ def __init__(self, m):

def arity(self):
r"""
Returns the arity of the trees of the set
Return the arity of the trees of the set.
EXAMPLES::
Expand All @@ -883,7 +885,7 @@ def _repr_(self):

def _an_element_(self):
"""
Returns a labelled m-ary tree
Return a labelled m-ary tree.
EXAMPLE::
Expand Down Expand Up @@ -912,7 +914,7 @@ def __call__(self, x=None, *args, **keywords):

def unlabelled_trees(self):
"""
Returns the set of unlabelled trees associated to ``self``
Return the set of unlabelled trees associated to ``self``.
EXAMPLES::
Expand All @@ -936,7 +938,7 @@ def unlabelled_trees(self):

def labelled_trees(self):
"""
Returns the set of labelled trees associated to ``self``
Return the set of labelled trees associated to ``self``.
EXAMPLES::
Expand Down

0 comments on commit 847d49a

Please sign in to comment.