| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # $Id: SynonymTypeDefSet.pm 2014-10-07 erick.antezana $ | 
| 2 |  |  |  |  |  |  | # | 
| 3 |  |  |  |  |  |  | # Module  : SynonymTypeDefSet.pm | 
| 4 |  |  |  |  |  |  | # Purpose : Synonym Type Definition Set. | 
| 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::SynonymTypeDefSet; | 
| 11 |  |  |  |  |  |  | # TODO This class is identical to OBO::Util::IDspaceSet | 
| 12 |  |  |  |  |  |  | # TODO This class is identical to OBO::Util::SubsetDefSet, which is not implemented... | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | our @ISA = qw(OBO::Util::Set); | 
| 15 | 8 |  |  | 8 |  | 6228 | use OBO::Util::Set; | 
|  | 8 |  |  |  |  | 17 |  | 
|  | 8 |  |  |  |  | 1381 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 8 |  |  | 8 |  | 39 | use strict; | 
|  | 8 |  |  |  |  | 14 |  | 
|  | 8 |  |  |  |  | 184 |  | 
| 18 | 8 |  |  | 8 |  | 44 | use warnings; | 
|  | 8 |  |  |  |  | 12 |  | 
|  | 8 |  |  |  |  | 3447 |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | =head2 contains | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | Usage    - $set->contains() | 
| 23 |  |  |  |  |  |  | Returns  - true if this set contains the given element | 
| 24 |  |  |  |  |  |  | Args     - the element (OBO::Core::SynonymTypeDef) to be checked | 
| 25 |  |  |  |  |  |  | Function - checks if this set constains the given element | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | =cut | 
| 28 |  |  |  |  |  |  | sub contains { | 
| 29 | 54 |  |  | 54 | 1 | 121 | my $self = shift; | 
| 30 | 54 |  |  |  |  | 75 | my $result = 0; | 
| 31 | 54 | 50 |  |  |  | 128 | if (@_){ | 
| 32 | 54 |  |  |  |  | 75 | my $target = shift; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 54 |  |  |  |  | 69 | foreach my $ele (@{$self->{SET}}){ | 
|  | 54 |  |  |  |  | 129 |  | 
| 35 | 89 | 100 |  |  |  | 241 | if ($target->equals($ele)) { | 
| 36 | 20 |  |  |  |  | 25 | $result = 1; | 
| 37 | 20 |  |  |  |  | 30 | last; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  | } | 
| 40 |  |  |  |  |  |  | } | 
| 41 | 54 |  |  |  |  | 190 | return $result; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | =head2 equals | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | Usage    - $set->equals() | 
| 47 |  |  |  |  |  |  | Returns  - true or false | 
| 48 |  |  |  |  |  |  | Args     - the set (OBO::Util::SynonymTypeDefSet) to compare with | 
| 49 |  |  |  |  |  |  | Function - tells whether this set is equal to the given one | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =cut | 
| 52 |  |  |  |  |  |  | sub equals { | 
| 53 | 1 |  |  | 1 | 1 | 2 | my $self = shift; | 
| 54 | 1 |  |  |  |  | 2 | my $result = 0; # I guess they'are NOT identical | 
| 55 | 1 | 50 |  |  |  | 5 | if (@_) { | 
| 56 | 1 |  |  |  |  | 2 | my $other_set = shift; | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 1 |  |  |  |  | 2 | my %count = (); | 
| 59 | 1 |  |  |  |  | 2 | my @this = map ({scalar $_;} @{$self->{SET}}); | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 1 |  |  |  |  | 3 |  | 
| 60 | 1 |  |  |  |  | 4 | my @that = map ({scalar $_;} $other_set->get_set()); | 
|  | 0 |  |  |  |  | 0 |  | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 1 | 50 |  |  |  | 6 | if ($#this == $#that) { | 
| 63 | 0 |  |  |  |  | 0 | foreach (@this, @that) { | 
| 64 | 0 |  |  |  |  | 0 | $count{$_}++; | 
| 65 |  |  |  |  |  |  | } | 
| 66 | 0 |  |  |  |  | 0 | foreach my $count (sort values %count) { | 
| 67 | 0 | 0 |  |  |  | 0 | if ($count != 2) { | 
| 68 | 0 |  |  |  |  | 0 | $result = 0; | 
| 69 | 0 |  |  |  |  | 0 | last; | 
| 70 |  |  |  |  |  |  | } else { | 
| 71 | 0 |  |  |  |  | 0 | $result = 1; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | } | 
| 75 |  |  |  |  |  |  | } | 
| 76 | 1 |  |  |  |  | 4 | return $result; | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | =head2 remove | 
| 80 |  |  |  |  |  |  |  | 
| 81 |  |  |  |  |  |  | Usage    - $set->remove($element) | 
| 82 |  |  |  |  |  |  | Returns  - the removed element | 
| 83 |  |  |  |  |  |  | Args     - the element (OBO::Core::SynonymTypeDef) to be removed | 
| 84 |  |  |  |  |  |  | Function - removes an element from this set | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | =cut | 
| 87 |  |  |  |  |  |  | sub remove { | 
| 88 | 3 |  |  | 3 | 1 | 10 | my $self = shift; | 
| 89 | 3 |  |  |  |  | 3 | my $result = undef; | 
| 90 | 3 | 50 |  |  |  | 17 | if (@_) { | 
| 91 | 3 |  |  |  |  | 4 | my $ele = shift; | 
| 92 | 3 | 100 |  |  |  | 29 | if ($self->size() > 0) { | 
| 93 | 2 |  |  |  |  | 5 | for (my $i = 0; $i < scalar(@{$self->{SET}}); $i++){ | 
|  | 5 |  |  |  |  | 17 |  | 
| 94 | 5 |  |  |  |  | 6 | my $e = ${$self->{SET}}[$i]; | 
|  | 5 |  |  |  |  | 10 |  | 
| 95 | 5 | 100 |  |  |  | 13 | if ($ele->equals($e)) { | 
| 96 | 2 | 100 |  |  |  | 6 | if ($self->size() > 1) { | 
|  |  | 50 |  |  |  |  |  | 
| 97 | 1 |  |  |  |  | 3 | my $first_elem = shift (@{$self->{SET}}); | 
|  | 1 |  |  |  |  | 2 |  | 
| 98 | 1 |  |  |  |  | 2 | ${$self->{SET}}[$i-1] = $first_elem; | 
|  | 1 |  |  |  |  | 3 |  | 
| 99 |  |  |  |  |  |  | } elsif ($self->size() == 1) { | 
| 100 | 1 |  |  |  |  | 2 | shift (@{$self->{SET}}); | 
|  | 1 |  |  |  |  | 2 |  | 
| 101 |  |  |  |  |  |  | } | 
| 102 | 2 |  |  |  |  | 3 | $result = $ele; | 
| 103 | 2 |  |  |  |  | 4 | last; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  | } | 
| 108 | 3 |  |  |  |  | 6 | return $result; | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | 1; | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | __END__ |