#################################################################### # COMBINE BRANCHES #------------------------------------------------------------------- # # Imagine a tree. Lets separate it into 3 parts: branches, twigs, # and leaves. Leaves are a certain color: green, red, golden, # ultraviolet... On this tree, there are different kinds of twigs, # each growing leaves of a certain set of colors. If the twig grows # leaves of a color, it is the ONLY twig to grow leaves of that # color. So one kind of twig might grow green, red, and yellow, # and another kind would grow purple, pink, and poka-dot. Twigs have # lots of empty space where they *could* be growing the other colors # of leaves; they just don't. Thus, we have a lot of redundant # twigs, and we should instead combine all the leaves onto one twig. # This subroutine accomplishes that. # # For the purposes of this subroutine, a tree is a arrays of arrays # (of arrays of arrays...) to an arbitrary depth. The user passes # a \$twig_depth and \$leaf_depth to the subroutine. The \$twig_depth # determines where branches become twigs, and similarly the # \$leaf_depth determines the depth of the leaves. The leaf 'color' # is its array index. Arrays are presumably sparsly populated at # the leaf level. # # To clarify/generalize, a \$twig could have more sub-\$twigs coming # off it and it is the FIRST twig which determines the colors of # the leaves. So when we're done combining leaves, we have one # kind of twig left (which will be in array index 0). That's # redundant too, so we remove that twig entirely, i.e. eliminating # that entire depth of the tree (\$depth -= 1 to all deeper # elements). # # The subroutine is passed a reference to a \$tree, the # \$twig_depth, and a \$leaf_depth, and returns a reference to a # \$tree. Mind you, no copy of the tree is made; the first \$tree is # destroyed by this subroutine. Returning the new tree is necessary # because \$twig_depth could be 0, in which case you cannot set the # original (outside of the subroutine) tree to point to the deeper # twig. Perhaps the better way to deal with this is to pass a # reference to a reference to an array, I am undecided. # # A future version of this subroutine will handle having the leaf # colors scattered across all twigs, specifically how to combine # them in case of collisions. See the TODO:'s towards the end. # ####################################################################