File Coverage

blib/lib/OBO/Util/Map.pm
Criterion Covered Total %
statement 83 85 97.6
branch 20 26 76.9
condition 1 3 33.3
subroutine 17 17 100.0
pod 12 13 92.3
total 133 144 92.3


line stmt bran cond sub pod time code
1             # $Id: Map.pm 2014-06-06 erick.antezana $
2             #
3             # Module : Map.pm
4             # Purpose : An implementation of a Map. An object that maps keys to values.
5             # License : Copyright (c) 2006-2014 by Erick Antezana. All rights reserved.
6             # This program is free software; you can redistribute it and/or
7             # modify it under the same terms as Perl itself.
8             # Contact : Erick Antezana
9             #
10             package OBO::Util::Map;
11              
12 11     11   6414 use OBO::Util::Set;
  11         16  
  11         253  
13              
14 11     11   41 use Carp;
  11         13  
  11         507  
15 11     11   42 use strict;
  11         14  
  11         372  
16 11     11   43 use warnings;
  11         11  
  11         6968  
17              
18              
19             sub new {
20 234     234 0 291 my $class = shift;
21 234         276 my $self = {};
22 234         240 %{$self->{MAP}} = (); # key; value
  234         512  
23            
24 234         335 bless ($self, $class);
25 234         482 return $self;
26             }
27              
28             =head2 clear
29              
30             Usage - $map->clear()
31             Returns - none
32             Args - none
33             Function - removes all mappings from this map
34            
35             =cut
36              
37             sub clear {
38 2     2 1 5 my $self = shift;
39 2         3 %{ $self->{MAP} } = ();
  2         9  
40             }
41              
42             =head2 contains_key
43              
44             Usage - $map->contains_key($key)
45             Returns - 1 (true) if this map contains a mapping for the specified key
46             Args - a key whose presence in this map is to be tested
47             Function - checks if this map contains a mapping for the specified key
48            
49             =cut
50              
51             sub contains_key {
52 155     155 1 1014 my ($self, $key) = @_;
53 155 100       559 return ( defined $self->{MAP}->{$key} ) ? 1 : 0;
54             }
55              
56             =head2 contains_value
57              
58             Usage - $map->contains_value($value)
59             Returns - 1 (true) if this map maps one or more keys to the specified value
60             Args - a value whose presence in this map is to be tested
61             Function - checks if this map maps one or more keys to the specified value
62            
63             =cut
64              
65             sub contains_value {
66 11     11 1 20 my ($self, $value) = @_;
67 11         14 my $found = 0;
68 11         17 foreach my $key ( sort keys %{$self->{MAP}} ) {
  11         47  
69 21 100       75 if ($self->{MAP}->{$key} eq $value) {
70 5         7 $found = 1;
71 5         9 last;
72             }
73             }
74 11         50 return $found;
75             }
76              
77             =head2 equals
78              
79             Usage - $map->equals($another_map)
80             Returns - either 1 (true) or 0 (false)
81             Args - the map (OBO::Util::Map) to compare with
82             Function - tells whether this map is equal to the given one
83            
84             =cut
85              
86             sub equals {
87 11     11 1 20 my $self = shift;
88 11         11 my $result = 0; # I initially guess they're NOT identical
89 11 50       23 if (@_) {
90 11         8 my $other_map = shift;
91 11 100       17 if ($self->size() == $other_map->size()) {
92 5         6 my %cmp = map { $_ => 1 } sort keys %{$self->{MAP}};
  22         46  
  5         27  
93 5         14 for my $key ($other_map->key_set()->get_set()) {
94 22 50       39 last unless exists $cmp{$key};
95 22 50       36 last unless $self->{MAP}->{$key} eq $other_map->get($key);
96 22         498 delete $cmp{$key};
97             }
98 5 50       23 if (%cmp) {
99             #warn "they don't have the same keys or values\n";
100 0         0 $result = 0;
101             } else {
102             #warn "they have the same keys or values\n";
103 5         8 $result = 1;
104             }
105             } else {
106 6         11 $result = 0;
107             }
108             }
109 11         37 return $result;
110             }
111              
112             =head2 get
113              
114             Usage - $map->get($key)
115             Returns - the value to which this map maps the specified key
116             Args - a key whose associated value is to be returned
117             Function - gets the value to which this map maps the specified key
118            
119             =cut
120              
121             sub get {
122 102     102 1 105 my ($self, $key) = @_;
123 102 50       144 return (!$self->is_empty())?$self->{MAP}->{$key}:undef;
124             }
125              
126             =head2 is_empty
127              
128             Usage - $map->is_empty()
129             Returns - true if this map contains no key-value mappings
130             Args - none
131             Function - checks if this map contains no key-value mappings
132            
133             =cut
134              
135             sub is_empty {
136 106     106 1 91 my $self = shift;
137 106 100       83 return (scalar keys %{$self->{MAP}} == 0)?1:0;
  106         2088  
138             }
139              
140             =head2 key_set
141              
142             Usage - $map->key_set()
143             Returns - a set (OBO::Util::Set) view of the keys contained in this map
144             Args - none
145             Function - gets a set view of the keys contained in this map
146            
147             =cut
148              
149             sub key_set {
150 56     56 1 66 my $self = shift;
151 56         163 my $set = OBO::Util::Set->new();
152 56         68 $set->add_all(sort keys %{$self->{MAP}});
  56         273  
153 56         166 return $set;
154             }
155              
156             =head2 put
157              
158             Usage - $map->put("GO", "Gene Ontology")
159             Returns - previous value associated with specified key, or undef if there was no mapping for key
160             Args - a key (string) with which the specified value is to be associated and a value to be associated with the specified key.
161             Function - associates the specified value with the specified key in this map (optional operation)
162             Remark - if the map previously contained a mapping for this key, the old value is replaced by the specified value
163            
164             =cut
165              
166             sub put {
167 59     59 1 657 my ( $self, $key, $value ) = @_;
168 59         62 my $old_value = undef;
169 59 50 33     231 if ( $key && $value ) {
170 59         119 my $has_key = $self->contains_key($key);
171 59 100       111 $old_value = $self->{MAP}->{$key} if ($has_key);
172 59         127 $self->{MAP}->{$key} = $value;
173             } else {
174 0         0 croak "You should provide both a key and value -> ('$key', '$value')\n";
175             }
176 59         98 return $old_value;
177             }
178              
179             =head2 put_all
180              
181             Usage - $map->put_all($my_other_map)
182             Returns - none
183             Args - a map (OBO::Util::Map) to be stored in this map
184             Function - copies all of the mappings from the specified map to this map (optional operation)
185             Remark - the effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map
186            
187             =cut
188              
189             sub put_all {
190 157     157 1 190 my ( $self, $my_other_map ) = @_;
191 157 100       374 if ( $my_other_map ) {
192 32         109 foreach my $key ($my_other_map->key_set()->get_set()) {
193 32         66 $self->{MAP}->{$key} = $my_other_map->get($key);
194             }
195             }
196             }
197              
198             =head2 remove
199              
200             Usage - $map->remove($key)
201             Returns - the previous value associated with specified key, or undef if there was no mapping for the given key
202             Args - a key whose mapping is to be removed from the map
203             Function - removes the mapping for this key from this map if it is present (optional operation)
204            
205             =cut
206              
207             sub remove {
208 13     13 1 43 my ($self, $key) = @_;
209 13         19 my $has_key = $self->contains_key($key);
210 13         13 my $old_value = undef;
211 13 100       28 $old_value = $self->{MAP}->{$key} if ($has_key);
212 13         20 delete $self->{MAP}->{$key};
213 13         18 return $old_value;
214             }
215              
216             =head2 size
217              
218             Usage - $map->size()
219             Returns - the size of this map
220             Args - none
221             Function - tells the number of elements held by this map
222            
223             =cut
224              
225             sub size {
226 81     81 1 74 my $self = shift;
227 81         64 my $s = 0;
228 81         68 $s += scalar keys %{$self->{MAP}};
  81         113  
229 81         169 return $s;
230             }
231              
232             =head2 values
233              
234             Usage - $map->values()
235             Returns - a collection view of the values contained in this map
236             Args - none
237             Function - gets a collection view of the values contained in this map
238            
239             =cut
240              
241             sub values {
242 204     204 1 244 my $self = shift;
243 204         179 my @collection = sort values %{$self->{MAP}};
  204         609  
244 204         522 return @collection;
245             }
246              
247             1;
248              
249             __END__