Subject: | next_successor unavailable in Traversal (PATCH) |
Though the documentation mentions it briefly, it is currently (as of
Graph-0.80) impossible to use the next_successor parameter when creating
a new Graph::Traversal::* object (it dies with "unknown attribute").
The attached patch enables, documents, and tests this feature. Thanks!
- Ted Carnahan
Subject: | Graph-0.80-next_successor.patch |
diff -r Graph-0.80/lib/Graph/Traversal.pm Graph-0.80-mod/lib/Graph/Traversal.pm
147c147
< next_root next_alphabetic next_numeric next_random
---
> next_root next_alphabetic next_numeric next_random next_successor
566a567,573
> =item next_successor
>
> Called when choosing the next vertex to visit. Called with arguments
> ($self, $next) where $next is a hash reference with the possible
> next vertices as keys. Use this to provide a custom ordering for
> choosing vertices, as opposed to C<next_numeric> or C<next_alphabetic>.
>
diff -r Graph-0.80/t/59_dfs.t Graph-0.80-mod/t/59_dfs.t
1c1
< use Test::More tests => 253;
---
> use Test::More tests => 272;
625a626,657
>
> {
> my @pre;
> my @post;
> my $t = Graph::Traversal::DFS->new($g0,
> first_root => 'a',
> pre => sub { push @pre, $_[0] },
> post => sub { push @post, $_[0] },
> next_successor => sub { shift; (reverse sort keys %{ $_[0] })[0] });
> my @t0 = $t->preorder;
> my @t1 = $t->postorder;
> my @t2 = $t->postorder;
>
> simple($g1, @t0);
> simple($g1, @t1);
> simple($g1, @t2);
>
> is("@pre", "a e f b d c", "pre");
> is("@post", "f e d c b a", "post");
> is("@t0", "@pre", "t0");
> is("@t1", "@post", "t1");
> is("@t2", "@post", "t2");
>
> is($t->unseen, 0, "unseen none");
> is($t->seen, 6, "seen all");
> is($t->seeing, 0, "seeing none");
> is("@{[sort $t->seen]}", "a b c d e f", "seen all");
> is("@{[$t->roots]}", "a", "roots");
> ok( $t->is_root('a') );
> ok(!$t->is_root('b') );
> ok(!$t->is_root('c') );
> }
diff -r Graph-0.80/t/60_bfs.t Graph-0.80-mod/t/60_bfs.t
1c1
< use Test::More tests => 77;
---
> use Test::More tests => 93;
178a179,206
> {
> my @pre;
> my @post;
> my $t = Graph::Traversal::BFS->new($g0,
> first_root => 'a',
> pre => sub { push @pre, $_[0] },
> post => sub { push @post, $_[0] },
> next_successor => sub { shift; (reverse sort keys %{ $_[0] })[0] });
> my @t0 = $t->preorder;
> my @t1 = $t->postorder;
> my @t2 = $t->bfs;
>
> simple($g1, @t0);
> simple($g1, @t1);
> simple($g1, @t2);
>
> is("@pre", "a e b f d c", "pre");
> is("@post", "a e b f d c", "post");
> is("@t0", "@pre", "t0");
> is("@t1", "@post", "t1");
> is("@t2", "@post", "t2");
>
> is($t->unseen, 0, "unseen none");
> is($t->seen, 6, "seen all");
> is($t->seeing, 0, "seeing none");
> is("@{[sort $t->seen]}", "a b c d e f", "seen all");
> is("@{[$t->roots]}", "a", "roots");
> }