File: Pure.pm

package info (click to toggle)
libforest-perl 0.09-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 316 kB
  • sloc: perl: 3,070; makefile: 2
file content (543 lines) | stat: -rw-r--r-- 11,774 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
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__

=pod

=head1 NAME

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

=head1 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");
  });

=head1 DESCRIPTION

This module is a base class for L<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 L<Forest::Tree> produces a graph with back references

=head1 ATTRIBUTES

=over 4

=item I<node>

=item I<children>

=over 4

=item B<get_child_at ($index)>

Return the child at this position. (zero-base index)

=item B<child_count>

Returns the number of children this tree has

=back

=item I<size>

=over 4

=item B<size>

=item B<has_size>

=back

=item I<height>

=over 4

=item B<height>

=item B<has_height>

=back

=back

=head1 METHODS

=over 4

=item B<is_leaf>

True if the current tree has no children

=item B<traverse (\&func)>

Takes a reference to a subroutine and traverses the tree applying this subroutine to
every descendant. (But not the root)

=item B<visit (&func)>

Traverse the entire tree, including the root.

=item B<fmap_cont (&func)>

A CPS form of C<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 C<$cont> is a code ref that when invoked will apply that same function to the children of C<$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.

=item B<add_children (@children)>

=item B<add_child ($child)>

Create a new tree node with the children appended.

The children must inherit C<Forest::Tree::Pure>

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

=item B<insert_child_at ($index, $child)>

Insert a child at this position. (zero-base index)

Returns a derived tree with overridden children.

=item B<set_child_at ($index, $child)>

Replaces the child at C<$index> with C<$child>.

=item B<remove_child_at ($index)>

Remove the child at this position. (zero-base index)

Returns a derived tree with overridden children.

=item B<locate (@path)>

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);

=item B<descend (@path)>

Like C<lookup> except that it returns every object in the path, not just the leaf.

=item C<transform (\@path, $method, @args)>

Performs a lookup on C<@path>, applies the method C<$method> with C<@args> to
the located node, and clones the path to the parent returning a derived tree.

This method is also implemented in L<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 C<[ 1, 3 ]>, call C<insert_child_at> on it,
creating a new version of C<[ 1, 3 ]>, and then return a cloned version of
C<[ 1 ]> and the root node recursively, such that C<$new> appears to be a
mutated C<$root>.

=item set_node $new

Returns a clone of the tree node with the node value changed.

=item C<replace $arg>

Returns the argument. This is useful when used with C<transform>.

=item B<clone>

Provided by L<MooseX::Clone>.

Deeply clones the entire tree.

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

=item B<reconstruct_with_class $class>

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

Does not use C<clone>.

=item B<to_mutable_tree>

Invokes C<reconstruct_with_class> with L<Forest::Tree> as the argument.

=item B<to_pure_tree>

Returns the invocant.

=item B<get_child_index ($child)>

Returns the index of C<$child> in C<children> or undef if it isn't a child of
the current tree.

=back

=head1 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.

=head1 AUTHOR

Yuval Kogman

=head1 COPYRIGHT AND LICENSE

Copyright 2008-2010 Infinity Interactive, Inc.

L<http://www.iinteractive.com>

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

=cut