Bio-RNA-BarMap

 view release on metacpan or  search on metacpan

lib/Bio/RNA/BarMap/Mapping/Set.pm  view on Meta::CPAN

# A simple set class implemented using a hash. Supports storing references.
# Faster then Set::Scalar for this specific use case. It significantly reduces
# the runtime.
package Bio::RNA::BarMap::Mapping::Set;
our $VERSION = '0.04';

use v5.12;
use warnings;

use autodie qw(:all);
use Moose;
use MooseX::StrictConstructor;
use namespace::autoclean;
use List::Util qw(pairmap);

# Elements are stored in a hash ref. For simple values, key is the element
# and value is undef. For references, the key gets stringified and the
# value stores the actual reference.
has _elems => (is => "ro", init_arg => undef, default => sub { {} });

# Return all elements. If defined, use the value, else the key.
sub elements { pairmap {$b // $a} %{ $_[0]->_elems } }

# Insert elements into the set. Returns itself.
sub insert {
    my $self = shift;
    # Don't store simple values twice, but preserve references.
    $self->_elems->{$_} = ref $_ ? $_ : undef foreach @_;
    $self;
}

__PACKAGE__->meta->make_immutable;

1; # End of Bio::RNA::BarMap::Mapping::Set


__END__

=pod

=encoding UTF-8

=head1 NAME

Bio::RNA::BarMap::Mapping::Set - Internally used class to store sets

=head1 SYNOPSIS

    use v5.12;              # for 'say()' and '//' a.k.a. logical defined-or
    use Bio::RNA::BarMap;

    # Construct new, empty set.
    my $set = Bio::RNA::BarMap::Mapping::Set->new();

    # Insert elements.
    $set->insert( qw(hello there foo) );

    # Retrieve elements.
    say "Elements in set: ", join q{, }, $set->elements;

=head1 DESCRIPTION

A simple, pure-Perl implementation of a set data structure. It is
significantly faster than L<Set::Scalar>, which became a bottleneck during the
implementation of this module. It supports storing references, but no deep
equality checks are performed.

=head1 METHODS

=head2 Bio::RNA::BarMap::Mapping::Set->new()

Returns a new, empty set. Adding elements during construction is currently not
supported (for no specific reason), use the C<insert()> method instead.



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