| 1 | ############################################################################## | ||||||||||||
| 2 | # The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. # | ||||||||||||
| 3 | # # | ||||||||||||
| 4 | # This program is free software; you can redistribute it and/or # | ||||||||||||
| 5 | # modify it under the terms of the GNU General Public License # | ||||||||||||
| 6 | # as published by the Free Software Foundation; either version 2 # | ||||||||||||
| 7 | # of the License, or (at your option) any later version. # | ||||||||||||
| 8 | # # | ||||||||||||
| 9 | # This program is distributed in the hope that it will be useful, # | ||||||||||||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||||||||||||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||||||||||||
| 12 | # GNU General Public License for more details. # | ||||||||||||
| 13 | # # | ||||||||||||
| 14 | # You should have received a copy of the GNU General Public License # | ||||||||||||
| 15 | # along with this program; if not, write to the Free Software # | ||||||||||||
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.# | ||||||||||||
| 17 | # # | ||||||||||||
| 18 | # Jon Howell can be contacted at: # | ||||||||||||
| 19 | # 6211 Sudikoff Lab, Dartmouth College # | ||||||||||||
| 20 | # Hanover, NH 03755-3510 # | ||||||||||||
| 21 | # jonh@cs.dartmouth.edu # | ||||||||||||
| 22 | # # | ||||||||||||
| 23 | # An electronic copy of the GPL is available at: # | ||||||||||||
| 24 | # http://www.gnu.org/copyleft/gpl.html # | ||||||||||||
| 25 | # # | ||||||||||||
| 26 | ############################################################################## | ||||||||||||
| 27 | |||||||||||||
| 28 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | |||||||||||||
| 30 | ### | ||||||||||||
| 31 | ### A FAQ::OMatic::Set keeps track of a nonrepeating list of strings, | ||||||||||||
| 32 | ### such as authors of a part. It can keep the strings in order of insertion, | ||||||||||||
| 33 | ### if desired. | ||||||||||||
| 34 | ### | ||||||||||||
| 35 | |||||||||||||
| 36 | package FAQ::OMatic::Set; | ||||||||||||
| 37 | |||||||||||||
| 38 | 1 | ▶ | use FAQ::OMatic; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 39 | |||||||||||||
| 40 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 41 | 0 | my ($class) = shift; | |||||||||||
| 42 | 0 | ▶ | my $keepOrdered = shift() || 0; | ||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 43 | |||||||||||||
| 44 | 0 | my $set = {}; | |||||||||||
| 45 | 0 | bless $set; | |||||||||||
| 46 | 0 | $set->{'Hash'} = {}; | |||||||||||
| 47 | 0 | $set->{'keepOrdered'} = $keepOrdered; | |||||||||||
| 48 | 0 | ▶ | if ($keepOrdered) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 49 | 0 | $set->{'List'} = []; | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 0 | return $set; | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | # insert members of list into this set | ||||||||||||
| 56 | 0 | ▶ | sub insert { | ||||||||||
|
Subroutine
insert: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 57 | 0 | my $self = shift; | |||||||||||
| 58 | |||||||||||||
| 59 | 0 | ▶ | while (scalar(@_) > 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 60 | 0 | my $arg = shift; | |||||||||||
| 61 | |||||||||||||
| 62 | # TODO debug block, delete | ||||||||||||
| 63 | 0 | ▶ | if (not defined $arg) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 0 | my @cl = caller(); | |||||||||||
| 65 | 0 | die "caller was: <b>".join("/", @cl)."</b>"; | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | 0 | ▶ | if (not exists $self->{'Hash'}->{$arg}) { # arg not in set yet | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | 0 | $self->{'Hash'}->{$arg} = 1; | |||||||||||
| 70 | 0 | ▶ | if ($self->{'keepOrdered'}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | 0 | push @{$self->{'List'}}, $arg; | |||||||||||
| 72 | } | ||||||||||||
| 73 | } | ||||||||||||
| 74 | } | ||||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | 0 | ▶ | sub remove { | ||||||||||
|
Subroutine
remove: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||
| 78 | 0 | my $self = shift; | |||||||||||
| 79 | |||||||||||||
| 80 | 0 | ▶ | while (scalar(@_) > 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 0 | my $arg = shift; | |||||||||||
| 82 | |||||||||||||
| 83 | 0 | ▶ | if ($self->{'Hash'}->{$arg}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | 0 | delete $self->{'Hash'}->{$arg}; | |||||||||||
| 85 | 0 | ▶ | if ($self->{'keepOrdered'}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 0 | my @newList = grep {$_ ne $arg} @{$self->{'List'}}; | |||||||||||
| 87 | 0 | $self->{'List'} = \@newList; | |||||||||||
| 88 | } | ||||||||||||
| 89 | } | ||||||||||||
| 90 | } | ||||||||||||
| 91 | } | ||||||||||||
| 92 | |||||||||||||
| 93 | 0 | ▶ | sub getList { | ||||||||||
|
Subroutine
getList: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 94 | 0 | my $self = shift; | |||||||||||
| 95 | |||||||||||||
| 96 | 0 | ▶ | if ($self->{'keepOrdered'}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | 0 | return @{$self->{'List'}}; | |||||||||||
| 98 | } else { | ||||||||||||
| 99 | 0 | return sort keys %{$self->{'Hash'}}; | |||||||||||
| 100 | } | ||||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | # return "deep copy" of myself | ||||||||||||
| 104 | 0 | ▶ | sub clone { | ||||||||||
|
Subroutine
clone: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||
| 105 | 0 | my $self = shift; | |||||||||||
| 106 | |||||||||||||
| 107 | 0 | my $newself = new FAQ::OMatic::Set($self->{'keepOrdered'}); | |||||||||||
| 108 | |||||||||||||
| 109 | 0 | $newself->{'Hash'} = { %{$self->{'Hash'}} }; | |||||||||||
| 110 | |||||||||||||
| 111 | 0 | ▶ | if ($self->{'keepOrdered'}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 0 | $newself->{'List'} = [ @{$self->{'List'}} ]; | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | 0 | return $newself; | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | 0 | ▶ | sub subtract { | ||||||||||
|
Subroutine
subtract: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||
| 120 | 0 | my $subtrahend = shift; # (set whose items we remove) | |||||||||||
| 121 | |||||||||||||
| 122 | 0 | my $difference = $self->clone; | |||||||||||
| 123 | 0 | $difference->remove($subtrahend->getList()); | |||||||||||
| 124 | 0 | return $difference; | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 1 | 1; | |||||||||||