| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # MatchSet.pm - A perl representation of matches in algebraic expretions | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # (c) Copyright 1998 Hakan Ardo <hakan@debian.org> | ||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | =head2 $s=new Math::Expr::MatchSet | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | Create a new MatchSet object. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 119 | ▶ | sub new {bless {}, shift} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 32 | ▶ | sub Copy { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Copy: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 32 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 32 | my $n = new Math::Expr::MatchSet; | |||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 32 | foreach (keys %{$self->{'Matches'}}) { | |||||||||||||||||||||||||||||||||||||||||
| 57 | 32 | $n->Set($_, $self->{'Matches'}{$_}->Copy); | |||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 32 | $n; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 12 | ▶ | sub Clear { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Clear: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 12 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 12 | foreach (keys %{$self->{'Matches'}}) { | |||||||||||||||||||||||||||||||||||||||||
| 66 | 12 | delete $self->{'Matches'}{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 32 | ▶ | sub AddPos { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
AddPos: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 32 | my ($self, $p) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 32 | my $t={}; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 32 | foreach (keys %{$self->{'Matches'}}) { | |||||||||||||||||||||||||||||||||||||||||
| 75 | 32 | $t->{$_.$p}=$self->{'Matches'}{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 32 | delete $self->{'Matches'}{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 32 | $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 | ▶ | sub Set {shift->Add(@_)} | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =head2 $s->Add($pos, $match) | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | Synonyme to Set. | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 107 | ▶ | sub Add { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Add: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 107 | my ($self, $pos, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | # Parameter sanity checks | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 107 | ▶ | defined $pos || warn "Bad param pos: $pos"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 107 | ▶ | $vars->isa("Math::Expr::VarSet") || warn "Bad param vars: $vars"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 107 | $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 | 96 | ▶ | sub Insert { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Insert: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 96 | my ($self, $mset) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | # Parameter sanity checks | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 96 | ▶ | $mset->isa("Math::Expr::MatchSet") || warn "Bad param mset: $mset"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 96 | foreach (keys %{$mset->{'Matches'}}) { | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if (defined $self->{'Matches'}{$_}) {warn "Overwriting previous settings";} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | 81 | $self->{'Matches'}{$_}=$mset->{'Matches'}{$_} | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 46 | ▶ | sub del { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
del: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 46 | my ($self, $pos) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 46 | 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 | 65 | ▶ | sub SetAll { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
SetAll: called CC=8 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 65 | my ($self, $var, $obj) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 140 | 65 | my $allgone=1; | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | # Parameter sanity checks | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 65 | ▶ | defined $var || warn "Bad param var: $var\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 65 | ▶ | $obj->isa("Math::Expr::Opp") || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$obj->isa("Math::Expr::Opp") or $obj->isa("Math::Expr::Num") or $obj->isa("Math::Expr::Var")
$obj->isa("Math::Expr::Opp") or $obj->isa("Math::Expr::Num")
MC/DC0%0/3
$obj->isa("Math::Expr::Opp") or $obj->isa("Math::Expr::Num") or $obj->isa("Math::Expr::Var")
$obj->isa("Math::Expr::Opp")
$obj->isa("Math::Expr::Num")
$obj->isa("Math::Expr::Var")
Truth table75%3/4
A or B or C
A= $obj->isa("Math::Expr::Opp")B= $obj->isa("Math::Expr::Num")C= $obj->isa("Math::Expr::Var")
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | $obj->isa("Math::Expr::Num") || | ||||||||||||||||||||||||||||||||||||||||||
| 147 | $obj->isa("Math::Expr::Var") || warn "Bad param obj: $obj\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 65 | foreach (keys %{$self->{'Matches'}}) { | |||||||||||||||||||||||||||||||||||||||||
| 150 | 71 | ▶ | if (!$self->{'Matches'}{$_}->Set($var,$obj)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 2 | delete $self->{'Matches'}{$_}; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 69 | $allgone=0; | |||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | 65 | ▶ | if ($allgone) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 160 | 63 | 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 | 75 | ▶ | sub tostr { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tostr: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | 75 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 172 | 75 | my $str=""; | |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 75 | foreach (keys %{$self->{'Matches'}}) { | |||||||||||||||||||||||||||||||||||||||||
| 176 | 75 | $str .= $_ . ":\n" . $self->{'Matches'}{$_}->tostr . "\n\n"; | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | 75 | $str; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =head2 $s->Get($pos) | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | Returns the Match at possition $pos. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | 35 | ▶ | sub Get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 189 | 35 | my ($self, $var) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | 35 | $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 | 11 | ▶ | sub Keys { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | 11 | keys %{$self->{'Matches'}}; | |||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||