Acme-Bitfield

 view release on metacpan or  search on metacpan

lib/Acme/Bitfield.pm  view on Meta::CPAN

use v5.42;
use feature 'class';
no warnings 'experimental::class';
#
class Acme::Bitfield v1.1.0 {
    field $size : reader : param;
    field $data : reader : param = "\0" x int( ( $size + 7 ) / 8 );
    ADJUST {
        $self->_clean;
    }

    method set_data ($val) {
        $data = $val;
        $self->_clean;    # We can't use the :writer because we must call this
    }

    # Internal helper to map BitTorrent bit index to vec index
    # BT: bit 0 is 0x80, bit 7 is 0x01
    # vec: bit 0 is 0x01, bit 7 is 0x80
    sub _map ($index) { ( $index & ~7 ) | ( 7 - ( $index & 7 ) ) }

    method get ($index) {
        return 0 if $index < 0 || $index >= $size;
        vec $data, _map($index), 1;
    }

    method set ($index) {
        return if $index < 0 || $index >= $size;
        vec( $data, _map($index), 1 ) = 1;
    }

    method clear ($index) {
        return if $index < 0 || $index >= $size;
        vec( $data, _map($index), 1 ) = 0;
    }

    method count () {
        return unpack( '%32b*', $data );
    }

    method is_full () {
        return $self->count == $size;
    }

    method is_empty () {
        return $data =~ tr/\0//c ? 0 : 1;
    }

    method union ($other) {
        my $new = __CLASS__->new( size => $size );
        $new->set_data( $data|.$other->data );
        return $new;
    }

    method intersection ($other) {
        my $new = __CLASS__->new( size => $size );
        $new->set_data( $data&.$other->data );
        return $new;
    }

    method difference ($other) {



( run in 1.748 second using v1.01-cache-2.11-cpan-39bf76dae61 )