File Coverage

blib/lib/Plucene/Bitvector.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 3 3 100.0
total 33 33 100.0


line stmt bran cond sub pod time code
1             package Plucene::Bitvector;
2              
3             =head1 NAME
4              
5             Plucene::Bitvector - a vector of bits
6              
7             =head1 SYNOPSIS
8              
9             # isa Bit::Vector::Minimal;
10              
11             my $bitvector = Plucene::Bitvector->read($stream);
12              
13             $bitvector->write($stream);
14            
15             my $count = $bitvector->count;
16              
17             =head1 DESCRIPTION
18              
19             A serialisable implementation of a vector of bits.
20              
21             This subclass of Bit::Vector::Minimal allows the writing (and reading) of
22             vectors to (and from) a Plucene stream.
23              
24             =head1 METHODS
25              
26             =cut
27              
28 19     19   963 use strict;
  19         40  
  19         718  
29 19     19   109 use warnings;
  19         39  
  19         694  
30              
31 19     19   100 use base 'Bit::Vector::Minimal';
  19         37  
  19         15714  
32              
33 19     19   18257 use List::Util qw(sum);
  19         53  
  19         11711  
34              
35             my @magic = (
36             0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4,
37             2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
38             2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4,
39             2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
40             2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
41             4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
42             2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5,
43             3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
44             2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,
45             4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
46             4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
47             );
48              
49             =head2 count
50              
51             my $count = $bitvector->count;
52              
53             Compute the number of one-bits.
54              
55             =cut
56              
57             sub count {
58 3395     3395 1 2507028 sum map $magic[ ord $_ ], split //, shift->{pattern};
59             }
60              
61             =head2 write
62              
63             $bitvector->write($stream);
64              
65             Write this vector to the passed in stream.
66              
67             =cut
68              
69             sub write {
70 1130     1130 1 2254 my ($self, $stream) = @_;
71 1130         4669 $stream->write_int($self->{size});
72 1130         3447 $stream->write_int($self->count); # Backwards compat.
73 1130         19196 $stream->print($self->{pattern});
74             }
75              
76             =head2 read
77              
78             my $bitvector = Plucene::Bitvector->read($stream);
79              
80             Read from the passed in stream.
81              
82             =cut
83              
84             sub read {
85 1131     1131 1 2211 my ($class, $stream) = @_;
86 1131         3813 my $size = $stream->read_int;
87 1131         4812 my $self = $class->new(size => $size, width => 1);
88 1131         37613 $stream->read_int;
89 1131         7011 $stream->read($self->{pattern}, 1 + $self->{size} / 8);
90 1131         3662 return $self;
91             }
92              
93             1;