| 1 | package Data::Multihash; | ||||||||||
| 2 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Set::Object qw(set); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | our $VERSION = '0.03'; | |||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | Data::Multihash - A hash table that supports multiple values per key. | ||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | use Data::Multihash; | ||||||||||
| 16 | |||||||||||
| 17 | my $multihash = Data::Multihash->new(); | ||||||||||
| 18 | $multihash->insert(key => 'value', key => 'other_value'); | ||||||||||
| 19 | $multihash->insert(other_key => 'value'); | ||||||||||
| 20 | |||||||||||
| 21 | print 'There are ' . $multihash->size . ' elements in the multihash.'; | ||||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | This module implements a multihash, which maps keys to sets of values. | ||||||||||
| 26 | Multihashes are unordered. | ||||||||||
| 27 | |||||||||||
| 28 | =head1 CONSTRUCTORS | ||||||||||
| 29 | |||||||||||
| 30 | =head2 new() | ||||||||||
| 31 | |||||||||||
| 32 | Create a new empty multihash. | ||||||||||
| 33 | |||||||||||
| 34 | =cut | ||||||||||
| 35 | |||||||||||
| 36 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 37 | 0 | bless {}; | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | =head1 INSTANCE METHODS | ||||||||||
| 41 | |||||||||||
| 42 | =head2 insert(%pairs) | ||||||||||
| 43 | |||||||||||
| 44 | Insert key-value pairs. | ||||||||||
| 45 | |||||||||||
| 46 | =cut | ||||||||||
| 47 | |||||||||||
| 48 | 0 | ▶ | sub insert { | ||||||||
|
Subroutine
insert: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 49 | 0 | my ($self, %pairs) = @_; | |||||||||
| 50 | 0 | ▶ | while (my ($k, $v) = each %pairs) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | 0 | ▶ | $self->{$k} = set() unless $self->{$k}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | $self->{$k}->insert($v); | |||||||||
| 53 | } | ||||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | =head2 remove_key(@keys), remove_keys(@keys) | ||||||||||
| 57 | |||||||||||
| 58 | Remove all given keys and their values. | ||||||||||
| 59 | |||||||||||
| 60 | =cut | ||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub remove_key { | ||||||||
|
Subroutine
remove_key: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 63 | 0 | my $self = shift; | |||||||||
| 64 | 0 | delete $self->{$_} for @_; | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 2 | *remove_keys = \&remove_key; | |||||||||
| 68 | |||||||||||
| 69 | =head2 remove_value(@values), remove_values(@values) | ||||||||||
| 70 | |||||||||||
| 71 | Remove all given values from the multihash. | ||||||||||
| 72 | |||||||||||
| 73 | =cut | ||||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | sub remove_value { | ||||||||
|
Subroutine
remove_value: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 76 | 0 | my $self = shift; | |||||||||
| 77 | 0 | for (keys %$self) { | |||||||||
| 78 | 0 | ▶ | next unless exists $self->{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | my $set = $self->{$_}; | |||||||||
| 80 | 0 | $set->remove(@_); | |||||||||
| 81 | 0 | ▶ | delete $self->{$_} unless $set->size; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | } | ||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 2 | *remove_values = \&remove_value; | |||||||||
| 86 | |||||||||||
| 87 | =head2 remove_pair(%pairs), remove_pairs(%pairs) | ||||||||||
| 88 | |||||||||||
| 89 | Remove all given (key, value) pairs from the multiset. | ||||||||||
| 90 | |||||||||||
| 91 | =cut | ||||||||||
| 92 | |||||||||||
| 93 | 0 | ▶ | sub remove_pair { | ||||||||
|
Subroutine
remove_pair: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 94 | 0 | my ($self, %pairs) = @_; | |||||||||
| 95 | 0 | ▶ | while (my ($k, $v) = each %pairs) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | ▶ | next unless exists $self->{$k}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 0 | my $set = $self->{$k}; | |||||||||
| 98 | 0 | $set->remove($v); | |||||||||
| 99 | 0 | ▶ | delete $self->{$k} unless $set->size; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | } | ||||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 2 | *remove_pairs = \&remove_pair; | |||||||||
| 104 | |||||||||||
| 105 | =head2 elements($key) | ||||||||||
| 106 | |||||||||||
| 107 | Return all elements associated with a key. | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | |||||||||||
| 111 | 0 | ▶ | sub elements { | ||||||||
|
Subroutine
elements: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 112 | 0 | my ($self, $key) = @_; | |||||||||
| 113 | 0 | ▶ | $self->{$key} || set(); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | =head2 values | ||||||||||
| 117 | |||||||||||
| 118 | Return all values in the multihash as a list. | ||||||||||
| 119 | |||||||||||
| 120 | =cut | ||||||||||
| 121 | |||||||||||
| 122 | 0 | ▶ | sub values { | ||||||||
|
Subroutine
values: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 123 | 0 | my $self = shift; | |||||||||
| 124 | 0 | my @sets = CORE::values %$self; | |||||||||
| 125 | 0 | map $_->elements, @sets; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | =head2 size | ||||||||||
| 129 | |||||||||||
| 130 | Return the number of values in the multihash. | ||||||||||
| 131 | |||||||||||
| 132 | =cut | ||||||||||
| 133 | |||||||||||
| 134 | 0 | ▶ | sub size { | ||||||||
|
Subroutine
size: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 135 | 0 | my $self = shift; | |||||||||
| 136 | 0 | my $result = 0; | |||||||||
| 137 | 0 | $result += $_->size for (CORE::values %$self); | |||||||||
| 138 | 0 | $result; | |||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | =head1 THREAD SAFETY | ||||||||||
| 142 | |||||||||||
| 143 | It is not safe to access a single multihash from multiple threads. | ||||||||||
| 144 | |||||||||||
| 145 | =head1 LICENSE | ||||||||||
| 146 | |||||||||||
| 147 | Copyright (c) 2014, Radek Slupik | ||||||||||
| 148 | All rights reserved. | ||||||||||
| 149 | |||||||||||
| 150 | Redistribution and use in source and binary forms, with or without modification, | ||||||||||
| 151 | are permitted provided that the following conditions are met: | ||||||||||
| 152 | |||||||||||
| 153 | =over | ||||||||||
| 154 | |||||||||||
| 155 | =item * | ||||||||||
| 156 | |||||||||||
| 157 | Redistributions of source code must retain the above copyright notice, this list | ||||||||||
| 158 | of conditions and the following disclaimer. | ||||||||||
| 159 | |||||||||||
| 160 | =item * | ||||||||||
| 161 | |||||||||||
| 162 | Redistributions in binary form must reproduce the above copyright notice, this | ||||||||||
| 163 | list of conditions and the following disclaimer in the documentation and/or | ||||||||||
| 164 | other materials provided with the distribution. | ||||||||||
| 165 | |||||||||||
| 166 | =item * | ||||||||||
| 167 | |||||||||||
| 168 | Neither the name of nor the names of its contributors may be used to endorse | ||||||||||
| 169 | or promote products derived from this software without specific prior written | ||||||||||
| 170 | permission. | ||||||||||
| 171 | |||||||||||
| 172 | =back | ||||||||||
| 173 | |||||||||||
| 174 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||||||||||
| 175 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||||||||||
| 176 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||||||||||
| 177 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | ||||||||||
| 178 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||||||||||
| 179 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||||||||||
| 180 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON | ||||||||||
| 181 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||||||||||
| 182 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||||||||||
| 183 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||
| 184 | |||||||||||
| 185 | =cut | ||||||||||
| 186 | |||||||||||
| 187 | 2 | 1; | |||||||||