caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
812 stars 186 forks source link

Use `reverse` as an input parameter to front-end functions only #108

Open GalacticStarfish opened 5 years ago

GalacticStarfish commented 5 years ago

The back-end private functions should not have both reverse and key as input parameters. It is fine if the front-end allows users to specify reverse and key, the back-end functions are simpler if we use key only.

key and reverse tell us how to order the nodes for printing purposes.

reverse is a Boolean, which if set to true, indicates that we should print the nodes in reverse order. If reverse is false, we are supposed to print the nodes in their natural order.

key allows more complicated node orderings than a mere reversal. key is also sufficiently general to define reverse node ordering.

Below, I have extracted behavior of functions only in-terms of input arguments key and reverse

def show(self, key=None, reverse=False):
    self.__print_backend(self, key, reverse)

def __str__(self, key=None, reverse=False):
    self.__print_backend(self, key, reverse)

save2file(self, key=None, reverse=False):
    self.__print_backend(self, key, reverse)

def __print_backend(self, key=None, reverse=False):
    # `key` will passed as input to the built-in python function `sorted`

    if key is None:
        def key(node):     
            # does **NOT** actually accept a node as input
            # accepts a node identifier
            # if `n` is a node, then `n._identifier` is the input to the key function.       
            return node

    __get_iter(key, reverse)

def __get_iter(self, key, reverse):
        if key:            
            children.sort(key=key, reverse=reverse)
             # `children` is a list of node identifiers (not the nodes themselves)
             # We are calling the list.sort() method

             # children = [self[i] for i in node.fpointer if filter_(self[i])]
             # node.fpointer returns node.fpointer
             # node.fpointer returns node.fpointer

        else: # key != none
                # This almost never happens, because if `key` was originally `None`
                # then `key` has long since been replaced by a function which simply returns
                # the node.
            if reverse: #

            children = reversed(children)

def expand_tree(self, key=None, reverse=False):
    queue.sort(key, reverse)
    expansion.sort(key=key, reverse=reverse)

If a user passes reverse = true into one of the front-end functions, we need only generate a key-function which reverses the keys.

if reverse:
    def bck_key(node):
        return (-1) * frnt_key(node)
            # key functions do **NOT** actually accept a nodes as input
            # they accept node identifiers
            # if `n` is a node, then `n._identifier` is the input to the key function.       

From then on, all back-end functions can use key only, instead of accepting both key and reversed as input arguments.