File Coverage

blib/lib/Bio/RNA/BarMap/Mapping/Set.pm
Criterion Covered Total %
statement 23 26 88.4
branch 1 2 50.0
condition 0 3 0.0
subroutine 8 10 80.0
pod 2 2 100.0
total 34 43 79.0


line stmt bran cond sub pod time code
1             # A simple set class implemented using a hash. Supports storing references.
2             # Faster then Set::Scalar for this specific use case. It significantly reduces
3             # the runtime.
4             package Bio::RNA::BarMap::Mapping::Set;
5             our $VERSION = '0.02';
6              
7 5     5   155 use v5.12;
  5         18  
8 5     5   30 use warnings;
  5         9  
  5         215  
9              
10 5     5   43 use autodie qw(:all);
  5         11  
  5         40  
11 5     5   29937 use Moose;
  5         11  
  5         67  
12 5     5   36999 use MooseX::StrictConstructor;
  5         17  
  5         49  
13 5     5   26151 use namespace::autoclean;
  5         15  
  5         50  
14 5     5   476 use List::Util qw(pairmap);
  5         12  
  5         1636  
15              
16             # Elements are stored in a hash ref. For simple values, key is the element
17             # and value is undef. For references, the key gets stringified and the
18             # value stores the actual reference.
19             has _elems => (is => "ro", init_arg => undef, default => sub { {} });
20              
21             # Return all elements. If defined, use the value, else the key.
22 0   0 0 1 0 sub elements { pairmap {$b // $a} %{ $_[0]->_elems } }
  0     0   0  
  0         0  
23              
24             # Insert elements into the set. Returns itself.
25             sub insert {
26 6732     6732 1 12437 my $self = shift;
27             # Don't store simple values twice, but preserve references.
28 6732 50       211216 $self->_elems->{$_} = ref $_ ? $_ : undef foreach @_;
29 6732         18237 $self;
30             }
31              
32             __PACKAGE__->meta->make_immutable;
33              
34             1; # End of Bio::RNA::BarMap::Mapping::Set
35              
36              
37             __END__
38              
39             =pod
40              
41             =encoding UTF-8
42              
43             =head1 NAME
44              
45             Bio::RNA::BarMap::Mapping::Set - Internally used class to store sets
46              
47             =head1 SYNOPSIS
48              
49             use v5.12; # for 'say()' and '//' a.k.a. logical defined-or
50             use Bio::RNA::BarMap;
51              
52             # Construct new, empty set.
53             my $set = Bio::RNA::BarMap::Mapping::Set->new();
54              
55             # Insert elements.
56             $set->insert( qw(hello there foo) );
57              
58             # Retrieve elements.
59             say "Elements in set: ", join q{, }, $set->elements;
60              
61             =head1 DESCRIPTION
62              
63             A simple, pure-Perl implementation of a set data structure. It is
64             significantly faster than L<Set::Scalar>, which became a bottleneck during the
65             implementation of this module. It supports storing references, but no deep
66             equality checks are performed.
67              
68             =head1 METHODS
69              
70             =head2 Bio::RNA::BarMap::Mapping::Set->new()
71              
72             Returns a new, empty set. Adding elements during construction is currently not
73             supported (for no specific reason), use the C<insert()> method instead.
74              
75             =head2 $set->elements()
76              
77             Return all items contained in this set.
78              
79             =head2 $set->insert(@elements)
80              
81             Insert one or more C<@elements> into the set. Supports references.
82              
83              
84             =head1 AUTHOR
85              
86             Felix Kuehnl, C<< <felix at bioinf.uni-leipzig.de> >>
87              
88              
89             =head1 BUGS
90              
91             Please report any bugs or feature requests by raising an issue at
92             L<https://github.com/xileF1337/Bio-RNA-BarMap/issues>.
93              
94             You can also do so by mailing to C<bug-bio-rna-barmap at rt.cpan.org>,
95             or through the web interface at
96             L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Bio-RNA-BarMap>. I will be
97             notified, and then you'll automatically be notified of progress on your bug as
98             I make changes.
99              
100              
101             =head1 SUPPORT
102              
103             You can find documentation for this module with the perldoc command.
104              
105             perldoc Bio::RNA::BarMap
106              
107              
108             You can also look for information at the official BarMap website:
109              
110             L<https://www.tbi.univie.ac.at/RNA/bar_map/>
111              
112              
113             =over 4
114              
115             =item * Github: the official repository
116              
117             L<https://github.com/xileF1337/Bio-RNA-BarMap>
118              
119             =item * RT: CPAN's request tracker (report bugs here)
120              
121             L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Bio-RNA-BarMap>
122              
123             =item * AnnoCPAN: Annotated CPAN documentation
124              
125             L<http://annocpan.org/dist/Bio-RNA-BarMap>
126              
127             =item * CPAN Ratings
128              
129             L<https://cpanratings.perl.org/d/Bio-RNA-BarMap>
130              
131             =item * Search CPAN
132              
133             L<https://metacpan.org/release/Bio-RNA-BarMap>
134              
135             =back
136              
137              
138             =head1 LICENSE AND COPYRIGHT
139              
140             Copyright 2019-2021 Felix Kuehnl.
141              
142             This program is free software: you can redistribute it and/or modify
143             it under the terms of the GNU General Public License as published by
144             the Free Software Foundation, either version 3 of the License, or
145             (at your option) any later version.
146              
147             This program is distributed in the hope that it will be useful,
148             but WITHOUT ANY WARRANTY; without even the implied warranty of
149             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
150             GNU General Public License for more details.
151              
152             You should have received a copy of the GNU General Public License
153             along with this program. If not, see L<http://www.gnu.org/licenses/>.
154              
155              
156             =cut
157              
158             # End of Bio/RNA/BarMap/Mapping/Set.pm