| 1 | package Bio::GMOD::Blast::Graph::ListEnumerator; | ||
| 2 | BEGIN { | ||
| 3 | $Bio::GMOD::Blast::Graph::ListEnumerator::AUTHORITY = 'cpan:RBUELS'; | ||
| 4 | } | ||
| 5 | BEGIN { | ||
| 6 | $Bio::GMOD::Blast::Graph::ListEnumerator::VERSION = '0.06'; | ||
| 7 | } | ||
| 8 | ##################################################################### | ||
| 9 | # | ||
| 10 | # Cared for by Shuai Weng <shuai@genome.stanford.edu> | ||
| 11 | # | ||
| 12 | # Originally created by John Slenk <jces@genome.stanford.edu> | ||
| 13 | # | ||
| 14 | # You may distribute this module under the same terms as perl itself | ||
| 15 | #----------------------------------------------------------------- | ||
| 16 | |||
| 17 | |||
| 18 | use Bio::GMOD::Blast::Graph::BaseObj; | ||
| 19 | use Bio::GMOD::Blast::Graph::MyUtils; | ||
| 20 | use Bio::GMOD::Blast::Graph::MyDebug qw( dmsg ); | ||
| 21 | use Bio::GMOD::Blast::Graph::List; | ||
| 22 | |||
| 23 | @ISA = qw( Bio::GMOD::Blast::Graph::BaseObj ); | ||
| 24 | |||
| 25 | my $kList = Bio::GMOD::Blast::Graph::MyUtils::makeVariableName( "list" ); | ||
| 26 | my $kIndex = Bio::GMOD::Blast::Graph::MyUtils::makeVariableName( "index" ); | ||
| 27 | my $kMaxDex = Bio::GMOD::Blast::Graph::MyUtils::makeVariableName( "max", "dex" ); | ||
| 28 | |||
| 29 | #################################################################### | ||
| 30 | sub init { | ||
| 31 | #################################################################### | ||
| 32 | my( $self, $list ) = @_; | ||
| 33 | |||
| 34 | #dmsg( "init(): ", $list->toString() ); | ||
| 35 | |||
| 36 | $self->{ $kList } = $list; | ||
| 37 | $self->{ $kMaxDex } = $list->getCount() - 1; | ||
| 38 | $self->reset(); | ||
| 39 | } | ||
| 40 | |||
| 41 | #################################################################### | ||
| 42 | sub reset { | ||
| 43 | #################################################################### | ||
| 44 | my( $self ) = shift; | ||
| 45 | $self->{ $kIndex } = -1; | ||
| 46 | } | ||
| 47 | |||
| 48 | #################################################################### | ||
| 49 | sub getList { | ||
| 50 | #################################################################### | ||
| 51 | my( $self ) = shift; | ||
| 52 | return( $self->{ $kList } ); | ||
| 53 | } | ||
| 54 | |||
| 55 | #################################################################### | ||
| 56 | sub previousIndex { | ||
| 57 | #################################################################### | ||
| 58 | my( $self ) = shift; | ||
| 59 | my( $dex ); | ||
| 60 | |||
| 61 | $dex = $self->getIndex(); | ||
| 62 | |||
| 63 | # we can go back to -1, even though that | ||
| 64 | # is sort of a cheesy hack, to show that | ||
| 65 | # we don't want anything in the list. | ||
| 66 | if( $dex > -1 ) | ||
| 67 | { | ||
| 68 | $dex--; | ||
| 69 | } | ||
| 70 | |||
| 71 | $self->putIndex( $dex ); | ||
| 72 | } | ||
| 73 | |||
| 74 | # have to call this first to start things off. | ||
| 75 | #################################################################### | ||
| 76 | sub nextIndex { | ||
| 77 | #################################################################### | ||
| 78 | my( $self ) = shift; | ||
| 79 | my( $dex ); | ||
| 80 | my( $maxDex ); | ||
| 81 | |||
| 82 | $dex = $self->getIndex(); | ||
| 83 | $maxDex = $self->getMaxIndex(); | ||
| 84 | |||
| 85 | #dmsg( "nextIndex(): before =", $dex ); | ||
| 86 | |||
| 87 | # should be able to move to maxDex+1 | ||
| 88 | # which signifies there are no more elements. | ||
| 89 | if( $dex <= $maxDex ) | ||
| 90 | { | ||
| 91 | $self->putIndex( ++$dex ); | ||
| 92 | } | ||
| 93 | |||
| 94 | #dmsg( "nextIndex(): after =", $self->getIndex() ); | ||
| 95 | } | ||
| 96 | |||
| 97 | #################################################################### | ||
| 98 | sub getCurrentElement { | ||
| 99 | #################################################################### | ||
| 100 | my( $self ) = shift; | ||
| 101 | my( $elem ); | ||
| 102 | my( $dex ); | ||
| 103 | my( $maxDex ); | ||
| 104 | |||
| 105 | $elem = undef; | ||
| 106 | $dex = $self->getIndex(); | ||
| 107 | $maxDex = $self->getMaxIndex(); | ||
| 108 | |||
| 109 | #dmsg( "nextIndex(): dex=$dex maxDex=$maxDex" ); | ||
| 110 | |||
| 111 | if( $dex <= $maxDex ) | ||
| 112 | { | ||
| 113 | $elem = $self->getList()->getElementAt( $dex ); | ||
| 114 | } | ||
| 115 | |||
| 116 | return( $elem ); | ||
| 117 | } | ||
| 118 | |||
| 119 | #################################################################### | ||
| 120 | sub getNextElement { | ||
| 121 | #################################################################### | ||
| 122 | my( $self ) = shift; | ||
| 123 | $self->nextIndex(); | ||
| 124 | return( $self->getCurrentElement() ); | ||
| 125 | } | ||
| 126 | |||
| 127 | #################################################################### | ||
| 128 | sub getMaxIndex { | ||
| 129 | #################################################################### | ||
| 130 | my( $self ) = shift; | ||
| 131 | return( $self->{ $kMaxDex } ); | ||
| 132 | } | ||
| 133 | |||
| 134 | #################################################################### | ||
| 135 | sub getIndex { | ||
| 136 | #################################################################### | ||
| 137 | my( $self ) = shift; | ||
| 138 | return( $self->{ $kIndex } ); | ||
| 139 | } | ||
| 140 | |||
| 141 | #################################################################### | ||
| 142 | sub putIndex { | ||
| 143 | #################################################################### | ||
| 144 | my( $self, $dex ) = @_; | ||
| 145 | $self->{ $kIndex } = $dex; | ||
| 146 | } | ||
| 147 | |||
| 148 | #################################################################### | ||
| 149 | sub getCount { | ||
| 150 | #################################################################### | ||
| 151 | my( $self ) = shift; | ||
| 152 | return( $self->getMaxIndex() ); | ||
| 153 | } | ||
| 154 | |||
| 155 | #################################################################### | ||
| 156 | 1; | ||
| 157 | #################################################################### | ||
| 158 | |||
| 159 | __END__ |