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 2015-02-12 erick.antezana $
2             #
3             # Module : ObjectSet.pm
4             # Purpose : A generic set of ontology objects (terms, relationships, dbxrefs, etc.).
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              
11             package OBO::Util::ObjectSet;
12              
13             our @ISA = qw(OBO::Util::ObjectIdSet);
14 23     23   11963 use OBO::Util::ObjectIdSet;
  23         94  
  23         658  
15              
16 23     23   133 use strict;
  23         43  
  23         484  
17 23     23   120 use warnings;
  23         43  
  23         9737  
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 46830     46830 1 65264 my ($self, $new_id) = @_;
30 46830         53584 my $result = undef; # nothing added
31 46830         117384 my $element_id = $new_id->id();
32 46830 100 100     150365 if ($element_id && !$self->contains($new_id)) {
33 46712         94146 $self->{MAP}->{$element_id} = $new_id;
34 46712         61091 $result = $new_id; # successfully added
35             } else {
36             # don't add repeated elements
37             }
38 46830         153377 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 148 my $self = shift;
52 38         60 my $result;
53 38         94 foreach my $ele (@_) {
54 165         363 $result = $self->add($ele);
55             }
56 38         94 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 166     166 1 322 my ($self, $element_to_be_removed) = @_;
70 166         660 my $result = $self->contains($element_to_be_removed);
71 166 100       578 delete $self->{MAP}->{$element_to_be_removed->id()} if ($result);
72 166         468 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 53933     53933 1 71014 my ($self, $target) = @_;
86 53933 50       89088 if (defined $target) {
87 53933         133039 my $id = $target->id();
88 53933 100 100     373378 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 29 my $self = shift;
105 10         16 my $result = 0; # I initially guess they're NOT identical
106 10         20 my $other_set = shift;
107 10         16 my %count = ();
108              
109 10         18 my @this = map ({$_->id();} sort values (%{$self->{MAP}}));
  53         188  
  10         97  
110 10         46 my @that = map ({$_->id();} $other_set->get_set());
  36         98  
111            
112 10 100       47 if ($#this == $#that) {
113 5         15 foreach (@this, @that) {
114 56         97 $count{$_}++;
115             }
116 5         28 foreach my $count (sort values %count) {
117 28 50       55 if ($count != 2) {
118 0         0 $result = 0;
119 0         0 last;
120             } else {
121 28         43 $result = 1;
122             }
123             }
124             }
125 10         114 return $result;
126             }
127              
128             1;
129              
130             __END__