line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# MatchSet.pm - A perl representation of matches in algebraic expretions |
4
|
|
|
|
|
|
|
# (c) Copyright 1998 Hakan Ardo |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify |
7
|
|
|
|
|
|
|
# it under the terms of the GNU General Public License as published by |
8
|
|
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or |
9
|
|
|
|
|
|
|
# any later version. |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful, |
12
|
|
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
|
|
|
|
# GNU General Public License for more details. |
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License |
17
|
|
|
|
|
|
|
# along with this program; if not, write to the Free Software |
18
|
|
|
|
|
|
|
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 NAME |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Math::Expr::MatchSet - Represents matches in algebraic expretions |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
require Math::Expr::MatchSet; |
27
|
|
|
|
|
|
|
$s=new Math::Expr::MatchSet; |
28
|
|
|
|
|
|
|
$s->Set($pos,$match); |
29
|
|
|
|
|
|
|
$s->Get($pos); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 DESCRIPTION |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Two expretion can be matched in several ways, therefor we need to be |
34
|
|
|
|
|
|
|
able to represent a set of matches keyed by the matchposition (the |
35
|
|
|
|
|
|
|
subexpretion, where the match where found). |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head1 METHODS |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=cut |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
package Math::Expr::MatchSet; |
42
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1103
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head2 $s=new Math::Expr::MatchSet |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Create a new MatchSet object. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
49
|
|
|
|
|
|
|
|
50
|
119
|
|
|
119
|
0
|
386
|
sub new {bless {}, shift} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub Copy { |
53
|
32
|
|
|
32
|
0
|
40
|
my $self = shift; |
54
|
32
|
|
|
|
|
65
|
my $n = new Math::Expr::MatchSet; |
55
|
|
|
|
|
|
|
|
56
|
32
|
|
|
|
|
50
|
foreach (keys %{$self->{'Matches'}}) { |
|
32
|
|
|
|
|
86
|
|
57
|
32
|
|
|
|
|
104
|
$n->Set($_, $self->{'Matches'}{$_}->Copy); |
58
|
|
|
|
|
|
|
} |
59
|
32
|
|
|
|
|
86
|
$n; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub Clear { |
63
|
12
|
|
|
12
|
0
|
17
|
my $self = shift; |
64
|
|
|
|
|
|
|
|
65
|
12
|
|
|
|
|
13
|
foreach (keys %{$self->{'Matches'}}) { |
|
12
|
|
|
|
|
35
|
|
66
|
12
|
|
|
|
|
54
|
delete $self->{'Matches'}{$_}; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub AddPos { |
71
|
32
|
|
|
32
|
0
|
47
|
my ($self, $p) = @_; |
72
|
32
|
|
|
|
|
42
|
my $t={}; |
73
|
|
|
|
|
|
|
|
74
|
32
|
|
|
|
|
42
|
foreach (keys %{$self->{'Matches'}}) { |
|
32
|
|
|
|
|
97
|
|
75
|
32
|
|
|
|
|
126
|
$t->{$_.$p}=$self->{'Matches'}{$_}; |
76
|
32
|
|
|
|
|
90
|
delete $self->{'Matches'}{$_}; |
77
|
|
|
|
|
|
|
} |
78
|
32
|
|
|
|
|
114
|
$self->{'Matches'}=$t; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head2 $s->Set($pos, $match) |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
Sets the match at $pos to $match. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=cut |
86
|
|
|
|
|
|
|
|
87
|
107
|
|
|
107
|
1
|
254
|
sub Set {shift->Add(@_)} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 $s->Add($pos, $match) |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Synonyme to Set. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub Add { |
96
|
107
|
|
|
107
|
1
|
236
|
my ($self, $pos, $vars) = @_; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Parameter sanity checks |
99
|
107
|
50
|
|
|
|
205
|
defined $pos || warn "Bad param pos: $pos"; |
100
|
107
|
50
|
|
|
|
385
|
$vars->isa("Math::Expr::VarSet") || warn "Bad param vars: $vars"; |
101
|
|
|
|
|
|
|
|
102
|
107
|
|
|
|
|
597
|
$self->{'Matches'}{$pos}=$vars; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head2 $s->Insert($mset) |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Inserts all mathes in the MatchSet £mset intho $s. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub Insert { |
112
|
96
|
|
|
96
|
1
|
158
|
my ($self, $mset) = @_; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# Parameter sanity checks |
115
|
96
|
50
|
|
|
|
378
|
$mset->isa("Math::Expr::MatchSet") || warn "Bad param mset: $mset"; |
116
|
|
|
|
|
|
|
|
117
|
96
|
|
|
|
|
100
|
foreach (keys %{$mset->{'Matches'}}) { |
|
96
|
|
|
|
|
364
|
|
118
|
81
|
50
|
|
|
|
196
|
if (defined $self->{'Matches'}{$_}) {warn "Overwriting previous settings";} |
|
0
|
|
|
|
|
0
|
|
119
|
81
|
|
|
|
|
423
|
$self->{'Matches'}{$_}=$mset->{'Matches'}{$_} |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub del { |
124
|
46
|
|
|
46
|
0
|
69
|
my ($self, $pos) = @_; |
125
|
|
|
|
|
|
|
|
126
|
46
|
|
|
|
|
164
|
delete $self->{'Matches'}{$pos}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 $s->SetAll($var, $obj) |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Sets the variable $var to $obj in all mathces in this set, and removes |
134
|
|
|
|
|
|
|
all matches that already had a diffrent value for the variable $var. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=cut |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub SetAll { |
139
|
65
|
|
|
65
|
1
|
96
|
my ($self, $var, $obj) = @_; |
140
|
65
|
|
|
|
|
69
|
my $allgone=1; |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# Parameter sanity checks |
144
|
65
|
50
|
|
|
|
121
|
defined $var || warn "Bad param var: $var\n"; |
145
|
65
|
50
|
100
|
|
|
586
|
$obj->isa("Math::Expr::Opp") || |
|
|
|
66
|
|
|
|
|
146
|
|
|
|
|
|
|
$obj->isa("Math::Expr::Num") || |
147
|
|
|
|
|
|
|
$obj->isa("Math::Expr::Var") || warn "Bad param obj: $obj\n"; |
148
|
|
|
|
|
|
|
|
149
|
65
|
|
|
|
|
77
|
foreach (keys %{$self->{'Matches'}}) { |
|
65
|
|
|
|
|
201
|
|
150
|
71
|
100
|
|
|
|
257
|
if (!$self->{'Matches'}{$_}->Set($var,$obj)) { |
151
|
2
|
|
|
|
|
6
|
delete $self->{'Matches'}{$_}; |
152
|
|
|
|
|
|
|
} else { |
153
|
69
|
|
|
|
|
162
|
$allgone=0; |
154
|
|
|
|
|
|
|
} |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
|
157
|
65
|
100
|
|
|
|
137
|
if ($allgone) { |
158
|
2
|
|
|
|
|
4
|
return 0; |
159
|
|
|
|
|
|
|
} else { |
160
|
63
|
|
|
|
|
203
|
return 1; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 $s->tostr |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Generates a string representation of the MatchSet, used for debugging. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=cut |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub tostr { |
171
|
75
|
|
|
75
|
1
|
95
|
my $self = shift; |
172
|
75
|
|
|
|
|
93
|
my $str=""; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
|
175
|
75
|
|
|
|
|
78
|
foreach (keys %{$self->{'Matches'}}) { |
|
75
|
|
|
|
|
217
|
|
176
|
75
|
|
|
|
|
312
|
$str .= $_ . ":\n" . $self->{'Matches'}{$_}->tostr . "\n\n"; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
75
|
|
|
|
|
220
|
$str; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 $s->Get($pos) |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Returns the Match at possition $pos. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub Get { |
189
|
35
|
|
|
35
|
1
|
71
|
my ($self, $var) = @_; |
190
|
|
|
|
|
|
|
|
191
|
35
|
|
|
|
|
244
|
$self->{'Matches'}{$var}; |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head2 $s->Keys |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Returns the positions at which there excists a match. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=cut |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
sub Keys { |
201
|
11
|
|
|
11
|
1
|
17
|
my ($self) = @_; |
202
|
|
|
|
|
|
|
|
203
|
11
|
|
|
|
|
15
|
keys %{$self->{'Matches'}}; |
|
11
|
|
|
|
|
52
|
|
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
1; |