File Coverage

blib/lib/FAQ/OMatic/Set.pm
Criterion Covered Total %
statement 6 55 10.9
branch 0 16 0.0
condition 0 2 0.0
subroutine 2 8 25.0
pod 0 6 0.0
total 8 87 9.2


line stmt bran cond sub pod time code
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     1   7 use strict;
  1         2  
  1         65  
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     1   7 use FAQ::OMatic;
  1         1  
  1         625  
39              
40             sub new {
41 0     0 0   my ($class) = shift;
42 0   0       my $keepOrdered = shift() || 0;
43              
44 0           my $set = {};
45 0           bless $set;
46 0           $set->{'Hash'} = {};
47 0           $set->{'keepOrdered'} = $keepOrdered;
48 0 0         if ($keepOrdered) {
49 0           $set->{'List'} = [];
50             }
51              
52 0           return $set;
53             }
54              
55             # insert members of list into this set
56             sub insert {
57 0     0 0   my $self = shift;
58              
59 0           while (scalar(@_) > 0) {
60 0           my $arg = shift;
61              
62             # TODO debug block, delete
63 0 0         if (not defined $arg) {
64 0           my @cl = caller();
65 0           die "caller was: ".join("/", @cl)."";
66             }
67              
68 0 0         if (not exists $self->{'Hash'}->{$arg}) { # arg not in set yet
69 0           $self->{'Hash'}->{$arg} = 1;
70 0 0         if ($self->{'keepOrdered'}) {
71 0           push @{$self->{'List'}}, $arg;
  0            
72             }
73             }
74             }
75             }
76              
77             sub remove {
78 0     0 0   my $self = shift;
79              
80 0           while (scalar(@_) > 0) {
81 0           my $arg = shift;
82              
83 0 0         if ($self->{'Hash'}->{$arg}) {
84 0           delete $self->{'Hash'}->{$arg};
85 0 0         if ($self->{'keepOrdered'}) {
86 0           my @newList = grep {$_ ne $arg} @{$self->{'List'}};
  0            
  0            
87 0           $self->{'List'} = \@newList;
88             }
89             }
90             }
91             }
92              
93             sub getList {
94 0     0 0   my $self = shift;
95              
96 0 0         if ($self->{'keepOrdered'}) {
97 0           return @{$self->{'List'}};
  0            
98             } else {
99 0           return sort keys %{$self->{'Hash'}};
  0            
100             }
101             }
102              
103             # return "deep copy" of myself
104             sub clone {
105 0     0 0   my $self = shift;
106              
107 0           my $newself = new FAQ::OMatic::Set($self->{'keepOrdered'});
108              
109 0           $newself->{'Hash'} = { %{$self->{'Hash'}} };
  0            
110              
111 0 0         if ($self->{'keepOrdered'}) {
112 0           $newself->{'List'} = [ @{$self->{'List'}} ];
  0            
113             }
114              
115 0           return $newself;
116             }
117              
118             sub subtract {
119 0     0 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;