From: kanschat Date: Sun, 4 Dec 2011 23:42:29 +0000 (+0000) Subject: add multisubst function X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d0f09d02c75d33fc712b8b649b07fac2d03f9dc6;p=dealii-svn.git add multisubst function git-svn-id: https://svn.dealii.org/trunk@24793 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/common/dealiitemplates.pm.in b/deal.II/common/dealiitemplates.pm.in index 126d598c0f..7acce371f6 100644 --- a/deal.II/common/dealiitemplates.pm.in +++ b/deal.II/common/dealiitemplates.pm.in @@ -16,7 +16,80 @@ BEGIN { @sequential_vectors @parallel_vectors @sparsity_patterns); } + our @EXPORT; + +###################################################################### +# A function substituting multiple patterns in a string with all +# combinations of values of the vectors provided and printing the result. +# +# Its arguments are +# +# 1. The text with patterns to be replaced +# 2. A reference to an array with all the patterns +# 3. For each pattern, a reference to an array with replacements +# +# The function will then loop through all vectors in lexicographical +# order, first fastest, and replace globally all occurences of each +# pattern. This is only done if the pattern is not part of a C++ token. +# +###################################################################### +# Usage example: +###################################################################### +# +# use strict; +# use dealiitemplates; +# +# my $text = <<'EOT' +# template class Test; +# template void Test::Dimension(); +# EOT +# ; +# my @patterns = qw(D S1 V1); +# +# multisubst($text, \@patterns, \@dimensions, \@real_scalars, \@sequential_vectors) +# +###################################################################### +sub multisubst +{ + my $text = shift; + my $patterns = shift; + my @sizes = map { $#{$_} } @_; + my $n = 1; + grep { $n *= ($_+1); } @sizes; + my @index; + + ($#_ == $#{$patterns}) || + die "multisubst function received $#{$patterns} replacement patterns, but $#_ vectors"; + + for (my $i=0;$i<$n;++$i) + { + my $t = $text; + for (my $j=0;$j<=$#sizes;++$j) + { + my $v = $patterns->[$j]; + my $r = $_[$j]->[$index[$j]]; + $t =~ s/(?