Archive Ensembl HomeArchive Ensembl Home
BitString.pm
Go to the documentation of this file.
00001 =head1 LICENSE
00002 
00003   Copyright (c) 1999-2012 The European Bioinformatics Institute and
00004   Genome Research Limited.  All rights reserved.
00005 
00006   This software is distributed under a modified Apache license.
00007   For license details, please see
00008 
00009     http://www.ensembl.org/info/about/code_licence.html
00010 
00011 =head1 CONTACT
00012 
00013   Please email comments or questions to the public Ensembl
00014   developers list at <dev@ensembl.org>.
00015 
00016   Questions may also be sent to the Ensembl help desk at
00017   <helpdesk@ensembl.org>.
00018 
00019 =cut
00020 
00021 =head1 NAME
00022 
00023 Bio::EnsEMBL::Utils::BitString - bitstring object implementation
00024 
00025 =head1 DESCRIPTION
00026 
00027 This is an implementation of a bitstring object, taken from Damian
00028 Convey's book "Object Oriented Perl".
00029 
00030 =head1 METHODS
00031 
00032 =cut
00033 
00034 
00035 package Bio::EnsEMBL::Utils::BitString;
00036 
00037 use strict;
00038 use warnings;
00039 no warnings 'uninitialized';
00040 
00041 
00042 sub new {
00043   my $class = ref($_[0])||$_[0];
00044   my $initbits = join '', map {$_?1:0} @_[1..$#_];
00045   my $bs = pack 'b*', $initbits;
00046   bless \$bs, $class;
00047 }
00048 
00049 
00050 sub get {
00051   my ($self, $bitnum) = @_;
00052   return vec($$self,$bitnum,1);
00053 }
00054 
00055 
00056 sub set {
00057   my ($self, $bitnum, $newval) = @_;
00058   vec($$self,$bitnum,1) = $newval?1:0;
00059 }
00060 
00061 
00062 sub bitcount {
00063   8 * length ${$_[0]};
00064 }
00065 
00066 
00067 sub complement {
00068   my ($self) = @_;
00069   my $complement = ~$$self;
00070   bless \$complement, ref($self);
00071 }
00072 
00073 
00074 sub print_me {
00075   my ($self) = @_;
00076   for (my $i=0; $i < $self->bitcount(); $i++)
00077   {
00078     print $self->get($i);
00079     print ' ' unless ($i+1)%8;
00080     print "\n" unless ($i+1)%64;
00081   }
00082   print "\n";
00083 }
00084 
00085 
00086 1;
00087