示例#1
0
 def __init__(
     self,
     items=None,
     item_class=None,
     ):
     PayloadTree.__init__(
         self,
         expr=items,
         item_class=item_class,
         )
示例#2
0
 def __init__(
     self,
     items=None,
     item_class=None,
     ):
     PayloadTree.__init__(
         self,
         expr=items,
         item_class=item_class,
         )
示例#3
0
    def get_node_at_position(self, position):
        r'''Gets cyclic payload tree node at `position`.

        ::

            >>> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
            >>> tree = datastructuretools.CyclicPayloadTree(sequence)

        ::

            >>> tree.get_node_at_position((2, 1))
            CyclicPayloadTree(5)

        ::

            >>> tree.get_node_at_position((2, 99))
            CyclicPayloadTree(5)

        ::

            >>> tree.get_node_at_position((82, 1))
            CyclicPayloadTree(5)

        ::

            >>> tree.get_node_at_position((82, 99))
            CyclicPayloadTree(5)

        Returns node.
        '''
        return PayloadTree.get_node_at_position(self, position)
示例#4
0
    def iterate_forever_depth_first(self, reverse=False):
        r'''Iterate cyclic payload tree tree depth first.

            >>> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
            >>> tree = datastructuretools.CyclicPayloadTree(sequence)

        ..  container:: example

            **Example 1.** Iterates from left to right:

            ::

                >>> generator = tree.iterate_forever_depth_first()
                >>> for i in range(20):
                ...     next(generator)
                CyclicPayloadTree([[0, 1], [2, 3], [4, 5], [6, 7]])
                CyclicPayloadTree([0, 1])
                CyclicPayloadTree(0)
                CyclicPayloadTree(1)
                CyclicPayloadTree([2, 3])
                CyclicPayloadTree(2)
                CyclicPayloadTree(3)
                CyclicPayloadTree([4, 5])
                CyclicPayloadTree(4)
                CyclicPayloadTree(5)
                CyclicPayloadTree([6, 7])
                CyclicPayloadTree(6)
                CyclicPayloadTree(7)
                CyclicPayloadTree([[0, 1], [2, 3], [4, 5], [6, 7]])
                CyclicPayloadTree([0, 1])
                CyclicPayloadTree(0)
                CyclicPayloadTree(1)
                CyclicPayloadTree([2, 3])
                CyclicPayloadTree(2)
                CyclicPayloadTree(3)

        ..  container:: example

            **Example 2.** Iterates from right to left:

            ::

                >>> generator = tree.iterate_forever_depth_first(
                ...     reverse=True)
                >>> for i in range(20):
                ...     next(generator)
                CyclicPayloadTree([[0, 1], [2, 3], [4, 5], [6, 7]])
                CyclicPayloadTree([6, 7])
                CyclicPayloadTree(7)
                CyclicPayloadTree(6)
                CyclicPayloadTree([4, 5])
                CyclicPayloadTree(5)
                CyclicPayloadTree(4)
                CyclicPayloadTree([2, 3])
                CyclicPayloadTree(3)
                CyclicPayloadTree(2)
                CyclicPayloadTree([0, 1])
                CyclicPayloadTree(1)
                CyclicPayloadTree(0)
                CyclicPayloadTree([[0, 1], [2, 3], [4, 5], [6, 7]])
                CyclicPayloadTree([6, 7])
                CyclicPayloadTree(7)
                CyclicPayloadTree(6)
                CyclicPayloadTree([4, 5])
                CyclicPayloadTree(5)
                CyclicPayloadTree(4)

        Yields cyclic payload tree nodes.
        '''
        while True:
            for node in PayloadTree.iterate_depth_first(self, reverse=reverse):
                yield node
示例#5
0
    def get_next_n_nodes_at_level(self, n, level):
        r'''Gets next `n` nodes of cyclic payload tree at `level`.

        ..  container:: example

            Cyclic payload tree of length greater than ``1`` for examples with
            positive `n`:

            ::

                >>> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
                >>> tree = datastructuretools.CyclicPayloadTree(sequence)

            Gets next 4 nodes at level 2:

            ::

                >>> for x in tree[0][0].get_next_n_nodes_at_level(4, 2):
                ...     x
                CyclicPayloadTree(1)
                CyclicPayloadTree(2)
                CyclicPayloadTree(3)
                CyclicPayloadTree(4)

            Gets next 10 nodes at level 2:

            ::

                >>> for node in tree[0][0].get_next_n_nodes_at_level(10, 2):
                ...     node
                CyclicPayloadTree(1)
                CyclicPayloadTree(2)
                CyclicPayloadTree(3)
                CyclicPayloadTree(4)
                CyclicPayloadTree(5)
                CyclicPayloadTree(6)
                CyclicPayloadTree(7)
                CyclicPayloadTree(1)
                CyclicPayloadTree(2)
                CyclicPayloadTree(3)

        ..  container:: example

            Cyclic payload tree of length greater than ``1`` for examples with
            negative `n`:

            ::

                >>> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
                >>> tree = datastructuretools.CyclicPayloadTree(sequence)

            Gets previous 4 nodes at level 2:

            ::

                >>> for x in tree[0][0].get_next_n_nodes_at_level(-4, 2):
                ...     x
                CyclicPayloadTree(7)
                CyclicPayloadTree(6)
                CyclicPayloadTree(5)
                CyclicPayloadTree(4)

            Gets previous 10 nodes at level 2:

            ::

                >>> for node in tree[0][0].get_next_n_nodes_at_level(-10, 2):
                ...     node
                CyclicPayloadTree(7)
                CyclicPayloadTree(6)
                CyclicPayloadTree(5)
                CyclicPayloadTree(4)
                CyclicPayloadTree(3)
                CyclicPayloadTree(2)
                CyclicPayloadTree(1)
                CyclicPayloadTree(7)
                CyclicPayloadTree(6)
                CyclicPayloadTree(5)

        ..  container:: example

            Cyclic payload tree of length ``1`` for examples with positive `n`:

            ::

                >>> sequence = [0]
                >>> tree = datastructuretools.CyclicPayloadTree(sequence)

            Gets next 4 nodes at level -1:

            ::

                >>> for x in tree.get_next_n_nodes_at_level(4, -1):
                ...     x
                CyclicPayloadTree(0)
                CyclicPayloadTree(0)
                CyclicPayloadTree(0)
                CyclicPayloadTree(0)

        ..  container:: example

            Cyclic payload tree of length ``1`` for examples with negative `n`:

            ::

                >>> sequence = [0]
                >>> tree = datastructuretools.CyclicPayloadTree(sequence)

            Gets next 4 nodes at level -1:

            ::

                >>> for x in tree.get_next_n_nodes_at_level(-4, -1):
                ...     x
                CyclicPayloadTree(0)
                CyclicPayloadTree(0)
                CyclicPayloadTree(0)
                CyclicPayloadTree(0)

        Returns list of nodes.
        '''
        return PayloadTree.get_next_n_nodes_at_level(self, n, level)
示例#6
0
    def get_next_n_nodes_at_level(self, n, level):
        r'''Gets next `n` nodes of cyclic payload tree at `level`.

        ::

            >>> sequence = [[0, 1], [2, 3], [4, 5], [6, 7]]
            >>> tree = datastructuretools.CyclicPayloadTree(sequence)

        Gets next 4 nodes at level 2:

        ::

            >>> for x in tree[0][0].get_next_n_nodes_at_level(4, 2):
            ...     x
            CyclicPayloadTree(1)
            CyclicPayloadTree(2)
            CyclicPayloadTree(3)
            CyclicPayloadTree(4)

        Gets next 10 nodes at level 2:

        ::

            >>> for node in tree[0][0].get_next_n_nodes_at_level(10, 2):
            ...     node
            CyclicPayloadTree(1)
            CyclicPayloadTree(2)
            CyclicPayloadTree(3)
            CyclicPayloadTree(4)
            CyclicPayloadTree(5)
            CyclicPayloadTree(6)
            CyclicPayloadTree(7)
            CyclicPayloadTree(1)
            CyclicPayloadTree(2)
            CyclicPayloadTree(3)

        ### PREVIOUS MNODES ###

        Gets previous 4 nodes at level 2:

        ::

            >>> for x in tree[0][0].get_next_n_nodes_at_level(-4, 2):
            ...     x
            CyclicPayloadTree(7)
            CyclicPayloadTree(6)
            CyclicPayloadTree(5)
            CyclicPayloadTree(4)

        Gets previous 10 nodes at level 2:

        ::

            >>> for node in tree[0][0].get_next_n_nodes_at_level(-10, 2):
            ...     node
            CyclicPayloadTree(7)
            CyclicPayloadTree(6)
            CyclicPayloadTree(5)
            CyclicPayloadTree(4)
            CyclicPayloadTree(3)
            CyclicPayloadTree(2)
            CyclicPayloadTree(1)
            CyclicPayloadTree(7)
            CyclicPayloadTree(6)
            CyclicPayloadTree(5)

        Returns list of nodes.
        '''
        return PayloadTree.get_next_n_nodes_at_level(self, n, level)