Пример #1
0
def push(node, val, compare=_compare):
    """
    Add a new value to heap.
    Addes to value as a branch than switches added branch
    with its parent if needed.(push and swim)
    """
    if not node:
        node = make_node(None, val, None)
    else:
        # push new value to smaller branch
        if is_add_to_left(node):
            child = left(node)
            set_fun = set_left
        else:
            child = right(node)
            set_fun = set_right

        child = push(child, val)

        if compare(value(child), value(node)):
            child_value = value(child)
            child = set_value(child, value(node))
            node = set_value(node, child_value)

        node = set_fun(node, child)
    return node
Пример #2
0
def pop(node, compare=_compare):
    """
    pop head of the heap
    remove last added node
    add last added node as root node
    switch root with children if necessary.(sink last added node)
    pop(heap) -> (new_heap, value)
    """
    val = value(node)
    node, poped = pop_leaf(node)
    if node:
        node = update_value(node, value(poped))
    return node, val
Пример #3
0
def update_value(node, val, compare=_compare):
    """
    updates value of a node.
    if childs are bigger than parent it switches child with parent
    """
    left_node = left(node)
    right_node = right(node)
    left_val = value(left_node) if left_node else None
    right_val = value(right_node) if right_node else None

    if left_node and compare(left_val, val):
        left_node = update_value(left_node, val)
        val = left_val

    if right_node and compare(right_val, val):
        right_node = update_value(right_node, val)
        val = right_val
    return make_node(left_node, val, right_node)
Пример #4
0
def is_consistent(node, compare=_compare):
    """
    Check if hash has a consistent state for given compare function.
    """
    if node is None:
        return True
    else:
        val = value(node)
        left_node = left(node)
        right_node = right(node)

        # if there is no left_node,
        # that means left branch is already consistent
        is_left_consistent = (compare(val, value(left_node)) and is_consistent(left_node)) if left_node else True

        is_right_consistent = (compare(val, value(right_node)) and is_consistent(right_node)) if right_node else True

        ## TODO make sure left and right branch has right deepness level
        return is_left_consistent and is_right_consistent
Пример #5
0
    def test_push(self):
        count = self.SIZE
        head = count - 1
        h = None
        for i in range(count):
            h = heap.push(h, i)
            self.assertTrue(heap.is_consistent(h))

        self.assertEqual(node.count(h), count)
        self.assertEqual(node.value(h), head)
Пример #6
0
 def substitute_for_node(self, node):
     """Returns a node that replaces the specified node (e.g. in music).
     
     For example: a variable reference returns its value.
     Returns nothing if the node is not substitutable.
     Returns the node itself if it was substitutable, but the substitution
     failed.
     
     """
     if isinstance(node, UserCommand):
         value = node.value()
         if value:
             return self.substitute_for_node(value) or value
         return node
     elif isinstance(node, Include):
         return self.get_included_document_node(node) or node
Пример #7
0
 def substitute_for_node(self, node):
     """Returns a node that replaces the specified node (e.g. in music).
     
     For example: a variable reference returns its value.
     Returns nothing if the node is not substitutable.
     Returns the node itself if it was substitutable, but the substitution
     failed.
     
     """
     if isinstance(node, UserCommand):
         value = node.value()
         if value:
             return self.substitute_for_node(value) or value
         return node
     elif isinstance(node, Include):
         return self.get_included_document_node(node) or node
Пример #8
0
 def music_events_til_position(self, position):
     """Return a list of tuples.
     
     Every tuple is a (parent, nodes, scaling). If an empty list is 
     returned, there is no music expression at this position.
     
     """
     node = self.node(position)
     # be nice and allow including an assignment
     if (isinstance(node, Assignment) and node.parent() is self
         and isinstance(node.value(), Music)):
         return [(node, [], 1)]
     
     if isinstance(node.parent(), Chord):
         node = node.parent()
     
     l = []
     mus = isinstance(node, (Music, Durable))
     if mus:
         l.append((node, [], 1))
     for p in node.ancestors():
         pmus = isinstance(p, Music)
         end = node.end_position()
         if pmus:
             if position > end:
                 preceding, s = p.preceding(node.next_sibling())
                 l = [(p, preceding, s)]
             elif position == end:
                 preceding, s = p.preceding(node)
                 l = [(p, preceding + [node], s)]
             else:
                 preceding, s = p.preceding(node)
                 l.append((p, preceding, s))
         elif mus:
             # we are at the musical top
             if position > end:
                 return []
             elif position == end:
                 l = [(p, [node], 1)]
             else:
                 l.append((p, [], 1))
             break
         node = p
         mus = pmus
     l.reverse()
     return l
Пример #9
0
    def music_events_til_position(self, position):
        """Return a list of tuples.
        
        Every tuple is a (parent, nodes, scaling). If an empty list is 
        returned, there is no music expression at this position.
        
        """
        node = self.node(position)
        # be nice and allow including an assignment
        if (isinstance(node, Assignment) and node.parent() is self
                and isinstance(node.value(), Music)):
            return [(node, [], 1)]

        if isinstance(node.parent(), Chord):
            node = node.parent()

        l = []
        mus = isinstance(node, (Music, Durable))
        if mus:
            l.append((node, [], 1))
        for p in node.ancestors():
            pmus = isinstance(p, Music)
            end = node.end_position()
            if pmus:
                if position > end:
                    preceding, s = p.preceding(node.next_sibling())
                    l = [(p, preceding, s)]
                elif position == end:
                    preceding, s = p.preceding(node)
                    l = [(p, preceding + [node], s)]
                else:
                    preceding, s = p.preceding(node)
                    l.append((p, preceding, s))
            elif mus:
                # we are at the musical top
                if position > end:
                    return []
                elif position == end:
                    l = [(p, [node], 1)]
                else:
                    l.append((p, [], 1))
                break
            node = p
            mus = pmus
        l.reverse()
        return l
Пример #10
0
def printTree(n):
    if n.left is not None:
        printTree(n.left)
    print(n.value())
    if n.right is not None:
        printTree(n.right)