| 1 | package Bio::ConnectDots::SimpleGraph::Traversal; | ||
| 2 | use vars qw(@ISA @AUTO_ATTRIBUTES @OTHER_ATTRIBUTES %SYNONYMS %DEFAULTS); | ||
| 3 | use Class::AutoClass::Root; | ||
| 4 | use Class::AutoClass; | ||
| 5 | use Bio::ConnectDots::SimpleGraph; | ||
| 6 | use strict; | ||
| 7 | @ISA = qw(Class::AutoClass); # AutoClass must be first!! | ||
| 8 | |||
| 9 | @AUTO_ATTRIBUTES=qw(order what graph start is_initialized | ||
| 10 | _past _present _future); | ||
| 11 | @OTHER_ATTRIBUTES=qw(); | ||
| 12 | %SYNONYMS=(); | ||
| 13 | %DEFAULTS=(order=>'dfs', | ||
| 14 | what=>'node', | ||
| 15 | _past=>{}, | ||
| 16 | _future=>[]); | ||
| 17 | Class::AutoClass::declare(__PACKAGE__); | ||
| 18 | |||
| 19 | sub _init_self { | ||
| 20 | my($self,$class,$args)=@_; | ||
| 21 | return unless $class eq __PACKAGE__; # to prevent subclasses from re-running this | ||
| 22 | $self->graph or $self->graph(new Bio::ConnectDots::SimpleGraph); # can't be in DEFAULTS - circular includes! | ||
| 23 | } | ||
| 24 | |||
| 25 | sub has_next { | ||
| 26 | my($self)=@_; | ||
| 27 | $self->reset unless $self->is_initialized; | ||
| 28 | @{$self->_future}>0; | ||
| 29 | } | ||
| 30 | sub get_next { | ||
| 31 | my($self)= @_; | ||
| 32 | $self->reset unless $self->is_initialized; | ||
| 33 | my $past=$self->_past; | ||
| 34 | my $future=$self->_future; | ||
| 35 | my $present; | ||
| 36 | my $graph=$self->graph; | ||
| 37 | while (@$future) { | ||
| 38 | $present=shift @$future; | ||
| 39 | unless($past->{$present}) { # this is a new node | ||
| 40 | $self->_present($present); | ||
| 41 | $past->{$present}=1; | ||
| 42 | if ($self->order =~ /^d/i) { | ||
| 43 | unshift(@$future,$graph->neighbors($present,$self->what)); | ||
| 44 | } else { | ||
| 45 | push(@$future,$graph->neighbors($present,$self->what)); | ||
| 46 | } | ||
| 47 | return $present; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | $self->_present(undef); | ||
| 51 | } | ||
| 52 | sub get_all { | ||
| 53 | my($self)= @_; | ||
| 54 | $self->reset unless $self->is_initialized; | ||
| 55 | my $past=$self->_past; | ||
| 56 | my $future=$self->_future; | ||
| 57 | my $present; | ||
| 58 | my $graph=$self->graph; | ||
| 59 | my $results=[]; | ||
| 60 | while (@$future) { | ||
| 61 | $present=shift @$future; | ||
| 62 | unless($past->{$present}) { # this is a new node | ||
| 63 | $past->{$present}=1; | ||
| 64 | push(@$results,$present); | ||
| 65 | if ($self->order =~ /^d/i) { | ||
| 66 | unshift(@$future,$graph->neighbors($present,$self->what)); | ||
| 67 | } else { | ||
| 68 | push(@$future,$graph->neighbors($present,$self->what)); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | $self->_present(undef); | ||
| 73 | wantarray? @$results: $results; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub get_this { | ||
| 77 | my($self)=@_; | ||
| 78 | $self->reset unless $self->is_initialized; | ||
| 79 | $self->_present; | ||
| 80 | } | ||
| 81 | sub reset { | ||
| 82 | my($self)= @_; | ||
| 83 | $self->_past({}); | ||
| 84 | $self->_present(undef); | ||
| 85 | $self->_future([]); | ||
| 86 | $self->is_initialized(1); | ||
| 87 | my $graph=$self->graph; | ||
| 88 | my $start=$self->start; | ||
| 89 | my $what=$self->what || 'node'; | ||
| 90 | if ($what=~/^n/i) { | ||
| 91 | defined $start or $start=$graph->nodes->[0]; | ||
| 92 | } elsif ($what=~/^e/i) { | ||
| 93 | $start=defined $start? $graph->edge($start): $graph->edges->[0]; | ||
| 94 | } else { | ||
| 95 | $self->throw("Unrecognized \$what parameter $what: should be 'node' or 'edge'"); | ||
| 96 | } | ||
| 97 | return unless defined $start; | ||
| 98 | $self->_future([$start]); | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | 1; |