File Coverage

blib/lib/Bit/Vector/Minimal.pm
Criterion Covered Total %
statement 30 30 100.0
branch 10 14 71.4
condition 1 3 33.3
subroutine 8 8 100.0
pod 4 4 100.0
total 53 59 89.8


line stmt bran cond sub pod time code
1             package Bit::Vector::Minimal;
2              
3 1     1   873 use 5.006;
  1         4  
  1         34  
4              
5 1     1   7 use strict;
  1         2  
  1         33  
6 1     1   14 use warnings;
  1         1  
  1         40  
7              
8 1     1   5 use Carp;
  1         2  
  1         526  
9              
10             our $VERSION = '1.3';
11              
12             =head1 NAME
13              
14             Bit::Vector::Minimal - Object-oriented wrapper around vec()
15              
16             =head1 SYNOPSIS
17              
18             use Bit::Vector::Minimal;
19             my $vec = Bit::Vector->new(size => 8, width => 1, endianness => "little");
20             # These are the defaults
21              
22             $vec->set(1); # $vec's internal vector now looks like "00000010"
23             $vec->get(3); # 0
24              
25             =head1 DESCRIPTION
26              
27             This is a much simplified, lightweight version of L, and
28             wraps Perl's (sometimes confusing) C function in an object-oriented
29             abstraction.
30              
31             =head1 METHODS
32              
33             =head2 new
34              
35             Creates a new bit vector. By default, this creates a one-byte vector
36             with 8 one-bit "slots", with bit zero on the right of the bit pattern.
37             These settings can be changed by passing parameters to the constructor:
38             C will alter the size in bits of the vector; C will alter
39             the width of the slots. The module will die if C is not an
40             integer divisor of C. C controls whether the zeroth
41             place is on the right or the left of the bit vector.
42              
43             =cut
44              
45             sub new {
46 5     5 1 2385 my $class = shift;
47 5         24 my $self = bless {
48             width => 1,
49             size => 8,
50             endianness => "little",
51             @_
52             }, $class;
53 5 50       32 croak "Don't know what endianness $self->{endianness} is meant to be"
54             unless $self->{endianness} =~ /^(little|big)$/i;
55              
56 5 50 33     29 croak "Width ought to be a power of two"
57             if !$self->{width}
58             or (($self->{width} - 1) & $self->{width});
59              
60 5         10 my $slots = $self->{size} / $self->{width};
61 5 50       11 croak "Cowardly refusing to store $slots items in a vector"
62             unless $slots == int($slots);
63 5 100       17 my $num_bytes =
64             $self->{size} % 8
65             ? (($self->{size} + (8 - $self->{size} % 8)) / 8)
66             : ($self->{size} / 8);
67 5         12 $self->{pattern} = "\0" x $num_bytes;
68 5         10 return $self;
69             }
70              
71             =head2 set(POS[, VALUE])
72              
73             Sets the bit or slot at position C to value C or "all bits
74             on" if C is not given.
75              
76             =cut
77              
78             sub set {
79 2     2 1 765 my ($self, $pos, $value) = @_;
80 2 100       9 $value = 2**$self->{width} - 1 unless defined $value;
81 2 100       7 $pos = 1 + $self->{width} - $pos if $self->{endianness} eq "big";
82 2         9 vec($self->{pattern}, $pos, $self->{width}) = $value;
83             }
84              
85             =head2 get(POS)
86              
87             Returns the bit or slot at position C.
88              
89             =cut
90              
91             sub get {
92 2     2 1 5 my ($self, $pos) = @_;
93 2 50       7 $pos = 1 + $self->{width} - $pos if $self->{endianness} eq "big";
94 2         12 return vec($self->{pattern}, $pos, $self->{width});
95             }
96              
97             =head2 display
98              
99             Display the vector. For debugging purposes.
100              
101             =cut
102              
103             sub display {
104 5     5 1 15 my $self = shift;
105 5         48 return join "", map sprintf("%08b", ord $_), split //, $self->{pattern};
106             }
107              
108             =head1 AUTHOR
109              
110             Current maintainer: Tony Bowden
111              
112             Original author: Simon Cozens
113              
114             =head1 BUGS and QUERIES
115              
116             Please direct all correspondence regarding this module to:
117             bug-Bit-Vector-Minimal@rt.cpan.org
118              
119             =head1 SEE ALSO
120              
121             L
122              
123             =head1 COPYRIGHT AND LICENSE
124              
125             Copyright 2003, 2004 by Kasei
126              
127             This library is free software; you can redistribute it and/or modify
128             it under the same terms as Perl itself.
129              
130             =cut
131