]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
add multisubst function
authorkanschat <kanschat@0785d39b-7218-0410-832d-ea1e28bc413d>
Sun, 4 Dec 2011 23:42:29 +0000 (23:42 +0000)
committerkanschat <kanschat@0785d39b-7218-0410-832d-ea1e28bc413d>
Sun, 4 Dec 2011 23:42:29 +0000 (23:42 +0000)
git-svn-id: https://svn.dealii.org/trunk@24793 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/common/dealiitemplates.pm.in

index 126d598c0f8047d0b99874b396321908943949bd..7acce371f665e4f402e7b1eaa9e1607de7285f80 100644 (file)
@@ -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<D,S1,V1>;
+#   template void Test<D,S1,V1>::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/(?<![\w_])$v(?![\w_])/$r /g;
+       }
+       print "$t\n";
+       for (my $j=0;$j<=$#sizes;++$j)
+       {
+           if ($index[$j] < $sizes[$j])
+           {
+               ++$index[$j];
+               last;
+           }
+           else
+           {
+               $index[$j] = 0;
+           }
+       }
+    }
+}
+
+
+
+
 our $t;
 
 # Dimensions

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.