parallel: let xapply_get and nest_get used a common read function

This commit is contained in:
Ole Tange 2011-05-05 11:33:48 +02:00
parent 9b773d03f4
commit e2cf6fdd0a
2 changed files with 78 additions and 63 deletions

View file

@ -1,3 +1,5 @@
unittest til xapply og uden.
parallel echo {1} {2} {3} ::: a b c :::: myfile ::: X Y parallel echo {1} {2} {3} ::: a b c :::: myfile ::: X Y
parallel oggenc -q {1} {2} {2.}_{1}.ogg ::: 1 5 10 :::: wavfiles parallel oggenc -q {1} {2} {2.}_{1}.ogg ::: 1 5 10 :::: wavfiles

View file

@ -3905,7 +3905,15 @@ sub new {
sub get { sub get {
my $self = shift; my $self = shift;
return $self->nest_get(); if($::opt_xapply) {
return $self->xapply_get();
} else {
return $self->nest_get();
}
}
sub xapply_get {
my $self = shift;
if(@{$self->{'unget'}}) { if(@{$self->{'unget'}}) {
return shift @{$self->{'unget'}}; return shift @{$self->{'unget'}};
} }
@ -3913,50 +3921,13 @@ return $self->nest_get();
my $prepend = undef; my $prepend = undef;
my $empty = 1; my $empty = 1;
for my $fh (@{$self->{'fhs'}}) { for my $fh (@{$self->{'fhs'}}) {
if(eof($fh)) { my $arg = read_arg_from_fh($fh);
if(defined $prepend) { if(defined $arg) {
push @record, Arg->new($prepend); push @record, $arg;
$empty = 0; $empty = 0;
} else { } else {
push @record, Arg->new($prepend||""); push @record, Arg->new("");
}
next;
} }
my $arg = <$fh>;
# Remove delimiter
$arg =~ s:$/$::;
if($Global::end_of_file_string and
$arg eq $Global::end_of_file_string) {
# Ignore the rest of input file
while (<$fh>) {}
::debug("EOF-string $arg\n");
if(defined $prepend) {
push @record, Arg->new($prepend);
$empty = 0;
} else {
push @record, Arg->new($prepend);
}
::debug("Is empty? $empty");
next;
}
if(defined $prepend) {
$arg = $prepend.$arg; # For line continuation
$prepend = undef; #undef;
}
if($Global::ignore_empty) {
if($arg =~ /^\s*$/) {
redo; # Try the next line
}
}
if($Global::max_lines) {
if($arg =~ /\s$/) {
# Trailing space => continued on next line
$prepend = $arg;
redo;
}
}
push @record, Arg->new($arg);
$empty = 0;
} }
if($empty) { if($empty) {
return undef; return undef;
@ -3979,47 +3950,37 @@ sub nest_get {
# read one line from each file # read one line from each file
my @first_arg_set; my @first_arg_set;
for (my $fhno = 0; $fhno < $no_of_inputs ; $fhno++) { for (my $fhno = 0; $fhno < $no_of_inputs ; $fhno++) {
my $fh = $self->{'fhs'}[$fhno]; $::arg_matrix[$fhno][0]=read_arg_from_fh($self->{'fhs'}[$fhno]);
my $arg = <$fh>;
# Remove delimiter
$arg =~ s:$/$::;
$::arg_matrix[$fhno][0]=Arg->new($arg);
push @first_arg_set, $::arg_matrix[$fhno][0]; push @first_arg_set, $::arg_matrix[$fhno][0];
} }
$self->unget([@first_arg_set]); $self->unget([@first_arg_set]);
} }
my @fh = @{$self->{'fhs'}}; for (my $fhno = $no_of_inputs - 1; $fhno >= 0; $fhno--) {
for (my $fhno = $no_of_inputs-1; $fhno >= 0; $fhno--) { if(eof($self->{'fhs'}[$fhno])) {
if(eof($fh[$fhno])) {
next; next;
} else { } else {
# read one # read one
my $f = $fh[$fhno]; my $len = $#{$::arg_matrix[$fhno]} + 1;
my $arg = <$f>; $::arg_matrix[$fhno][$len]=read_arg_from_fh($self->{'fhs'}[$fhno]);
# Remove delimiter
$arg =~ s:$/$::;
my $len = $#{$::arg_matrix[$fhno]}+1;
$::arg_matrix[$fhno][$len]=Arg->new($arg);
# make all new combinations # make all new combinations
my @combarg = (); my @combarg = ();
for (my $fhn = 0; $fhn < $no_of_inputs; $fhn++) { for (my $fhn = 0; $fhn < $no_of_inputs; $fhn++) {
push @combarg, [0, $#{$::arg_matrix[$fhn]}]; push @combarg, [0, $#{$::arg_matrix[$fhn]}];
} }
$combarg[$fhno] = [$len,$len]; # Find only combinations with this new entry $combarg[$fhno] = [$len,$len]; # Find only combinations with this new entry
my @combinations = expand_combinations(@combarg);
# map combinations # map combinations
# [ 1, 3, 7], [ 2, 4, 1 ] # [ 1, 3, 7 ], [ 2, 4, 1 ]
# => # =>
# [ m[0][1], m[1][3], m[3][7]], [ m[0][2], m[1][4], m[2][1] ] # [ m[0][1], m[1][3], m[3][7] ], [ m[0][2], m[1][4], m[2][1] ]
my @mapped; my @mapped;
for my $c (@combinations) { for my $c (expand_combinations(@combarg)) {
my @a; my @a;
for my $n (0 .. $no_of_inputs - 1 ) { for my $n (0 .. $no_of_inputs - 1 ) {
push @a, $::arg_matrix[$n][$$c[$n]]; push @a, $::arg_matrix[$n][$$c[$n]];
} }
push @mapped, [@a]; push @mapped, [@a];
} }
# unget the mapped # append the mapped to the ungotten arguments
push @{$self->{'unget'}}, @mapped; push @{$self->{'unget'}}, @mapped;
# get the first # get the first
return shift @{$self->{'unget'}}; return shift @{$self->{'unget'}};
@ -4029,6 +3990,58 @@ sub nest_get {
return undef; return undef;
} }
sub read_arg_from_fh {
# Read one Arg from filehandle
# Returns:
# Arg-object with one read line
# undef if end of file
my $fh = shift;
my $prepend = undef;
loop:
if(eof($fh)) {
if(defined $prepend) {
return Arg->new($prepend);
} else {
return undef;
}
}
my $arg = <$fh>;
# Remove delimiter
$arg =~ s:$/$::;
if($Global::end_of_file_string and
$arg eq $Global::end_of_file_string) {
# Ignore the rest of input file
while (<$fh>) {}
::debug("EOF-string $arg\n");
if(defined $prepend) {
return Arg->new($prepend);
} else {
return undef;
}
}
if(defined $prepend) {
$arg = $prepend.$arg; # For line continuation
$prepend = undef; #undef;
}
if($Global::ignore_empty) {
if($arg =~ /^\s*$/) {
redo; # Try the next line
}
}
if($Global::max_lines) {
if($arg =~ /\s$/) {
# Trailing space => continued on next line
$prepend = $arg;
redo;
}
}
if(defined $arg) {
return Arg->new($arg);
} else {
::die_bug("multiread arg undefined");
}
}
sub expand_combinations { sub expand_combinations {
# Input: # Input:
# ([xmin,xmax], [ymin,ymax], ...) # ([xmin,xmax], [ymin,ymax], ...)