| 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 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 29 | 19 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 30 | |||
| 31 | 19 | ▶ | use base 'Bit::Vector::Minimal'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | |||
| 33 | 19 | ▶ | use List::Util qw(sum); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | |||
| 35 | 19 | 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 | 3395 | ▶ | sub count { |
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||
| 58 | 3395 | 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 | 1130 | ▶ | sub write { |
|
Subroutine
write: called CC=1 SCAR=0.0
Pod
documented
| |||
| 70 | 1130 | my ($self, $stream) = @_; | |
| 71 | 1130 | $stream->write_int($self->{size}); | |
| 72 | 1130 | $stream->write_int($self->count); # Backwards compat. | |
| 73 | 1130 | $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 | 1131 | ▶ | sub read { |
|
Subroutine
read: called CC=1 SCAR=0.0
Pod
documented
| |||
| 85 | 1131 | my ($class, $stream) = @_; | |
| 86 | 1131 | my $size = $stream->read_int; | |
| 87 | 1131 | my $self = $class->new(size => $size, width => 1); | |
| 88 | 1131 | $stream->read_int; | |
| 89 | 1131 | $stream->read($self->{pattern}, 1 + $self->{size} / 8); | |
| 90 | 1131 | return $self; | |
| 91 | } | ||
| 92 | |||
| 93 | 19 | 1; | |