X-Git-Url: http://dxcluster.net/gitweb/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=perl%2FChain.pm;h=c065d17a62305b6c9fb224f0eb51a45b5ab34c1b;hb=31fca8dfb43587fe8f7b6bb657bfa654e7a8566b;hp=cb3a2be83feaf27eabf182ae5be8b84e058aea27;hpb=bf0078cc89a908d46a3f28c7f1c152c2cb4d6fc5;p=spider.git diff --git a/perl/Chain.pm b/perl/Chain.pm index cb3a2be8..c065d17a 100644 --- a/perl/Chain.pm +++ b/perl/Chain.pm @@ -2,15 +2,19 @@ package Chain; use strict; use Carp; - -use vars qw($VERSION $docheck); -$VERSION = do { my @r = (q$Revision$ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r}; +use vars qw($VERSION $BRANCH); +$VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ ); +$BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0)); +$main::build += $VERSION; +$main::branch += $BRANCH; use constant NEXT => 0; use constant PREV => 1; use constant OBJ => 2; +use vars qw($docheck); + $docheck = 1; sub _check @@ -30,10 +34,11 @@ sub setcheck # constructor sub new { - my $name = shift; - my $ref = shift; + my $pkg = shift; + my $name = ref $pkg || $pkg; + my $self = []; - push @$self, $self, $self, $ref; + push @$self, $self, $self, @_; return bless $self, $name; } @@ -44,7 +49,7 @@ sub ins $docheck && _check($p); - my $q = ref $ref && $ref->isa('Chain') ? $ref : new Chain $ref; + my $q = ref $ref && $ref->isa('Chain') ? $ref : Chain->new($ref); $q->[PREV] = $p->[PREV]; $q->[NEXT] = $p; $p->[PREV]->[NEXT] = $q; @@ -68,9 +73,10 @@ sub del $docheck && _check($p); - $p->[PREV]->[NEXT] = $p->[NEXT]; + my $q = $p->[PREV]->[NEXT] = $p->[NEXT]; $p->[NEXT]->[PREV] = $p->[PREV]; - return $p->[PREV]; + $p->[NEXT] = $p->[PREV] = undef; + return $q; } # Is this chain empty? @@ -104,7 +110,7 @@ sub prev $docheck && _check($base); - return $base->[NEXT] == $base ? undef : $base->[PREV] unless $p; + return $base->[PREV] == $base ? undef : $base->[PREV] unless $p; $docheck && _check($p); @@ -150,6 +156,13 @@ sub count return $count; } +sub close +{ + my $base = shift; + $base->flush; + $base->[PREV] = $base->[NEXT] = undef; +} + 1; __END__ # Below is the stub of documentation for your module. You better edit it! @@ -161,9 +174,9 @@ Chain - Double linked circular chain handler =head1 SYNOPSIS use Chain; - $base = new Chain; - $p->ins($ref); - $p->add($ref); + $base = new Chain [$obj]; + $p->ins($ref [,$obj]); + $p->add($ref [,$obj]); $ref = $p->obj or $p->obj($ref); $q = $base->next($p); $q = $base->prev($p);