File Coverage

blib/lib/OBO/Util/ObjectSet.pm
Criterion Covered Total %
statement 45 48 93.7
branch 10 12 83.3
condition 6 6 100.0
subroutine 8 8 100.0
pod 5 5 100.0
total 74 79 93.6


line stmt bran cond sub pod time code
1             # $Id: ObjectSet.pm 2014-09-29 erick.antezana $
2             #
3             # Module : ObjectSet.pm
4             # Purpose : A generic set of ontology objects (terms, relationships, dbxrefs, etc.).
5             # License : Copyright (c) 2007, 2008, 2009, 2010 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              
11             package OBO::Util::ObjectSet;
12              
13             our @ISA = qw(OBO::Util::ObjectIdSet);
14 23     23   8058 use OBO::Util::ObjectIdSet;
  23         78  
  23         580  
15              
16 23     23   116 use strict;
  23         29  
  23         605  
17 23     23   212 use warnings;
  23         28  
  23         7656  
18            
19             =head2 add
20              
21             Usage - $set->add($element)
22             Returns - the added element
23             Args - the element to be added. It must have an ID
24             Function - adds an element to this set
25            
26             =cut
27              
28             sub add {
29 46817     46817 1 42816 my ($self, $new_id) = @_;
30 46817         35596 my $result = undef; # nothing added
31 46817         68656 my $element_id = $new_id->id();
32 46817 100 100     97734 if ($element_id && !$self->contains($new_id)) {
33 46699         61103 $self->{MAP}->{$element_id} = $new_id;
34 46699         38716 $result = $new_id; # successfully added
35             } else {
36             # don't add repeated elements
37             }
38 46817         106321 return $result;
39             }
40              
41             =head2 add_all
42              
43             Usage - $set->add_all($ele1, $ele2, $ele3, ...)
44             Returns - the last added id (e.g. an object of type OBO::XO::OBO_ID)
45             Args - the elements to be added
46             Function - adds the given elements to this set
47            
48             =cut
49              
50             sub add_all {
51 38     38 1 121 my $self = shift;
52 38         40 my $result;
53 38         70 foreach my $ele (@_) {
54 164         227 $result = $self->add($ele);
55             }
56 38         64 return $result;
57             }
58              
59             =head2 remove
60              
61             Usage - $set->remove($element_to_be_removed)
62             Returns - 1 (true) if this set contained the given element
63             Args - element (it must have an ID) to be removed from this set, if present
64             Function - removes an element from this set if it is present
65            
66             =cut
67              
68             sub remove {
69 165     165 1 185 my ($self, $element_to_be_removed) = @_;
70 165         205 my $result = $self->contains($element_to_be_removed);
71 165 100       356 delete $self->{MAP}->{$element_to_be_removed->id()} if ($result);
72 165         239 return $result;
73             }
74              
75             =head2 contains
76              
77             Usage - $set->contains($id)
78             Returns - 1 (true) or 0 (false)
79             Args - the element (it must have an ID) to look up
80             Function - tells if the given ID is in this set
81            
82             =cut
83              
84             sub contains {
85 53919     53919 1 42352 my ($self, $target) = @_;
86 53919 50       63620 if (defined $target) {
87 53919         76112 my $id = $target->id();
88 53919 100 100     262471 return (defined $id && defined $self->{MAP}->{$id})?1:0;
89             } else {
90 0         0 return 0;
91             }
92             }
93              
94             =head2 equals
95              
96             Usage - $set->equals($other_set)
97             Returns - 1 (true) or 0 (false)
98             Args - the other set to check with
99             Function - tells if this set is equal to the given one
100            
101             =cut
102              
103             sub equals {
104 10     10 1 24 my $self = shift;
105 10         14 my $result = 0; # I initially guess they're NOT identical
106 10         8 my $other_set = shift;
107 10         20 my %count = ();
108              
109 10         11 my @this = map ({$_->id();} sort values (%{$self->{MAP}}));
  53         81  
  10         79  
110 10         36 my @that = map ({$_->id();} $other_set->get_set());
  36         51  
111            
112 10 100       36 if ($#this == $#that) {
113 5         14 foreach (@this, @that) {
114 56         68 $count{$_}++;
115             }
116 5         25 foreach my $count (sort values %count) {
117 28 50       42 if ($count != 2) {
118 0         0 $result = 0;
119 0         0 last;
120             } else {
121 28         31 $result = 1;
122             }
123             }
124             }
125 10         53 return $result;
126             }
127              
128             1;
129              
130             __END__