| Forest documentation | Contained in the Forest distribution. |
transform (\@path, $method, @args)replace $argForest::Tree::Pure - An n-ary tree
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");
});
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
Return the child at this position. (zero-base index)
Returns the number of children this tree has
True if the current tree has no children
Takes a reference to a subroutine and traverses the tree applying this subroutine to every descendant. (But not the root)
Traverse the entire tree, including the root.
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.
Create a new tree node with the children appended.
The children must inherit Forest::Tree::Pure
Note that this method does not mutate the tree, instead it clones and returns a tree with the augmented list of children.
Insert a child at this position. (zero-base index)
Returns a derived tree with overridden children.
Replaces the child at $index with $child.
Remove the child at this position. (zero-base index)
Returns a derived tree with overridden children.
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);
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.
Returns a clone of the tree node with the node value changed.
replace $argReturns the argument. This is useful when used with transform.
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.
Recursively recreates the tree by passing constructor arguments to $class.
Does not use clone.
Invokes reconstruct_with_class with Forest::Tree as the argument.
Returns the invocant.
Returns the index of $child in children or undef if it isn't a child of
the current tree.
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.
Yuval Kogman
Copyright 2008-2010 Infinity Interactive, Inc.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Forest documentation | Contained in the Forest distribution. |
package Forest::Tree::Pure; use Moose; use MooseX::AttributeHelpers; our $VERSION = '0.09'; our $AUTHORITY = 'cpan:STEVAN'; use Scalar::Util 'reftype', 'refaddr'; use List::Util 'sum', 'max'; with qw(MooseX::Clone); has 'node' => ( is => 'ro', isa => 'Item', predicate => 'has_node', ); has 'uid' => ( is => 'rw', isa => 'Value', lazy => 1, default => sub { (overload::StrVal($_[0]) =~ /\((.*?)\)$/)[0] }, ); has 'children' => ( metaclass => 'Collection::Array', is => 'ro', isa => 'ArrayRef[Forest::Tree::Pure]', lazy => 1, default => sub { [] }, provides => { 'get' => 'get_child_at', 'count' => 'child_count', }, ); has 'size' => ( traits => [qw(NoClone)], is => 'ro', isa => 'Int', lazy_build => 1, ); sub _build_size { my $self = shift; if ( $self->is_leaf ) { return 1; } else { return 1 + sum map { $_->size } @{ $self->children }; } } has 'height' => ( traits => [qw(NoClone)], is => 'ro', isa => 'Int', lazy_build => 1, ); sub _build_height { my $self = shift; if ( $self->is_leaf ) { return 0; } else { return 1 + max map { $_->height } @{ $self->children }; } } ## informational sub is_leaf { (shift)->child_count == 0 } ## traversal sub traverse { my ($self, @args) = @_; $_->visit(@args) for @{ $self->children }; } sub visit { my ( $self, $f, @args ) = @_; $self->fmap_cont(sub { my ( $tree, $cont, @args ) = @_; $tree->$f(@args); $cont->(); }); } sub fmap_cont { my ( $self, @args ) = @_; unshift @args, "callback" if @args % 2 == 1; my %args = ( depth => 0, path => [], index_path => [], @args ); my $f = $args{callback}; (defined($f)) || confess "Cannot traverse without traversal function"; (!ref($f) or reftype($f) eq "CODE") || confess "Traversal function must be a CODE reference or method name, not: $f"; $self->$f( sub { my ( @inner_args ) = @_; unshift @inner_args, "callback" if @inner_args % 2 == 1; my $children = $args{children} || $self->children; my %child_args = ( %args, depth => $args{depth} + 1, path => [ @{ $args{path} }, $self ], parent => $self, @inner_args ); my @index_path = @{ $args{index_path} }; my $i = 0; map { my $index = $i++; $_->fmap_cont( %child_args, index => $index, index_path => [ @index_path, $index ], ) } @$children; }, %args, ); } sub locate { my ( $self, @path ) = @_; my @nodes = $self->descend(@path); return $nodes[-1]; } sub descend { my ( $self, @path ) = @_; if ( @path ) { my ( $head, @tail ) = @path; if ( my $child = $self->get_child_at($head) ) { return ( $self, $child->descend(@tail) ); } else { confess "No such child $head"; } } else { return $self; } } sub transform { my ( $self, $path, $method, @args ) = @_; if ( @$path ) { my ( $i, @path ) = @$path; my $targ = $self->get_child_at($i); my $transformed = $targ->transform(\@path, $method, @args); if ( refaddr($transformed) == refaddr($targ) ) { return $self; } else { return $self->set_child_at( $i => $transformed ); } } else { return $self->$method(@args); } } sub set_node { my ( $self, $node ) = @_; $self->clone( node => $node ); } sub replace { my ( $self, $replacement ) = @_; return $replacement; } sub add_children { my ( $self, @additional_children ) = @_; foreach my $child ( @additional_children ) { (blessed($child) && $child->isa(ref $self)) || confess "Child parameter must be a " . ref($self) . " not (" . (defined $child ? $child : 'undef') . ")"; } my @children = @{ $self->children }; push @children, @additional_children; return $self->clone( children => \@children ); } sub add_child { my ( $self, $child ) = @_; $self->add_children($child); } sub set_child_at { my ( $self, $index, $child ) = @_; (blessed($child) && $child->isa(ref $self)) || confess "Child parameter must be a " . ref($self) . " not (" . (defined $child ? $child : 'undef') . ")"; my @children = @{ $self->children }; $children[$index] = $child; $self->clone( children => \@children ); } sub remove_child_at { my ( $self, $index ) = @_; my @children = @{ $self->children }; confess "No child at index '$index'" if @children <= $index; splice @children, $index, 1; $self->clone( children => \@children ); } sub insert_child_at { my ( $self, $index, $child ) = @_; (blessed($child) && $child->isa('Forest::Tree::Pure')) || confess "Child parameter must be a Forest::Tree::Pure not (" . (defined $child ? $child : 'undef') . ")"; my @children = @{ $self->children }; confess "'$index' is out of bounds" if @children < $index; splice @children, $index, 0, $child; $self->clone( children => \@children ); } sub get_child_index { my ( $self, $child ) = @_; my $index = 0; foreach my $sibling (@{ $self->children }) { (refaddr($sibling) eq refaddr($child)) && return $index; $index++; } return; } sub reconstruct_with_class { my ( $self, $class ) = @_; confess "No class provided" unless defined($class); return $class->new( node => $self->node, children => [ map { $_->reconstruct_with_class($class) } @{ $self->children }, ], ); } sub to_pure_tree { my $self = shift; return $self; } sub to_mutable_tree { my $self = shift; $self->reconstruct_with_class("Forest::Tree"); } __PACKAGE__->meta->make_immutable; no Moose; 1; __END__