File Coverage

blib/lib/Data/BitStream/MinimalVec.pm
Criterion Covered Total %
statement 46 46 100.0
branch 17 24 70.8
condition 8 18 44.4
subroutine 8 8 100.0
pod 2 2 100.0
total 81 98 82.6


line stmt bran cond sub pod time code
1             package Data::BitStream::MinimalVec;
2 21     21   107775 use strict;
  21         59  
  21         877  
3 21     21   141 use warnings;
  21         53  
  21         1094  
4             BEGIN {
5 21     21   59 $Data::BitStream::MinimalVec::AUTHORITY = 'cpan:DANAJ';
6 21         398 $Data::BitStream::MinimalVec::VERSION = '0.08';
7             }
8              
9 21     21   126 use Moo;
  21         114  
  21         186  
10              
11             with 'Data::BitStream::Base',
12             'Data::BitStream::Code::Gamma',
13             'Data::BitStream::Code::Delta',
14             'Data::BitStream::Code::Omega',
15             'Data::BitStream::Code::Levenstein',
16             'Data::BitStream::Code::EvenRodeh',
17             'Data::BitStream::Code::Fibonacci',
18             'Data::BitStream::Code::Golomb',
19             'Data::BitStream::Code::Rice',
20             'Data::BitStream::Code::GammaGolomb',
21             'Data::BitStream::Code::ExponentialGolomb',
22             'Data::BitStream::Code::Baer',
23             'Data::BitStream::Code::BoldiVigna',
24             'Data::BitStream::Code::ARice',
25             'Data::BitStream::Code::Additive',
26             'Data::BitStream::Code::Comma',
27             'Data::BitStream::Code::Taboo',
28             'Data::BitStream::Code::BER',
29             'Data::BitStream::Code::Varint',
30             'Data::BitStream::Code::StartStop';
31              
32             has '_vec' => (is => 'rw', default => sub{''});
33              
34 208576     208576   3648606 sub _vecref { \shift->{_vec} }
35             after 'erase' => sub { shift->_vec(''); 1; };
36              
37             sub read {
38 136419     136419 1 185593 my $self = shift;
39 136419 50       349432 $self->error_stream_mode('read') if $self->writing;
40 136419         179533 my $bits = shift;
41 136419 50 33     843389 $self->error_code('param', 'bits must be in range 1-' . $self->maxbits)
      33        
42             unless defined $bits && $bits > 0 && $bits <= $self->maxbits;
43 136419   66     448107 my $peek = (defined $_[0]) && ($_[0] eq 'readahead');
44              
45 136419         237090 my $pos = $self->pos;
46 136419         202335 my $len = $self->len;
47 136419 100       262426 return if $pos >= $len;
48 136120 50 66     486948 $self->error_off_stream if !$peek && ($pos+$bits) > $len;
49              
50 136120         188768 my $val = 0;
51 136120         270284 my $rvec = $self->_vecref;
52 136120         276505 foreach my $bit (0 .. $bits-1) {
53 3997528         5632514 $val = ($val << 1) | vec($$rvec, $pos+$bit, 1);
54             }
55 136120 100       440278 $self->_setpos( $pos + $bits ) unless $peek;
56 136120         440592 $val;
57             }
58             sub write {
59 72456     72456 1 100195 my $self = shift;
60 72456 50       174933 $self->error_stream_mode('write') unless $self->writing;
61 72456         87545 my $bits = shift;
62 72456 50 33     2671587 $self->error_code('param', 'bits must be > 0') unless defined $bits && $bits > 0;
63 72456         85457 my $val = shift;
64 72456 50 33     282035 $self->error_code('zeroval') unless defined $val and $val >= 0;
65              
66 72456         169242 my $len = $self->len;
67 72456         148143 my $rvec = $self->_vecref;
68              
69 72456 100       190293 if ($val == 0) {
    100          
70             # nothing
71             } elsif ($val == 1) {
72 9909         31826 vec($$rvec, $len + $bits - 1, 1) = 1;
73             } else {
74 57394 50       166592 $self->error_code('param', 'bits must be <= ' . $self->maxbits) if $bits > $self->maxbits;
75              
76 57394         88373 my $wpos = $len + $bits-1;
77 57394         106112 foreach my $bit (0 .. $bits-1) {
78 1140265 100       3339923 vec($$rvec, $wpos - $bit, 1) = 1 if (($val >> $bit) & 1);
79             }
80             }
81              
82 72456         167257 $self->_setlen( $len + $bits);
83 72456         253525 1;
84             }
85              
86             # default everything else
87              
88             __PACKAGE__->meta->make_immutable;
89 21     21   20270 no Moo;
  21         56  
  21         128  
90             1;
91              
92             # ABSTRACT: A minimal implementation of Data::BitStream
93              
94             =pod
95              
96             =head1 NAME
97              
98             Data::BitStream::MinimalVec - A minimal implementation of Data::BitStream
99              
100             =head1 SYNOPSIS
101              
102             use Data::BitStream::MinimalVec;
103             my $stream = Data::BitStream::MinimalVec->new;
104             $stream->put_gamma($_) for (1 .. 20);
105             $stream->rewind_for_read;
106             my @values = $stream->get_gamma(-1);
107              
108             =head1 DESCRIPTION
109              
110             An implementation of L. See the documentation for that
111             module for many more examples, and L for the API.
112             This document only describes the unique features of this implementation,
113             which is of limited value to people purely using L.
114              
115             This implementation uses a Perl C to store the data, and shows basically
116             the minimal work required to get an implementation working. Everything else
117             is provided by the base class. It is slow, and L
118             is recommended for real work.
119              
120             =head2 DATA
121              
122             =over 4
123              
124             =item B< _vec >
125              
126             A private scalar holding the data as a vector.
127              
128             =back
129              
130             =head2 CLASS METHODS
131              
132             =over 4
133              
134             =item B< _vecref >
135              
136             Retrieves a reference to the private vector.
137              
138             =item I B< erase >
139              
140             Sets the private vector to the empty string C<''>.
141              
142             =item B< read >
143              
144             =item B< write >
145              
146             These methods have custom implementations.
147              
148             =back
149              
150             =head2 ROLES
151              
152             The following roles are included.
153              
154             =over 4
155              
156             =item L
157              
158             =item L
159              
160             =item L
161              
162             =item L
163              
164             =item L
165              
166             =item L
167              
168             =item L
169              
170             =item L
171              
172             =item L
173              
174             =item L
175              
176             =item L
177              
178             =item L
179              
180             =item L
181              
182             =item L
183              
184             =item L
185              
186             =item L
187              
188             =item L
189              
190             =item L
191              
192             =back
193              
194             =head1 SEE ALSO
195              
196             =over 4
197              
198             =item L
199              
200             =item L
201              
202             =item L
203              
204             =back
205              
206             =head1 AUTHORS
207              
208             Dana Jacobsen Edana@acm.orgE
209              
210             =head1 COPYRIGHT
211              
212             Copyright 2011-2012 by Dana Jacobsen Edana@acm.orgE
213              
214             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
215              
216             =cut