| 1 | package Tie::Array::Pack; | ||||||||||||
| 2 | # | ||||||||||||
| 3 | # $Id: Pack.pm,v 0.2 2006/12/22 03:20:22 dankogai Exp $ | ||||||||||||
| 4 | # | ||||||||||||
| 5 | 2 | ▶ | use 5.008001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | our $VERSION = sprintf "%d.%02d", q$Revision: 0.2 $ =~ /(\d+)/g; | |||||||||||
| 9 | 2 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | # we don't need Tie::Array anymore -- all methods implemented! | ||||||||||||
| 12 | # use base 'Tie::Array'; | ||||||||||||
| 13 | 2 | ▶ | use bytes (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 2 | our $DEBUG = 0; | |||||||||||
| 16 | |||||||||||||
| 17 | 0 | ▶ | sub DESTROY { } # no-op | ||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | 18 | ▶ | sub TIEARRAY { | ||||||||||
|
Subroutine
TIEARRAY: called CC=3 SCAR=11.2
| |||||||||||||
| 20 | 18 | my $class = shift; | |||||||||||
| 21 | 18 | my $fmt = shift; | |||||||||||
| 22 | 18 | ▶ | my $empty = shift || 0; | ||||||||||
|
Condition50%1/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 23 | 18 | my $size = eval { bytes::length( pack( $fmt, $empty ) ) }; | |||||||||||
| 24 | 18 | ▶ | croak $@ if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | 18 | bless { | |||||||||||
| 26 | str => '', | ||||||||||||
| 27 | fmt => $fmt, | ||||||||||||
| 28 | size => $size, | ||||||||||||
| 29 | empty => $empty | ||||||||||||
| 30 | }; | ||||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | 2844 | ▶ | sub FETCH { | ||||||||||
|
Subroutine
FETCH: called CC=2 SCAR=7.2
| |||||||||||||
| 34 | 2844 | ▶ | $DEBUG and warn sprintf "%s->FETCH(%d)", @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | unpack( $_[0]->{fmt}, | ||||||||||||
| 36 | 2844 | substr( $_[0]->{str}, $_[0]->{size} * $_[1], $_[0]->{size} ) ); | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 2268 | ▶ | sub FETCHSIZE { | ||||||||||
|
Subroutine
FETCHSIZE: called CC=2 SCAR=7.0
| |||||||||||||
| 40 | 2268 | ▶ | $DEBUG and warn sprintf( "%s->FETCHSIZE", $_[0] ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 41 | 2 | ▶ | use integer; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 42 | 2268 | bytes::length( $_[0]->{str} ) / $_[0]->{size}; | |||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | 144 | ▶ | sub STORE { | ||||||||||
|
Subroutine
STORE: called CC=3 SCAR=11.4
| |||||||||||||
| 46 | 144 | my ( $this, $index, $value ) = @_; | |||||||||||
| 47 | 144 | ▶ | $DEBUG and warn "$this->STORE($index, $value)"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 48 | 144 | ▶ | my $retval = | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | $this->FETCHSIZE - $index < 1 | ||||||||||||
| 50 | ? $this->STORESIZE( $index + 1 ) | ||||||||||||
| 51 | : $value; | ||||||||||||
| 52 | substr( | ||||||||||||
| 53 | $this->{str}, $this->{size} * $index, | ||||||||||||
| 54 | 144 | $this->{size}, pack( $this->{fmt}, $value ) | |||||||||||
| 55 | ); | ||||||||||||
| 56 | } | ||||||||||||
| 57 | |||||||||||||
| 58 | 36 | ▶ | sub STORESIZE { | ||||||||||
|
Subroutine
STORESIZE: called CC=4 SCAR=14.0
| |||||||||||||
| 59 | 36 | my ( $this, $count ) = @_; | |||||||||||
| 60 | 36 | ▶ | $DEBUG and warn "$this->STORESIZE($count)"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 36 | ▶ | return $this->EXTEND($count) if $this->FETCHSIZE < $count; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 62 | 36 | ▶ | if ( $this->FETCHSIZE > $count ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 63 | 18 | substr( $this->{str}, $this->{size} * $count, $this->{size}, '' ); | |||||||||||
| 64 | } | ||||||||||||
| 65 | 36 | return $count; | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | 0 | ▶ | sub EXISTS { | ||||||||||
|
Subroutine
EXISTS: not called CC=2 SCAR=17.9
| |||||||||||||
| 69 | 0 | my ( $this, $key ) = @_; | |||||||||||
| 70 | 0 | ▶ | $DEBUG and warn "$this->EXISTS($key)"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 0 | return $this->FETCHSIZE > $key; | |||||||||||
| 72 | } | ||||||||||||
| 73 | |||||||||||||
| 74 | 18 | ▶ | sub EXTEND { | ||||||||||
|
Subroutine
EXTEND: called CC=3 SCAR=11.2
| |||||||||||||
| 75 | 18 | my ( $this, $count ) = @_; | |||||||||||
| 76 | 18 | ▶ | $DEBUG and warn "$this->EXTEND($count)"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | 18 | my $extend = $count - $this->FETCHSIZE; | |||||||||||
| 78 | 18 | ▶ | if ( $extend > 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 18 | $this->{str} .= pack( $this->{fmt}, $this->{empty} ) x $extend; | |||||||||||
| 80 | 18 | $this->STORESIZE($count); | |||||||||||
| 81 | } | ||||||||||||
| 82 | 18 | return undef; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 18 | ▶ | sub DELETE { | ||||||||||
|
Subroutine
DELETE: called CC=2 SCAR=7.1
| |||||||||||||
| 86 | 18 | my ( $this, $index ) = @_; | |||||||||||
| 87 | 18 | ▶ | $DEBUG and warn "$this->DELETE($index)"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | 18 | substr( $this->{str}, $this->{size} * $index, $this->{size}, '' ); | |||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 18 | ▶ | sub CLEAR { | ||||||||||
|
Subroutine
CLEAR: called CC=2 SCAR=7.1
| |||||||||||||
| 92 | 18 | my ( $this, $index ) = @_; | |||||||||||
| 93 | 18 | ▶ | $DEBUG and warn "$this->CLEAR"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 18 | $this->{str} = ''; | |||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | 18 | ▶ | sub PUSH { # append | ||||||||||
|
Subroutine
PUSH: called CC=2 SCAR=8.4
| |||||||||||||
| 98 | 18 | my $this = shift; | |||||||||||
| 99 | 18 | ▶ | if ($DEBUG) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | 0 | local ($") = ","; | |||||||||||
| 101 | 0 | warn "$this->PUSH(@_)"; | |||||||||||
| 102 | } | ||||||||||||
| 103 | 18 | $this->{str} .= pack( $this->{fmt} x @_, @_ ); | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 18 | ▶ | sub UNSHIFT { # prepend | ||||||||||
|
Subroutine
UNSHIFT: called CC=2 SCAR=8.4
| |||||||||||||
| 107 | 18 | my $this = shift; | |||||||||||
| 108 | 18 | ▶ | if ($DEBUG) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 109 | 0 | local ($") = ","; | |||||||||||
| 110 | 0 | warn "$this->UNSHIFT(@_)"; | |||||||||||
| 111 | } | ||||||||||||
| 112 | 18 | $this->{str} = pack( $this->{fmt} x @_, @_ ) . $this->{str}; | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | 18 | ▶ | sub POP { | ||||||||||
|
Subroutine
POP: called CC=3 SCAR=11.1
| |||||||||||||
| 116 | 18 | my $this = shift; | |||||||||||
| 117 | 18 | ▶ | $DEBUG and warn "$this->POP"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 118 | 18 | my $val; | |||||||||||
| 119 | 18 | my $newsize = $this->FETCHSIZE - 1; | |||||||||||
| 120 | 18 | ▶ | if ( $newsize >= 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 18 | $val = $this->FETCH($newsize); | |||||||||||
| 122 | 18 | $this->STORESIZE($newsize); | |||||||||||
| 123 | } | ||||||||||||
| 124 | 18 | $val; | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 18 | ▶ | sub SHIFT { | ||||||||||
|
Subroutine
SHIFT: called CC=3 SCAR=11.1
| |||||||||||||
| 128 | 18 | my $this = shift; | |||||||||||
| 129 | 18 | ▶ | $DEBUG and warn "$this->SHIFT"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 130 | 18 | my $val; | |||||||||||
| 131 | 18 | my $newsize = $this->FETCHSIZE - 1; | |||||||||||
| 132 | 18 | ▶ | if ( $newsize >= 0 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | 18 | $val = $this->FETCH(0); | |||||||||||
| 134 | 18 | $this->DELETE(0); | |||||||||||
| 135 | } | ||||||||||||
| 136 | 18 | $val; | |||||||||||
| 137 | } | ||||||||||||
| 138 | |||||||||||||
| 139 | 72 | ▶ | sub SPLICE { | ||||||||||
|
Subroutine
SPLICE: called CC=7 SCAR=21.3
| |||||||||||||
| 140 | 72 | my $this = shift; | |||||||||||
| 141 | 72 | ▶ | if ($DEBUG) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | 0 | local ($") = ","; | |||||||||||
| 143 | 0 | warn "$this->SPLICE(@_)"; | |||||||||||
| 144 | } | ||||||||||||
| 145 | 72 | ▶ | my $off = (@_) ? shift: 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 146 | 72 | ▶ | $off += $this->FETCHSIZE if ( $off < 0 ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 147 | 72 | ▶ | my $len = (@_) ? shift: $this->FETCHSIZE - $off; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 72 | ▶ | $len += $this->FETCHSIZE - $off if $len < 0; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 149 | my @result = unpack( | ||||||||||||
| 150 | $this->{fmt} . $len, | ||||||||||||
| 151 | substr( $this->{str}, $off * $this->{size}, $len * $this->{size} ) | ||||||||||||
| 152 | 72 | ); | |||||||||||
| 153 | substr( | ||||||||||||
| 154 | $this->{str}, | ||||||||||||
| 155 | $off * $this->{size}, | ||||||||||||
| 156 | $len * $this->{size}, | ||||||||||||
| 157 | 72 | pack( $this->{fmt} x @_, @_ ) | |||||||||||
| 158 | ); | ||||||||||||
| 159 | 72 | ▶ | return wantarray ? @result : pop @result; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | 2 | 1; | |||||||||||
| 163 | __END__ | ||||||||||||