* lib/Automake/DisjConditions.pm (new): Precompute 'string' and 'conds'
authorAlexandre Duret-Lutz <adl@gnu.org>
Sun, 8 Aug 2004 20:14:34 +0000 (20:14 +0000)
committerAlexandre Duret-Lutz <adl@gnu.org>
Sun, 8 Aug 2004 20:14:34 +0000 (20:14 +0000)
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.

ChangeLog
lib/Automake/DisjConditions.pm

index 59caab1..f9b76cc 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2004-08-08  Alexandre Duret-Lutz  <adl@gnu.org>
 
+       * 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.
+
        * automake.in (%am_file_cache): New hash.
        (make_paragraphs): Cache .am files with comments stripped to save
        some useless input and substitutions.
index 45745c4..7e80e11 100644 (file)
@@ -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>