X-Git-Url: http://dxcluster.org/gitweb/gitweb.cgi?a=blobdiff_plain;f=perl%2FChain.pm;h=27b966c869e823e8f879fcf45501af0c70803922;hb=refs%2Fheads%2Fip_address;hp=cb3a2be83feaf27eabf182ae5be8b84e058aea27;hpb=bf0078cc89a908d46a3f28c7f1c152c2cb4d6fc5;p=spider.git diff --git a/perl/Chain.pm b/perl/Chain.pm index cb3a2be8..27b966c8 100644 --- a/perl/Chain.pm +++ b/perl/Chain.pm @@ -2,16 +2,14 @@ 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 constant NEXT => 0; use constant PREV => 1; use constant OBJ => 2; -$docheck = 1; +use vars qw($docheck); + +$docheck = 0; sub _check { @@ -30,10 +28,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 +43,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 +67,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 +104,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 +150,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 +168,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);