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-2015 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
|
|
7730
|
use OBO::Util::Set; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
266
|
|
13
|
|
|
|
|
|
|
|
14
|
11
|
|
|
11
|
|
54
|
use Carp; |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
595
|
|
15
|
11
|
|
|
11
|
|
52
|
use strict; |
|
11
|
|
|
|
|
21
|
|
|
11
|
|
|
|
|
208
|
|
16
|
11
|
|
|
11
|
|
62
|
use warnings; |
|
11
|
|
|
|
|
19
|
|
|
11
|
|
|
|
|
9009
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub new { |
20
|
234
|
|
|
234
|
0
|
368
|
my $class = shift; |
21
|
234
|
|
|
|
|
374
|
my $self = {}; |
22
|
234
|
|
|
|
|
310
|
%{$self->{MAP}} = (); # key; value |
|
234
|
|
|
|
|
663
|
|
23
|
|
|
|
|
|
|
|
24
|
234
|
|
|
|
|
398
|
bless ($self, $class); |
25
|
234
|
|
|
|
|
620
|
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
|
|
|
|
|
7
|
|
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
|
401
|
my ($self, $key) = @_; |
53
|
155
|
100
|
|
|
|
639
|
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
|
|
|
|
|
15
|
my $found = 0; |
68
|
11
|
|
|
|
|
13
|
foreach my $key ( sort keys %{$self->{MAP}} ) { |
|
11
|
|
|
|
|
40
|
|
69
|
21
|
100
|
|
|
|
64
|
if ($self->{MAP}->{$key} eq $value) { |
70
|
5
|
|
|
|
|
6
|
$found = 1; |
71
|
5
|
|
|
|
|
10
|
last; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
} |
74
|
11
|
|
|
|
|
48
|
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
|
19
|
my $self = shift; |
88
|
11
|
|
|
|
|
14
|
my $result = 0; # I initially guess they're NOT identical |
89
|
11
|
50
|
|
|
|
25
|
if (@_) { |
90
|
11
|
|
|
|
|
14
|
my $other_map = shift; |
91
|
11
|
100
|
|
|
|
19
|
if ($self->size() == $other_map->size()) { |
92
|
5
|
|
|
|
|
8
|
my %cmp = map { $_ => 1 } sort keys %{$self->{MAP}}; |
|
22
|
|
|
|
|
41
|
|
|
5
|
|
|
|
|
16
|
|
93
|
5
|
|
|
|
|
16
|
for my $key ($other_map->key_set()->get_set()) { |
94
|
22
|
50
|
|
|
|
40
|
last unless exists $cmp{$key}; |
95
|
22
|
50
|
|
|
|
44
|
last unless $self->{MAP}->{$key} eq $other_map->get($key); |
96
|
22
|
|
|
|
|
43
|
delete $cmp{$key}; |
97
|
|
|
|
|
|
|
} |
98
|
5
|
50
|
|
|
|
24
|
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
|
|
|
|
|
9
|
$result = 1; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} else { |
106
|
6
|
|
|
|
|
9
|
$result = 0; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
} |
109
|
11
|
|
|
|
|
38
|
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
|
149
|
my ($self, $key) = @_; |
123
|
102
|
50
|
|
|
|
188
|
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
|
129
|
my $self = shift; |
137
|
106
|
100
|
|
|
|
117
|
return (scalar keys %{$self->{MAP}} == 0)?1:0; |
|
106
|
|
|
|
|
568
|
|
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
|
83
|
my $self = shift; |
151
|
56
|
|
|
|
|
175
|
my $set = OBO::Util::Set->new(); |
152
|
56
|
|
|
|
|
83
|
$set->add_all(sort keys %{$self->{MAP}}); |
|
56
|
|
|
|
|
376
|
|
153
|
56
|
|
|
|
|
217
|
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
|
239
|
my ( $self, $key, $value ) = @_; |
168
|
59
|
|
|
|
|
78
|
my $old_value = undef; |
169
|
59
|
50
|
33
|
|
|
265
|
if ( $key && $value ) { |
170
|
59
|
|
|
|
|
147
|
my $has_key = $self->contains_key($key); |
171
|
59
|
100
|
|
|
|
140
|
$old_value = $self->{MAP}->{$key} if ($has_key); |
172
|
59
|
|
|
|
|
162
|
$self->{MAP}->{$key} = $value; |
173
|
|
|
|
|
|
|
} else { |
174
|
0
|
|
|
|
|
0
|
croak "You should provide both a key and value -> ('$key', '$value')\n"; |
175
|
|
|
|
|
|
|
} |
176
|
59
|
|
|
|
|
138
|
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
|
244
|
my ( $self, $my_other_map ) = @_; |
191
|
157
|
100
|
|
|
|
528
|
if ( $my_other_map ) { |
192
|
32
|
|
|
|
|
122
|
foreach my $key ($my_other_map->key_set()->get_set()) { |
193
|
32
|
|
|
|
|
81
|
$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
|
53
|
my ($self, $key) = @_; |
209
|
13
|
|
|
|
|
24
|
my $has_key = $self->contains_key($key); |
210
|
13
|
|
|
|
|
19
|
my $old_value = undef; |
211
|
13
|
100
|
|
|
|
35
|
$old_value = $self->{MAP}->{$key} if ($has_key); |
212
|
13
|
|
|
|
|
19
|
delete $self->{MAP}->{$key}; |
213
|
13
|
|
|
|
|
31
|
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
|
102
|
my $self = shift; |
227
|
81
|
|
|
|
|
89
|
my $s = 0; |
228
|
81
|
|
|
|
|
88
|
$s += scalar keys %{$self->{MAP}}; |
|
81
|
|
|
|
|
140
|
|
229
|
81
|
|
|
|
|
221
|
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
|
296
|
my $self = shift; |
243
|
204
|
|
|
|
|
258
|
my @collection = sort values %{$self->{MAP}}; |
|
204
|
|
|
|
|
627
|
|
244
|
204
|
|
|
|
|
669
|
return @collection; |
245
|
|
|
|
|
|
|
} |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
1; |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
__END__ |