summaryrefslogtreecommitdiff
path: root/lib/Automake/DisjConditions.pm
diff options
context:
space:
mode:
authorAlexandre Duret-Lutz <adl@gnu.org>2004-08-08 20:14:34 +0000
committerAlexandre Duret-Lutz <adl@gnu.org>2004-08-08 20:14:34 +0000
commitd7b81c1ed1405fdef10ee7e83c8aa2f6fb50756e (patch)
tree9f97db04453e9612e5939e725aaa551924fc3668 /lib/Automake/DisjConditions.pm
parent9fcdf1df019d2e7e6a7ccbb8a1e7ff71c299f8fd (diff)
downloadautomake-d7b81c1ed1405fdef10ee7e83c8aa2f6fb50756e.tar.gz
* lib/Automake/DisjConditions.pm (new): Precompute 'string' and 'conds'
in place instead of as a side-effect of calling ->string and ->conds. This saves method-lookup time, simplify ->string and ->conds, and allows to create the object only when necessary. (string, conds): Simplify, now that the result is precomputed.
Diffstat (limited to 'lib/Automake/DisjConditions.pm')
-rw-r--r--lib/Automake/DisjConditions.pm67
1 files changed, 33 insertions, 34 deletions
diff --git a/lib/Automake/DisjConditions.pm b/lib/Automake/DisjConditions.pm
index 45745c4af..7e80e119d 100644
--- a/lib/Automake/DisjConditions.pm
+++ b/lib/Automake/DisjConditions.pm
@@ -145,11 +145,7 @@ use vars '%_disjcondition_singletons';
sub new ($;@)
{
my ($class, @conds) = @_;
- my $self = {
- hash => {},
- };
- bless $self, $class;
-
+ my @filtered_conds = ();
for my $cond (@conds)
{
confess "`$cond' isn't a reference" unless ref $cond;
@@ -161,19 +157,40 @@ sub new ($;@)
# DisjConditions as false for this reason.
next if $cond->false;
- # Store conditions as keys AND as values, because blessed
- # objects are converted to string when used as keys (so
- # at least we still have the value when we need to call
- # a method).
- $self->{'hash'}{$cond} = $cond;
+ push @filtered_conds, $cond;
}
- my $key = $self->string;
- if (exists $_disjcondition_singletons{$key})
+ my $string;
+ if (@filtered_conds)
+ {
+ @filtered_conds = sort { $a->string cmp $b->string } @filtered_conds;
+ $string = join (' | ', map { $_->string } @filtered_conds);
+ }
+ else
{
- return $_disjcondition_singletons{$key};
+ $string = 'FALSE';
}
- $_disjcondition_singletons{$key} = $self;
+
+ # Return any existing identical DisjConditions.
+ my $me = $_disjcondition_singletons{$string};
+ return $me if $me;
+
+ # Else, create a new DisjConditions.
+
+ # Store conditions as keys AND as values, because blessed
+ # objects are converted to string when used as keys (so
+ # at least we still have the value when we need to call
+ # a method).
+ my %h = map {$_ => $_} @filtered_conds;
+
+ my $self = {
+ hash => \%h,
+ string => $string,
+ conds => \@filtered_conds,
+ };
+ bless $self, $class;
+
+ $_disjcondition_singletons{$string} = $self;
return $self;
}
@@ -186,11 +203,7 @@ Return the list of C<Condition> objects involved in C<$set>.
sub conds ($ )
{
my ($self) = @_;
- return @{$self->{'conds'}} if exists $self->{'conds'};
- my @conds = values %{$self->{'hash'}};
- @conds = sort { $a->string cmp $b->string } @conds;
- $self->{'conds'} = [@conds];
- return @conds;
+ return @{$self->{'conds'}};
}
=item C<$cond = $set-E<gt>one_cond>
@@ -241,21 +254,7 @@ Build a string which denotes the C<DisjConditions>.
sub string ($ )
{
my ($self) = @_;
-
- return $self->{'string'} if defined $self->{'string'};
-
- my $res = '';
- if ($self->false)
- {
- $res = 'FALSE';
- }
- else
- {
- $res = join (' | ', map { $_->string } $self->conds);
- }
-
- $self->{'string'} = $res;
- return $res;
+ return $self->{'string'};
}
=item C<$cond-E<gt>human>