GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  FOREST::TREE::PURE (3)

.ds Aq ’

NAME

Forest::Tree::Pure - An n-ary tree

CONTENTS

SYNOPSIS



  use Forest::Tree;

  my $t = Forest::Tree::Pure->new(
      node     => 1,
      children => [
          Forest::Tree::Pure->new(
              node     => 1.1,
              children => [
                  Forest::Tree::Pure->new(node => 1.1.1),
                  Forest::Tree::Pure->new(node => 1.1.2),
                  Forest::Tree::Pure->new(node => 1.1.3),
              ]
          ),
          Forest::Tree::Pure->new(node => 1.2),
          Forest::Tree::Pure->new(
              node     => 1.3,
              children => [
                  Forest::Tree::Pure->new(node => 1.3.1),
                  Forest::Tree::Pure->new(node => 1.3.2),
              ]
          ),
      ]
  );

  $t->traverse(sub {
      my $t = shift;
      print((     x $t->depth) . ($t->node || \undef) . "\n");
  });



DESCRIPTION

This module is a base class for Forest::Tree providing functionality for immutable trees.

It can be used independently for trees that require sharing of children between parents.

There is no single authoritative parent (no upward links at all), and changing of data is not supported.

This class is appropriate when many tree roots share the same children (e.g. in a versioned tree).

This class is strictly a DAG, wheras Forest::Tree produces a graph with back references

ATTRIBUTES

node
children
<B>get_child_at ($index)B> Return the child at this position. (zero-base index)
<B>child_countB> Returns the number of children this tree has
size
<B>sizeB>
<B>has_sizeB>
height
<B>heightB>
<B>has_heightB>

METHODS

<B>is_leafB> True if the current tree has no children
<B>traverse (\&func)B> Takes a reference to a subroutine and traverses the tree applying this subroutine to every descendant. (But not the root)
<B>visit (&func)B> Traverse the entire tree, including the root.
<B>fmap_cont (&func)B> A CPS form of visit that lets you control when and how data flows from the children.

It takes a callback in the form:



    sub {
        my ( $tree, $cont, @args ) = @_;

        ...
    }



and $cont is a code ref that when invoked will apply that same function to the children of $tree.

This allows you to do things like computing the sum of all the node values in a tree, for instance:



    use List::Util qw(sum);

    my $sum = $tree->fmap_cont(sub {
        my ( $tree, $cont ) = @_;

        return sum( $tree->node, $cont->() );
    });



And also allows to stop traversal at a given point.

<B>add_children (@children)B>
<B>add_child ($child)B> Create a new tree node with the children appended.

The children must inherit Forest::Tree::Pure

Note that this method does <B>notB> mutate the tree, instead it clones and returns a tree with the augmented list of children.

<B>insert_child_at ($index, B>$child<B>)B> Insert a child at this position. (zero-base index)

Returns a derived tree with overridden children.

<B>set_child_at ($index, B>$child<B>)B> Replaces the child at $index with $child.
<B>remove_child_at ($index)B> Remove the child at this position. (zero-base index)

Returns a derived tree with overridden children.

<B>locate (@path)B> Find a child using a path of child indexes. These two examples return the same object:



    $tree->get_child_at(0)->get_child_at(1)->get_child_at(0);

    $tree->locate(0, 1, 0);



<B>descend (@path)B> Like lookup except that it returns every object in the path, not just the leaf.
transform (\@path, $method, @args) Performs a lookup on @path, applies the method $method with @args to the located node, and clones the path to the parent returning a derived tree.

This method is also implemented in Forest::Tree by mutating the tree in place and returning the original tree, so the same transformations should work on both pure trees and mutable ones.

This code:



    my $new = $root->transform([ 1, 3 ], insert_child_at => 3, $new_child);



will locate the child at the path [ 1, 3 ], call insert_child_at on it, creating a new version of [ 1, 3 ], and then return a cloned version of [ 1 ] and the root node recursively, such that $new appears to be a mutated $root.

set_node $new Returns a clone of the tree node with the node value changed.
replace $arg Returns the argument. This is useful when used with transform.
<B>cloneB> Provided by MooseX::Clone.

Deeply clones the entire tree.

Subclasses should use MooseX::Clone traits to specify the correct cloning behavior for additional attributes if cloning is used.

<B>reconstruct_with_class B>$class<B>B> Recursively recreates the tree by passing constructor arguments to $class.

Does not use clone.

<B>to_mutable_treeB> Invokes reconstruct_with_class with Forest::Tree as the argument.
<B>to_pure_treeB> Returns the invocant.
<B>get_child_index ($child)B> Returns the index of $child in children or undef if it isn’t a child of the current tree.

BUGS

All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.

AUTHOR

Yuval Kogman

COPYRIGHT AND LICENSE

Copyright 2008-2014 Infinity Interactive, Inc.

<http://www.iinteractive.com>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 FOREST::TREE::PURE (3) 2013-12-18

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.