From: dpvc v. a. <we...@ma...> - 2005-08-13 16:57:15
|
Log Message: ----------- Added isReduced method to tell if a Union, Set or Interval is already reduced. Fixed up sort to use CORE::sort, and added sort and reduce to Intervals, which do nothing, but are there for consistency. Modified Files: -------------- pg/lib/Value: Interval.pm Set.pm Union.pm Revision Data ------------- Index: Set.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Set.pm,v retrieving revision 1.5 retrieving revision 1.6 diff -Llib/Value/Set.pm -Llib/Value/Set.pm -u -r1.5 -r1.6 --- lib/Value/Set.pm +++ lib/Value/Set.pm @@ -186,6 +186,11 @@ return scalar(@l) - scalar(@r); } +############################################ +# +# Utility routines +# + # # Remove redundant values # @@ -201,11 +206,20 @@ } # +# True if the set is reduced +# +sub isReduced { + my $self = shift; + return 1 if $self->{isReduced} || $self->length < 2; + return $self->reduce->length == $self->length; +} + +# # Sort the data for a set # sub sort { my $self = shift; - return $self->make(sort {$a <=> $b} $self->value); + return $self->make(CORE::sort {$a <=> $b} $self->value); } ########################################################################### Index: Interval.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Interval.pm,v retrieving revision 1.24 retrieving revision 1.25 diff -Llib/Value/Interval.pm -Llib/Value/Interval.pm -u -r1.24 -r1.25 --- lib/Value/Interval.pm +++ lib/Value/Interval.pm @@ -225,6 +225,15 @@ return $l->{close} cmp $r->{close}; } +############################################ +# +# Utility routines +# + +sub reduce {shift} +sub isReduced {1} +sub sort {shift} + ########################################################################### 1; Index: Union.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/Union.pm,v retrieving revision 1.18 retrieving revision 1.19 diff -Llib/Value/Union.pm -Llib/Value/Union.pm -u -r1.18 -r1.19 --- lib/Value/Union.pm +++ lib/Value/Union.pm @@ -201,9 +201,12 @@ ############################################ # -# Reduce unions to simplest form +# Utility routines # +# +# Reduce unions to simplest form +# sub reduce { my $self = shift; return $self if $self->{isReduced} || $self->length < 2; @@ -245,7 +248,21 @@ return $pkg->make(@union)->with(isReduced=>1); } -############################################ +# +# True if a union is reduced +# +sub isReduced { + my $self = shift; + return 1 if $self->{isReduced} || $self->length < 2; + my $reduced = $self->reduce; + return unless $reduced->type eq 'Union' && $reduced->length == $self->length; + my @R = $reduced->sort->value; my @S = $self->sort->value; + foreach my $i (0..$#R) { + return unless $R[$i] == $S[$i] && $R[$i]->length == $S[$i]->length; + } + return 1; +} + # # Sort a union lexicographically # |